Mercurial
comparison third_party/wrk/src/units.c @ 186:8cf4ec5e2191 hg-web
Fixed merge conflict.
| author | MrJuneJune <me@mrjunejune.com> |
|---|---|
| date | Fri, 23 Jan 2026 22:38:59 -0800 |
| parents | 94705b5986b3 |
| children |
comparison
equal
deleted
inserted
replaced
| 176:fed99fc04e12 | 186:8cf4ec5e2191 |
|---|---|
| 1 // Copyright (C) 2012 - Will Glozer. All rights reserved. | |
| 2 | |
| 3 #include <stdlib.h> | |
| 4 #include <stdio.h> | |
| 5 #include <strings.h> | |
| 6 #include <inttypes.h> | |
| 7 | |
| 8 #include "units.h" | |
| 9 #include "aprintf.h" | |
| 10 | |
| 11 typedef struct { | |
| 12 int scale; | |
| 13 char *base; | |
| 14 char *units[]; | |
| 15 } units; | |
| 16 | |
| 17 units time_units_us = { | |
| 18 .scale = 1000, | |
| 19 .base = "us", | |
| 20 .units = { "ms", "s", NULL } | |
| 21 }; | |
| 22 | |
| 23 units time_units_s = { | |
| 24 .scale = 60, | |
| 25 .base = "s", | |
| 26 .units = { "m", "h", NULL } | |
| 27 }; | |
| 28 | |
| 29 units binary_units = { | |
| 30 .scale = 1024, | |
| 31 .base = "", | |
| 32 .units = { "K", "M", "G", "T", "P", NULL } | |
| 33 }; | |
| 34 | |
| 35 units metric_units = { | |
| 36 .scale = 1000, | |
| 37 .base = "", | |
| 38 .units = { "k", "M", "G", "T", "P", NULL } | |
| 39 }; | |
| 40 | |
| 41 static char *format_units(long double n, units *m, int p) { | |
| 42 long double amt = n, scale; | |
| 43 char *unit = m->base; | |
| 44 char *msg = NULL; | |
| 45 | |
| 46 scale = m->scale * 0.85; | |
| 47 | |
| 48 for (int i = 0; m->units[i+1] && amt >= scale; i++) { | |
| 49 amt /= m->scale; | |
| 50 unit = m->units[i]; | |
| 51 } | |
| 52 | |
| 53 aprintf(&msg, "%.*Lf%s", p, amt, unit); | |
| 54 | |
| 55 return msg; | |
| 56 } | |
| 57 | |
| 58 static int scan_units(char *s, uint64_t *n, units *m) { | |
| 59 uint64_t base, scale = 1; | |
| 60 char unit[3] = { 0, 0, 0 }; | |
| 61 int i, c; | |
| 62 | |
| 63 if ((c = sscanf(s, "%"SCNu64"%2s", &base, unit)) < 1) return -1; | |
| 64 | |
| 65 if (c == 2 && strncasecmp(unit, m->base, 3)) { | |
| 66 for (i = 0; m->units[i] != NULL; i++) { | |
| 67 scale *= m->scale; | |
| 68 if (!strncasecmp(unit, m->units[i], 3)) break; | |
| 69 } | |
| 70 if (m->units[i] == NULL) return -1; | |
| 71 } | |
| 72 | |
| 73 *n = base * scale; | |
| 74 return 0; | |
| 75 } | |
| 76 | |
| 77 char *format_binary(long double n) { | |
| 78 return format_units(n, &binary_units, 2); | |
| 79 } | |
| 80 | |
| 81 char *format_metric(long double n) { | |
| 82 return format_units(n, &metric_units, 2); | |
| 83 } | |
| 84 | |
| 85 char *format_time_us(long double n) { | |
| 86 units *units = &time_units_us; | |
| 87 if (n >= 1000000.0) { | |
| 88 n /= 1000000.0; | |
| 89 units = &time_units_s; | |
| 90 } | |
| 91 return format_units(n, units, 2); | |
| 92 } | |
| 93 | |
| 94 char *format_time_s(long double n) { | |
| 95 return format_units(n, &time_units_s, 0); | |
| 96 } | |
| 97 | |
| 98 int scan_metric(char *s, uint64_t *n) { | |
| 99 return scan_units(s, n, &metric_units); | |
| 100 } | |
| 101 | |
| 102 int scan_time(char *s, uint64_t *n) { | |
| 103 return scan_units(s, n, &time_units_s); | |
| 104 } |