sched-pipe.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. *
  3. * builtin-bench-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 int simple = 0;
  31. static const struct option options[] = {
  32. OPT_INTEGER('l', "loop", &loops,
  33. "Specify number of loops"),
  34. OPT_BOOLEAN('s', "simple-output", &simple,
  35. "Do simple output (this maybe useful for"
  36. "processing by scripts or graph tools like gnuplot)"),
  37. OPT_END()
  38. };
  39. static const char * const bench_sched_pipe_usage[] = {
  40. "perf bench sched pipe <options>",
  41. NULL
  42. };
  43. int bench_sched_pipe(int argc, const char **argv,
  44. const char *prefix __used)
  45. {
  46. int pipe_1[2], pipe_2[2];
  47. int m = 0, i;
  48. struct timeval start, stop, diff;
  49. unsigned long long result_usec = 0;
  50. /*
  51. * why does "ret" exist?
  52. * discarding returned value of read(), write()
  53. * causes error in building environment for perf
  54. */
  55. int ret, wait_stat;
  56. pid_t pid, retpid;
  57. argc = parse_options(argc, argv, options,
  58. bench_sched_pipe_usage, 0);
  59. assert(!pipe(pipe_1));
  60. assert(!pipe(pipe_2));
  61. pid = fork();
  62. assert(pid >= 0);
  63. gettimeofday(&start, NULL);
  64. if (!pid) {
  65. for (i = 0; i < loops; i++) {
  66. ret = read(pipe_1[0], &m, sizeof(int));
  67. ret = write(pipe_2[1], &m, sizeof(int));
  68. }
  69. } else {
  70. for (i = 0; i < loops; i++) {
  71. ret = write(pipe_1[1], &m, sizeof(int));
  72. ret = read(pipe_2[0], &m, sizeof(int));
  73. }
  74. }
  75. gettimeofday(&stop, NULL);
  76. timersub(&stop, &start, &diff);
  77. if (pid) {
  78. retpid = waitpid(pid, &wait_stat, 0);
  79. assert((retpid == pid) && WIFEXITED(wait_stat));
  80. return 0;
  81. }
  82. if (simple)
  83. printf("%lu.%03lu\n",
  84. diff.tv_sec, diff.tv_usec / 1000);
  85. else {
  86. printf("(executing %d pipe operations between two tasks)\n\n",
  87. loops);
  88. result_usec = diff.tv_sec * 1000000;
  89. result_usec += diff.tv_usec;
  90. printf("\tTotal time:%lu.%03lu sec\n",
  91. diff.tv_sec, diff.tv_usec / 1000);
  92. printf("\t\t%lf usecs/op\n",
  93. (double)result_usec / (double)loops);
  94. printf("\t\t%d ops/sec\n",
  95. (int)((double)loops /
  96. ((double)result_usec / (double)1000000)));
  97. }
  98. return 0;
  99. }