rpc_latency.libos.c 2.7 KB

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