sig_latency.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. #include <sched.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 DO_BENCH
  9. #define NTRIES 1000
  10. #define TEST_TIMES 32
  11. int count = 0;
  12. int pids[TEST_TIMES][2];
  13. int firstpid;
  14. int secondpid;
  15. void sighand1(int signum, siginfo_t* sinfo, void* ucontext) {
  16. count++;
  17. #ifndef DO_BENCH
  18. if (count % 100 == 0)
  19. printf("Received a SIGUSR1 (%d) (count = %d) from %d\n", signum, count, sinfo->si_pid);
  20. #endif
  21. if (count > NTRIES)
  22. return;
  23. kill(secondpid, SIGUSR1);
  24. }
  25. void sighand2(int signum, siginfo_t* sinfo, void* ucontext) {
  26. count++;
  27. #ifndef DO_BENCH
  28. if (count % 100 == 0)
  29. printf("Received a SIGUSR1 (%d) (count = %d) from %d\n", signum, count, sinfo->si_pid);
  30. #endif
  31. if (count > NTRIES)
  32. return;
  33. kill(firstpid, SIGUSR1);
  34. }
  35. void (*sighand)(int signum, siginfo_t* sinfo, void* ucontext) = NULL;
  36. void sigact(int signum, siginfo_t* sinfo, void* ucontext) {
  37. if (sighand)
  38. sighand(signum, sinfo, ucontext);
  39. }
  40. int main(int argc, char** argv) {
  41. int times = TEST_TIMES;
  42. int pipes[8];
  43. int i = 0;
  44. if (argc >= 2) {
  45. times = atoi(argv[1]) / 2;
  46. if (times > TEST_TIMES)
  47. return -1;
  48. }
  49. setvbuf(stdout, NULL, _IONBF, 0);
  50. signal(SIGUSR1, (void*)sigact);
  51. pipe(&pipes[0]);
  52. pipe(&pipes[2]);
  53. pipe(&pipes[4]);
  54. pipe(&pipes[6]);
  55. for (i = 0; i < times; i++) {
  56. pids[i][0] = fork();
  57. if (pids[i][0] < 0) {
  58. printf("fork failed\n");
  59. return -1;
  60. }
  61. if (pids[i][0] == 0) {
  62. sighand = sighand1;
  63. close(pipes[0]);
  64. close(pipes[1]);
  65. close(pipes[3]);
  66. close(pipes[4]);
  67. close(pipes[7]);
  68. count = 0;
  69. read(pipes[6], &pids[i][1], sizeof(int));
  70. secondpid = pids[i][1];
  71. close(pipes[6]);
  72. char byte;
  73. write(pipes[5], &byte, 1);
  74. close(pipes[5]);
  75. while (count < NTRIES) {
  76. sched_yield();
  77. }
  78. read(pipes[2], &byte, 1);
  79. close(pipes[2]);
  80. exit(0);
  81. }
  82. pids[i][1] = fork();
  83. if (pids[i][1] < 0) {
  84. printf("fork failed\n");
  85. return -1;
  86. }
  87. if (pids[i][1] == 0) {
  88. sighand = sighand2;
  89. close(pipes[1]);
  90. close(pipes[3]);
  91. close(pipes[4]);
  92. close(pipes[6]);
  93. firstpid = pids[i][0];
  94. int pid = getpid();
  95. write(pipes[7], &pid, sizeof(int));
  96. close(pipes[7]);
  97. char byte;
  98. write(pipes[5], &byte, 1);
  99. read(pipes[0], &byte, 1);
  100. struct timeval timevals[2];
  101. gettimeofday(&timevals[0], NULL);
  102. count = 0;
  103. kill(firstpid, SIGUSR1);
  104. while (count < NTRIES - 1) {
  105. sched_yield();
  106. }
  107. gettimeofday(&timevals[1], NULL);
  108. close(pipes[0]);
  109. write(pipes[5], timevals, sizeof(struct timeval) * 2);
  110. close(pipes[5]);
  111. read(pipes[2], &byte, 1);
  112. close(pipes[2]);
  113. exit(0);
  114. }
  115. }
  116. close(pipes[0]);
  117. close(pipes[2]);
  118. close(pipes[5]);
  119. close(pipes[6]);
  120. close(pipes[7]);
  121. for (int i = 0; i < times * 2; i++) {
  122. char i;
  123. while (read(pipes[4], &i, 1) < 0)
  124. ;
  125. }
  126. printf("all processes ready\n");
  127. sleep(1);
  128. char bytes[times * 2];
  129. write(pipes[1], bytes, times);
  130. close(pipes[1]);
  131. unsigned long long start_time = 0;
  132. unsigned long long end_time = 0;
  133. struct timeval timevals[2];
  134. for (int i = 0; i < times; i++) {
  135. while (read(pipes[4], timevals, sizeof(struct timeval) * 2) < 0)
  136. ;
  137. unsigned long s = timevals[0].tv_sec * 1000000ULL + timevals[0].tv_usec;
  138. unsigned long e = timevals[1].tv_sec * 1000000ULL + timevals[1].tv_usec;
  139. if (!start_time || s < start_time)
  140. start_time = s;
  141. if (!end_time || e > end_time)
  142. end_time = e;
  143. }
  144. close(pipes[4]);
  145. write(pipes[3], bytes, times * 2);
  146. close(pipes[3]);
  147. for (i = 0; i < times; i++) {
  148. waitpid(pids[i][0], NULL, 0);
  149. waitpid(pids[i][1], NULL, 0);
  150. }
  151. printf("throughput for %d processes to send %d signals: %lf signals/second\n", times, NTRIES,
  152. 1.0 * NTRIES * 2 * times * 1000000 / (end_time - start_time));
  153. return 0;
  154. }