sig_latency.c 4.5 KB

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