sig_latency.c 5.5 KB

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