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