xref: /titanic_41/usr/src/lib/libzpool/common/util.c (revision 5fbb41393be5d63f75952b1d72d4df2642d22557)
1 /*
2  * CDDL HEADER START
3  *
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.
7  *
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.
12  *
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]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <assert.h>
27 #include <sys/zfs_context.h>
28 #include <sys/avl.h>
29 #include <string.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <sys/spa.h>
33 #include <sys/fs/zfs.h>
34 #include <sys/refcount.h>
35 
36 /*
37  * Routines needed by more than one client of libzpool.
38  */
39 
40 void
41 nicenum(uint64_t num, char *buf)
42 {
43 	uint64_t n = num;
44 	int index = 0;
45 	char u;
46 
47 	while (n >= 1024) {
48 		n = (n + (1024 / 2)) / 1024; /* Round up or down */
49 		index++;
50 	}
51 
52 	u = " KMGTPE"[index];
53 
54 	if (index == 0) {
55 		(void) sprintf(buf, "%llu", (u_longlong_t)n);
56 	} else if (n < 10 && (num & (num - 1)) != 0) {
57 		(void) sprintf(buf, "%.2f%c",
58 		    (double)num / (1ULL << 10 * index), u);
59 	} else if (n < 100 && (num & (num - 1)) != 0) {
60 		(void) sprintf(buf, "%.1f%c",
61 		    (double)num / (1ULL << 10 * index), u);
62 	} else {
63 		(void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
64 	}
65 }
66 
67 static void
68 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
69 {
70 	vdev_stat_t *vs;
71 	vdev_stat_t v0 = { 0 };
72 	uint64_t sec;
73 	uint64_t is_log = 0;
74 	nvlist_t **child;
75 	uint_t c, children;
76 	char used[6], avail[6];
77 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
78 	char *prefix = "";
79 
80 	if (indent == 0 && desc != NULL) {
81 		(void) printf("                           "
82 		    " capacity   operations   bandwidth  ---- errors ----\n");
83 		(void) printf("description                "
84 		    "used avail  read write  read write  read write cksum\n");
85 	}
86 
87 	if (desc != NULL) {
88 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
89 
90 		if (is_log)
91 			prefix = "log ";
92 
93 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
94 		    (uint64_t **)&vs, &c) != 0)
95 			vs = &v0;
96 
97 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
98 
99 		nicenum(vs->vs_alloc, used);
100 		nicenum(vs->vs_space - vs->vs_alloc, avail);
101 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
102 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
103 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
104 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
105 		nicenum(vs->vs_read_errors, rerr);
106 		nicenum(vs->vs_write_errors, werr);
107 		nicenum(vs->vs_checksum_errors, cerr);
108 
109 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
110 		    indent, "",
111 		    prefix,
112 		    indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
113 		    desc,
114 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
115 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
116 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
117 	}
118 
119 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
120 		return;
121 
122 	for (c = 0; c < children; c++) {
123 		nvlist_t *cnv = child[c];
124 		char *cname, *tname;
125 		uint64_t np;
126 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
127 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
128 			cname = "<unknown>";
129 		tname = calloc(1, strlen(cname) + 2);
130 		(void) strcpy(tname, cname);
131 		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
132 			tname[strlen(tname)] = '0' + np;
133 		show_vdev_stats(tname, ctype, cnv, indent + 2);
134 		free(tname);
135 	}
136 }
137 
138 void
139 show_pool_stats(spa_t *spa)
140 {
141 	nvlist_t *config, *nvroot;
142 	char *name;
143 
144 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
145 
146 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
147 	    &nvroot) == 0);
148 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
149 	    &name) == 0);
150 
151 	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
152 	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
153 	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
154 
155 	nvlist_free(config);
156 }
157