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