sched-pipe.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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 "../util/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 <pthread.h>
  28. struct thread_data {
  29. int nr;
  30. int pipe_read;
  31. int pipe_write;
  32. pthread_t pthread;
  33. };
  34. #define LOOPS_DEFAULT 1000000
  35. static int loops = LOOPS_DEFAULT;
  36. /* Use processes by default: */
  37. static bool threaded;
  38. static const struct option options[] = {
  39. OPT_INTEGER('l', "loop", &loops, "Specify number of loops"),
  40. OPT_BOOLEAN('T', "threaded", &threaded, "Specify threads/process based task setup"),
  41. OPT_END()
  42. };
  43. static const char * const bench_sched_pipe_usage[] = {
  44. "perf bench sched pipe <options>",
  45. NULL
  46. };
  47. static void *worker_thread(void *__tdata)
  48. {
  49. struct thread_data *td = __tdata;
  50. int m = 0, i;
  51. int ret;
  52. for (i = 0; i < loops; i++) {
  53. if (!td->nr) {
  54. ret = read(td->pipe_read, &m, sizeof(int));
  55. BUG_ON(ret != sizeof(int));
  56. ret = write(td->pipe_write, &m, sizeof(int));
  57. BUG_ON(ret != sizeof(int));
  58. } else {
  59. ret = write(td->pipe_write, &m, sizeof(int));
  60. BUG_ON(ret != sizeof(int));
  61. ret = read(td->pipe_read, &m, sizeof(int));
  62. BUG_ON(ret != sizeof(int));
  63. }
  64. }
  65. return NULL;
  66. }
  67. int bench_sched_pipe(int argc, const char **argv, const char *prefix __maybe_unused)
  68. {
  69. struct thread_data threads[2], *td;
  70. int pipe_1[2], pipe_2[2];
  71. struct timeval start, stop, diff;
  72. unsigned long long result_usec = 0;
  73. int nr_threads = 2;
  74. int t;
  75. /*
  76. * why does "ret" exist?
  77. * discarding returned value of read(), write()
  78. * causes error in building environment for perf
  79. */
  80. int __maybe_unused ret, wait_stat;
  81. pid_t pid, retpid __maybe_unused;
  82. argc = parse_options(argc, argv, options, bench_sched_pipe_usage, 0);
  83. BUG_ON(pipe(pipe_1));
  84. BUG_ON(pipe(pipe_2));
  85. gettimeofday(&start, NULL);
  86. for (t = 0; t < nr_threads; t++) {
  87. td = threads + t;
  88. td->nr = t;
  89. if (t == 0) {
  90. td->pipe_read = pipe_1[0];
  91. td->pipe_write = pipe_2[1];
  92. } else {
  93. td->pipe_write = pipe_1[1];
  94. td->pipe_read = pipe_2[0];
  95. }
  96. }
  97. if (threaded) {
  98. for (t = 0; t < nr_threads; t++) {
  99. td = threads + t;
  100. ret = pthread_create(&td->pthread, NULL, worker_thread, td);
  101. BUG_ON(ret);
  102. }
  103. for (t = 0; t < nr_threads; t++) {
  104. td = threads + t;
  105. ret = pthread_join(td->pthread, NULL);
  106. BUG_ON(ret);
  107. }
  108. } else {
  109. pid = fork();
  110. assert(pid >= 0);
  111. if (!pid) {
  112. worker_thread(threads + 0);
  113. exit(0);
  114. } else {
  115. worker_thread(threads + 1);
  116. }
  117. retpid = waitpid(pid, &wait_stat, 0);
  118. assert((retpid == pid) && WIFEXITED(wait_stat));
  119. }
  120. gettimeofday(&stop, NULL);
  121. timersub(&stop, &start, &diff);
  122. switch (bench_format) {
  123. case BENCH_FORMAT_DEFAULT:
  124. printf("# Executed %d pipe operations between two %s\n\n",
  125. loops, threaded ? "threads" : "processes");
  126. result_usec = diff.tv_sec * 1000000;
  127. result_usec += diff.tv_usec;
  128. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  129. diff.tv_sec,
  130. (unsigned long) (diff.tv_usec/1000));
  131. printf(" %14lf usecs/op\n",
  132. (double)result_usec / (double)loops);
  133. printf(" %14d ops/sec\n",
  134. (int)((double)loops /
  135. ((double)result_usec / (double)1000000)));
  136. break;
  137. case BENCH_FORMAT_SIMPLE:
  138. printf("%lu.%03lu\n",
  139. diff.tv_sec,
  140. (unsigned long) (diff.tv_usec / 1000));
  141. break;
  142. default:
  143. /* reaching here is something disaster */
  144. fprintf(stderr, "Unknown format:%d\n", bench_format);
  145. exit(1);
  146. break;
  147. }
  148. return 0;
  149. }