1 // SPDX-License-Identifier: GPL-2.0
3 #include <subcmd/parse-options.h>
7 #include <linux/time64.h>
17 #define CLOCKID_MAP(n, c) \
18 { .name = n, .clockid = (c), }
20 #define CLOCKID_END { .name = NULL, }
24 * Add the missing ones, we need to build on many distros...
26 #ifndef CLOCK_MONOTONIC_RAW
27 #define CLOCK_MONOTONIC_RAW 4
29 #ifndef CLOCK_BOOTTIME
30 #define CLOCK_BOOTTIME 7
36 static const struct clockid_map clockids[] = {
37 /* available for all events, NMI safe */
38 CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
39 CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
41 /* available for some events */
42 CLOCKID_MAP("realtime", CLOCK_REALTIME),
43 CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
44 CLOCKID_MAP("tai", CLOCK_TAI),
46 /* available for the lazy */
47 CLOCKID_MAP("mono", CLOCK_MONOTONIC),
48 CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
49 CLOCKID_MAP("real", CLOCK_REALTIME),
50 CLOCKID_MAP("boot", CLOCK_BOOTTIME),
55 static int get_clockid_res(clockid_t clk_id, u64 *res_ns)
60 if (!clock_getres(clk_id, &res))
61 *res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC;
63 pr_warning("WARNING: Failed to determine specified clock resolution.\n");
68 int parse_clockid(const struct option *opt, const char *str, int unset)
70 struct record_opts *opts = (struct record_opts *)opt->value;
71 const struct clockid_map *cm;
72 const char *ostr = str;
75 opts->use_clockid = 0;
83 /* no setting it twice */
84 if (opts->use_clockid)
87 opts->use_clockid = true;
89 /* if its a number, we're done */
90 if (sscanf(str, "%d", &opts->clockid) == 1)
91 return get_clockid_res(opts->clockid, &opts->clockid_res_ns);
93 /* allow a "CLOCK_" prefix to the name */
94 if (!strncasecmp(str, "CLOCK_", 6))
97 for (cm = clockids; cm->name; cm++) {
98 if (!strcasecmp(str, cm->name)) {
99 opts->clockid = cm->clockid;
100 return get_clockid_res(opts->clockid,
101 &opts->clockid_res_ns);
105 opts->use_clockid = false;
106 ui__warning("unknown clockid %s, check man page\n", ostr);
110 const char *clockid_name(clockid_t clk_id)
112 const struct clockid_map *cm;
114 for (cm = clockids; cm->name; cm++) {
115 if (cm->clockid == clk_id)
118 return "(not found)";