xref: /titanic_51/usr/src/lib/libzpool/common/util.c (revision fc30d4661fdf50c4c69dacbc572af35439c28cd8)
1fa9e4066Sahrens /*
2fa9e4066Sahrens  * CDDL HEADER START
3fa9e4066Sahrens  *
4fa9e4066Sahrens  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * 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 /*
223f9d6ad7SLin Ling  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23fa9e4066Sahrens  */
24fa9e4066Sahrens 
25fa9e4066Sahrens #include <assert.h>
26fa9e4066Sahrens #include <sys/zfs_context.h>
27fa9e4066Sahrens #include <sys/avl.h>
28fa9e4066Sahrens #include <string.h>
29fa9e4066Sahrens #include <stdio.h>
30fa9e4066Sahrens #include <stdlib.h>
31fa9e4066Sahrens #include <sys/spa.h>
32fa9e4066Sahrens #include <sys/fs/zfs.h>
33ea8dc4b6Seschrock #include <sys/refcount.h>
34fa9e4066Sahrens 
35fa9e4066Sahrens /*
36fa9e4066Sahrens  * Routines needed by more than one client of libzpool.
37fa9e4066Sahrens  */
38fa9e4066Sahrens 
39fa9e4066Sahrens static void
40e14bb325SJeff Bonwick show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
41fa9e4066Sahrens {
42fa9e4066Sahrens 	vdev_stat_t *vs;
43e14bb325SJeff Bonwick 	vdev_stat_t v0 = { 0 };
44fa9e4066Sahrens 	uint64_t sec;
458654d025Sperrin 	uint64_t is_log = 0;
46e14bb325SJeff Bonwick 	nvlist_t **child;
47e14bb325SJeff Bonwick 	uint_t c, children;
48fa9e4066Sahrens 	char used[6], avail[6];
49fa9e4066Sahrens 	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
508654d025Sperrin 	char *prefix = "";
51fa9e4066Sahrens 
52e14bb325SJeff Bonwick 	if (indent == 0 && desc != NULL) {
53fa9e4066Sahrens 		(void) printf("                           "
54fa9e4066Sahrens 		    " capacity   operations   bandwidth  ---- errors ----\n");
55fa9e4066Sahrens 		(void) printf("description                "
56fa9e4066Sahrens 		    "used avail  read write  read write  read write cksum\n");
57fa9e4066Sahrens 	}
58fa9e4066Sahrens 
59e14bb325SJeff Bonwick 	if (desc != NULL) {
608654d025Sperrin 		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
618654d025Sperrin 
628654d025Sperrin 		if (is_log)
638654d025Sperrin 			prefix = "log ";
64fa9e4066Sahrens 
653f9d6ad7SLin Ling 		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
66e14bb325SJeff Bonwick 		    (uint64_t **)&vs, &c) != 0)
67e14bb325SJeff Bonwick 			vs = &v0;
68e14bb325SJeff Bonwick 
69fa9e4066Sahrens 		sec = MAX(1, vs->vs_timestamp / NANOSEC);
70fa9e4066Sahrens 
71*fc30d466SJason King 		nicenum(vs->vs_alloc, used, sizeof (used));
72*fc30d466SJason King 		nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
73*fc30d466SJason King 		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
74*fc30d466SJason King 		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
75*fc30d466SJason King 		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
76*fc30d466SJason King 		    sizeof (rbytes));
77*fc30d466SJason King 		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
78*fc30d466SJason King 		    sizeof (wbytes));
79*fc30d466SJason King 		nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
80*fc30d466SJason King 		nicenum(vs->vs_write_errors, werr, sizeof (werr));
81*fc30d466SJason King 		nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
82fa9e4066Sahrens 
838654d025Sperrin 		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
84fa9e4066Sahrens 		    indent, "",
858654d025Sperrin 		    prefix,
86e14bb325SJeff Bonwick 		    indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
87e14bb325SJeff Bonwick 		    desc,
88fa9e4066Sahrens 		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
89fa9e4066Sahrens 		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
90fa9e4066Sahrens 		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
91e14bb325SJeff Bonwick 	}
92fa9e4066Sahrens 
93e14bb325SJeff Bonwick 	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
94fa9e4066Sahrens 		return;
95fa9e4066Sahrens 
96fa9e4066Sahrens 	for (c = 0; c < children; c++) {
97fa9e4066Sahrens 		nvlist_t *cnv = child[c];
9899653d4eSeschrock 		char *cname, *tname;
9999653d4eSeschrock 		uint64_t np;
100fa9e4066Sahrens 		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
101fa9e4066Sahrens 		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
102fa9e4066Sahrens 			cname = "<unknown>";
10399653d4eSeschrock 		tname = calloc(1, strlen(cname) + 2);
10499653d4eSeschrock 		(void) strcpy(tname, cname);
10599653d4eSeschrock 		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
10699653d4eSeschrock 			tname[strlen(tname)] = '0' + np;
107e14bb325SJeff Bonwick 		show_vdev_stats(tname, ctype, cnv, indent + 2);
10899653d4eSeschrock 		free(tname);
109fa9e4066Sahrens 	}
110fa9e4066Sahrens }
111fa9e4066Sahrens 
112fa9e4066Sahrens void
113fa9e4066Sahrens show_pool_stats(spa_t *spa)
114fa9e4066Sahrens {
1150373e76bSbonwick 	nvlist_t *config, *nvroot;
1160373e76bSbonwick 	char *name;
117fa9e4066Sahrens 
118e14bb325SJeff Bonwick 	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
1190373e76bSbonwick 
120fa9e4066Sahrens 	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
121fa9e4066Sahrens 	    &nvroot) == 0);
1220373e76bSbonwick 	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1230373e76bSbonwick 	    &name) == 0);
124fa9e4066Sahrens 
125e14bb325SJeff Bonwick 	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
126e14bb325SJeff Bonwick 	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
127e14bb325SJeff Bonwick 	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
128e14bb325SJeff Bonwick 
129e14bb325SJeff Bonwick 	nvlist_free(config);
130fa9e4066Sahrens }
131