pipe_latency.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. pipe(&pipes[0]);
  20. pipe(&pipes[2]);
  21. pipe(&pipes[4]);
  22. for (i = 0; i < times; i++) {
  23. int pair_pipes[4];
  24. pipe(&pair_pipes[0]);
  25. pipe(&pair_pipes[2]);
  26. pids[i][0] = fork();
  27. if (pids[i][0] < 0) {
  28. printf("fork failed\n");
  29. return -1;
  30. }
  31. if (pids[i][0] == 0) {
  32. close(pipes[0]);
  33. close(pipes[1]);
  34. close(pipes[3]);
  35. close(pipes[4]);
  36. close(pipes[5]);
  37. close(pair_pipes[1]);
  38. close(pair_pipes[2]);
  39. char byte;
  40. for (int i = 0; i < NTRIES; i++) {
  41. read(pair_pipes[0], &byte, 1);
  42. write(pair_pipes[3], &byte, 1);
  43. }
  44. read(pipes[2], &byte, 1);
  45. close(pipes[2]);
  46. close(pair_pipes[0]);
  47. close(pair_pipes[3]);
  48. exit(0);
  49. }
  50. pids[i][1] = fork();
  51. if (pids[i][1] < 0) {
  52. printf("fork failed\n");
  53. return -1;
  54. }
  55. if (pids[i][1] == 0) {
  56. close(pipes[1]);
  57. close(pipes[3]);
  58. close(pipes[4]);
  59. close(pair_pipes[0]);
  60. close(pair_pipes[3]);
  61. char byte;
  62. read(pipes[0], &byte, 1);
  63. struct timeval timevals[2];
  64. gettimeofday(&timevals[0], NULL);
  65. for (int i = 0; i < NTRIES; i++) {
  66. write(pair_pipes[1], &byte, 1);
  67. read(pair_pipes[2], &byte, 1);
  68. }
  69. gettimeofday(&timevals[1], NULL);
  70. close(pair_pipes[1]);
  71. close(pair_pipes[2]);
  72. close(pipes[0]);
  73. write(pipes[5], timevals, sizeof(struct timeval) * 2);
  74. close(pipes[5]);
  75. read(pipes[2], &byte, 1);
  76. close(pipes[2]);
  77. exit(0);
  78. }
  79. }
  80. close(pipes[0]);
  81. close(pipes[2]);
  82. close(pipes[5]);
  83. sleep(1);
  84. char bytes[times * 2];
  85. write(pipes[1], bytes, times);
  86. close(pipes[1]);
  87. unsigned long long start_time = 0;
  88. unsigned long long end_time = 0;
  89. struct timeval timevals[2];
  90. for (int i = 0; i < times; i++) {
  91. read(pipes[4], timevals, sizeof(struct timeval) * 2);
  92. unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
  93. unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
  94. if (!start_time || s < start_time)
  95. start_time = s;
  96. if (!end_time || e > end_time)
  97. end_time = e;
  98. }
  99. close(pipes[4]);
  100. write(pipes[3], bytes, times * 2);
  101. close(pipes[3]);
  102. for (i = 0; i < times; i++) {
  103. waitpid(pids[i][0], NULL, 0);
  104. waitpid(pids[i][1], NULL, 0);
  105. }
  106. printf("throughput for %d processes to send %d message: %lf bytes/second\n", times, NTRIES,
  107. 1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
  108. return 0;
  109. }