xref: /freebsd-src/sys/contrib/openzfs/lib/libzutil/zutil_nicenum.c (revision 271171e0d97b88ba2a7c3bf750c9672b484c1c13)
1eda14cbcSMatt Macy /*
2eda14cbcSMatt Macy  * CDDL HEADER START
3eda14cbcSMatt Macy  *
4eda14cbcSMatt Macy  * The contents of this file are subject to the terms of the
5eda14cbcSMatt Macy  * Common Development and Distribution License (the "License").
6eda14cbcSMatt Macy  * You may not use this file except in compliance with the License.
7eda14cbcSMatt Macy  *
8eda14cbcSMatt Macy  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*271171e0SMartin Matuska  * or https://opensource.org/licenses/CDDL-1.0.
10eda14cbcSMatt Macy  * See the License for the specific language governing permissions
11eda14cbcSMatt Macy  * and limitations under the License.
12eda14cbcSMatt Macy  *
13eda14cbcSMatt Macy  * When distributing Covered Code, include this CDDL HEADER in each
14eda14cbcSMatt Macy  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15eda14cbcSMatt Macy  * If applicable, add the following below this CDDL HEADER, with the
16eda14cbcSMatt Macy  * fields enclosed by brackets "[]" replaced with your own identifying
17eda14cbcSMatt Macy  * information: Portions Copyright [yyyy] [name of copyright owner]
18eda14cbcSMatt Macy  *
19eda14cbcSMatt Macy  * CDDL HEADER END
20eda14cbcSMatt Macy  */
21eda14cbcSMatt Macy 
22eda14cbcSMatt Macy /*
23eda14cbcSMatt Macy  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24eda14cbcSMatt Macy  */
25eda14cbcSMatt Macy 
26eda14cbcSMatt Macy #include <ctype.h>
27eda14cbcSMatt Macy #include <math.h>
28eda14cbcSMatt Macy #include <stdio.h>
29eda14cbcSMatt Macy #include <libzutil.h>
3081b22a98SMartin Matuska #include <string.h>
31eda14cbcSMatt Macy 
32eda14cbcSMatt Macy /*
33eda14cbcSMatt Macy  * Return B_TRUE if "str" is a number string, B_FALSE otherwise.
34eda14cbcSMatt Macy  * Works for integer and floating point numbers.
35eda14cbcSMatt Macy  */
36eda14cbcSMatt Macy boolean_t
zfs_isnumber(const char * str)37eda14cbcSMatt Macy zfs_isnumber(const char *str)
38eda14cbcSMatt Macy {
3916038816SMartin Matuska 	if (!*str)
4016038816SMartin Matuska 		return (B_FALSE);
4116038816SMartin Matuska 
42eda14cbcSMatt Macy 	for (; *str; str++)
43eda14cbcSMatt Macy 		if (!(isdigit(*str) || (*str == '.')))
44eda14cbcSMatt Macy 			return (B_FALSE);
45eda14cbcSMatt Macy 
4681b22a98SMartin Matuska 	/*
4781b22a98SMartin Matuska 	 * Numbers should not end with a period ("." ".." or "5." are
4881b22a98SMartin Matuska 	 * not valid)
4981b22a98SMartin Matuska 	 */
5081b22a98SMartin Matuska 	if (str[strlen(str) - 1] == '.') {
5181b22a98SMartin Matuska 		return (B_FALSE);
5281b22a98SMartin Matuska 	}
5381b22a98SMartin Matuska 
54eda14cbcSMatt Macy 	return (B_TRUE);
55eda14cbcSMatt Macy }
56eda14cbcSMatt Macy 
57eda14cbcSMatt Macy /*
58eda14cbcSMatt Macy  * Convert a number to an appropriately human-readable output.
59eda14cbcSMatt Macy  */
60eda14cbcSMatt Macy void
zfs_nicenum_format(uint64_t num,char * buf,size_t buflen,enum zfs_nicenum_format format)61eda14cbcSMatt Macy zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
62eda14cbcSMatt Macy     enum zfs_nicenum_format format)
63eda14cbcSMatt Macy {
64eda14cbcSMatt Macy 	uint64_t n = num;
65eda14cbcSMatt Macy 	int index = 0;
66eda14cbcSMatt Macy 	const char *u;
67eda14cbcSMatt Macy 	const char *units[3][7] = {
68eda14cbcSMatt Macy 	    [ZFS_NICENUM_1024] = {"", "K", "M", "G", "T", "P", "E"},
69eda14cbcSMatt Macy 	    [ZFS_NICENUM_BYTES] = {"B", "K", "M", "G", "T", "P", "E"},
70eda14cbcSMatt Macy 	    [ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"}
71eda14cbcSMatt Macy 	};
72eda14cbcSMatt Macy 
73eda14cbcSMatt Macy 	const int units_len[] = {[ZFS_NICENUM_1024] = 6,
74eda14cbcSMatt Macy 	    [ZFS_NICENUM_BYTES] = 6,
75eda14cbcSMatt Macy 	    [ZFS_NICENUM_TIME] = 4};
76eda14cbcSMatt Macy 
77eda14cbcSMatt Macy 	const int k_unit[] = {	[ZFS_NICENUM_1024] = 1024,
78eda14cbcSMatt Macy 	    [ZFS_NICENUM_BYTES] = 1024,
79eda14cbcSMatt Macy 	    [ZFS_NICENUM_TIME] = 1000};
80eda14cbcSMatt Macy 
81eda14cbcSMatt Macy 	double val;
82eda14cbcSMatt Macy 
83eda14cbcSMatt Macy 	if (format == ZFS_NICENUM_RAW) {
84eda14cbcSMatt Macy 		snprintf(buf, buflen, "%llu", (u_longlong_t)num);
85eda14cbcSMatt Macy 		return;
86eda14cbcSMatt Macy 	} else if (format == ZFS_NICENUM_RAWTIME && num > 0) {
87eda14cbcSMatt Macy 		snprintf(buf, buflen, "%llu", (u_longlong_t)num);
88eda14cbcSMatt Macy 		return;
89eda14cbcSMatt Macy 	} else if (format == ZFS_NICENUM_RAWTIME && num == 0) {
90eda14cbcSMatt Macy 		snprintf(buf, buflen, "%s", "-");
91eda14cbcSMatt Macy 		return;
92eda14cbcSMatt Macy 	}
93eda14cbcSMatt Macy 
94eda14cbcSMatt Macy 	while (n >= k_unit[format] && index < units_len[format]) {
95eda14cbcSMatt Macy 		n /= k_unit[format];
96eda14cbcSMatt Macy 		index++;
97eda14cbcSMatt Macy 	}
98eda14cbcSMatt Macy 
99eda14cbcSMatt Macy 	u = units[format][index];
100eda14cbcSMatt Macy 
101eda14cbcSMatt Macy 	/* Don't print zero latencies since they're invalid */
102eda14cbcSMatt Macy 	if ((format == ZFS_NICENUM_TIME) && (num == 0)) {
103eda14cbcSMatt Macy 		(void) snprintf(buf, buflen, "-");
104eda14cbcSMatt Macy 	} else if ((index == 0) || ((num %
105eda14cbcSMatt Macy 	    (uint64_t)powl(k_unit[format], index)) == 0)) {
106eda14cbcSMatt Macy 		/*
107eda14cbcSMatt Macy 		 * If this is an even multiple of the base, always display
108eda14cbcSMatt Macy 		 * without any decimal precision.
109eda14cbcSMatt Macy 		 */
110eda14cbcSMatt Macy 		(void) snprintf(buf, buflen, "%llu%s", (u_longlong_t)n, u);
111eda14cbcSMatt Macy 
112eda14cbcSMatt Macy 	} else {
113eda14cbcSMatt Macy 		/*
114eda14cbcSMatt Macy 		 * We want to choose a precision that reflects the best choice
115eda14cbcSMatt Macy 		 * for fitting in 5 characters.  This can get rather tricky when
116eda14cbcSMatt Macy 		 * we have numbers that are very close to an order of magnitude.
117eda14cbcSMatt Macy 		 * For example, when displaying 10239 (which is really 9.999K),
118eda14cbcSMatt Macy 		 * we want only a single place of precision for 10.0K.  We could
119eda14cbcSMatt Macy 		 * develop some complex heuristics for this, but it's much
120eda14cbcSMatt Macy 		 * easier just to try each combination in turn.
121eda14cbcSMatt Macy 		 */
122eda14cbcSMatt Macy 		int i;
123eda14cbcSMatt Macy 		for (i = 2; i >= 0; i--) {
124eda14cbcSMatt Macy 			val = (double)num /
125eda14cbcSMatt Macy 			    (uint64_t)powl(k_unit[format], index);
126eda14cbcSMatt Macy 
127eda14cbcSMatt Macy 			/*
128eda14cbcSMatt Macy 			 * Don't print floating point values for time.  Note,
129eda14cbcSMatt Macy 			 * we use floor() instead of round() here, since
130eda14cbcSMatt Macy 			 * round can result in undesirable results.  For
131eda14cbcSMatt Macy 			 * example, if "num" is in the range of
132eda14cbcSMatt Macy 			 * 999500-999999, it will print out "1000us".  This
133eda14cbcSMatt Macy 			 * doesn't happen if we use floor().
134eda14cbcSMatt Macy 			 */
135eda14cbcSMatt Macy 			if (format == ZFS_NICENUM_TIME) {
136eda14cbcSMatt Macy 				if (snprintf(buf, buflen, "%d%s",
137eda14cbcSMatt Macy 				    (unsigned int) floor(val), u) <= 5)
138eda14cbcSMatt Macy 					break;
139eda14cbcSMatt Macy 
140eda14cbcSMatt Macy 			} else {
141eda14cbcSMatt Macy 				if (snprintf(buf, buflen, "%.*f%s", i,
142eda14cbcSMatt Macy 				    val, u) <= 5)
143eda14cbcSMatt Macy 					break;
144eda14cbcSMatt Macy 			}
145eda14cbcSMatt Macy 		}
146eda14cbcSMatt Macy 	}
147eda14cbcSMatt Macy }
148eda14cbcSMatt Macy 
149eda14cbcSMatt Macy /*
150eda14cbcSMatt Macy  * Convert a number to an appropriately human-readable output.
151eda14cbcSMatt Macy  */
152eda14cbcSMatt Macy void
zfs_nicenum(uint64_t num,char * buf,size_t buflen)153eda14cbcSMatt Macy zfs_nicenum(uint64_t num, char *buf, size_t buflen)
154eda14cbcSMatt Macy {
155eda14cbcSMatt Macy 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_1024);
156eda14cbcSMatt Macy }
157eda14cbcSMatt Macy 
158eda14cbcSMatt Macy /*
159eda14cbcSMatt Macy  * Convert a time to an appropriately human-readable output.
160eda14cbcSMatt Macy  * @num:	Time in nanoseconds
161eda14cbcSMatt Macy  */
162eda14cbcSMatt Macy void
zfs_nicetime(uint64_t num,char * buf,size_t buflen)163eda14cbcSMatt Macy zfs_nicetime(uint64_t num, char *buf, size_t buflen)
164eda14cbcSMatt Macy {
165eda14cbcSMatt Macy 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_TIME);
166eda14cbcSMatt Macy }
167eda14cbcSMatt Macy 
168eda14cbcSMatt Macy /*
169eda14cbcSMatt Macy  * Print out a raw number with correct column spacing
170eda14cbcSMatt Macy  */
171eda14cbcSMatt Macy void
zfs_niceraw(uint64_t num,char * buf,size_t buflen)172eda14cbcSMatt Macy zfs_niceraw(uint64_t num, char *buf, size_t buflen)
173eda14cbcSMatt Macy {
174eda14cbcSMatt Macy 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_RAW);
175eda14cbcSMatt Macy }
176eda14cbcSMatt Macy 
177eda14cbcSMatt Macy /*
178eda14cbcSMatt Macy  * Convert a number of bytes to an appropriately human-readable output.
179eda14cbcSMatt Macy  */
180eda14cbcSMatt Macy void
zfs_nicebytes(uint64_t num,char * buf,size_t buflen)181eda14cbcSMatt Macy zfs_nicebytes(uint64_t num, char *buf, size_t buflen)
182eda14cbcSMatt Macy {
183eda14cbcSMatt Macy 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
184eda14cbcSMatt Macy }
185