xref: /freebsd/sys/contrib/openzfs/lib/libzutil/zutil_nicenum.c (revision 22649d4dba730d46244fd2dff4fd174903c8379f)
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 <ctype.h>
18 #include <math.h>
19 #include <stdio.h>
20 #include <libzutil.h>
21 #include <string.h>
22 
23 /*
24  * Return B_TRUE if "str" is a number string, B_FALSE otherwise.
25  * Works for integer and floating point numbers.
26  */
27 boolean_t
zfs_isnumber(const char * str)28 zfs_isnumber(const char *str)
29 {
30 	if (!*str)
31 		return (B_FALSE);
32 
33 	for (; *str; str++)
34 		if (!(isdigit(*str) || (*str == '.')))
35 			return (B_FALSE);
36 
37 	/*
38 	 * Numbers should not end with a period ("." ".." or "5." are
39 	 * not valid)
40 	 */
41 	if (str[strlen(str) - 1] == '.') {
42 		return (B_FALSE);
43 	}
44 
45 	return (B_TRUE);
46 }
47 
48 /*
49  * Convert a number to an appropriately human-readable output.
50  */
51 void
zfs_nicenum_format(uint64_t num,char * buf,size_t buflen,enum zfs_nicenum_format format)52 zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
53     enum zfs_nicenum_format format)
54 {
55 	uint64_t n = num;
56 	int index = 0;
57 	const char *u;
58 	const char *units[3][7] = {
59 	    [ZFS_NICENUM_1024] = {"", "K", "M", "G", "T", "P", "E"},
60 	    [ZFS_NICENUM_BYTES] = {"B", "K", "M", "G", "T", "P", "E"},
61 	    [ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"}
62 	};
63 
64 	const int units_len[] = {[ZFS_NICENUM_1024] = 6,
65 	    [ZFS_NICENUM_BYTES] = 6,
66 	    [ZFS_NICENUM_TIME] = 4};
67 
68 	const int k_unit[] = {	[ZFS_NICENUM_1024] = 1024,
69 	    [ZFS_NICENUM_BYTES] = 1024,
70 	    [ZFS_NICENUM_TIME] = 1000};
71 
72 	double val;
73 
74 	if (format == ZFS_NICENUM_RAW) {
75 		snprintf(buf, buflen, "%llu", (u_longlong_t)num);
76 		return;
77 	} else if (format == ZFS_NICENUM_RAWTIME && num > 0) {
78 		snprintf(buf, buflen, "%llu", (u_longlong_t)num);
79 		return;
80 	} else if (format == ZFS_NICENUM_RAWTIME && num == 0) {
81 		snprintf(buf, buflen, "%s", "-");
82 		return;
83 	}
84 
85 	while (n >= k_unit[format] && index < units_len[format]) {
86 		n /= k_unit[format];
87 		index++;
88 	}
89 
90 	u = units[format][index];
91 
92 	/* Don't print zero latencies since they're invalid */
93 	if ((format == ZFS_NICENUM_TIME) && (num == 0)) {
94 		(void) snprintf(buf, buflen, "-");
95 	} else if ((index == 0) || ((num %
96 	    (uint64_t)powl(k_unit[format], index)) == 0)) {
97 		/*
98 		 * If this is an even multiple of the base, always display
99 		 * without any decimal precision.
100 		 */
101 		(void) snprintf(buf, buflen, "%llu%s", (u_longlong_t)n, u);
102 
103 	} else {
104 		/*
105 		 * We want to choose a precision that reflects the best choice
106 		 * for fitting in 5 characters.  This can get rather tricky when
107 		 * we have numbers that are very close to an order of magnitude.
108 		 * For example, when displaying 10239 (which is really 9.999K),
109 		 * we want only a single place of precision for 10.0K.  We could
110 		 * develop some complex heuristics for this, but it's much
111 		 * easier just to try each combination in turn.
112 		 */
113 		int i;
114 		for (i = 2; i >= 0; i--) {
115 			val = (double)num /
116 			    (uint64_t)powl(k_unit[format], index);
117 
118 			/*
119 			 * Don't print floating point values for time.  Note,
120 			 * we use floor() instead of round() here, since
121 			 * round can result in undesirable results.  For
122 			 * example, if "num" is in the range of
123 			 * 999500-999999, it will print out "1000us".  This
124 			 * doesn't happen if we use floor().
125 			 */
126 			if (format == ZFS_NICENUM_TIME) {
127 				if (snprintf(buf, buflen, "%d%s",
128 				    (unsigned int) floor(val), u) <= 5)
129 					break;
130 
131 			} else {
132 				if (snprintf(buf, buflen, "%.*f%s", i,
133 				    val, u) <= 5)
134 					break;
135 			}
136 		}
137 	}
138 }
139 
140 /*
141  * Convert a number to an appropriately human-readable output.
142  */
143 void
zfs_nicenum(uint64_t num,char * buf,size_t buflen)144 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
145 {
146 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_1024);
147 }
148 
149 /*
150  * Convert a time to an appropriately human-readable output.
151  * @num:	Time in nanoseconds
152  */
153 void
zfs_nicetime(uint64_t num,char * buf,size_t buflen)154 zfs_nicetime(uint64_t num, char *buf, size_t buflen)
155 {
156 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_TIME);
157 }
158 
159 /*
160  * Print out a raw number with correct column spacing
161  */
162 void
zfs_niceraw(uint64_t num,char * buf,size_t buflen)163 zfs_niceraw(uint64_t num, char *buf, size_t buflen)
164 {
165 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_RAW);
166 }
167 
168 /*
169  * Convert a number of bytes to an appropriately human-readable output.
170  */
171 void
zfs_nicebytes(uint64_t num,char * buf,size_t buflen)172 zfs_nicebytes(uint64_t num, char *buf, size_t buflen)
173 {
174 	zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);
175 }
176