]> git.proxmox.com Git - mirror_zfs.git/blame - lib/libzpool/util.c
Pool allocation classes
[mirror_zfs.git] / lib / libzpool / util.c
CommitLineData
34dc7c2f
BB
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/*
428870ff 22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
ed828c0c 23 * Copyright (c) 2016 by Delphix. All rights reserved.
f3c8c9e6 24 * Copyright 2017 Jason King
cc99f275 25 * Copyright (c) 2017, Intel Corporation.
34dc7c2f
BB
26 */
27
34dc7c2f
BB
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>
ed828c0c 37#include <dlfcn.h>
34dc7c2f
BB
38
39/*
40 * Routines needed by more than one client of libzpool.
41 */
42
f3c8c9e6
JK
43/* The largest suffix that can fit, aka an exabyte (2^60 / 10^18) */
44#define INDEX_MAX (6)
45
46/* Verify INDEX_MAX fits */
47CTASSERT_GLOBAL(INDEX_MAX * 10 < sizeof (uint64_t) * 8);
48
34dc7c2f 49void
f3c8c9e6
JK
50nicenum_scale(uint64_t n, size_t units, char *buf, size_t buflen,
51 uint32_t flags)
34dc7c2f 52{
f3c8c9e6
JK
53 uint64_t divamt = 1024;
54 uint64_t divisor = 1;
34dc7c2f 55 int index = 0;
f3c8c9e6 56 int rc = 0;
34dc7c2f
BB
57 char u;
58
f3c8c9e6
JK
59 if (units == 0)
60 units = 1;
61
62 if (n > 0) {
63 n *= units;
64 if (n < units)
65 goto overflow;
66 }
67
68 if (flags & NN_DIVISOR_1000)
69 divamt = 1000;
70
71 /*
72 * This tries to find the suffix S(n) such that
73 * S(n) <= n < S(n+1), where S(n) = 2^(n*10) | 10^(3*n)
74 * (i.e. 1024/1000, 1,048,576/1,000,000, etc). Stop once S(n)
75 * is the largest prefix supported (i.e. don't bother computing
76 * and checking S(n+1). Since INDEX_MAX should be the largest
77 * suffix that fits (currently an exabyte), S(INDEX_MAX + 1) is
78 * never checked as it would overflow.
79 */
80 while (index < INDEX_MAX) {
81 uint64_t newdiv = divisor * divamt;
82
83 /* CTASSERT() guarantee these never trip */
84 VERIFY3U(newdiv, >=, divamt);
85 VERIFY3U(newdiv, >=, divisor);
86
87 if (n < newdiv)
88 break;
89
90 divisor = newdiv;
34dc7c2f
BB
91 index++;
92 }
93
94 u = " KMGTPE"[index];
95
96 if (index == 0) {
f3c8c9e6
JK
97 rc = snprintf(buf, buflen, "%llu", (u_longlong_t)n);
98 } else if (n % divisor == 0) {
99 /*
100 * If this is an even multiple of the base, always display
101 * without any decimal precision.
102 */
103 rc = snprintf(buf, buflen, "%llu%c",
104 (u_longlong_t)(n / divisor), u);
34dc7c2f 105 } else {
f3c8c9e6
JK
106 /*
107 * We want to choose a precision that reflects the best choice
108 * for fitting in 5 characters. This can get rather tricky
109 * when we have numbers that are very close to an order of
110 * magnitude. For example, when displaying 10239 (which is
111 * really 9.999K), we want only a single place of precision
112 * for 10.0K. We could develop some complex heuristics for
113 * this, but it's much easier just to try each combination
114 * in turn.
115 */
116 int i;
117 for (i = 2; i >= 0; i--) {
118 if ((rc = snprintf(buf, buflen, "%.*f%c", i,
119 (double)n / divisor, u)) <= 5)
120 break;
121 }
34dc7c2f 122 }
f3c8c9e6
JK
123
124 if (rc + 1 > buflen || rc < 0)
125 goto overflow;
126
127 return;
128
129overflow:
130 /* prefer a more verbose message if possible */
131 if (buflen > 10)
132 (void) strlcpy(buf, "<overflow>", buflen);
133 else
134 (void) strlcpy(buf, "??", buflen);
135}
136
137void
138nicenum(uint64_t num, char *buf, size_t buflen)
139{
140 nicenum_scale(num, 1, buf, buflen, 0);
34dc7c2f
BB
141}
142
143static void
b128c09f 144show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
34dc7c2f 145{
34dc7c2f 146 vdev_stat_t *vs;
193a37cb 147 vdev_stat_t *v0 = { 0 };
34dc7c2f
BB
148 uint64_t sec;
149 uint64_t is_log = 0;
b128c09f
BB
150 nvlist_t **child;
151 uint_t c, children;
34dc7c2f
BB
152 char used[6], avail[6];
153 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
34dc7c2f 154
193a37cb
TH
155 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
156
b128c09f
BB
157 if (indent == 0 && desc != NULL) {
158 (void) printf(" "
34dc7c2f 159 " capacity operations bandwidth ---- errors ----\n");
b128c09f 160 (void) printf("description "
34dc7c2f
BB
161 "used avail read write read write read write cksum\n");
162 }
163
b128c09f 164 if (desc != NULL) {
cc99f275
DB
165 char *suffix = "", *bias = NULL;
166 char bias_suffix[32];
b128c09f 167
cc99f275
DB
168 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
169 (void) nvlist_lookup_string(nv, ZPOOL_CONFIG_ALLOCATION_BIAS,
170 &bias);
428870ff 171 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
b128c09f 172 (uint64_t **)&vs, &c) != 0)
193a37cb 173 vs = v0;
b128c09f 174
cc99f275
DB
175 if (bias != NULL) {
176 (void) snprintf(bias_suffix, sizeof (bias_suffix),
177 " (%s)", bias);
178 suffix = bias_suffix;
179 } else if (is_log) {
180 suffix = " (log)";
181 }
182
b128c09f
BB
183 sec = MAX(1, vs->vs_timestamp / NANOSEC);
184
f3c8c9e6
JK
185 nicenum(vs->vs_alloc, used, sizeof (used));
186 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
187 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
188 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
189 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
190 sizeof (rbytes));
191 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
192 sizeof (wbytes));
193 nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
194 nicenum(vs->vs_write_errors, werr, sizeof (werr));
195 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
b128c09f
BB
196
197 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
198 indent, "",
b128c09f 199 desc,
cc99f275
DB
200 (int)(indent+strlen(desc)-25-(vs->vs_space ? 0 : 12)),
201 suffix,
b128c09f
BB
202 vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
203 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
204 rops, wops, rbytes, wbytes, rerr, werr, cerr);
205 }
193a37cb 206 free(v0);
b128c09f
BB
207
208 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
34dc7c2f
BB
209 return;
210
211 for (c = 0; c < children; c++) {
212 nvlist_t *cnv = child[c];
ccc92611 213 char *cname = NULL, *tname;
34dc7c2f 214 uint64_t np;
ccc92611 215 int len;
34dc7c2f
BB
216 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
217 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
218 cname = "<unknown>";
ccc92611 219 len = strlen(cname) + 2;
220 tname = umem_zalloc(len, UMEM_NOFAIL);
221 (void) strlcpy(tname, cname, len);
34dc7c2f
BB
222 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
223 tname[strlen(tname)] = '0' + np;
b128c09f 224 show_vdev_stats(tname, ctype, cnv, indent + 2);
34dc7c2f
BB
225 free(tname);
226 }
227}
228
229void
230show_pool_stats(spa_t *spa)
231{
232 nvlist_t *config, *nvroot;
233 char *name;
234
b128c09f 235 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
34dc7c2f
BB
236
237 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
238 &nvroot) == 0);
239 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
240 &name) == 0);
241
b128c09f
BB
242 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
243 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
244 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
245
246 nvlist_free(config);
34dc7c2f 247}
ed828c0c
GM
248
249/*
250 * Sets given global variable in libzpool to given unsigned 32-bit value.
251 * arg: "<variable>=<value>"
252 */
253int
254set_global_var(char *arg)
255{
256 void *zpoolhdl;
257 char *varname = arg, *varval;
258 u_longlong_t val;
259
260#ifndef _LITTLE_ENDIAN
261 /*
262 * On big endian systems changing a 64-bit variable would set the high
263 * 32 bits instead of the low 32 bits, which could cause unexpected
264 * results.
265 */
266 fprintf(stderr, "Setting global variables is only supported on "
267 "little-endian systems\n");
268 return (ENOTSUP);
269#endif
7b0dc2a3 270 if (arg != NULL && (varval = strchr(arg, '=')) != NULL) {
ed828c0c
GM
271 *varval = '\0';
272 varval++;
273 val = strtoull(varval, NULL, 0);
274 if (val > UINT32_MAX) {
275 fprintf(stderr, "Value for global variable '%s' must "
276 "be a 32-bit unsigned integer\n", varname);
277 return (EOVERFLOW);
278 }
279 } else {
280 return (EINVAL);
281 }
282
283 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
284 if (zpoolhdl != NULL) {
285 uint32_t *var;
286 var = dlsym(zpoolhdl, varname);
287 if (var == NULL) {
288 fprintf(stderr, "Global variable '%s' does not exist "
289 "in libzpool.so\n", varname);
290 return (EINVAL);
291 }
292 *var = (uint32_t)val;
293
294 dlclose(zpoolhdl);
295 } else {
296 fprintf(stderr, "Failed to open libzpool.so to set global "
297 "variable\n");
298 return (EIO);
299 }
300
301 return (0);
302}