clockid.c 2.5 KB

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