rpc_latency.libos.c 3.0 KB

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