sched-pipe.c 3.9 KB

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