xref: /titanic_50/usr/src/lib/libzpool/common/util.c (revision 2a52a1627973d84afe3af6de87c1fe00ac3d087e)
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 static void
40 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
41 {
42 	vdev_stat_t *vs;
43 	vdev_stat_t v0 = { 0 };
44 	uint64_t sec;
45 	uint64_t is_log = 0;
46 	nvlist_t **child;
47 	uint_t c, children;
48 	char used[6], avail[6];
49 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
50 	char *prefix = "";
51 
52 	if (indent == 0 && desc != NULL) {
53 		(void) printf("                           "
54 		    " capacity   operations   bandwidth  ---- errors ----\n");
55 		(void) printf("description                "
56 		    "used avail  read write  read write  read write cksum\n");
57 	}
58 
59 	if (desc != NULL) {
60 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
61 
62 		if (is_log)
63 			prefix = "log ";
64 
65 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
66 		    (uint64_t **)&vs, &c) != 0)
67 			vs = &v0;
68 
69 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
70 
71 		nicenum(vs->vs_alloc, used, sizeof (used));
72 		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
73 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
74 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
75 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
76 		    sizeof (rbytes));
77 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
78 		    sizeof (wbytes));
79 		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
80 		nicenum(vs->vs_write_errors, werr, sizeof (werr));
81 		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
82 
83 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
84 		    indent, "",
85 		    prefix,
86 		    indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
87 		    desc,
88 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
89 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
90 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
91 	}
92 
93 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
94 		return;
95 
96 	for (c = 0; c < children; c++) {
97 		nvlist_t *cnv = child[c];
98 		char *cname, *tname;
99 		uint64_t np;
100 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
101 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
102 			cname = "<unknown>";
103 		tname = calloc(1, strlen(cname) + 2);
104 		(void) strcpy(tname, cname);
105 		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
106 			tname[strlen(tname)] = '0' + np;
107 		show_vdev_stats(tname, ctype, cnv, indent + 2);
108 		free(tname);
109 	}
110 }
111 
112 void
113 show_pool_stats(spa_t *spa)
114 {
115 	nvlist_t *config, *nvroot;
116 	char *name;
117 
118 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
119 
120 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
121 	    &nvroot) == 0);
122 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
123 	    &name) == 0);
124 
125 	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
126 	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
127 	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
128 
129 	nvlist_free(config);
130 }
131