1168404Spjd/*
2168404Spjd * CDDL HEADER START
3168404Spjd *
4168404Spjd * The contents of this file are subject to the terms of the
5168404Spjd * Common Development and Distribution License (the "License").
6168404Spjd * You may not use this file except in compliance with the License.
7168404Spjd *
8168404Spjd * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9168404Spjd * or http://www.opensolaris.org/os/licensing.
10168404Spjd * See the License for the specific language governing permissions
11168404Spjd * and limitations under the License.
12168404Spjd *
13168404Spjd * When distributing Covered Code, include this CDDL HEADER in each
14168404Spjd * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15168404Spjd * If applicable, add the following below this CDDL HEADER, with the
16168404Spjd * fields enclosed by brackets "[]" replaced with your own identifying
17168404Spjd * information: Portions Copyright [yyyy] [name of copyright owner]
18168404Spjd *
19168404Spjd * CDDL HEADER END
20168404Spjd */
21168404Spjd/*
22219089Spjd * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
23168404Spjd */
24168404Spjd
25168404Spjd#include <assert.h>
26168404Spjd#include <sys/zfs_context.h>
27168404Spjd#include <sys/avl.h>
28168404Spjd#include <string.h>
29168404Spjd#include <stdio.h>
30168404Spjd#include <stdlib.h>
31168404Spjd#include <sys/spa.h>
32168404Spjd#include <sys/fs/zfs.h>
33168404Spjd#include <sys/refcount.h>
34168404Spjd
35168404Spjd/*
36168404Spjd * Routines needed by more than one client of libzpool.
37168404Spjd */
38168404Spjd
39168404Spjdvoid
40168404Spjdnicenum(uint64_t num, char *buf)
41168404Spjd{
42168404Spjd	uint64_t n = num;
43168404Spjd	int index = 0;
44168404Spjd	char u;
45168404Spjd
46168404Spjd	while (n >= 1024) {
47168404Spjd		n = (n + (1024 / 2)) / 1024; /* Round up or down */
48168404Spjd		index++;
49168404Spjd	}
50168404Spjd
51168404Spjd	u = " KMGTPE"[index];
52168404Spjd
53168404Spjd	if (index == 0) {
54168404Spjd		(void) sprintf(buf, "%llu", (u_longlong_t)n);
55168404Spjd	} else if (n < 10 && (num & (num - 1)) != 0) {
56168404Spjd		(void) sprintf(buf, "%.2f%c",
57168404Spjd		    (double)num / (1ULL << 10 * index), u);
58168404Spjd	} else if (n < 100 && (num & (num - 1)) != 0) {
59168404Spjd		(void) sprintf(buf, "%.1f%c",
60168404Spjd		    (double)num / (1ULL << 10 * index), u);
61168404Spjd	} else {
62168404Spjd		(void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
63168404Spjd	}
64168404Spjd}
65168404Spjd
66168404Spjdstatic void
67185029Spjdshow_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
68168404Spjd{
69185029Spjd	vdev_stat_t *vs;
70185029Spjd	vdev_stat_t v0 = { 0 };
71185029Spjd	uint64_t sec;
72185029Spjd	uint64_t is_log = 0;
73168404Spjd	nvlist_t **child;
74168404Spjd	uint_t c, children;
75168404Spjd	char used[6], avail[6];
76168404Spjd	char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
77185029Spjd	char *prefix = "";
78168404Spjd
79185029Spjd	if (indent == 0 && desc != NULL) {
80185029Spjd		(void) printf("                           "
81168404Spjd		    " capacity   operations   bandwidth  ---- errors ----\n");
82185029Spjd		(void) printf("description                "
83168404Spjd		    "used avail  read write  read write  read write cksum\n");
84168404Spjd	}
85168404Spjd
86185029Spjd	if (desc != NULL) {
87185029Spjd		(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
88168404Spjd
89185029Spjd		if (is_log)
90185029Spjd			prefix = "log ";
91168404Spjd
92219089Spjd		if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
93185029Spjd		    (uint64_t **)&vs, &c) != 0)
94185029Spjd			vs = &v0;
95168404Spjd
96185029Spjd		sec = MAX(1, vs->vs_timestamp / NANOSEC);
97168404Spjd
98185029Spjd		nicenum(vs->vs_alloc, used);
99185029Spjd		nicenum(vs->vs_space - vs->vs_alloc, avail);
100185029Spjd		nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
101185029Spjd		nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
102185029Spjd		nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
103185029Spjd		nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
104185029Spjd		nicenum(vs->vs_read_errors, rerr);
105185029Spjd		nicenum(vs->vs_write_errors, werr);
106185029Spjd		nicenum(vs->vs_checksum_errors, cerr);
107185029Spjd
108185029Spjd		(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
109185029Spjd		    indent, "",
110185029Spjd		    prefix,
111185029Spjd		    indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
112185029Spjd		    desc,
113185029Spjd		    vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
114185029Spjd		    vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
115185029Spjd		    rops, wops, rbytes, wbytes, rerr, werr, cerr);
116185029Spjd	}
117185029Spjd
118185029Spjd	if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
119168404Spjd		return;
120168404Spjd
121168404Spjd	for (c = 0; c < children; c++) {
122168404Spjd		nvlist_t *cnv = child[c];
123168404Spjd		char *cname, *tname;
124168404Spjd		uint64_t np;
125168404Spjd		if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
126168404Spjd		    nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
127168404Spjd			cname = "<unknown>";
128168404Spjd		tname = calloc(1, strlen(cname) + 2);
129168404Spjd		(void) strcpy(tname, cname);
130168404Spjd		if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
131168404Spjd			tname[strlen(tname)] = '0' + np;
132185029Spjd		show_vdev_stats(tname, ctype, cnv, indent + 2);
133168404Spjd		free(tname);
134168404Spjd	}
135168404Spjd}
136168404Spjd
137168404Spjdvoid
138168404Spjdshow_pool_stats(spa_t *spa)
139168404Spjd{
140168404Spjd	nvlist_t *config, *nvroot;
141168404Spjd	char *name;
142168404Spjd
143185029Spjd	VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
144168404Spjd
145168404Spjd	VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
146168404Spjd	    &nvroot) == 0);
147168404Spjd	VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
148168404Spjd	    &name) == 0);
149168404Spjd
150185029Spjd	show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
151185029Spjd	show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
152185029Spjd	show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
153185029Spjd
154185029Spjd	nvlist_free(config);
155168404Spjd}
156