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]
23 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
33 * Return B_TRUE if "str" is a number string, B_FALSE otherwise.
34 * Works for integer and floating point numbers.
37 zfs_isnumber(const char *str
)
43 if (!(isdigit(*str
) || (*str
== '.')))
47 * Numbers should not end with a period ("." ".." or "5." are
50 if (str
[strlen(str
) - 1] == '.') {
58 * Convert a number to an appropriately human-readable output.
61 zfs_nicenum_format(uint64_t num
, char *buf
, size_t buflen
,
62 enum zfs_nicenum_format format
)
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};
83 if (format
== ZFS_NICENUM_RAW
) {
84 snprintf(buf
, buflen
, "%llu", (u_longlong_t
)num
);
86 } else if (format
== ZFS_NICENUM_RAWTIME
&& num
> 0) {
87 snprintf(buf
, buflen
, "%llu", (u_longlong_t
)num
);
89 } else if (format
== ZFS_NICENUM_RAWTIME
&& num
== 0) {
90 snprintf(buf
, buflen
, "%s", "-");
94 while (n
>= k_unit
[format
] && index
< units_len
[format
]) {
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
);
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.
123 for (i
= 2; i
>= 0; i
--) {
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)
141 if (snprintf(buf
, buflen
, "%.*f%s", i
,
150 * Convert a number to an appropriately human-readable output.
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
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
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.
181 zfs_nicebytes(uint64_t num
, char *buf
, size_t buflen
)
183 zfs_nicenum_format(num
, buf
, buflen
, ZFS_NICENUM_BYTES
);