1*789Sahrens /* 2*789Sahrens * CDDL HEADER START 3*789Sahrens * 4*789Sahrens * The contents of this file are subject to the terms of the 5*789Sahrens * Common Development and Distribution License, Version 1.0 only 6*789Sahrens * (the "License"). You may not use this file except in compliance 7*789Sahrens * with the License. 8*789Sahrens * 9*789Sahrens * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 10*789Sahrens * or http://www.opensolaris.org/os/licensing. 11*789Sahrens * See the License for the specific language governing permissions 12*789Sahrens * and limitations under the License. 13*789Sahrens * 14*789Sahrens * When distributing Covered Code, include this CDDL HEADER in each 15*789Sahrens * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 16*789Sahrens * If applicable, add the following below this CDDL HEADER, with the 17*789Sahrens * fields enclosed by brackets "[]" replaced with your own identifying 18*789Sahrens * information: Portions Copyright [yyyy] [name of copyright owner] 19*789Sahrens * 20*789Sahrens * CDDL HEADER END 21*789Sahrens */ 22*789Sahrens /* 23*789Sahrens * Copyright 2005 Sun Microsystems, Inc. All rights reserved. 24*789Sahrens * Use is subject to license terms. 25*789Sahrens */ 26*789Sahrens 27*789Sahrens #pragma ident "%Z%%M% %I% %E% SMI" 28*789Sahrens 29*789Sahrens #include <assert.h> 30*789Sahrens #include <sys/zfs_context.h> 31*789Sahrens #include <sys/avl.h> 32*789Sahrens #include <string.h> 33*789Sahrens #include <stdio.h> 34*789Sahrens #include <stdlib.h> 35*789Sahrens #include <sys/spa.h> 36*789Sahrens #include <sys/fs/zfs.h> 37*789Sahrens 38*789Sahrens /* 39*789Sahrens * Routines needed by more than one client of libzpool. 40*789Sahrens */ 41*789Sahrens 42*789Sahrens void 43*789Sahrens nicenum(uint64_t num, char *buf) 44*789Sahrens { 45*789Sahrens uint64_t n = num; 46*789Sahrens int index = 0; 47*789Sahrens char u; 48*789Sahrens 49*789Sahrens while (n >= 1024) { 50*789Sahrens n = (n + (1024 / 2)) / 1024; /* Round up or down */ 51*789Sahrens index++; 52*789Sahrens } 53*789Sahrens 54*789Sahrens u = " KMGTPE"[index]; 55*789Sahrens 56*789Sahrens if (index == 0) { 57*789Sahrens (void) sprintf(buf, "%llu", (u_longlong_t)n); 58*789Sahrens } else if (n < 10 && (num & (num - 1)) != 0) { 59*789Sahrens (void) sprintf(buf, "%.2f%c", 60*789Sahrens (double)num / (1ULL << 10 * index), u); 61*789Sahrens } else if (n < 100 && (num & (num - 1)) != 0) { 62*789Sahrens (void) sprintf(buf, "%.1f%c", 63*789Sahrens (double)num / (1ULL << 10 * index), u); 64*789Sahrens } else { 65*789Sahrens (void) sprintf(buf, "%llu%c", (u_longlong_t)n, u); 66*789Sahrens } 67*789Sahrens } 68*789Sahrens 69*789Sahrens static void 70*789Sahrens show_vdev_stats(const char *desc, nvlist_t *nv, int indent) 71*789Sahrens { 72*789Sahrens nvlist_t **child; 73*789Sahrens uint_t c, children; 74*789Sahrens vdev_stat_t *vs; 75*789Sahrens uint64_t sec; 76*789Sahrens char used[6], avail[6]; 77*789Sahrens char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6]; 78*789Sahrens 79*789Sahrens if (indent == 0) { 80*789Sahrens (void) printf(" " 81*789Sahrens " capacity operations bandwidth ---- errors ----\n"); 82*789Sahrens (void) printf("description " 83*789Sahrens "used avail read write read write read write cksum\n"); 84*789Sahrens } 85*789Sahrens 86*789Sahrens VERIFY(nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_STATS, 87*789Sahrens (uint64_t **)&vs, &c) == 0); 88*789Sahrens 89*789Sahrens sec = MAX(1, vs->vs_timestamp / NANOSEC); 90*789Sahrens 91*789Sahrens nicenum(vs->vs_alloc, used); 92*789Sahrens nicenum(vs->vs_space - vs->vs_alloc, avail); 93*789Sahrens nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops); 94*789Sahrens nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops); 95*789Sahrens nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes); 96*789Sahrens nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes); 97*789Sahrens nicenum(vs->vs_read_errors, rerr); 98*789Sahrens nicenum(vs->vs_write_errors, werr); 99*789Sahrens nicenum(vs->vs_checksum_errors, cerr); 100*789Sahrens 101*789Sahrens (void) printf("%*s%*s%*s%*s %5s %5s %5s %5s %5s %5s %5s\n", 102*789Sahrens indent, "", 103*789Sahrens indent - 19 - (vs->vs_space ? 0 : 12), desc, 104*789Sahrens vs->vs_space ? 6 : 0, vs->vs_space ? used : "", 105*789Sahrens vs->vs_space ? 6 : 0, vs->vs_space ? avail : "", 106*789Sahrens rops, wops, rbytes, wbytes, rerr, werr, cerr); 107*789Sahrens 108*789Sahrens if (nvlist_lookup_nvlist_array(nv, ZPOOL_CONFIG_CHILDREN, 109*789Sahrens &child, &children) != 0) 110*789Sahrens return; 111*789Sahrens 112*789Sahrens for (c = 0; c < children; c++) { 113*789Sahrens nvlist_t *cnv = child[c]; 114*789Sahrens char *cname; 115*789Sahrens if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) && 116*789Sahrens nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname)) 117*789Sahrens cname = "<unknown>"; 118*789Sahrens show_vdev_stats(cname, cnv, indent + 2); 119*789Sahrens } 120*789Sahrens } 121*789Sahrens 122*789Sahrens void 123*789Sahrens show_pool_stats(spa_t *spa) 124*789Sahrens { 125*789Sahrens nvlist_t *config = NULL; 126*789Sahrens nvlist_t *nvroot = NULL; 127*789Sahrens 128*789Sahrens spa_config_enter(spa, RW_READER); 129*789Sahrens VERIFY(spa_get_stats(spa_name(spa), &config) == 0); 130*789Sahrens VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE, 131*789Sahrens &nvroot) == 0); 132*789Sahrens 133*789Sahrens show_vdev_stats(spa_name(spa), nvroot, 0); 134*789Sahrens spa_config_exit(spa); 135*789Sahrens } 136