vdso_test_clock_getres.c 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note
  2. /*
  3. * vdso_clock_getres.c: Sample code to test clock_getres.
  4. * Copyright (c) 2019 Arm Ltd.
  5. *
  6. * Compile with:
  7. * gcc -std=gnu99 vdso_clock_getres.c
  8. *
  9. * Tested on ARM, ARM64, MIPS32, x86 (32-bit and 64-bit),
  10. * Power (32-bit and 64-bit), S390x (32-bit and 64-bit).
  11. * Might work on other architectures.
  12. */
  13. #define _GNU_SOURCE
  14. #include <elf.h>
  15. #include <err.h>
  16. #include <fcntl.h>
  17. #include <stdint.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <time.h>
  21. #include <sys/auxv.h>
  22. #include <sys/mman.h>
  23. #include <sys/time.h>
  24. #include <unistd.h>
  25. #include <sys/syscall.h>
  26. #include "../kselftest.h"
  27. static long syscall_clock_getres(clockid_t _clkid, struct timespec *_ts)
  28. {
  29. long ret;
  30. ret = syscall(SYS_clock_getres, _clkid, _ts);
  31. return ret;
  32. }
  33. const char *vdso_clock_name[12] = {
  34. "CLOCK_REALTIME",
  35. "CLOCK_MONOTONIC",
  36. "CLOCK_PROCESS_CPUTIME_ID",
  37. "CLOCK_THREAD_CPUTIME_ID",
  38. "CLOCK_MONOTONIC_RAW",
  39. "CLOCK_REALTIME_COARSE",
  40. "CLOCK_MONOTONIC_COARSE",
  41. "CLOCK_BOOTTIME",
  42. "CLOCK_REALTIME_ALARM",
  43. "CLOCK_BOOTTIME_ALARM",
  44. "CLOCK_SGI_CYCLE",
  45. "CLOCK_TAI",
  46. };
  47. /*
  48. * This function calls clock_getres in vdso and by system call
  49. * with different values for clock_id.
  50. *
  51. * Example of output:
  52. *
  53. * clock_id: CLOCK_REALTIME [PASS]
  54. * clock_id: CLOCK_BOOTTIME [PASS]
  55. * clock_id: CLOCK_TAI [PASS]
  56. * clock_id: CLOCK_REALTIME_COARSE [PASS]
  57. * clock_id: CLOCK_MONOTONIC [PASS]
  58. * clock_id: CLOCK_MONOTONIC_RAW [PASS]
  59. * clock_id: CLOCK_MONOTONIC_COARSE [PASS]
  60. */
  61. static inline int vdso_test_clock(unsigned int clock_id)
  62. {
  63. struct timespec x, y;
  64. printf("clock_id: %s", vdso_clock_name[clock_id]);
  65. clock_getres(clock_id, &x);
  66. syscall_clock_getres(clock_id, &y);
  67. if ((x.tv_sec != y.tv_sec) || (x.tv_nsec != y.tv_nsec)) {
  68. printf(" [FAIL]\n");
  69. return KSFT_FAIL;
  70. }
  71. printf(" [PASS]\n");
  72. return KSFT_PASS;
  73. }
  74. int main(int argc, char **argv)
  75. {
  76. int ret = 0;
  77. #if _POSIX_TIMERS > 0
  78. #ifdef CLOCK_REALTIME
  79. ret += vdso_test_clock(CLOCK_REALTIME);
  80. #endif
  81. #ifdef CLOCK_BOOTTIME
  82. ret += vdso_test_clock(CLOCK_BOOTTIME);
  83. #endif
  84. #ifdef CLOCK_TAI
  85. ret += vdso_test_clock(CLOCK_TAI);
  86. #endif
  87. #ifdef CLOCK_REALTIME_COARSE
  88. ret += vdso_test_clock(CLOCK_REALTIME_COARSE);
  89. #endif
  90. #ifdef CLOCK_MONOTONIC
  91. ret += vdso_test_clock(CLOCK_MONOTONIC);
  92. #endif
  93. #ifdef CLOCK_MONOTONIC_RAW
  94. ret += vdso_test_clock(CLOCK_MONOTONIC_RAW);
  95. #endif
  96. #ifdef CLOCK_MONOTONIC_COARSE
  97. ret += vdso_test_clock(CLOCK_MONOTONIC_COARSE);
  98. #endif
  99. #endif
  100. if (ret > 0)
  101. return KSFT_FAIL;
  102. return KSFT_PASS;
  103. }