xref: /OK3568_Linux_fs/kernel/tools/testing/selftests/timens/gettime_perf.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun #define _GNU_SOURCE
3*4882a593Smuzhiyun #include <sys/types.h>
4*4882a593Smuzhiyun #include <sys/stat.h>
5*4882a593Smuzhiyun #include <errno.h>
6*4882a593Smuzhiyun #include <fcntl.h>
7*4882a593Smuzhiyun #include <sched.h>
8*4882a593Smuzhiyun #include <time.h>
9*4882a593Smuzhiyun #include <stdio.h>
10*4882a593Smuzhiyun #include <unistd.h>
11*4882a593Smuzhiyun #include <sys/syscall.h>
12*4882a593Smuzhiyun #include <dlfcn.h>
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #include "log.h"
15*4882a593Smuzhiyun #include "timens.h"
16*4882a593Smuzhiyun 
17*4882a593Smuzhiyun typedef int (*vgettime_t)(clockid_t, struct timespec *);
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun vgettime_t vdso_clock_gettime;
20*4882a593Smuzhiyun 
fill_function_pointers(void)21*4882a593Smuzhiyun static void fill_function_pointers(void)
22*4882a593Smuzhiyun {
23*4882a593Smuzhiyun 	void *vdso = dlopen("linux-vdso.so.1",
24*4882a593Smuzhiyun 			    RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
25*4882a593Smuzhiyun 	if (!vdso)
26*4882a593Smuzhiyun 		vdso = dlopen("linux-gate.so.1",
27*4882a593Smuzhiyun 			      RTLD_LAZY | RTLD_LOCAL | RTLD_NOLOAD);
28*4882a593Smuzhiyun 	if (!vdso) {
29*4882a593Smuzhiyun 		pr_err("[WARN]\tfailed to find vDSO\n");
30*4882a593Smuzhiyun 		return;
31*4882a593Smuzhiyun 	}
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	vdso_clock_gettime = (vgettime_t)dlsym(vdso, "__vdso_clock_gettime");
34*4882a593Smuzhiyun 	if (!vdso_clock_gettime)
35*4882a593Smuzhiyun 		pr_err("Warning: failed to find clock_gettime in vDSO\n");
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun }
38*4882a593Smuzhiyun 
test(clock_t clockid,char * clockstr,bool in_ns)39*4882a593Smuzhiyun static void test(clock_t clockid, char *clockstr, bool in_ns)
40*4882a593Smuzhiyun {
41*4882a593Smuzhiyun 	struct timespec tp, start;
42*4882a593Smuzhiyun 	long i = 0;
43*4882a593Smuzhiyun 	const int timeout = 3;
44*4882a593Smuzhiyun 
45*4882a593Smuzhiyun 	vdso_clock_gettime(clockid, &start);
46*4882a593Smuzhiyun 	tp = start;
47*4882a593Smuzhiyun 	for (tp = start; start.tv_sec + timeout > tp.tv_sec ||
48*4882a593Smuzhiyun 			 (start.tv_sec + timeout == tp.tv_sec &&
49*4882a593Smuzhiyun 			  start.tv_nsec > tp.tv_nsec); i++) {
50*4882a593Smuzhiyun 		vdso_clock_gettime(clockid, &tp);
51*4882a593Smuzhiyun 	}
52*4882a593Smuzhiyun 
53*4882a593Smuzhiyun 	ksft_test_result_pass("%s:\tclock: %10s\tcycles:\t%10ld\n",
54*4882a593Smuzhiyun 			      in_ns ? "ns" : "host", clockstr, i);
55*4882a593Smuzhiyun }
56*4882a593Smuzhiyun 
main(int argc,char * argv[])57*4882a593Smuzhiyun int main(int argc, char *argv[])
58*4882a593Smuzhiyun {
59*4882a593Smuzhiyun 	time_t offset = 10;
60*4882a593Smuzhiyun 	int nsfd;
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun 	ksft_set_plan(8);
63*4882a593Smuzhiyun 
64*4882a593Smuzhiyun 	fill_function_pointers();
65*4882a593Smuzhiyun 
66*4882a593Smuzhiyun 	test(CLOCK_MONOTONIC, "monotonic", false);
67*4882a593Smuzhiyun 	test(CLOCK_MONOTONIC_COARSE, "monotonic-coarse", false);
68*4882a593Smuzhiyun 	test(CLOCK_MONOTONIC_RAW, "monotonic-raw", false);
69*4882a593Smuzhiyun 	test(CLOCK_BOOTTIME, "boottime", false);
70*4882a593Smuzhiyun 
71*4882a593Smuzhiyun 	nscheck();
72*4882a593Smuzhiyun 
73*4882a593Smuzhiyun 	if (unshare_timens())
74*4882a593Smuzhiyun 		return 1;
75*4882a593Smuzhiyun 
76*4882a593Smuzhiyun 	nsfd = open("/proc/self/ns/time_for_children", O_RDONLY);
77*4882a593Smuzhiyun 	if (nsfd < 0)
78*4882a593Smuzhiyun 		return pr_perror("Can't open a time namespace");
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	if (_settime(CLOCK_MONOTONIC, offset))
81*4882a593Smuzhiyun 		return 1;
82*4882a593Smuzhiyun 	if (_settime(CLOCK_BOOTTIME, offset))
83*4882a593Smuzhiyun 		return 1;
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 	if (setns(nsfd, CLONE_NEWTIME))
86*4882a593Smuzhiyun 		return pr_perror("setns");
87*4882a593Smuzhiyun 
88*4882a593Smuzhiyun 	test(CLOCK_MONOTONIC, "monotonic", true);
89*4882a593Smuzhiyun 	test(CLOCK_MONOTONIC_COARSE, "monotonic-coarse", true);
90*4882a593Smuzhiyun 	test(CLOCK_MONOTONIC_RAW, "monotonic-raw", true);
91*4882a593Smuzhiyun 	test(CLOCK_BOOTTIME, "boottime", true);
92*4882a593Smuzhiyun 
93*4882a593Smuzhiyun 	ksft_exit_pass();
94*4882a593Smuzhiyun 	return 0;
95*4882a593Smuzhiyun }
96