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.
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.
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]
22 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23 * Copyright (c) 2016 by Delphix. All rights reserved.
27 #include <sys/zfs_context.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/refcount.h>
38 * Routines needed by more than one client of libzpool.
42 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
45 vdev_stat_t v0 = { 0 };
50 char used[6], avail[6];
51 char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
54 if (indent == 0 && desc != NULL) {
56 " capacity operations bandwidth ---- errors ----\n");
57 (void) printf("description "
58 "used avail read write read write read write cksum\n");
62 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
67 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
68 (uint64_t **)&vs, &c) != 0)
71 sec = MAX(1, vs->vs_timestamp / NANOSEC);
73 nicenum(vs->vs_alloc, used, sizeof (used));
74 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
75 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
76 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
77 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
79 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
81 nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
82 nicenum(vs->vs_write_errors, werr, sizeof (werr));
83 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
85 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
88 (int)(indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12)),
90 vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
91 vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
92 rops, wops, rbytes, wbytes, rerr, werr, cerr);
95 if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
98 for (c = 0; c < children; c++) {
99 nvlist_t *cnv = child[c];
102 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
103 nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
105 tname = calloc(1, strlen(cname) + 2);
106 (void) strcpy(tname, cname);
107 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
108 tname[strlen(tname)] = '0' + np;
109 show_vdev_stats(tname, ctype, cnv, indent + 2);
115 show_pool_stats(spa_t *spa)
117 nvlist_t *config, *nvroot;
120 VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
122 VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
124 VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
127 show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
128 show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
129 show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
135 * Sets given global variable in libzpool to given unsigned 32-bit value.
136 * arg: "<variable>=<value>"
139 set_global_var(char *arg)
142 char *varname = arg, *varval;
145 #ifndef _LITTLE_ENDIAN
147 * On big endian systems changing a 64-bit variable would set the high
148 * 32 bits instead of the low 32 bits, which could cause unexpected
151 fprintf(stderr, "Setting global variables is only supported on "
152 "little-endian systems\n", varname);
155 if ((varval = strchr(arg, '=')) != NULL) {
158 val = strtoull(varval, NULL, 0);
159 if (val > UINT32_MAX) {
160 fprintf(stderr, "Value for global variable '%s' must "
161 "be a 32-bit unsigned integer\n", varname);
168 zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
169 if (zpoolhdl != NULL) {
171 var = dlsym(zpoolhdl, varname);
173 fprintf(stderr, "Global variable '%s' does not exist "
174 "in libzpool.so\n", varname);
177 *var = (uint32_t)val;
181 fprintf(stderr, "Failed to open libzpool.so to set global "