rpc_latency2.c 3.7 KB

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