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