rpc_latency.libos.c 2.9 KB

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