sig_latency.c 4.7 KB

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