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