fork_latency.c 3.1 KB

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