]> git.proxmox.com Git - mirror_zfs-debian.git/blame - lib/libzpool/util.c
New upstream version 0.7.2
[mirror_zfs-debian.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.
cae5b340 23 * Copyright (c) 2016 by Delphix. All rights reserved.
34dc7c2f
BB
24 */
25
34dc7c2f
BB
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>
cae5b340 35#include <dlfcn.h>
34dc7c2f
BB
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
b128c09f 69show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
34dc7c2f 70{
34dc7c2f 71 vdev_stat_t *vs;
cae5b340 72 vdev_stat_t *v0 = { 0 };
34dc7c2f
BB
73 uint64_t sec;
74 uint64_t is_log = 0;
b128c09f
BB
75 nvlist_t **child;
76 uint_t c, children;
34dc7c2f
BB
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
cae5b340
AX
81 v0 = umem_zalloc(sizeof (*v0), UMEM_NOFAIL);
82
b128c09f
BB
83 if (indent == 0 && desc != NULL) {
84 (void) printf(" "
34dc7c2f 85 " capacity operations bandwidth ---- errors ----\n");
b128c09f 86 (void) printf("description "
34dc7c2f
BB
87 "used avail read write read write read write cksum\n");
88 }
89
b128c09f
BB
90 if (desc != NULL) {
91 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
92
93 if (is_log)
94 prefix = "log ";
95
428870ff 96 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
b128c09f 97 (uint64_t **)&vs, &c) != 0)
cae5b340 98 vs = v0;
b128c09f
BB
99
100 sec = MAX(1, vs->vs_timestamp / NANOSEC);
101
102 nicenum(vs->vs_alloc, used);
103 nicenum(vs->vs_space - vs->vs_alloc, avail);
104 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
105 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
106 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
107 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
108 nicenum(vs->vs_read_errors, rerr);
109 nicenum(vs->vs_write_errors, werr);
110 nicenum(vs->vs_checksum_errors, cerr);
111
112 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
113 indent, "",
114 prefix,
b8864a23 115 (int)(indent+strlen(prefix)-25-(vs->vs_space ? 0 : 12)),
b128c09f
BB
116 desc,
117 vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
118 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
119 rops, wops, rbytes, wbytes, rerr, werr, cerr);
120 }
cae5b340 121 free(v0);
b128c09f
BB
122
123 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
34dc7c2f
BB
124 return;
125
126 for (c = 0; c < children; c++) {
127 nvlist_t *cnv = child[c];
cae5b340 128 char *cname = NULL, *tname;
34dc7c2f 129 uint64_t np;
cae5b340 130 int len;
34dc7c2f
BB
131 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
132 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
133 cname = "<unknown>";
cae5b340
AX
134 len = strlen(cname) + 2;
135 tname = umem_zalloc(len, UMEM_NOFAIL);
136 (void) strlcpy(tname, cname, len);
34dc7c2f
BB
137 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
138 tname[strlen(tname)] = '0' + np;
b128c09f 139 show_vdev_stats(tname, ctype, cnv, indent + 2);
34dc7c2f
BB
140 free(tname);
141 }
142}
143
144void
145show_pool_stats(spa_t *spa)
146{
147 nvlist_t *config, *nvroot;
148 char *name;
149
b128c09f 150 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
34dc7c2f
BB
151
152 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
153 &nvroot) == 0);
154 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
155 &name) == 0);
156
b128c09f
BB
157 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
158 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
159 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
160
161 nvlist_free(config);
34dc7c2f 162}
cae5b340
AX
163
164/*
165 * Sets given global variable in libzpool to given unsigned 32-bit value.
166 * arg: "<variable>=<value>"
167 */
168int
169set_global_var(char *arg)
170{
171 void *zpoolhdl;
172 char *varname = arg, *varval;
173 u_longlong_t val;
174
175#ifndef _LITTLE_ENDIAN
176 /*
177 * On big endian systems changing a 64-bit variable would set the high
178 * 32 bits instead of the low 32 bits, which could cause unexpected
179 * results.
180 */
181 fprintf(stderr, "Setting global variables is only supported on "
182 "little-endian systems\n");
183 return (ENOTSUP);
184#endif
185 if (arg != NULL && (varval = strchr(arg, '=')) != NULL) {
186 *varval = '\0';
187 varval++;
188 val = strtoull(varval, NULL, 0);
189 if (val > UINT32_MAX) {
190 fprintf(stderr, "Value for global variable '%s' must "
191 "be a 32-bit unsigned integer\n", varname);
192 return (EOVERFLOW);
193 }
194 } else {
195 return (EINVAL);
196 }
197
198 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
199 if (zpoolhdl != NULL) {
200 uint32_t *var;
201 var = dlsym(zpoolhdl, varname);
202 if (var == NULL) {
203 fprintf(stderr, "Global variable '%s' does not exist "
204 "in libzpool.so\n", varname);
205 return (EINVAL);
206 }
207 *var = (uint32_t)val;
208
209 dlclose(zpoolhdl);
210 } else {
211 fprintf(stderr, "Failed to open libzpool.so to set global "
212 "variable\n");
213 return (EIO);
214 }
215
216 return (0);
217}