tests/block_cloning: rename and document get_same_blocks helper
[zfs.git] / lib / libzutil / zutil_nicenum.c
blobe05bcb40a8cee46de121da049629d687f97623d5
1 /*
2 * CDDL HEADER START
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.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or https://opensource.org/licenses/CDDL-1.0.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
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]
19 * CDDL HEADER END
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
26 #include <ctype.h>
27 #include <math.h>
28 #include <stdio.h>
29 #include <libzutil.h>
30 #include <string.h>
33 * Return B_TRUE if "str" is a number string, B_FALSE otherwise.
34 * Works for integer and floating point numbers.
36 boolean_t
37 zfs_isnumber(const char *str)
39 if (!*str)
40 return (B_FALSE);
42 for (; *str; str++)
43 if (!(isdigit(*str) || (*str == '.')))
44 return (B_FALSE);
47 * Numbers should not end with a period ("." ".." or "5." are
48 * not valid)
50 if (str[strlen(str) - 1] == '.') {
51 return (B_FALSE);
54 return (B_TRUE);
58 * Convert a number to an appropriately human-readable output.
60 void
61 zfs_nicenum_format(uint64_t num, char *buf, size_t buflen,
62 enum zfs_nicenum_format format)
64 uint64_t n = num;
65 int index = 0;
66 const char *u;
67 const char *units[3][7] = {
68 [ZFS_NICENUM_1024] = {"", "K", "M", "G", "T", "P", "E"},
69 [ZFS_NICENUM_BYTES] = {"B", "K", "M", "G", "T", "P", "E"},
70 [ZFS_NICENUM_TIME] = {"ns", "us", "ms", "s", "?", "?", "?"}
73 const int units_len[] = {[ZFS_NICENUM_1024] = 6,
74 [ZFS_NICENUM_BYTES] = 6,
75 [ZFS_NICENUM_TIME] = 4};
77 const int k_unit[] = { [ZFS_NICENUM_1024] = 1024,
78 [ZFS_NICENUM_BYTES] = 1024,
79 [ZFS_NICENUM_TIME] = 1000};
81 double val;
83 if (format == ZFS_NICENUM_RAW) {
84 snprintf(buf, buflen, "%llu", (u_longlong_t)num);
85 return;
86 } else if (format == ZFS_NICENUM_RAWTIME && num > 0) {
87 snprintf(buf, buflen, "%llu", (u_longlong_t)num);
88 return;
89 } else if (format == ZFS_NICENUM_RAWTIME && num == 0) {
90 snprintf(buf, buflen, "%s", "-");
91 return;
94 while (n >= k_unit[format] && index < units_len[format]) {
95 n /= k_unit[format];
96 index++;
99 u = units[format][index];
101 /* Don't print zero latencies since they're invalid */
102 if ((format == ZFS_NICENUM_TIME) && (num == 0)) {
103 (void) snprintf(buf, buflen, "-");
104 } else if ((index == 0) || ((num %
105 (uint64_t)powl(k_unit[format], index)) == 0)) {
107 * If this is an even multiple of the base, always display
108 * without any decimal precision.
110 (void) snprintf(buf, buflen, "%llu%s", (u_longlong_t)n, u);
112 } else {
114 * We want to choose a precision that reflects the best choice
115 * for fitting in 5 characters. This can get rather tricky when
116 * we have numbers that are very close to an order of magnitude.
117 * For example, when displaying 10239 (which is really 9.999K),
118 * we want only a single place of precision for 10.0K. We could
119 * develop some complex heuristics for this, but it's much
120 * easier just to try each combination in turn.
122 int i;
123 for (i = 2; i >= 0; i--) {
124 val = (double)num /
125 (uint64_t)powl(k_unit[format], index);
128 * Don't print floating point values for time. Note,
129 * we use floor() instead of round() here, since
130 * round can result in undesirable results. For
131 * example, if "num" is in the range of
132 * 999500-999999, it will print out "1000us". This
133 * doesn't happen if we use floor().
135 if (format == ZFS_NICENUM_TIME) {
136 if (snprintf(buf, buflen, "%d%s",
137 (unsigned int) floor(val), u) <= 5)
138 break;
140 } else {
141 if (snprintf(buf, buflen, "%.*f%s", i,
142 val, u) <= 5)
143 break;
150 * Convert a number to an appropriately human-readable output.
152 void
153 zfs_nicenum(uint64_t num, char *buf, size_t buflen)
155 zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_1024);
159 * Convert a time to an appropriately human-readable output.
160 * @num: Time in nanoseconds
162 void
163 zfs_nicetime(uint64_t num, char *buf, size_t buflen)
165 zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_TIME);
169 * Print out a raw number with correct column spacing
171 void
172 zfs_niceraw(uint64_t num, char *buf, size_t buflen)
174 zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_RAW);
178 * Convert a number of bytes to an appropriately human-readable output.
180 void
181 zfs_nicebytes(uint64_t num, char *buf, size_t buflen)
183 zfs_nicenum_format(num, buf, buflen, ZFS_NICENUM_BYTES);