xref: /illumos-gate/usr/src/lib/libzpool/common/util.c (revision 4c28a617e3922d92a58e813a5b955eb526b9c386)
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  */
26 
27 #include <assert.h>
28 #include <sys/zfs_context.h>
29 #include <sys/avl.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <sys/spa.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/refcount.h>
36 #include <dlfcn.h>
37 
38 extern void nicenum(uint64_t num, char *buf, size_t);
39 
40 /*
41  * Routines needed by more than one client of libzpool.
42  */
43 
44 static void
45 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
46 {
47 	vdev_stat_t *vs;
48 	vdev_stat_t v0 = { 0 };
49 	uint64_t sec;
50 	uint64_t is_log = 0;
51 	nvlist_t **child;
52 	uint_t c, children;
53 	char used[6], avail[6];
54 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
55 	char *prefix = "";
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 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
66 
67 		if (is_log)
68 			prefix = "log ";
69 
70 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
71 		    (uint64_t **)&vs, &c) != 0)
72 			vs = &v0;
73 
74 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
75 
76 		nicenum(vs->vs_alloc, used, sizeof (used));
77 		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
78 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
79 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
80 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
81 		    sizeof (rbytes));
82 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
83 		    sizeof (wbytes));
84 		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
85 		nicenum(vs->vs_write_errors, werr, sizeof (werr));
86 		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
87 
88 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
89 		    indent, "",
90 		    prefix,
91 		    indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
92 		    desc,
93 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
94 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
95 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
96 	}
97 
98 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
99 		return;
100 
101 	for (c = 0; c < children; c++) {
102 		nvlist_t *cnv = child[c];
103 		char *cname, *tname;
104 		uint64_t np;
105 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
106 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
107 			cname = "<unknown>";
108 		tname = calloc(1, strlen(cname) + 2);
109 		(void) strcpy(tname, cname);
110 		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
111 			tname[strlen(tname)] = '0' + np;
112 		show_vdev_stats(tname, ctype, cnv, indent + 2);
113 		free(tname);
114 	}
115 }
116 
117 void
118 show_pool_stats(spa_t *spa)
119 {
120 	nvlist_t *config, *nvroot;
121 	char *name;
122 
123 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
124 
125 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
126 	    &nvroot) == 0);
127 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
128 	    &name) == 0);
129 
130 	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
131 	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
132 	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
133 
134 	nvlist_free(config);
135 }
136 
137 /*
138  * Sets given global variable in libzpool to given unsigned 32-bit value.
139  * arg: "<variable>=<value>"
140  */
141 int
142 set_global_var(char *arg)
143 {
144 	void *zpoolhdl;
145 	char *varname = arg, *varval;
146 	u_longlong_t val;
147 
148 #ifndef _LITTLE_ENDIAN
149 	/*
150 	 * On big endian systems changing a 64-bit variable would set the high
151 	 * 32 bits instead of the low 32 bits, which could cause unexpected
152 	 * results.
153 	 */
154 	fprintf(stderr, "Setting global variables is only supported on "
155 	    "little-endian systems\n", varname);
156 	return (ENOTSUP);
157 #endif
158 	if ((varval = strchr(arg, '=')) != NULL) {
159 		*varval = '\0';
160 		varval++;
161 		val = strtoull(varval, NULL, 0);
162 		if (val > UINT32_MAX) {
163 			fprintf(stderr, "Value for global variable '%s' must "
164 			    "be a 32-bit unsigned integer\n", varname);
165 			return (EOVERFLOW);
166 		}
167 	} else {
168 		return (EINVAL);
169 	}
170 
171 	zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
172 	if (zpoolhdl != NULL) {
173 		uint32_t *var;
174 		var = dlsym(zpoolhdl, varname);
175 		if (var == NULL) {
176 			fprintf(stderr, "Global variable '%s' does not exist "
177 			    "in libzpool.so\n", varname);
178 			return (EINVAL);
179 		}
180 		*var = (uint32_t)val;
181 
182 		dlclose(zpoolhdl);
183 	} else {
184 		fprintf(stderr, "Failed to open libzpool.so to set global "
185 		    "variable\n");
186 		return (EIO);
187 	}
188 
189 	return (0);
190 }
191