null_syscall.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Test null syscall performance
  4. *
  5. * Copyright (C) 2009-2015 Anton Blanchard, IBM
  6. */
  7. #define NR_LOOPS 10000000
  8. #include <string.h>
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <unistd.h>
  12. #include <time.h>
  13. #include <sys/types.h>
  14. #include <sys/time.h>
  15. #include <sys/syscall.h>
  16. #include <signal.h>
  17. static volatile int soak_done;
  18. unsigned long long clock_frequency;
  19. unsigned long long timebase_frequency;
  20. double timebase_multiplier;
  21. static inline unsigned long mftb(void)
  22. {
  23. unsigned long low;
  24. asm volatile("mftb %0" : "=r" (low));
  25. return low;
  26. }
  27. static void sigalrm_handler(int unused)
  28. {
  29. soak_done = 1;
  30. }
  31. /*
  32. * Use a timer instead of busy looping on clock_gettime() so we don't
  33. * pollute profiles with glibc and VDSO hits.
  34. */
  35. static void cpu_soak_usecs(unsigned long usecs)
  36. {
  37. struct itimerval val;
  38. memset(&val, 0, sizeof(val));
  39. val.it_value.tv_usec = usecs;
  40. signal(SIGALRM, sigalrm_handler);
  41. setitimer(ITIMER_REAL, &val, NULL);
  42. while (1) {
  43. if (soak_done)
  44. break;
  45. }
  46. signal(SIGALRM, SIG_DFL);
  47. }
  48. /*
  49. * This only works with recent kernels where cpufreq modifies
  50. * /proc/cpuinfo dynamically.
  51. */
  52. static void get_proc_frequency(void)
  53. {
  54. FILE *f;
  55. char line[128];
  56. char *p, *end;
  57. unsigned long v;
  58. double d;
  59. char *override;
  60. /* Try to get out of low power/low frequency mode */
  61. cpu_soak_usecs(0.25 * 1000000);
  62. f = fopen("/proc/cpuinfo", "r");
  63. if (f == NULL)
  64. return;
  65. timebase_frequency = 0;
  66. while (fgets(line, sizeof(line), f) != NULL) {
  67. if (strncmp(line, "timebase", 8) == 0) {
  68. p = strchr(line, ':');
  69. if (p != NULL) {
  70. v = strtoull(p + 1, &end, 0);
  71. if (end != p + 1)
  72. timebase_frequency = v;
  73. }
  74. }
  75. if (((strncmp(line, "clock", 5) == 0) ||
  76. (strncmp(line, "cpu MHz", 7) == 0))) {
  77. p = strchr(line, ':');
  78. if (p != NULL) {
  79. d = strtod(p + 1, &end);
  80. if (end != p + 1) {
  81. /* Find fastest clock frequency */
  82. if ((d * 1000000ULL) > clock_frequency)
  83. clock_frequency = d * 1000000ULL;
  84. }
  85. }
  86. }
  87. }
  88. fclose(f);
  89. override = getenv("FREQUENCY");
  90. if (override)
  91. clock_frequency = strtoull(override, NULL, 10);
  92. if (timebase_frequency)
  93. timebase_multiplier = (double)clock_frequency
  94. / timebase_frequency;
  95. else
  96. timebase_multiplier = 1;
  97. }
  98. static void do_null_syscall(unsigned long nr)
  99. {
  100. unsigned long i;
  101. for (i = 0; i < nr; i++)
  102. syscall(__NR_gettid);
  103. }
  104. #define TIME(A, STR) \
  105. int main(void)
  106. {
  107. unsigned long tb_start, tb_now;
  108. struct timespec tv_start, tv_now;
  109. unsigned long long elapsed_ns, elapsed_tb;
  110. get_proc_frequency();
  111. clock_gettime(CLOCK_MONOTONIC, &tv_start);
  112. tb_start = mftb();
  113. do_null_syscall(NR_LOOPS);
  114. clock_gettime(CLOCK_MONOTONIC, &tv_now);
  115. tb_now = mftb();
  116. elapsed_ns = (tv_now.tv_sec - tv_start.tv_sec) * 1000000000ULL +
  117. (tv_now.tv_nsec - tv_start.tv_nsec);
  118. elapsed_tb = tb_now - tb_start;
  119. printf("%10.2f ns %10.2f cycles\n", (float)elapsed_ns / NR_LOOPS,
  120. (float)elapsed_tb * timebase_multiplier / NR_LOOPS);
  121. return 0;
  122. }