null_syscall.c 3.1 KB

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