1 /*
2 * Format clock_t-typed values.
3 * Copyright (c) 2018-2022 The strace developers.
4 * All rights reserved.
5 *
6 * SPDX-License-Identifier: GPL-2.0-or-later
7 */
8
9 #include "tests.h"
10 #include <inttypes.h>
11 #include <math.h>
12 #include <stdint.h>
13 #include <stdio.h>
14 #include <unistd.h>
15
16 const char *
17 clock_t_str(uint64_t val, char *str, size_t str_size)
18 {
19 static long clk_tck;
20 static int precision;
21
22 if (!clk_tck) {
23 clk_tck = sysconf(_SC_CLK_TCK);
24 precision = clk_tck > 1 ? MIN((int) ceil(log10(clk_tck - 1)), 9)
25 : 0;
26 }
27
28 if ((clk_tck > 0) && val) {
29 snprintf(str, str_size, "%" PRIu64 " /* %" PRIu64 ".%0*u s */",
30 val, val / clk_tck, precision,
31 (unsigned) round(((double) (val % clk_tck) / clk_tck)
32 * pow(10, precision)));
33 } else {
34 snprintf(str, str_size, "%" PRIu64, val);
35 }
36
37 return str;
38 }