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.
24 * Copyright 2017 RackTop Systems.
28 #include <sys/zfs_context.h>
34 #include <sys/fs/zfs.h>
35 #include <sys/refcount.h>
38 extern void nicenum(uint64_t num
, char *buf
, size_t);
41 * Routines needed by more than one client of libzpool.
45 show_vdev_stats(const char *desc
, const char *ctype
, nvlist_t
*nv
, int indent
)
48 vdev_stat_t v0
= { 0 };
53 char used
[6], avail
[6];
54 char rops
[6], wops
[6], rbytes
[6], wbytes
[6], rerr
[6], werr
[6], cerr
[6];
57 if (indent
== 0 && desc
!= NULL
) {
59 " capacity operations bandwidth ---- errors ----\n");
60 (void) printf("description "
61 "used avail read write read write read write cksum\n");
65 (void) nvlist_lookup_uint64(nv
, ZPOOL_CONFIG_IS_LOG
, &is_log
);
70 if (nvlist_lookup_uint64_array(nv
, ZPOOL_CONFIG_VDEV_STATS
,
71 (uint64_t **)&vs
, &c
) != 0)
74 sec
= MAX(1, vs
->vs_timestamp
/ NANOSEC
);
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
,
82 nicenum(vs
->vs_bytes
[ZIO_TYPE_WRITE
] / sec
, 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
));
88 (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
91 indent
+ strlen(prefix
) - 25 - (vs
->vs_space
? 0 : 12),
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
);
98 if (nvlist_lookup_nvlist_array(nv
, ctype
, &child
, &children
) != 0)
101 for (c
= 0; c
< children
; c
++) {
102 nvlist_t
*cnv
= child
[c
];
105 if (nvlist_lookup_string(cnv
, ZPOOL_CONFIG_PATH
, &cname
) &&
106 nvlist_lookup_string(cnv
, ZPOOL_CONFIG_TYPE
, &cname
))
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);
118 show_pool_stats(spa_t
*spa
)
120 nvlist_t
*config
, *nvroot
;
123 VERIFY(spa_get_stats(spa_name(spa
), &config
, NULL
, 0) == 0);
125 VERIFY(nvlist_lookup_nvlist(config
, ZPOOL_CONFIG_VDEV_TREE
,
127 VERIFY(nvlist_lookup_string(config
, ZPOOL_CONFIG_POOL_NAME
,
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);
138 * Sets given global variable in libzpool to given unsigned 32-bit value.
139 * arg: "<variable>=<value>"
142 set_global_var(char *arg
)
145 char *varname
= arg
, *varval
;
148 #ifndef _LITTLE_ENDIAN
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
154 fprintf(stderr
, "Setting global variables is only supported on "
155 "little-endian systems\n", varname
);
158 if ((varval
= strchr(arg
, '=')) != NULL
) {
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
);
171 zpoolhdl
= dlopen("libzpool.so", RTLD_LAZY
);
172 if (zpoolhdl
!= NULL
) {
174 var
= dlsym(zpoolhdl
, varname
);
176 fprintf(stderr
, "Global variable '%s' does not exist "
177 "in libzpool.so\n", varname
);
180 *var
= (uint32_t)val
;
184 fprintf(stderr
, "Failed to open libzpool.so to set global "