xref: /illumos-gate/usr/src/lib/libzpool/common/util.c (revision ea8dc4b6)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5*ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6*ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
7fa9e4066Sahrens  *
8fa9e4066Sahrens  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fa9e4066Sahrens  * or http://www.opensolaris.org/os/licensing.
10fa9e4066Sahrens  * See the License for the specific language governing permissions
11fa9e4066Sahrens  * and limitations under the License.
12fa9e4066Sahrens  *
13fa9e4066Sahrens  * When distributing Covered Code, include this CDDL HEADER in each
14fa9e4066Sahrens  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fa9e4066Sahrens  * If applicable, add the following below this CDDL HEADER, with the
16fa9e4066Sahrens  * fields enclosed by brackets "[]" replaced with your own identifying
17fa9e4066Sahrens  * information: Portions Copyright [yyyy] [name of copyright owner]
18fa9e4066Sahrens  *
19fa9e4066Sahrens  * CDDL HEADER END
20fa9e4066Sahrens  */
21fa9e4066Sahrens /*
22*ea8dc4b6Seschrock  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
23fa9e4066Sahrens  * Use is subject to license terms.
24fa9e4066Sahrens  */
25fa9e4066Sahrens 
26fa9e4066Sahrens #pragma ident	"%Z%%M%	%I%	%E% SMI"
27fa9e4066Sahrens 
28fa9e4066Sahrens #include <assert.h>
29fa9e4066Sahrens #include <sys/zfs_context.h>
30fa9e4066Sahrens #include <sys/avl.h>
31fa9e4066Sahrens #include <string.h>
32fa9e4066Sahrens #include <stdio.h>
33fa9e4066Sahrens #include <stdlib.h>
34fa9e4066Sahrens #include <sys/spa.h>
35fa9e4066Sahrens #include <sys/fs/zfs.h>
36*ea8dc4b6Seschrock #include <sys/refcount.h>
37fa9e4066Sahrens 
38fa9e4066Sahrens /*
39fa9e4066Sahrens  * Routines needed by more than one client of libzpool.
40fa9e4066Sahrens  */
41fa9e4066Sahrens 
42fa9e4066Sahrens void
43fa9e4066Sahrens nicenum(uint64_t num, char *buf)
44fa9e4066Sahrens {
45fa9e4066Sahrens 	uint64_t n = num;
46fa9e4066Sahrens 	int index = 0;
47fa9e4066Sahrens 	char u;
48fa9e4066Sahrens 
49fa9e4066Sahrens 	while (n >= 1024) {
50fa9e4066Sahrens 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
51fa9e4066Sahrens 		index++;
52fa9e4066Sahrens 	}
53fa9e4066Sahrens 
54fa9e4066Sahrens 	u = " KMGTPE"[index];
55fa9e4066Sahrens 
56fa9e4066Sahrens 	if (index == 0) {
57fa9e4066Sahrens 		(void) sprintf(buf, "%llu", (u_longlong_t)n);
58fa9e4066Sahrens 	} else if (n < 10 && (num & (num - 1)) != 0) {
59fa9e4066Sahrens 		(void) sprintf(buf, "%.2f%c",
60fa9e4066Sahrens 		    (double)num / (1ULL << 10 * index), u);
61fa9e4066Sahrens 	} else if (n < 100 && (num & (num - 1)) != 0) {
62fa9e4066Sahrens 		(void) sprintf(buf, "%.1f%c",
63fa9e4066Sahrens 		    (double)num / (1ULL << 10 * index), u);
64fa9e4066Sahrens 	} else {
65fa9e4066Sahrens 		(void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
66fa9e4066Sahrens 	}
67fa9e4066Sahrens }
68fa9e4066Sahrens 
69fa9e4066Sahrens static void
70fa9e4066Sahrens show_vdev_stats(const char *desc, nvlist_t *nv, int indent)
71fa9e4066Sahrens {
72fa9e4066Sahrens 	nvlist_t **child;
73fa9e4066Sahrens 	uint_t c, children;
74fa9e4066Sahrens 	vdev_stat_t *vs;
75fa9e4066Sahrens 	uint64_t sec;
76fa9e4066Sahrens 	char used[6], avail[6];
77fa9e4066Sahrens 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
78fa9e4066Sahrens 
79fa9e4066Sahrens 	if (indent == 0) {
80fa9e4066Sahrens 		(void) printf("                     "
81fa9e4066Sahrens 		    " capacity   operations   bandwidth  ---- errors ----\n");
82fa9e4066Sahrens 		(void) printf("description          "
83fa9e4066Sahrens 		    "used avail  read write  read write  read write cksum\n");
84fa9e4066Sahrens 	}
85fa9e4066Sahrens 
86fa9e4066Sahrens 	VERIFY(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
87fa9e4066Sahrens 	    (uint64_t **)&vs, &c) == 0);
88fa9e4066Sahrens 
89fa9e4066Sahrens 	sec = MAX(1, vs->vs_timestamp / NANOSEC);
90fa9e4066Sahrens 
91fa9e4066Sahrens 	nicenum(vs->vs_alloc, used);
92fa9e4066Sahrens 	nicenum(vs->vs_space - vs->vs_alloc, avail);
93fa9e4066Sahrens 	nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
94fa9e4066Sahrens 	nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
95fa9e4066Sahrens 	nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
96fa9e4066Sahrens 	nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
97fa9e4066Sahrens 	nicenum(vs->vs_read_errors, rerr);
98fa9e4066Sahrens 	nicenum(vs->vs_write_errors, werr);
99fa9e4066Sahrens 	nicenum(vs->vs_checksum_errors, cerr);
100fa9e4066Sahrens 
101fa9e4066Sahrens 	(void) printf("%*s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
102fa9e4066Sahrens 	    indent, "",
103fa9e4066Sahrens 	    indent - 19 - (vs->vs_space ? 0 : 12), desc,
104fa9e4066Sahrens 	    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
105fa9e4066Sahrens 	    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
106fa9e4066Sahrens 	    rops, wops, rbytes, wbytes, rerr, werr, cerr);
107fa9e4066Sahrens 
108fa9e4066Sahrens 	if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
109fa9e4066Sahrens 	    &child, &children) != 0)
110fa9e4066Sahrens 		return;
111fa9e4066Sahrens 
112fa9e4066Sahrens 	for (c = 0; c < children; c++) {
113fa9e4066Sahrens 		nvlist_t *cnv = child[c];
114fa9e4066Sahrens 		char *cname;
115fa9e4066Sahrens 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
116fa9e4066Sahrens 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
117fa9e4066Sahrens 			cname = "<unknown>";
118fa9e4066Sahrens 		show_vdev_stats(cname, cnv, indent + 2);
119fa9e4066Sahrens 	}
120fa9e4066Sahrens }
121fa9e4066Sahrens 
122fa9e4066Sahrens void
123fa9e4066Sahrens show_pool_stats(spa_t *spa)
124fa9e4066Sahrens {
125fa9e4066Sahrens 	nvlist_t *config = NULL;
126fa9e4066Sahrens 	nvlist_t *nvroot = NULL;
127fa9e4066Sahrens 
128*ea8dc4b6Seschrock 	spa_config_enter(spa, RW_READER, FTAG);
129*ea8dc4b6Seschrock 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
130fa9e4066Sahrens 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
131fa9e4066Sahrens 	    &nvroot) == 0);
132fa9e4066Sahrens 
133fa9e4066Sahrens 	show_vdev_stats(spa_name(spa), nvroot, 0);
134*ea8dc4b6Seschrock 	spa_config_exit(spa, FTAG);
135fa9e4066Sahrens }
136