1 /*
   2  * CDDL HEADER START
   3  *
   4  * The contents of this file are subject to the terms of the
   5  * Common Development and Distribution License (the "License").
   6  * You may not use this file except in compliance with the License.
   7  *
   8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
   9  * or http://www.opensolaris.org/os/licensing.
  10  * See the License for the specific language governing permissions
  11  * and limitations under the License.
  12  *
  13  * When distributing Covered Code, include this CDDL HEADER in each
  14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  15  * If applicable, add the following below this CDDL HEADER, with the
  16  * fields enclosed by brackets "[]" replaced with your own identifying
  17  * information: Portions Copyright [yyyy] [name of copyright owner]
  18  *
  19  * CDDL HEADER END
  20  */
  21 /*
  22  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
  23  * Copyright 2013 Nexenta Systems, Inc.  All rights reserved.
  24  * Copyright (c) 2016 by Delphix. All rights reserved.
  25  * Copyright 2017 RackTop Systems.
  26  */
  27 
  28 #include <assert.h>
  29 #include <sys/zfs_context.h>
  30 #include <sys/avl.h>
  31 #include <string.h>
  32 #include <stdio.h>
  33 #include <stdlib.h>
  34 #include <sys/spa.h>
  35 #include <sys/fs/zfs.h>
  36 #include <sys/refcount.h>
  37 #include <dlfcn.h>
  38 
  39 extern void nicenum(uint64_t num, char *buf, size_t);
  40 
  41 /*
  42  * Routines needed by more than one client of libzpool.
  43  */
  44 
  45 static void
  46 show_vdev_stats(const char *desc, const char *ctype, nvlist_t *nv, int indent)
  47 {
  48         vdev_stat_t *vs;
  49         vdev_stat_t v0 = { 0 };
  50         uint64_t sec, ops_rd, ops_wr;
  51         uint64_t is_log = 0;
  52         nvlist_t **child;
  53         uint_t c, children;
  54         char used[6], avail[6];
  55         char rops[6], wops[6], rbytes[6], wbytes[6], rerr[6], werr[6], cerr[6];
  56         char riotime[8], wiotime[8];
  57         char *prefix = "";
  58 
  59         if (indent == 0 && desc != NULL) {
  60                 (void) printf("                           "
  61                     " capacity   operations   bandwidth   --- latency ---  "
  62                     "---- errors ----\n");
  63                 (void) printf("description                "
  64                     "used avail  read write  read write     read    write  "
  65                     "read write cksum\n");
  66         }
  67 
  68         if (desc != NULL) {
  69                 (void) nvlist_lookup_uint64(nv, ZPOOL_CONFIG_IS_LOG, &is_log);
  70 
  71                 if (is_log)
  72                         prefix = "log ";
  73 
  74                 if (nvlist_lookup_uint64_array(nv, ZPOOL_CONFIG_VDEV_STATS,
  75                     (uint64_t **)&vs, &c) != 0)
  76                         vs = &v0;
  77 
  78                 sec = MAX(1, vs->vs_timestamp / NANOSEC);
  79                 ops_rd = MAX(1, vs->vs_ops[ZIO_TYPE_READ]);
  80                 ops_wr = MAX(1, vs->vs_ops[ZIO_TYPE_WRITE]);
  81 
  82                 nicenum(vs->vs_alloc, used, sizeof (used));
  83                 nicenum(vs->vs_space - vs->vs_alloc, avail, sizeof (avail));
  84                 nicenum(vs->vs_ops[ZIO_TYPE_READ] / sec, rops, sizeof (rops));
  85                 nicenum(vs->vs_ops[ZIO_TYPE_WRITE] / sec, wops, sizeof (wops));
  86                 nicenum(vs->vs_bytes[ZIO_TYPE_READ] / sec, rbytes,
  87                     sizeof (rbytes));
  88                 nicenum(vs->vs_bytes[ZIO_TYPE_WRITE] / sec, wbytes,
  89                     sizeof (wbytes));
  90                 nicenum(vs->vs_iotime[ZIO_TYPE_READ] / ops_rd, riotime,
  91                     sizeof (riotime));
  92                 nicenum(vs->vs_iotime[ZIO_TYPE_WRITE] / ops_wr, wiotime,
  93                     sizeof (wiotime));
  94                 nicenum(vs->vs_read_errors, rerr, sizeof (rerr));
  95                 nicenum(vs->vs_write_errors, werr, sizeof (werr));
  96                 nicenum(vs->vs_checksum_errors, cerr, sizeof (cerr));
  97 
  98                 (void) printf(
  99                     "%*s%s%*s%*s%*s %5s %5s %5s %5s %8s %8s %5s %5s %5s\n",
 100                     indent, "",
 101                     prefix,
 102                     indent + strlen(prefix) - 25 - (vs->vs_space ? 0 : 12),
 103                     desc,
 104                     vs->vs_space ? 6 : 0, vs->vs_space ? used : "",
 105                     vs->vs_space ? 6 : 0, vs->vs_space ? avail : "",
 106                     rops, wops, rbytes, wbytes, riotime, wiotime, rerr, werr,
 107                     cerr);
 108         }
 109 
 110         if (nvlist_lookup_nvlist_array(nv, ctype, &child, &children) != 0)
 111                 return;
 112 
 113         for (c = 0; c < children; c++) {
 114                 nvlist_t *cnv = child[c];
 115                 char *cname, *tname;
 116                 uint64_t np;
 117                 if (nvlist_lookup_string(cnv, ZPOOL_CONFIG_PATH, &cname) &&
 118                     nvlist_lookup_string(cnv, ZPOOL_CONFIG_TYPE, &cname))
 119                         cname = "<unknown>";
 120                 tname = calloc(1, strlen(cname) + 2);
 121                 (void) strcpy(tname, cname);
 122                 if (nvlist_lookup_uint64(cnv, ZPOOL_CONFIG_NPARITY, &np) == 0)
 123                         tname[strlen(tname)] = '0' + np;
 124                 show_vdev_stats(tname, ctype, cnv, indent + 2);
 125                 free(tname);
 126         }
 127 }
 128 
 129 void
 130 show_pool_stats(spa_t *spa)
 131 {
 132         nvlist_t *config, *nvroot;
 133         char *name;
 134 
 135         VERIFY(spa_get_stats(spa_name(spa), &config, NULL, 0) == 0);
 136 
 137         VERIFY(nvlist_lookup_nvlist(config, ZPOOL_CONFIG_VDEV_TREE,
 138             &nvroot) == 0);
 139         VERIFY(nvlist_lookup_string(config, ZPOOL_CONFIG_POOL_NAME,
 140             &name) == 0);
 141 
 142         show_vdev_stats(name, ZPOOL_CONFIG_CHILDREN, nvroot, 0);
 143         show_vdev_stats(NULL, ZPOOL_CONFIG_L2CACHE, nvroot, 0);
 144         show_vdev_stats(NULL, ZPOOL_CONFIG_SPARES, nvroot, 0);
 145 
 146         nvlist_free(config);
 147 }
 148 
 149 /*
 150  * Sets given global variable in libzpool to given unsigned 32-bit value.
 151  * arg: "<variable>=<value>"
 152  */
 153 int
 154 set_global_var(char *arg)
 155 {
 156         void *zpoolhdl;
 157         char *varname = arg, *varval;
 158         u_longlong_t val;
 159 
 160 #ifndef _LITTLE_ENDIAN
 161         /*
 162          * On big endian systems changing a 64-bit variable would set the high
 163          * 32 bits instead of the low 32 bits, which could cause unexpected
 164          * results.
 165          */
 166         fprintf(stderr, "Setting global variables is only supported on "
 167             "little-endian systems\n", varname);
 168         return (ENOTSUP);
 169 #endif
 170         if ((varval = strchr(arg, '=')) != NULL) {
 171                 *varval = '\0';
 172                 varval++;
 173                 val = strtoull(varval, NULL, 0);
 174                 if (val > UINT32_MAX) {
 175                         fprintf(stderr, "Value for global variable '%s' must "
 176                             "be a 32-bit unsigned integer\n", varname);
 177                         return (EOVERFLOW);
 178                 }
 179         } else {
 180                 return (EINVAL);
 181         }
 182 
 183         zpoolhdl = dlopen("libzpool.so", RTLD_LAZY);
 184         if (zpoolhdl != NULL) {
 185                 uint32_t *var;
 186                 var = dlsym(zpoolhdl, varname);
 187                 if (var == NULL) {
 188                         fprintf(stderr, "Global variable '%s' does not exist "
 189                             "in libzpool.so\n", varname);
 190                         return (EINVAL);
 191                 }
 192                 *var = (uint32_t)val;
 193 
 194                 dlclose(zpoolhdl);
 195         } else {
 196                 fprintf(stderr, "Failed to open libzpool.so to set global "
 197                     "variable\n");
 198                 return (EIO);
 199         }
 200 
 201         return (0);
 202 }