1
0

rsleept2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. /*
  2. * This file is part of Jehanne.
  3. *
  4. * Copyright (C) 2015 Giacomo Tesio <giacomo@tesio.it>
  5. *
  6. * Jehanne is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, version 2 of the License.
  9. *
  10. * Jehanne is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #include <u.h>
  19. #include <lib9.h>
  20. /* verify that rsleept returns 0 on timeout */
  21. #define NPROC 50
  22. QLock al;
  23. Rendez neverAwakened; /* sleepers will timeout */
  24. int killerProc; /* pid, will kill the other processes if starved */
  25. int elapsed[NPROC];
  26. int32_t completed;
  27. QLock rl;
  28. Rendez rStart;
  29. Rendez rCompleted;
  30. int verbose = 0;
  31. void
  32. killKiller(void)
  33. {
  34. postnote(PNPROC, killerProc, "interrupt");
  35. }
  36. void
  37. stopAllAfter(int seconds)
  38. {
  39. int pid;
  40. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  41. {
  42. case 0:
  43. if(verbose)
  44. print("killer proc started: pid %d\n", getpid());
  45. sleep(seconds * 1000);
  46. postnote(PNGROUP, killerProc, "timedout");
  47. if(verbose)
  48. print("killer proc timedout: pid %d\n", getpid());
  49. exits("FAIL");
  50. case -1:
  51. fprint(2, "%r\n");
  52. exits("sys_rfork fails");
  53. default:
  54. killerProc = pid;
  55. atexit(killKiller);
  56. }
  57. }
  58. int
  59. handletimeout(void *v, char *s)
  60. {
  61. // extern void printdebugrendezvouslogs(void);
  62. /* just not exit, please */
  63. if(strcmp(s, "timedout") == 0){
  64. if(verbose)
  65. print("%d: noted: %s\n", getpid(), s);
  66. print("FAIL: %s timedout\n", argv0);
  67. // printdebugrendezvouslogs();
  68. exits("FAIL");
  69. }
  70. return 0;
  71. }
  72. int
  73. handlefail(void *v, char *s)
  74. {
  75. /* just not exit, please */
  76. if(strncmp(s, "fail", 4) == 0){
  77. if(verbose)
  78. print("%d: noted: %s\n", getpid(), s);
  79. print("FAIL: %s\n");
  80. exits("FAIL");
  81. }
  82. return 0;
  83. }
  84. char *
  85. sleeper(int index)
  86. {
  87. int64_t start, end;
  88. /* wait for the other sleepers to be ready to start */
  89. qlock(&rl);
  90. while(!neverAwakened.l)
  91. rsleep(&rStart);
  92. qunlock(&rl);
  93. /* try to sleep for ~1000 ms */
  94. start = nsec();
  95. end = start;
  96. if(verbose)
  97. print("sleeper %d: started\n", getpid());
  98. qlock(neverAwakened.l);
  99. if(rsleept(&neverAwakened, 1)){
  100. if(verbose)
  101. print("sleeper %d failed: awakened by rwakeup\n", getpid());
  102. postnote(PNGROUP, getpid(), smprint("fail: sleeper %d awakened by rwakeup", getpid()));
  103. } else {
  104. end = nsec();
  105. if(verbose)
  106. print("sleeper %d: rsleept timedout in %lld ms\n", getpid(), (end - start) / (1000*1000));
  107. }
  108. qunlock(neverAwakened.l);
  109. /* notify the main process that we have completed */
  110. qlock(&rl);
  111. elapsed[index] = end - start;
  112. ++completed;
  113. rwakeup(&rCompleted);
  114. qunlock(&rl);
  115. return end != start ? nil : "FAIL";
  116. }
  117. int lastspawn;
  118. void
  119. spawnsleeper(int index)
  120. {
  121. int pid, ls = lastspawn;
  122. char * res;
  123. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  124. {
  125. case 0:
  126. ++lastspawn;
  127. res = sleeper(index);
  128. exits(res);
  129. break;
  130. case -1:
  131. print("spawnsleeper: %r\n");
  132. exits("sys_rfork fails");
  133. break;
  134. default:
  135. while(ls == lastspawn)
  136. ;
  137. if(verbose)
  138. print("spawn sleeper %d\n", pid);
  139. break;
  140. }
  141. }
  142. void
  143. main(int argc, char* argv[])
  144. {
  145. int i;
  146. int64_t average;
  147. ARGBEGIN{
  148. }ARGEND;
  149. sys_rfork(RFNOTEG|RFREND);
  150. rStart.l = &rl;
  151. rCompleted.l = &rl;
  152. if(verbose)
  153. print("main: started with pid %d\n", getpid());
  154. for(i = 0; i < NPROC; ++i){
  155. spawnsleeper(i);
  156. }
  157. stopAllAfter(30);
  158. if (!atnotify(handletimeout, 1)){
  159. fprint(2, "%r\n");
  160. exits("atnotify fails");
  161. }
  162. if (!atnotify(handlefail, 1)){
  163. fprint(2, "%r\n");
  164. exits("atnotify fails");
  165. }
  166. qlock(&rl);
  167. neverAwakened.l = &al;
  168. if(verbose)
  169. print("main: wakeup all sleepers...\n");
  170. rwakeupall(&rStart);
  171. if(verbose)
  172. print("main: wakeup all sleepers... done\n");
  173. qunlock(&rl);
  174. qlock(&rl);
  175. if(verbose)
  176. print("main: waiting all sleepers to timeout...\n");
  177. while(completed < NPROC){
  178. rsleep(&rCompleted);
  179. if(verbose && completed < NPROC)
  180. print("main: awaked, but %d sleepers are still pending\n", NPROC - completed);
  181. }
  182. qunlock(&rl);
  183. if(verbose)
  184. print("main: waiting all sleepers to timeout... done\n");
  185. average = 0;
  186. for(i = 0; i < NPROC; ++i){
  187. average += elapsed[i];
  188. }
  189. average = average / NPROC / (1000 * 1000);
  190. if(average < 2000) /* we asked for 1ms... we are dumb, after all */
  191. {
  192. print("PASS\n");
  193. exits("PASS");
  194. }
  195. print("FAIL: %s: average timeout too long %lld ms\n", argv0, average);
  196. exits("FAIL");
  197. }