xref: /illumos-gate/usr/src/lib/libzpool/common/util.c (revision 663207adb1669640c01c5ec6949ce78fd806efae)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23  * Copyright (c) 2016 by Delphix. All rights reserved.
24  * Copyright 2017 RackTop Systems.
25  * Copyright (c) 2017, Intel Corporation.
26  */
27 
28 #include <assert.h>
29 #include <sys/zfs_context.h>
30 #include <sys/avl.h>
31 #include <string.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/spa.h>
35 #include <sys/fs/zfs.h>
36 #include <sys/refcount.h>
37 #include <dlfcn.h>
38 
39 extern void nicenum(uint64_t num, char *buf, size_t);
40 
41 /*
42  * Routines needed by more than one client of libzpool.
43  */
44 
45 static void
46 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
47 {
48 	vdev_stat_t *vs;
49 	vdev_stat_t v0 = { 0 };
50 	uint64_t sec;
51 	uint64_t is_log = 0;
52 	nvlist_t **child;
53 	uint_t c, children;
54 	char used[6], avail[6];
55 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
56 
57 	if (indent == 0 && desc != NULL) {
58 		(void) printf("                           "
59 		    " capacity   operations   bandwidth  ---- errors ----\n");
60 		(void) printf("description                "
61 		    "used avail  read write  read write  read write cksum\n");
62 	}
63 
64 	if (desc != NULL) {
65 		char *suffix = "", *bias = NULL;
66 		char bias_suffix[32];
67 
68 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
69 		(void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
70 		    &bias);
71 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
72 		    (uint64_t **)&vs, &c) != 0)
73 			vs = &v0;
74 
75 		if (bias != NULL) {
76 			(void) snprintf(bias_suffix, sizeof (bias_suffix),
77 			    " (%s)", bias);
78 			suffix = bias_suffix;
79 		} else if (is_log) {
80 			suffix = " (log)";
81 		}
82 
83 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
84 
85 		nicenum(vs->vs_alloc, used, sizeof (used));
86 		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
87 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
88 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
89 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
90 		    sizeof (rbytes));
91 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
92 		    sizeof (wbytes));
93 		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
94 		nicenum(vs->vs_write_errors, werr, sizeof (werr));
95 		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
96 
97 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
98 		    indent, "",
99 		    desc,
100 		    (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
101 		    suffix,
102 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
103 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
104 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
105 	}
106 
107 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
108 		return;
109 
110 	for (c = 0; c < children; c++) {
111 		nvlist_t *cnv = child[c];
112 		char *cname, *tname;
113 		uint64_t np;
114 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
115 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
116 			cname = "<unknown>";
117 		tname = calloc(1, strlen(cname) + 2);
118 		(void) strcpy(tname, cname);
119 		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
120 			tname[strlen(tname)] = '0' + np;
121 		show_vdev_stats(tname, ctype, cnv, indent + 2);
122 		free(tname);
123 	}
124 }
125 
126 void
127 show_pool_stats(spa_t *spa)
128 {
129 	nvlist_t *config, *nvroot;
130 	char *name;
131 
132 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
133 
134 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
135 	    &nvroot) == 0);
136 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
137 	    &name) == 0);
138 
139 	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
140 	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
141 	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
142 
143 	nvlist_free(config);
144 }
145 
146 /*
147  * Sets given global variable in libzpool to given unsigned 32-bit value.
148  * arg: "<variable>=<value>"
149  */
150 int
151 set_global_var(char *arg)
152 {
153 	void *zpoolhdl;
154 	char *varname = arg, *varval;
155 	u_longlong_t val;
156 
157 #ifndef _LITTLE_ENDIAN
158 	/*
159 	 * On big endian systems changing a 64-bit variable would set the high
160 	 * 32 bits instead of the low 32 bits, which could cause unexpected
161 	 * results.
162 	 */
163 	fprintf(stderr, "Setting global variables is only supported on "
164 	    "little-endian systems\n", varname);
165 	return (ENOTSUP);
166 #endif
167 	if ((varval = strchr(arg, '=')) != NULL) {
168 		*varval = '\0';
169 		varval++;
170 		val = strtoull(varval, NULL, 0);
171 		if (val > UINT32_MAX) {
172 			fprintf(stderr, "Value for global variable '%s' must "
173 			    "be a 32-bit unsigned integer\n", varname);
174 			return (EOVERFLOW);
175 		}
176 	} else {
177 		return (EINVAL);
178 	}
179 
180 	zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
181 	if (zpoolhdl != NULL) {
182 		uint32_t *var;
183 		var = dlsym(zpoolhdl, varname);
184 		if (var == NULL) {
185 			fprintf(stderr, "Global variable '%s' does not exist "
186 			    "in libzpool.so\n", varname);
187 			return (EINVAL);
188 		}
189 		*var = (uint32_t)val;
190 
191 		dlclose(zpoolhdl);
192 	} else {
193 		fprintf(stderr, "Failed to open libzpool.so to set global "
194 		    "variable\n");
195 		return (EIO);
196 	}
197 
198 	return (0);
199 }
200