diff options
author | Eugene Syromyatnikov <evgsyr@gmail.com> | 2023-05-10 19:55:30 +0200 |
---|---|---|
committer | Dmitry V. Levin <ldv@strace.io> | 2023-05-10 18:08:00 +0000 |
commit | 4f1fad56cba89009d8aaf63d24b34279d912b333 (patch) | |
tree | 22d3d43f388ed3c36ddb13c40db4779af3a53c72 | |
parent | 6387801953c6d04fac0fcc86db23507774a706c8 (diff) | |
download | strace-4f1fad56cba89009d8aaf63d24b34279d912b333.tar.gz |
util: factor out printing of time interval in a human-readable form
Also, remove all this dance with separate fractional part printing as %f
doesn't trim the trailing zeroes and checking whether %g does the right
thing in all cases is too much hassle.
* src/defs.h (print_ticks): New declaration.
* src/util.c (print_ticks): New function.
(print_clock_t): Call it.
-rw-r--r-- | src/defs.h | 2 | ||||
-rw-r--r-- | src/util.c | 39 |
2 files changed, 14 insertions, 27 deletions
diff --git a/src/defs.h b/src/defs.h index df7894df5..846a7ae5a 100644 --- a/src/defs.h +++ b/src/defs.h @@ -1554,6 +1554,8 @@ extern const struct timespec *ts_min(const struct timespec *, const struct times extern const struct timespec *ts_max(const struct timespec *, const struct timespec *); extern int parse_ts(const char *s, struct timespec *t); +/** Print a time interval value specified as a number 1/freq-sized ticks. */ +extern void print_ticks(uint64_t val, long freq, unsigned int precision); /** Print a clock_t value, as used by times(2) (and not clock(3)). */ extern void print_clock_t(uint64_t val); diff --git a/src/util.c b/src/util.c index 343cf8850..a63d80678 100644 --- a/src/util.c +++ b/src/util.c @@ -220,6 +220,17 @@ ilog10(uint64_t val) } void +print_ticks(uint64_t val, long freq, unsigned int precision) +{ + PRINT_VAL_U(val); + if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_RAW + && freq > 0 && val > 0) { + tprintf_comment("%" PRIu64 ".%0*" PRIu64 " s", + val / freq, precision, val % freq); + } +} + +void print_clock_t(uint64_t val) { static long clk_tck; @@ -236,33 +247,7 @@ print_clock_t(uint64_t val) frac_width = MIN(ilog10(clk_tck), 9); } - PRINT_VAL_U(val); - if (xlat_verbose(xlat_verbosity) != XLAT_STYLE_RAW - && clk_tck > 0 && val > 0) { - /* - * This dance here is due to the fact that this calculation - * may occasionally hit double precision limitations (52-bit - * mantissa) with large values of val. - */ - char buf[sizeof(uint64_t) * 3 + sizeof("0.0 s")]; - size_t offs = ilog10(val / clk_tck); - /* - * This check is mostly to appease covscan, which thinks - * that offs can go as high as 31 (it cannot), but since - * there is no proper sanity checks against offs overrunning - * buf down the code, it may as well be here. - */ - if (offs > (sizeof(buf) - sizeof("0.0 s"))) - return; - int ret = snprintf(buf + offs, sizeof(buf) - offs, "%.*f s", - frac_width, - (double) (val % clk_tck) / clk_tck); - if (ret >= 0 && (unsigned) ret < sizeof(buf) - offs) { - snprintf(buf, offs + 2, "%" PRIu64, val / clk_tck); - buf[offs + 1] = '.'; - tprints_comment(buf); - } - } + print_ticks(val, clk_tck, frac_width); } #if !defined HAVE_STPCPY |