sched-pipe.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. *
  4. * sched-pipe.c
  5. *
  6. * pipe: Benchmark for pipe()
  7. *
  8. * Based on pipe-test-1m.c by Ingo Molnar <mingo@redhat.com>
  9. * http://people.redhat.com/mingo/cfs-scheduler/tools/pipe-test-1m.c
  10. * Ported to perf by Hitoshi Mitake <mitake@dcl.info.waseda.ac.jp>
  11. */
  12. #include "../perf.h"
  13. #include "../util/util.h"
  14. #include <subcmd/parse-options.h>
  15. #include "../builtin.h"
  16. #include "bench.h"
  17. #include <unistd.h>
  18. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <signal.h>
  21. #include <sys/wait.h>
  22. #include <string.h>
  23. #include <errno.h>
  24. #include <assert.h>
  25. #include <sys/time.h>
  26. #include <sys/types.h>
  27. #include <sys/syscall.h>
  28. #include <linux/time64.h>
  29. #include <pthread.h>
  30. struct thread_data {
  31. int nr;
  32. int pipe_read;
  33. int pipe_write;
  34. pthread_t pthread;
  35. };
  36. #define LOOPS_DEFAULT 1000000
  37. static int loops = LOOPS_DEFAULT;
  38. /* Use processes by default: */
  39. static bool threaded;
  40. static const struct option options[] = {
  41. OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
  42. OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
  43. OPT_END()
  44. };
  45. static const char * const bench_sched_pipe_usage[] = {
  46. "perf bench sched pipe <options>",
  47. NULL
  48. };
  49. static void *worker_thread(void *__tdata)
  50. {
  51. struct thread_data *td = __tdata;
  52. int m = 0, i;
  53. int ret;
  54. for (i = 0; i < loops; i++) {
  55. if (!td->nr) {
  56. ret = read(td->pipe_read, &m, sizeof(int));
  57. BUG_ON(ret != sizeof(int));
  58. ret = write(td->pipe_write, &m, sizeof(int));
  59. BUG_ON(ret != sizeof(int));
  60. } else {
  61. ret = write(td->pipe_write, &m, sizeof(int));
  62. BUG_ON(ret != sizeof(int));
  63. ret = read(td->pipe_read, &m, sizeof(int));
  64. BUG_ON(ret != sizeof(int));
  65. }
  66. }
  67. return NULL;
  68. }
  69. int bench_sched_pipe(int argc, const char **argv)
  70. {
  71. struct thread_data threads[2], *td;
  72. int pipe_1[2], pipe_2[2];
  73. struct timeval start, stop, diff;
  74. unsigned long long result_usec = 0;
  75. int nr_threads = 2;
  76. int t;
  77. /*
  78. * why does "ret" exist?
  79. * discarding returned value of read(), write()
  80. * causes error in building environment for perf
  81. */
  82. int __maybe_unused ret, wait_stat;
  83. pid_t pid, retpid __maybe_unused;
  84. argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
  85. BUG_ON(pipe(pipe_1));
  86. BUG_ON(pipe(pipe_2));
  87. gettimeofday(&start, NULL);
  88. for (t = 0; t < nr_threads; t++) {
  89. td = threads + t;
  90. td->nr = t;
  91. if (t == 0) {
  92. td->pipe_read = pipe_1[0];
  93. td->pipe_write = pipe_2[1];
  94. } else {
  95. td->pipe_write = pipe_1[1];
  96. td->pipe_read = pipe_2[0];
  97. }
  98. }
  99. if (threaded) {
  100. for (t = 0; t < nr_threads; t++) {
  101. td = threads + t;
  102. ret = pthread_create(&td->pthread, NULL, worker_thread, td);
  103. BUG_ON(ret);
  104. }
  105. for (t = 0; t < nr_threads; t++) {
  106. td = threads + t;
  107. ret = pthread_join(td->pthread, NULL);
  108. BUG_ON(ret);
  109. }
  110. } else {
  111. pid = fork();
  112. assert(pid >= 0);
  113. if (!pid) {
  114. worker_thread(threads + 0);
  115. exit(0);
  116. } else {
  117. worker_thread(threads + 1);
  118. }
  119. retpid = waitpid(pid, &wait_stat, 0);
  120. assert((retpid == pid) && WIFEXITED(wait_stat));
  121. }
  122. gettimeofday(&stop, NULL);
  123. timersub(&stop, &start, &diff);
  124. switch (bench_format) {
  125. case BENCH_FORMAT_DEFAULT:
  126. printf("# Executed %d pipe operations between two %s\n\n",
  127. loops, threaded ? "threads" : "processes");
  128. result_usec = diff.tv_sec * USEC_PER_SEC;
  129. result_usec += diff.tv_usec;
  130. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  131. diff.tv_sec,
  132. (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
  133. printf(" %14lf usecs/op\n",
  134. (double)result_usec / (double)loops);
  135. printf(" %14d ops/sec\n",
  136. (int)((double)loops /
  137. ((double)result_usec / (double)USEC_PER_SEC)));
  138. break;
  139. case BENCH_FORMAT_SIMPLE:
  140. printf("%lu.%03lu\n",
  141. diff.tv_sec,
  142. (unsigned long) (diff.tv_usec / USEC_PER_MSEC));
  143. break;
  144. default:
  145. /* reaching here is something disaster */
  146. fprintf(stderr, "Unknown format:%d\n", bench_format);
  147. exit(1);
  148. break;
  149. }
  150. return 0;
  151. }