sched-pipe.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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. */
  12. #include "../perf.h"
  13. #include "../util/util.h"
  14. #include "../util/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 <linux/unistd.h>
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <assert.h>
  26. #include <sys/time.h>
  27. #include <sys/types.h>
  28. #define LOOPS_DEFAULT 1000000
  29. static int loops = LOOPS_DEFAULT;
  30. static const struct option options[] = {
  31. OPT_INTEGER('l', "loop", &loops,
  32. "Specify number of loops"),
  33. OPT_END()
  34. };
  35. static const char * const bench_sched_pipe_usage[] = {
  36. "perf bench sched pipe <options>",
  37. NULL
  38. };
  39. int bench_sched_pipe(int argc, const char **argv,
  40. const char *prefix __used)
  41. {
  42. int pipe_1[2], pipe_2[2];
  43. int m = 0, i;
  44. struct timeval start, stop, diff;
  45. unsigned long long result_usec = 0;
  46. /*
  47. * why does "ret" exist?
  48. * discarding returned value of read(), write()
  49. * causes error in building environment for perf
  50. */
  51. int __used ret, wait_stat;
  52. pid_t pid, retpid;
  53. argc = parse_options(argc, argv, options,
  54. bench_sched_pipe_usage, 0);
  55. assert(!pipe(pipe_1));
  56. assert(!pipe(pipe_2));
  57. pid = fork();
  58. assert(pid >= 0);
  59. gettimeofday(&start, NULL);
  60. if (!pid) {
  61. for (i = 0; i < loops; i++) {
  62. ret = read(pipe_1[0], &m, sizeof(int));
  63. ret = write(pipe_2[1], &m, sizeof(int));
  64. }
  65. } else {
  66. for (i = 0; i < loops; i++) {
  67. ret = write(pipe_1[1], &m, sizeof(int));
  68. ret = read(pipe_2[0], &m, sizeof(int));
  69. }
  70. }
  71. gettimeofday(&stop, NULL);
  72. timersub(&stop, &start, &diff);
  73. if (pid) {
  74. retpid = waitpid(pid, &wait_stat, 0);
  75. assert((retpid == pid) && WIFEXITED(wait_stat));
  76. } else {
  77. exit(0);
  78. }
  79. switch (bench_format) {
  80. case BENCH_FORMAT_DEFAULT:
  81. printf("# Executed %d pipe operations between two tasks\n\n",
  82. loops);
  83. result_usec = diff.tv_sec * 1000000;
  84. result_usec += diff.tv_usec;
  85. printf(" %14s: %lu.%03lu [sec]\n\n", "Total time",
  86. diff.tv_sec,
  87. (unsigned long) (diff.tv_usec/1000));
  88. printf(" %14lf usecs/op\n",
  89. (double)result_usec / (double)loops);
  90. printf(" %14d ops/sec\n",
  91. (int)((double)loops /
  92. ((double)result_usec / (double)1000000)));
  93. break;
  94. case BENCH_FORMAT_SIMPLE:
  95. printf("%lu.%03lu\n",
  96. diff.tv_sec,
  97. (unsigned long) (diff.tv_usec / 1000));
  98. break;
  99. default:
  100. /* reaching here is something disaster */
  101. fprintf(stderr, "Unknown format:%d\n", bench_format);
  102. exit(1);
  103. break;
  104. }
  105. return 0;
  106. }