1 // SPDX-License-Identifier: CDDL-1.0
2 /*
3 * This file and its contents are supplied under the terms of the
4 * Common Development and Distribution License ("CDDL"), version 1.0.
5 * You may only use this file in accordance with the terms of version
6 * 1.0 of the CDDL.
7 *
8 * A full copy of the text of the CDDL should have accompanied this
9 * source. A copy of the CDDL is also available via the Internet at
10 * https://opensource.org/license/CDDL-1.0.
11 */
12
13 /*
14 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
15 */
16
17 #include <stdio.h>
18 #include <stdlib.h>
19 #include <string.h>
20 #include <sys/nvpair.h>
21 #include <sys/fs/zfs.h>
22 #include <math.h>
23
24 #include <libzutil.h>
25
26 static void
dump_ddt_stat(const ddt_stat_t * dds,int h,boolean_t parsable)27 dump_ddt_stat(const ddt_stat_t *dds, int h, boolean_t parsable)
28 {
29 char refcnt[32];
30 char blocks[32], lsize[32], psize[32], dsize[32];
31 char ref_blocks[32], ref_lsize[32], ref_psize[32], ref_dsize[32];
32
33 enum zfs_nicenum_format count_format = parsable ?
34 ZFS_NICENUM_RAW : ZFS_NICENUM_1024;
35 enum zfs_nicenum_format byte_format = parsable ?
36 ZFS_NICENUM_RAW : ZFS_NICENUM_BYTES;
37
38 if (dds == NULL || dds->dds_blocks == 0)
39 return;
40
41 if (h == -1)
42 (void) strcpy(refcnt, "Total");
43 else
44 zfs_nicenum_format(1ULL << h, refcnt, sizeof (refcnt),
45 count_format);
46
47
48 zfs_nicenum_format(dds->dds_blocks, blocks, sizeof (blocks),
49 count_format);
50 zfs_nicenum_format(dds->dds_lsize, lsize, sizeof (lsize),
51 byte_format);
52 zfs_nicenum_format(dds->dds_psize, psize, sizeof (psize),
53 byte_format);
54 zfs_nicenum_format(dds->dds_dsize, dsize, sizeof (dsize),
55 byte_format);
56 zfs_nicenum_format(dds->dds_ref_blocks, ref_blocks,
57 sizeof (ref_blocks), count_format);
58 zfs_nicenum_format(dds->dds_ref_lsize, ref_lsize,
59 sizeof (ref_lsize), byte_format);
60 zfs_nicenum_format(dds->dds_ref_psize, ref_psize,
61 sizeof (ref_psize), byte_format);
62 zfs_nicenum_format(dds->dds_ref_dsize, ref_dsize,
63 sizeof (ref_dsize), byte_format);
64
65 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
66 refcnt,
67 blocks, lsize, psize, dsize,
68 ref_blocks, ref_lsize, ref_psize, ref_dsize);
69 }
70
71 /*
72 * Print the DDT histogram and the column totals.
73 */
74 void
zpool_dump_ddt(const ddt_stat_t * dds_total,const ddt_histogram_t * ddh,boolean_t parsable)75 zpool_dump_ddt(const ddt_stat_t *dds_total, const ddt_histogram_t *ddh,
76 boolean_t parsable)
77 {
78 int h;
79
80 (void) printf("\n");
81
82 (void) printf("bucket "
83 " allocated "
84 " referenced \n");
85 (void) printf("______ "
86 "______________________________ "
87 "______________________________\n");
88
89 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
90 "refcnt",
91 "blocks", "LSIZE", "PSIZE", "DSIZE",
92 "blocks", "LSIZE", "PSIZE", "DSIZE");
93
94 (void) printf("%6s %6s %5s %5s %5s %6s %5s %5s %5s\n",
95 "------",
96 "------", "-----", "-----", "-----",
97 "------", "-----", "-----", "-----");
98
99 for (h = 0; h < 64; h++)
100 dump_ddt_stat(&ddh->ddh_stat[h], h, parsable);
101
102 dump_ddt_stat(dds_total, -1, parsable);
103
104 (void) printf("\n");
105 }
106
107 /*
108 * Process the buffer of nvlists, unpacking and storing each nvlist record
109 * into 'records'. 'leftover' is set to the number of bytes that weren't
110 * processed as there wasn't a complete record.
111 */
112 int
zpool_history_unpack(char * buf,uint64_t bytes_read,uint64_t * leftover,nvlist_t *** records,uint_t * numrecords)113 zpool_history_unpack(char *buf, uint64_t bytes_read, uint64_t *leftover,
114 nvlist_t ***records, uint_t *numrecords)
115 {
116 uint64_t reclen;
117 nvlist_t *nv;
118 int i;
119 void *tmp;
120
121 while (bytes_read > sizeof (reclen)) {
122
123 /* get length of packed record (stored as little endian) */
124 for (i = 0, reclen = 0; i < sizeof (reclen); i++)
125 reclen += (uint64_t)(((uchar_t *)buf)[i]) << (8*i);
126
127 if (bytes_read < sizeof (reclen) + reclen)
128 break;
129
130 /* unpack record */
131 int err = nvlist_unpack(buf + sizeof (reclen), reclen, &nv, 0);
132 if (err != 0)
133 return (err);
134 bytes_read -= sizeof (reclen) + reclen;
135 buf += sizeof (reclen) + reclen;
136
137 /* add record to nvlist array */
138 (*numrecords)++;
139 if (ISP2(*numrecords + 1)) {
140 tmp = realloc(*records,
141 *numrecords * 2 * sizeof (nvlist_t *));
142 if (tmp == NULL) {
143 nvlist_free(nv);
144 (*numrecords)--;
145 return (ENOMEM);
146 }
147 *records = tmp;
148 }
149 (*records)[*numrecords - 1] = nv;
150 }
151
152 *leftover = bytes_read;
153 return (0);
154 }
155
156 /*
157 * Floating point sleep(). Allows you to pass in a floating point value for
158 * seconds.
159 */
160 void
fsleep(float sec)161 fsleep(float sec)
162 {
163 struct timespec req;
164 req.tv_sec = floor(sec);
165 req.tv_nsec = (sec - (float)req.tv_sec) * NANOSEC;
166 nanosleep(&req, NULL);
167 }
168
169 /*
170 * Get environment variable 'env' and return it as an integer.
171 * If 'env' is not set, then return 'default_val' instead.
172 */
173 int
zpool_getenv_int(const char * env,int default_val)174 zpool_getenv_int(const char *env, int default_val)
175 {
176 char *str;
177 int val;
178 str = getenv(env);
179 if ((str == NULL) || sscanf(str, "%d", &val) != 1 ||
180 val < 0) {
181 val = default_val;
182 }
183 return (val);
184 }
185