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