pipe_latency.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5. #include <unistd.h>
  6. #include <sys/wait.h>
  7. #include <signal.h>
  8. #include <sys/time.h>
  9. #define NTRIES 10000
  10. #define TEST_TIMES 32
  11. int main(int argc, char ** argv)
  12. {
  13. int times = TEST_TIMES;
  14. int pipes[6];
  15. int pids[TEST_TIMES][2];
  16. int i = 0;
  17. if (argc >= 2) {
  18. times = atoi(argv[1]) / 2;
  19. if (times > TEST_TIMES)
  20. return -1;
  21. }
  22. pipe(&pipes[0]);
  23. pipe(&pipes[2]);
  24. pipe(&pipes[4]);
  25. for (i = 0 ; i < times ; i++ ) {
  26. int pair_pipes[4];
  27. pipe(&pair_pipes[0]);
  28. pipe(&pair_pipes[2]);
  29. pids[i][0] = fork();
  30. if (pids[i][0] < 0) {
  31. printf("fork failed\n");
  32. return -1;
  33. }
  34. if (pids[i][0] == 0) {
  35. close(pipes[0]);
  36. close(pipes[1]);
  37. close(pipes[3]);
  38. close(pipes[4]);
  39. close(pipes[5]);
  40. close(pair_pipes[1]);
  41. close(pair_pipes[2]);
  42. char byte;
  43. for (int i = 0 ; i < NTRIES ; i++) {
  44. read(pair_pipes[0], &byte, 1);
  45. write(pair_pipes[3], &byte, 1);
  46. }
  47. read(pipes[2], &byte, 1);
  48. close(pipes[2]);
  49. close(pair_pipes[0]);
  50. close(pair_pipes[3]);
  51. exit(0);
  52. }
  53. pids[i][1] = fork();
  54. if (pids[i][1] < 0) {
  55. printf("fork failed\n");
  56. return -1;
  57. }
  58. if (pids[i][1] == 0) {
  59. close(pipes[1]);
  60. close(pipes[3]);
  61. close(pipes[4]);
  62. close(pair_pipes[0]);
  63. close(pair_pipes[3]);
  64. char byte;
  65. read(pipes[0], &byte, 1);
  66. struct timeval timevals[2];
  67. gettimeofday(&timevals[0], NULL);
  68. for (int i = 0 ; i < NTRIES ; i++) {
  69. write(pair_pipes[1], &byte, 1);
  70. read(pair_pipes[2], &byte, 1);
  71. }
  72. gettimeofday(&timevals[1], NULL);
  73. close(pair_pipes[1]);
  74. close(pair_pipes[2]);
  75. close(pipes[0]);
  76. write(pipes[5], timevals, sizeof(struct timeval) * 2);
  77. close(pipes[5]);
  78. read(pipes[2], &byte, 1);
  79. close(pipes[2]);
  80. exit(0);
  81. }
  82. }
  83. close(pipes[0]);
  84. close(pipes[2]);
  85. close(pipes[5]);
  86. sleep(1);
  87. char bytes[times * 2];
  88. write(pipes[1], bytes, times);
  89. close(pipes[1]);
  90. unsigned long long start_time = 0;
  91. unsigned long long end_time = 0;
  92. struct timeval timevals[2];
  93. for (int i = 0 ; i < times ; i++) {
  94. read(pipes[4], timevals, sizeof(struct timeval) * 2);
  95. unsigned long s = timevals[0].tv_sec * 1000000ULL +
  96. timevals[0].tv_usec;
  97. unsigned long e = timevals[1].tv_sec * 1000000ULL +
  98. timevals[1].tv_usec;
  99. if (!start_time || s < start_time)
  100. start_time = s;
  101. if (!end_time || e > end_time)
  102. end_time = e;
  103. }
  104. close(pipes[4]);
  105. write(pipes[3], bytes, times * 2);
  106. close(pipes[3]);
  107. for (i = 0 ; i < times ; i++) {
  108. waitpid(pids[i][0], NULL, 0);
  109. waitpid(pids[i][1], NULL, 0);
  110. }
  111. printf("throughput for %d processes to send %d message: %lf bytes/second\n",
  112. times, NTRIES,
  113. 1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
  114. return 0;
  115. }