2 * Test null syscall performance
4 * Copyright (C) 2009-2015 Anton Blanchard, IBM
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version
9 * 2 of the License, or (at your option) any later version.
12 #define NR_LOOPS 10000000
19 #include <sys/types.h>
23 static volatile int soak_done;
24 unsigned long long clock_frequency;
25 unsigned long long timebase_frequency;
26 double timebase_multiplier;
28 static inline unsigned long long mftb(void)
32 asm volatile("mftb %0" : "=r" (low));
37 static void sigalrm_handler(int unused)
43 * Use a timer instead of busy looping on clock_gettime() so we don't
44 * pollute profiles with glibc and VDSO hits.
46 static void cpu_soak_usecs(unsigned long usecs)
50 memset(&val, 0, sizeof(val));
51 val.it_value.tv_usec = usecs;
53 signal(SIGALRM, sigalrm_handler);
54 setitimer(ITIMER_REAL, &val, NULL);
61 signal(SIGALRM, SIG_DFL);
65 * This only works with recent kernels where cpufreq modifies
66 * /proc/cpuinfo dynamically.
68 static void get_proc_frequency(void)
77 /* Try to get out of low power/low frequency mode */
78 cpu_soak_usecs(0.25 * 1000000);
80 f = fopen("/proc/cpuinfo", "r");
84 timebase_frequency = 0;
86 while (fgets(line, sizeof(line), f) != NULL) {
87 if (strncmp(line, "timebase", 8) == 0) {
88 p = strchr(line, ':');
90 v = strtoull(p + 1, &end, 0);
92 timebase_frequency = v;
96 if (((strncmp(line, "clock", 5) == 0) ||
97 (strncmp(line, "cpu MHz", 7) == 0))) {
98 p = strchr(line, ':');
100 d = strtod(p + 1, &end);
102 /* Find fastest clock frequency */
103 if ((d * 1000000ULL) > clock_frequency)
104 clock_frequency = d * 1000000ULL;
112 override = getenv("FREQUENCY");
114 clock_frequency = strtoull(override, NULL, 10);
116 if (timebase_frequency)
117 timebase_multiplier = (double)clock_frequency
118 / timebase_frequency;
120 timebase_multiplier = 1;
123 static void do_null_syscall(unsigned long nr)
127 for (i = 0; i < nr; i++)
131 #define TIME(A, STR) \
135 unsigned long tb_start, tb_now;
136 struct timespec tv_start, tv_now;
137 unsigned long long elapsed_ns, elapsed_tb;
139 get_proc_frequency();
141 clock_gettime(CLOCK_MONOTONIC, &tv_start);
144 do_null_syscall(NR_LOOPS);
146 clock_gettime(CLOCK_MONOTONIC, &tv_now);
149 elapsed_ns = (tv_now.tv_sec - tv_start.tv_sec) * 1000000000ULL +
150 (tv_now.tv_nsec - tv_start.tv_nsec);
151 elapsed_tb = tb_now - tb_start;
153 printf("%10.2f ns %10.2f cycles\n", (float)elapsed_ns / NR_LOOPS,
154 (float)elapsed_tb * timebase_multiplier / NR_LOOPS);