1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Test null syscall performance
5 * Copyright (C) 2009-2015 Anton Blanchard, IBM
8 #define NR_LOOPS 10000000
15 #include <sys/types.h>
17 #include <sys/syscall.h>
20 static volatile int soak_done
;
21 unsigned long long clock_frequency
;
22 unsigned long long timebase_frequency
;
23 double timebase_multiplier
;
25 static inline unsigned long mftb(void)
29 asm volatile("mftb %0" : "=r" (low
));
34 static void sigalrm_handler(int unused
)
40 * Use a timer instead of busy looping on clock_gettime() so we don't
41 * pollute profiles with glibc and VDSO hits.
43 static void cpu_soak_usecs(unsigned long usecs
)
47 memset(&val
, 0, sizeof(val
));
48 val
.it_value
.tv_usec
= usecs
;
50 signal(SIGALRM
, sigalrm_handler
);
51 setitimer(ITIMER_REAL
, &val
, NULL
);
58 signal(SIGALRM
, SIG_DFL
);
62 * This only works with recent kernels where cpufreq modifies
63 * /proc/cpuinfo dynamically.
65 static void get_proc_frequency(void)
74 /* Try to get out of low power/low frequency mode */
75 cpu_soak_usecs(0.25 * 1000000);
77 f
= fopen("/proc/cpuinfo", "r");
81 timebase_frequency
= 0;
83 while (fgets(line
, sizeof(line
), f
) != NULL
) {
84 if (strncmp(line
, "timebase", 8) == 0) {
85 p
= strchr(line
, ':');
87 v
= strtoull(p
+ 1, &end
, 0);
89 timebase_frequency
= v
;
93 if (((strncmp(line
, "clock", 5) == 0) ||
94 (strncmp(line
, "cpu MHz", 7) == 0))) {
95 p
= strchr(line
, ':');
97 d
= strtod(p
+ 1, &end
);
99 /* Find fastest clock frequency */
100 if ((d
* 1000000ULL) > clock_frequency
)
101 clock_frequency
= d
* 1000000ULL;
109 override
= getenv("FREQUENCY");
111 clock_frequency
= strtoull(override
, NULL
, 10);
113 if (timebase_frequency
)
114 timebase_multiplier
= (double)clock_frequency
115 / timebase_frequency
;
117 timebase_multiplier
= 1;
120 static void do_null_syscall(unsigned long nr
)
124 for (i
= 0; i
< nr
; i
++)
125 syscall(__NR_gettid
);
128 #define TIME(A, STR) \
132 unsigned long tb_start
, tb_now
;
133 struct timespec tv_start
, tv_now
;
134 unsigned long long elapsed_ns
, elapsed_tb
;
136 get_proc_frequency();
138 clock_gettime(CLOCK_MONOTONIC
, &tv_start
);
141 do_null_syscall(NR_LOOPS
);
143 clock_gettime(CLOCK_MONOTONIC
, &tv_now
);
146 elapsed_ns
= (tv_now
.tv_sec
- tv_start
.tv_sec
) * 1000000000ULL +
147 (tv_now
.tv_nsec
- tv_start
.tv_nsec
);
148 elapsed_tb
= tb_now
- tb_start
;
150 printf("%10.2f ns %10.2f cycles\n", (float)elapsed_ns
/ NR_LOOPS
,
151 (float)elapsed_tb
* timebase_multiplier
/ NR_LOOPS
);