summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/defs.h2
-rw-r--r--src/util.c39
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