tostr.h revision 1.1 1 /*
2 * tostr.h - DTrace To-String include file.
3 *
4 * $Id: tostr.h,v 1.1 2015/09/30 22:01:09 christos Exp $
5 *
6 * COPYRIGHT: Copyright (c) 2007 Brendan Gregg.
7 *
8 * CDDL HEADER START
9 *
10 * The contents of this file are subject to the terms of the
11 * Common Development and Distribution License, Version 1.0 only
12 * (the "License"). You may not use this file except in compliance
13 * with the License.
14 *
15 * You can obtain a copy of the license at Docs/cddl1.txt
16 * or http://www.opensolaris.org/os/licensing.
17 * See the License for the specific language governing permissions
18 * and limitations under the License.
19 *
20 * CDDL HEADER END
21 *
22 * 16-Sep-2007 Brendan Gregg Created this.
23 */
24
25 /*
26 * NUM_TO_STR(n) - takes a number and returns a string with a prefix,
27 * intended to fit withen 6 chars.
28 *
29 * Input Output
30 * 0 0
31 * 1 1
32 * 10 10
33 * 999 999
34 * 1000 1.0K
35 * 1100 1.1K
36 * 10000 10.0K
37 * 999999 999.0K
38 * 1000000 1.0M
39 * 10000000 10.0M
40 * 999999999 999.9M
41 */
42 #define NUM_TO_STR(n) \
43 n >= 1000000 ? \
44 strjoin(strjoin(strjoin(lltostr(n / 1000000), "."), \
45 lltostr((n % 1000000) / 100000)), "M") : n >= 1000 ? \
46 strjoin(strjoin(strjoin(lltostr(n / 1000), "."), \
47 lltostr((n % 1000) / 100)), "K") : lltostr(n)
48
49 /*
50 * BYTES_TO_STR(n) - takes a byte count and returns a string with a prefix,
51 * intended to fit withen 6 chars.
52 *
53 * Input Output
54 * 0 0
55 * 1 1
56 * 10 10
57 * 999 0.9K
58 * 1000 0.9K
59 * 1024 1.0K
60 * 10240 10.0K
61 * 1000000 976.5K
62 * 1048576 1.0M
63 * 1000000000 953.6M
64 */
65 #define BYTES_TO_STR(n) \
66 n >= 1024000 ? \
67 strjoin(strjoin(strjoin(lltostr(n / 1048576), "."), \
68 lltostr((n % 1048576) / 104858)), "M") : n >= 1000 ? \
69 strjoin(strjoin(strjoin(lltostr(n / 1024), "."), \
70 lltostr((n % 1024) / 103)), "K") : lltostr(n)
71
72 /*
73 * US_TO_STR(n) - takes microseconds and returns a string with a prefix,
74 * intended to fit withen 6 chars.
75 *
76 * Input Output
77 * 0 0
78 * 1 1u
79 * 10 10u
80 * 999 999u
81 * 1000 1.0m
82 * 1100 1.1m
83 * 10000 10.0m
84 * 999999 999.0m
85 */
86 #define US_TO_STR(n) \
87 n == 0 ? "0" : n >= 1000 ? \
88 strjoin(lltostr(n / 1000), "m") : strjoin(lltostr(n), "u")
89
90