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