util.c revision 8654d0253136055bd4cc2423d87378e8a37f2eb5
2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#pragma ident "%Z%%M% %I% %E% SMI"
2N/A
2N/A#include <assert.h>
2N/A#include <sys/zfs_context.h>
2N/A#include <sys/avl.h>
2N/A#include <string.h>
2N/A#include <stdio.h>
2N/A#include <stdlib.h>
2N/A#include <sys/spa.h>
2N/A#include <sys/fs/zfs.h>
2N/A#include <sys/refcount.h>
2N/A
2N/A/*
2N/A * Routines needed by more than one client of libzpool.
2N/A */
2N/A
2N/Avoid
2N/Anicenum(uint64_t num, char *buf)
2N/A{
2N/A uint64_t n = num;
2N/A int index = 0;
2N/A char u;
2N/A
2N/A while (n >= 1024) {
2N/A n = (n + (1024 / 2)) / 1024; /* Round up or down */
2N/A index++;
2N/A }
2N/A
2N/A u = " KMGTPE"[index];
2N/A
2N/A if (index == 0) {
2N/A (void) sprintf(buf, "%llu", (u_longlong_t)n);
2N/A } else if (n < 10 && (num & (num - 1)) != 0) {
2N/A (void) sprintf(buf, "%.2f%c",
2N/A (double)num / (1ULL << 10 * index), u);
2N/A } else if (n < 100 && (num & (num - 1)) != 0) {
2N/A (void) sprintf(buf, "%.1f%c",
2N/A (double)num / (1ULL << 10 * index), u);
2N/A } else {
2N/A (void) sprintf(buf, "%llu%c", (u_longlong_t)n, u);
2N/A }
2N/A}
2N/A
2N/Astatic void
2N/Ashow_vdev_stats(const char *desc, nvlist_t *nv, int indent)
2N/A{
2N/A nvlist_t **child;
2N/A uint_t c, children;
2N/A vdev_stat_t *vs;
2N/A uint64_t sec;
2N/A uint64_t is_log = 0;
2N/A char used[6], avail[6];
char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
char *prefix = "";
if (indent == 0) {
(void) printf(" "
" capacity operations bandwidth ---- errors ----\n");
(void) printf("description "
"used avail read write read write read write cksum\n");
}
VERIFY(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS,
(uint64_t **)&vs, &c) == 0);
(void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
if (is_log)
prefix = "log ";
sec = MAX(1, vs->vs_timestamp / NANOSEC);
nicenum(vs->vs_alloc, used);
nicenum(vs->vs_space - vs->vs_alloc, avail);
nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops);
nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops);
nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes);
nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes);
nicenum(vs->vs_read_errors, rerr);
nicenum(vs->vs_write_errors, werr);
nicenum(vs->vs_checksum_errors, cerr);
(void) printf("%*s%s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n",
indent, "",
prefix,
indent + strlen(prefix) - 19 - (vs->vs_space ? 0 : 12), desc,
vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
rops, wops, rbytes, wbytes, rerr, werr, cerr);
if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN,
&child, &children) != 0)
return;
for (c = 0; c < children; c++) {
nvlist_t *cnv = child[c];
char *cname, *tname;
uint64_t np;
if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
cname = "<unknown>";
tname = calloc(1, strlen(cname) + 2);
(void) strcpy(tname, cname);
if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
tname[strlen(tname)] = '0' + np;
show_vdev_stats(tname, cnv, indent + 2);
free(tname);
}
}
void
show_pool_stats(spa_t *spa)
{
nvlist_t *config, *nvroot;
char *name;
spa_config_enter(spa, RW_READER, FTAG);
config = spa_config_generate(spa, NULL, -1ULL, B_TRUE);
spa_config_exit(spa, FTAG);
VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
&nvroot) == 0);
VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
&name) == 0);
show_vdev_stats(name, nvroot, 0);
}