xref: /onnv-gate/usr/src/lib/libzpool/common/util.c (revision 1635:0ab1193d47cb)
1789Sahrens /*
2789Sahrens  * CDDL HEADER START
3789Sahrens  *
4789Sahrens  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
7789Sahrens  *
8789Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens  * or http://www.opensolaris.org/os/licensing.
10789Sahrens  * See the License for the specific language governing permissions
11789Sahrens  * and limitations under the License.
12789Sahrens  *
13789Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens  *
19789Sahrens  * CDDL HEADER END
20789Sahrens  */
21789Sahrens /*
221544Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23789Sahrens  * Use is subject to license terms.
24789Sahrens  */
25789Sahrens 
26789Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27789Sahrens 
28789Sahrens #include <assert.h>
29789Sahrens #include <sys/zfs_context.h>
30789Sahrens #include <sys/avl.h>
31789Sahrens #include <string.h>
32789Sahrens #include <stdio.h>
33789Sahrens #include <stdlib.h>
34789Sahrens #include <sys/spa.h>
35789Sahrens #include <sys/fs/zfs.h>
361544Seschrock #include <sys/refcount.h>
37789Sahrens 
38789Sahrens /*
39789Sahrens  * Routines needed by more than one client of libzpool.
40789Sahrens  */
41789Sahrens 
42789Sahrens void
43789Sahrens nicenum(uint64_t num, char *buf)
44789Sahrens {
45789Sahrens 	uint64_t n = num;
46789Sahrens 	int index = 0;
47789Sahrens 	char u;
48789Sahrens 
49789Sahrens 	while (n >= 1024) {
50789Sahrens 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
51789Sahrens 		index++;
52789Sahrens 	}
53789Sahrens 
54789Sahrens 	u = " KMGTPE"[index];
55789Sahrens 
56789Sahrens 	if (index == 0) {
57789Sahrens 		(void) sprintf(buf, "%llu", (u_longlong_t)n);
58789Sahrens 	} else if (n < 10 && (num & (num - 1)) != 0) {
59789Sahrens 		(void) sprintf(buf, "%.2f%c",
60789Sahrens 		    (double)num / (1ULL << 10 * index), u);
61789Sahrens 	} else if (n < 100 && (num & (num - 1)) != 0) {
62789Sahrens 		(void) sprintf(buf, "%.1f%c",
63789Sahrens 		    (double)num / (1ULL << 10 * index), u);
64789Sahrens 	} else {
65789Sahrens 		(void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
66789Sahrens 	}
67789Sahrens }
68789Sahrens 
69789Sahrens static void
70789Sahrens show_vdev_stats(const char *desc, nvlist_t *nv, int indent)
71789Sahrens {
72789Sahrens 	nvlist_t **child;
73789Sahrens 	uint_t c, children;
74789Sahrens 	vdev_stat_t *vs;
75789Sahrens 	uint64_t sec;
76789Sahrens 	char used[6], avail[6];
77789Sahrens 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
78789Sahrens 
79789Sahrens 	if (indent == 0) {
80789Sahrens 		(void) printf("                     "
81789Sahrens 		    " capacity   operations   bandwidth  ---- errors ----\n");
82789Sahrens 		(void) printf("description          "
83789Sahrens 		    "used avail  read write  read write  read write cksum\n");
84789Sahrens 	}
85789Sahrens 
86789Sahrens 	VERIFY(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
87789Sahrens 	    (uint64_t **)&vs, &c) == 0);
88789Sahrens 
89789Sahrens 	sec = MAX(1, vs->vs_timestamp / NANOSEC);
90789Sahrens 
91789Sahrens 	nicenum(vs->vs_alloc, used);
92789Sahrens 	nicenum(vs->vs_space - vs->vs_alloc, avail);
93789Sahrens 	nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
94789Sahrens 	nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
95789Sahrens 	nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
96789Sahrens 	nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
97789Sahrens 	nicenum(vs->vs_read_errors, rerr);
98789Sahrens 	nicenum(vs->vs_write_errors, werr);
99789Sahrens 	nicenum(vs->vs_checksum_errors, cerr);
100789Sahrens 
101789Sahrens 	(void) printf("%*s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
102789Sahrens 	    indent, "",
103789Sahrens 	    indent - 19 - (vs->vs_space ? 0 : 12), desc,
104789Sahrens 	    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
105789Sahrens 	    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
106789Sahrens 	    rops, wops, rbytes, wbytes, rerr, werr, cerr);
107789Sahrens 
108789Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
109789Sahrens 	    &child, &children) != 0)
110789Sahrens 		return;
111789Sahrens 
112789Sahrens 	for (c = 0; c < children; c++) {
113789Sahrens 		nvlist_t *cnv = child[c];
114789Sahrens 		char *cname;
115789Sahrens 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
116789Sahrens 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
117789Sahrens 			cname = "<unknown>";
118789Sahrens 		show_vdev_stats(cname, cnv, indent + 2);
119789Sahrens 	}
120789Sahrens }
121789Sahrens 
122789Sahrens void
123789Sahrens show_pool_stats(spa_t *spa)
124789Sahrens {
125*1635Sbonwick 	nvlist_t *config, *nvroot;
126*1635Sbonwick 	char *name;
127789Sahrens 
1281544Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
129*1635Sbonwick 	config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
130*1635Sbonwick 	spa_config_exit(spa, FTAG);
131*1635Sbonwick 
132789Sahrens 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
133789Sahrens 	    &nvroot) == 0);
134*1635Sbonwick 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
135*1635Sbonwick 	    &name) == 0);
136789Sahrens 
137*1635Sbonwick 	show_vdev_stats(name, nvroot, 0);
138789Sahrens }
139