^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) #include <subcmd/parse-options.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #include <time.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <strings.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <linux/time64.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include "debug.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include "clockid.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include "record.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) struct clockid_map {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) const char *name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) int clockid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #define CLOCKID_MAP(n, c) \
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) { .name = n, .clockid = (c), }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #define CLOCKID_END { .name = NULL, }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) * Add the missing ones, we need to build on many distros...
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #ifndef CLOCK_MONOTONIC_RAW
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #define CLOCK_MONOTONIC_RAW 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifndef CLOCK_BOOTTIME
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) #define CLOCK_BOOTTIME 7
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) #ifndef CLOCK_TAI
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) #define CLOCK_TAI 11
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) static const struct clockid_map clockids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) /* available for all events, NMI safe */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) CLOCKID_MAP("monotonic", CLOCK_MONOTONIC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) CLOCKID_MAP("monotonic_raw", CLOCK_MONOTONIC_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) /* available for some events */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) CLOCKID_MAP("realtime", CLOCK_REALTIME),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) CLOCKID_MAP("boottime", CLOCK_BOOTTIME),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) CLOCKID_MAP("tai", CLOCK_TAI),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) /* available for the lazy */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) CLOCKID_MAP("mono", CLOCK_MONOTONIC),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) CLOCKID_MAP("raw", CLOCK_MONOTONIC_RAW),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) CLOCKID_MAP("real", CLOCK_REALTIME),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) CLOCKID_MAP("boot", CLOCK_BOOTTIME),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) CLOCKID_END,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) static int get_clockid_res(clockid_t clk_id, u64 *res_ns)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) struct timespec res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) *res_ns = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) if (!clock_getres(clk_id, &res))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) *res_ns = res.tv_nsec + res.tv_sec * NSEC_PER_SEC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) pr_warning("WARNING: Failed to determine specified clock resolution.\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) int parse_clockid(const struct option *opt, const char *str, int unset)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) struct record_opts *opts = (struct record_opts *)opt->value;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) const struct clockid_map *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) const char *ostr = str;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) if (unset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) opts->use_clockid = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) /* no arg passed */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (!str)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) /* no setting it twice */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (opts->use_clockid)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) opts->use_clockid = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) /* if its a number, we're done */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) if (sscanf(str, "%d", &opts->clockid) == 1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) return get_clockid_res(opts->clockid, &opts->clockid_res_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) /* allow a "CLOCK_" prefix to the name */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) if (!strncasecmp(str, "CLOCK_", 6))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) str += 6;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) for (cm = clockids; cm->name; cm++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (!strcasecmp(str, cm->name)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) opts->clockid = cm->clockid;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) return get_clockid_res(opts->clockid,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) &opts->clockid_res_ns);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) opts->use_clockid = false;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) ui__warning("unknown clockid %s, check man page\n", ostr);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) const char *clockid_name(clockid_t clk_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) const struct clockid_map *cm;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) for (cm = clockids; cm->name; cm++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) if (cm->clockid == clk_id)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) return cm->name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) return "(not found)";
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) }