pipe_latency.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. #include <signal.h>
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <sys/time.h>
  5. #include <sys/wait.h>
  6. #include <unistd.h>
  7. #define NTRIES 10000
  8. #define TEST_TIMES 32
  9. int main(int argc, char** argv) {
  10. int times = TEST_TIMES;
  11. int pipes[6];
  12. int pids[TEST_TIMES][2];
  13. int i = 0;
  14. if (argc >= 2) {
  15. times = atoi(argv[1]) / 2;
  16. if (times > TEST_TIMES)
  17. return 1;
  18. }
  19. if (pipe(&pipes[0]) < 0 || pipe(&pipes[2]) < 0 || pipe(&pipes[4]) < 0) {
  20. perror("pipe error");
  21. return 1;
  22. }
  23. for (i = 0; i < times; i++) {
  24. int pair_pipes[4];
  25. if (pipe(&pair_pipes[0]) < 0 || pipe(&pair_pipes[2]) < 0) {
  26. perror("pipe error");
  27. return 1;
  28. }
  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. if (read(pair_pipes[0], &byte, 1) != 1) {
  45. perror("read error");
  46. exit(1);
  47. }
  48. if (write(pair_pipes[3], &byte, 1) != 1) {
  49. perror("write error");
  50. exit(1);
  51. }
  52. }
  53. if (read(pipes[2], &byte, 1) != 1) {
  54. perror("read error");
  55. exit(1);
  56. }
  57. close(pipes[2]);
  58. close(pair_pipes[0]);
  59. close(pair_pipes[3]);
  60. exit(0);
  61. }
  62. pids[i][1] = fork();
  63. if (pids[i][1] < 0) {
  64. printf("fork failed\n");
  65. return 1;
  66. }
  67. if (pids[i][1] == 0) {
  68. close(pipes[1]);
  69. close(pipes[3]);
  70. close(pipes[4]);
  71. close(pair_pipes[0]);
  72. close(pair_pipes[3]);
  73. char byte;
  74. if (read(pipes[0], &byte, 1) != 1) {
  75. perror("read error");
  76. exit(1);
  77. }
  78. struct timeval timevals[2];
  79. gettimeofday(&timevals[0], NULL);
  80. for (int i = 0; i < NTRIES; i++) {
  81. if (write(pair_pipes[1], &byte, 1) != 1) {
  82. perror("write error");
  83. exit(1);
  84. }
  85. if (read(pair_pipes[2], &byte, 1) != 1) {
  86. perror("read error");
  87. exit(1);
  88. }
  89. }
  90. gettimeofday(&timevals[1], NULL);
  91. close(pair_pipes[1]);
  92. close(pair_pipes[2]);
  93. close(pipes[0]);
  94. if (write(pipes[5], timevals, sizeof(struct timeval) * 2)
  95. != sizeof(struct timeval) * 2) {
  96. perror("write error");
  97. exit(1);
  98. }
  99. close(pipes[5]);
  100. if (read(pipes[2], &byte, 1) != 1) {
  101. perror("read error");
  102. exit(1);
  103. }
  104. close(pipes[2]);
  105. exit(0);
  106. }
  107. }
  108. close(pipes[0]);
  109. close(pipes[2]);
  110. close(pipes[5]);
  111. sleep(1);
  112. char bytes[times * 2];
  113. if (write(pipes[1], bytes, times) != times) {
  114. perror("write error");
  115. return 1;
  116. }
  117. close(pipes[1]);
  118. unsigned long long start_time = 0;
  119. unsigned long long end_time = 0;
  120. struct timeval timevals[2];
  121. for (int i = 0; i < times; i++) {
  122. if (read(pipes[4], timevals, sizeof(struct timeval) * 2) != sizeof(struct timeval) * 2) {
  123. perror("read error");
  124. exit(1);
  125. }
  126. unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
  127. unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
  128. if (!start_time || s < start_time)
  129. start_time = s;
  130. if (!end_time || e > end_time)
  131. end_time = e;
  132. }
  133. close(pipes[4]);
  134. if (write(pipes[3], bytes, times * 2) != times * 2) {
  135. perror ("write error");
  136. return 1;
  137. }
  138. close(pipes[3]);
  139. for (i = 0; i < times; i++) {
  140. waitpid(pids[i][0], NULL, 0);
  141. waitpid(pids[i][1], NULL, 0);
  142. }
  143. printf("throughput for %d processes to send %d message: %lf bytes/second\n", times, NTRIES,
  144. 1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
  145. return 0;
  146. }