1789Sahrens /*
2789Sahrens * CDDL HEADER START
3789Sahrens *
4789Sahrens * The contents of this file are subject to the terms of the
51544Seschrock * Common Development and Distribution License (the "License").
61544Seschrock * You may not use this file except in compliance with the License.
7789Sahrens *
8789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9789Sahrens * or http://www.opensolaris.org/os/licensing.
10789Sahrens * See the License for the specific language governing permissions
11789Sahrens * and limitations under the License.
12789Sahrens *
13789Sahrens * When distributing Covered Code, include this CDDL HEADER in each
14789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15789Sahrens * If applicable, add the following below this CDDL HEADER, with the
16789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying
17789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner]
18789Sahrens *
19789Sahrens * CDDL HEADER END
20789Sahrens */
21789Sahrens /*
22*12296SLin.Ling@Sun.COM * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23789Sahrens */
24789Sahrens
25789Sahrens #include <assert.h>
26789Sahrens #include <sys/zfs_context.h>
27789Sahrens #include <sys/avl.h>
28789Sahrens #include <string.h>
29789Sahrens #include <stdio.h>
30789Sahrens #include <stdlib.h>
31789Sahrens #include <sys/spa.h>
32789Sahrens #include <sys/fs/zfs.h>
331544Seschrock #include <sys/refcount.h>
34789Sahrens
35789Sahrens /*
36789Sahrens * Routines needed by more than one client of libzpool.
37789Sahrens */
38789Sahrens
39789Sahrens void
nicenum(uint64_t num,char * buf)40789Sahrens nicenum(uint64_t num, char *buf)
41789Sahrens {
42789Sahrens uint64_t n = num;
43789Sahrens int index = 0;
44789Sahrens char u;
45789Sahrens
46789Sahrens while (n >= 1024) {
47789Sahrens n = (n + (1024 / 2)) / 1024; /* Round up or down */
48789Sahrens index++;
49789Sahrens }
50789Sahrens
51789Sahrens u = " KMGTPE"[index];
52789Sahrens
53789Sahrens if (index == 0) {
54789Sahrens (void) sprintf(buf, "%llu", (u_longlong_t)n);
55789Sahrens } else if (n < 10 && (num & (num - 1)) != 0) {
56789Sahrens (void) sprintf(buf, "%.2f%c",
57789Sahrens (double)num / (1ULL << 10 * index), u);
58789Sahrens } else if (n < 100 && (num & (num - 1)) != 0) {
59789Sahrens (void) sprintf(buf, "%.1f%c",
60789Sahrens (double)num / (1ULL << 10 * index), u);
61789Sahrens } else {
62789Sahrens (void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
63789Sahrens }
64789Sahrens }
65789Sahrens
66789Sahrens static void
show_vdev_stats(const char * desc,const char * ctype,nvlist_t * nv,int indent)677754SJeff.Bonwick@Sun.COM show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
68789Sahrens {
697754SJeff.Bonwick@Sun.COM vdev_stat_t *vs;
707754SJeff.Bonwick@Sun.COM vdev_stat_t v0 = { 0 };
717754SJeff.Bonwick@Sun.COM uint64_t sec;
727754SJeff.Bonwick@Sun.COM uint64_t is_log = 0;
73789Sahrens nvlist_t **child;
74789Sahrens uint_t c, children;
75789Sahrens char used[6], avail[6];
76789Sahrens char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
774527Sperrin char *prefix = "";
78789Sahrens
797754SJeff.Bonwick@Sun.COM if (indent == 0 && desc != NULL) {
807754SJeff.Bonwick@Sun.COM (void) printf(" "
81789Sahrens " capacity operations bandwidth ---- errors ----\n");
827754SJeff.Bonwick@Sun.COM (void) printf("description "
83789Sahrens "used avail read write read write read write cksum\n");
84789Sahrens }
85789Sahrens
867754SJeff.Bonwick@Sun.COM if (desc != NULL) {
877754SJeff.Bonwick@Sun.COM (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
887754SJeff.Bonwick@Sun.COM
897754SJeff.Bonwick@Sun.COM if (is_log)
907754SJeff.Bonwick@Sun.COM prefix = "log ";
914527Sperrin
92*12296SLin.Ling@Sun.COM if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
937754SJeff.Bonwick@Sun.COM (uint64_t **)&vs, &c) != 0)
947754SJeff.Bonwick@Sun.COM vs = &v0;
95789Sahrens
967754SJeff.Bonwick@Sun.COM sec = MAX(1, vs->vs_timestamp / NANOSEC);
97789Sahrens
987754SJeff.Bonwick@Sun.COM nicenum(vs->vs_alloc, used);
997754SJeff.Bonwick@Sun.COM nicenum(vs->vs_space - vs->vs_alloc, avail);
1007754SJeff.Bonwick@Sun.COM nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
1017754SJeff.Bonwick@Sun.COM nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
1027754SJeff.Bonwick@Sun.COM nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
1037754SJeff.Bonwick@Sun.COM nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
1047754SJeff.Bonwick@Sun.COM nicenum(vs->vs_read_errors, rerr);
1057754SJeff.Bonwick@Sun.COM nicenum(vs->vs_write_errors, werr);
1067754SJeff.Bonwick@Sun.COM nicenum(vs->vs_checksum_errors, cerr);
107789Sahrens
1087754SJeff.Bonwick@Sun.COM (void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
1097754SJeff.Bonwick@Sun.COM indent, "",
1107754SJeff.Bonwick@Sun.COM prefix,
1117754SJeff.Bonwick@Sun.COM indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
1127754SJeff.Bonwick@Sun.COM desc,
1137754SJeff.Bonwick@Sun.COM vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
1147754SJeff.Bonwick@Sun.COM vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
1157754SJeff.Bonwick@Sun.COM rops, wops, rbytes, wbytes, rerr, werr, cerr);
1167754SJeff.Bonwick@Sun.COM }
117789Sahrens
1187754SJeff.Bonwick@Sun.COM if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
119789Sahrens return;
120789Sahrens
121789Sahrens for (c = 0; c < children; c++) {
122789Sahrens nvlist_t *cnv = child[c];
1232082Seschrock char *cname, *tname;
1242082Seschrock uint64_t np;
125789Sahrens if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
126789Sahrens nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
127789Sahrens cname = "<unknown>";
1282082Seschrock tname = calloc(1, strlen(cname) + 2);
1292082Seschrock (void) strcpy(tname, cname);
1302082Seschrock if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
1312082Seschrock tname[strlen(tname)] = '0' + np;
1327754SJeff.Bonwick@Sun.COM show_vdev_stats(tname, ctype, cnv, indent + 2);
1332082Seschrock free(tname);
134789Sahrens }
135789Sahrens }
136789Sahrens
137789Sahrens void
show_pool_stats(spa_t * spa)138789Sahrens show_pool_stats(spa_t *spa)
139789Sahrens {
1401635Sbonwick nvlist_t *config, *nvroot;
1411635Sbonwick char *name;
142789Sahrens
1437754SJeff.Bonwick@Sun.COM VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
1441635Sbonwick
145789Sahrens VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
146789Sahrens &nvroot) == 0);
1471635Sbonwick VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
1481635Sbonwick &name) == 0);
149789Sahrens
1507754SJeff.Bonwick@Sun.COM show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
1517754SJeff.Bonwick@Sun.COM show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
1527754SJeff.Bonwick@Sun.COM show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
1537754SJeff.Bonwick@Sun.COM
1547754SJeff.Bonwick@Sun.COM nvlist_free(config);
155789Sahrens }
156