1 // SPDX-License-Identifier: GPL-2.0
11 #include <sys/syscall.h>
17 typedef int (*vgettime_t)(clockid_t, struct timespec *);
19 vgettime_t vdso_clock_gettime;
21 static void fill_function_pointers(void)
23 void *vdso = dlopen("linux-vdso.so.1",
24 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
26 vdso = dlopen("linux-gate.so.1",
27 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
29 vdso = dlopen("linux-vdso32.so.1",
30 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
32 vdso = dlopen("linux-vdso64.so.1",
33 RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
35 pr_err("[WARN]\tfailed to find vDSO\n");
39 vdso_clock_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
40 if (!vdso_clock_gettime)
41 vdso_clock_gettime = (vgettime_t)dlsym(vdso, "__kernel_clock_gettime");
42 if (!vdso_clock_gettime)
43 pr_err("Warning: failed to find clock_gettime in vDSO\n");
47 static void test(clock_t clockid, char *clockstr, bool in_ns)
49 struct timespec tp, start;
51 const int timeout = 3;
53 vdso_clock_gettime(clockid, &start);
55 for (tp = start; start.tv_sec + timeout > tp.tv_sec ||
56 (start.tv_sec + timeout == tp.tv_sec &&
57 start.tv_nsec > tp.tv_nsec); i++) {
58 vdso_clock_gettime(clockid, &tp);
61 ksft_test_result_pass("%s:\tclock: %10s\tcycles:\t%10ld\n",
62 in_ns ? "ns" : "host", clockstr, i);
65 int main(int argc, char *argv[])
72 fill_function_pointers();
74 test(CLOCK_MONOTONIC, "monotonic", false);
75 test(CLOCK_MONOTONIC_COARSE, "monotonic-coarse", false);
76 test(CLOCK_MONOTONIC_RAW, "monotonic-raw", false);
77 test(CLOCK_BOOTTIME, "boottime", false);
84 nsfd = open("/proc/self/ns/time_for_children", O_RDONLY);
86 return pr_perror("Can't open a time namespace");
88 if (_settime(CLOCK_MONOTONIC, offset))
90 if (_settime(CLOCK_BOOTTIME, offset))
93 if (setns(nsfd, CLONE_NEWTIME))
94 return pr_perror("setns");
96 test(CLOCK_MONOTONIC, "monotonic", true);
97 test(CLOCK_MONOTONIC_COARSE, "monotonic-coarse", true);
98 test(CLOCK_MONOTONIC_RAW, "monotonic-raw", true);
99 test(CLOCK_BOOTTIME, "boottime", true);