| 1 | #include <stdlib.h> |
| 2 | #include <stdio.h> |
| 3 | #include <string.h> |
| 4 | |
| 5 | #include "../fio.h" |
| 6 | |
| 7 | /** |
| 8 | * num2str() - Cheesy number->string conversion, complete with carry rounding error. |
| 9 | * @num: quantity (e.g., number of blocks, bytes or bits) |
| 10 | * @maxlen: max number of digits in the output string (not counting prefix and units) |
| 11 | * @base: multiplier for num (e.g., if num represents Ki, use 1024) |
| 12 | * @pow2: select unit prefix - 0=power-of-10 decimal SI, nonzero=power-of-2 binary IEC |
| 13 | * @units: select units - N2S_* macros defined in fio.h |
| 14 | * @returns a malloc'd buffer containing "number[<unit prefix>][<units>]" |
| 15 | */ |
| 16 | char *num2str(uint64_t num, int maxlen, int base, int pow2, int units) |
| 17 | { |
| 18 | const char *sistr[] = { "", "k", "M", "G", "T", "P" }; |
| 19 | const char *iecstr[] = { "", "Ki", "Mi", "Gi", "Ti", "Pi" }; |
| 20 | const char **unitprefix; |
| 21 | const char *unitstr[] = { "", "/s", "B", "bit", "B/s", "bit/s" }; |
| 22 | const unsigned int thousand[] = { 1000, 1024 }; |
| 23 | unsigned int modulo, decimals; |
| 24 | int unit_index = 0, post_index, carry = 0; |
| 25 | char tmp[32]; |
| 26 | char *buf; |
| 27 | |
| 28 | compiletime_assert(sizeof(sistr) == sizeof(iecstr), "unit prefix arrays must be identical sizes"); |
| 29 | |
| 30 | buf = malloc(128); |
| 31 | if (!buf) |
| 32 | return NULL; |
| 33 | |
| 34 | if (pow2) |
| 35 | unitprefix = iecstr; |
| 36 | else |
| 37 | unitprefix = sistr; |
| 38 | |
| 39 | for (post_index = 0; base > 1; post_index++) |
| 40 | base /= thousand[!!pow2]; |
| 41 | |
| 42 | switch (units) { |
| 43 | case N2S_PERSEC: |
| 44 | unit_index = 1; |
| 45 | break; |
| 46 | case N2S_BYTE: |
| 47 | unit_index = 2; |
| 48 | break; |
| 49 | case N2S_BIT: |
| 50 | unit_index = 3; |
| 51 | num *= 8; |
| 52 | break; |
| 53 | case N2S_BYTEPERSEC: |
| 54 | unit_index = 4; |
| 55 | break; |
| 56 | case N2S_BITPERSEC: |
| 57 | unit_index = 5; |
| 58 | num *= 8; |
| 59 | break; |
| 60 | } |
| 61 | |
| 62 | modulo = -1U; |
| 63 | while (post_index < sizeof(sistr)) { |
| 64 | sprintf(tmp, "%llu", (unsigned long long) num); |
| 65 | if (strlen(tmp) <= maxlen) |
| 66 | break; |
| 67 | |
| 68 | modulo = num % thousand[!!pow2]; |
| 69 | num /= thousand[!!pow2]; |
| 70 | carry = modulo >= thousand[!!pow2] / 2; |
| 71 | post_index++; |
| 72 | } |
| 73 | |
| 74 | if (modulo == -1U) { |
| 75 | done: |
| 76 | if (post_index >= ARRAY_SIZE(sistr)) |
| 77 | post_index = 0; |
| 78 | |
| 79 | sprintf(buf, "%llu%s%s", (unsigned long long) num, |
| 80 | unitprefix[post_index], unitstr[unit_index]); |
| 81 | return buf; |
| 82 | } |
| 83 | |
| 84 | sprintf(tmp, "%llu", (unsigned long long) num); |
| 85 | decimals = maxlen - strlen(tmp); |
| 86 | if (decimals <= 1) { |
| 87 | if (carry) |
| 88 | num++; |
| 89 | goto done; |
| 90 | } |
| 91 | |
| 92 | do { |
| 93 | sprintf(tmp, "%u", modulo); |
| 94 | if (strlen(tmp) <= decimals - 1) |
| 95 | break; |
| 96 | |
| 97 | modulo = (modulo + 9) / 10; |
| 98 | } while (1); |
| 99 | |
| 100 | sprintf(buf, "%llu.%u%s%s", (unsigned long long) num, modulo, |
| 101 | unitprefix[post_index], unitstr[unit_index]); |
| 102 | return buf; |
| 103 | } |