rpc_latency2.libos.c 3.2 KB

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