rsleept1.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 lockt returns 1 if the lock is released and
  21. * it can take it
  22. */
  23. #define NPROC 50
  24. QLock l;
  25. Rendez afterAWhile; /* wakened after a while by main, then sleepers will be rwakeup */
  26. int killerProc; /* pid, will kill the other processes if starved */
  27. int elapsed[NPROC];
  28. int32_t completed;
  29. QLock rl;
  30. Rendez rStart;
  31. Rendez rCompleted;
  32. int verbose = 0;
  33. void
  34. killKiller(void)
  35. {
  36. postnote(PNPROC, killerProc, "interrupt");
  37. }
  38. void
  39. stopAllAfter(int seconds)
  40. {
  41. int pid;
  42. switch((pid = rfork(RFMEM|RFPROC|RFNOWAIT)))
  43. {
  44. case 0:
  45. if(verbose)
  46. print("killer proc started: pid %d\n", getpid());
  47. sleep(seconds * 1000);
  48. postnote(PNGROUP, killerProc, "timedout");
  49. if(verbose)
  50. print("killer proc timedout: pid %d\n", getpid());
  51. exits("FAIL");
  52. case -1:
  53. fprint(2, "%r\n");
  54. exits("rfork fails");
  55. default:
  56. killerProc = pid;
  57. atexit(killKiller);
  58. }
  59. }
  60. int
  61. handletimeout(void *v, char *s)
  62. {
  63. /* just not exit, please */
  64. if(strcmp(s, "timedout") == 0){
  65. if(verbose)
  66. print("%d: noted: %s\n", getpid(), s);
  67. print("FAIL: timedout\n");
  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. exits("FAIL");
  80. }
  81. return 0;
  82. }
  83. char *
  84. sleeper(int index)
  85. {
  86. int64_t start, end;
  87. /* wait for the afterAWhile to be locked by the main process */
  88. qlock(&rl);
  89. while(!afterAWhile.l)
  90. rsleep(&rStart);
  91. qunlock(&rl);
  92. /* try to sleep for ~10 s, but it will be available after ~1s */
  93. start = nsec();
  94. end = start;
  95. if(verbose)
  96. print("sleeper %d: started\n", getpid());
  97. qlock(afterAWhile.l);
  98. if(rsleept(&afterAWhile, 10000)){
  99. end = nsec();
  100. if(verbose)
  101. print("sleeper %d: awakened in %lld ms\n", getpid(), (end - start) / (1000*1000));
  102. if((end - start) / (1000*1000) > 2500)
  103. postnote(PNGROUP, getpid(), smprint("fail: sleeper %d awakened after %lld ms", getpid(), (end - start) / (1000*1000)));
  104. } else {
  105. end = nsec();
  106. if(verbose)
  107. print("sleeper %d failed: rsleept timedout in %lld ms\n", getpid(), (end - start) / (1000*1000));
  108. postnote(PNGROUP, getpid(), smprint("fail: sleeper %d timedout", getpid()));
  109. }
  110. qunlock(afterAWhile.l);
  111. /* notify the main process that we have completed */
  112. qlock(&rl);
  113. elapsed[index] = end - start;
  114. ++completed;
  115. rwakeup(&rCompleted);
  116. qunlock(&rl);
  117. return (end - start) / (1000*1000) < 1300 ? nil : "FAIL";
  118. }
  119. void
  120. spawnWaiter(int index)
  121. {
  122. int pid;
  123. char * res;
  124. switch((pid = rfork(RFMEM|RFPROC|RFNOWAIT)))
  125. {
  126. case 0:
  127. res = sleeper(index);
  128. exits(res);
  129. break;
  130. case -1:
  131. print("spawnWaiter: %r\n");
  132. exits("rfork fails");
  133. break;
  134. default:
  135. if(verbose)
  136. print("spawn sleeper %d\n", pid);
  137. break;
  138. }
  139. }
  140. void
  141. main(void)
  142. {
  143. int i, awakened;
  144. int64_t average, end;
  145. rfork(RFNOTEG|RFREND);
  146. rStart.l = &rl;
  147. rCompleted.l = &rl;
  148. if(verbose)
  149. print("main: started with pid %d\n", getpid());
  150. for(i = 0; i < NPROC; ++i){
  151. spawnWaiter(i);
  152. }
  153. stopAllAfter(30);
  154. if (!atnotify(handletimeout, 1)){
  155. fprint(2, "%r\n");
  156. exits("atnotify fails");
  157. }
  158. if (!atnotify(handlefail, 1)){
  159. fprint(2, "%r\n");
  160. exits("atnotify fails");
  161. }
  162. qlock(&rl);
  163. afterAWhile.l = &l;
  164. if(verbose)
  165. print("main: wakeup all sleepers...\n");
  166. rwakeupall(&rStart);
  167. if(verbose)
  168. print("main: wakeup all sleepers... done\n");
  169. end = nsec() + 1000*1000*1000;
  170. qunlock(&rl);
  171. /* wait for ~1 sec */
  172. do { sleep(100); } while(nsec() < end);
  173. qlock(&l);
  174. awakened = rwakeupall(&afterAWhile);
  175. qunlock(&l);
  176. if(verbose)
  177. print("main: released the afterAWhile\n");
  178. qlock(&rl);
  179. if(verbose)
  180. print("main: waiting all sleepers to wakeup...\n");
  181. while(completed < NPROC){
  182. rsleep(&rCompleted);
  183. if(verbose && completed < NPROC)
  184. print("main: awaked, but %d sleepers are still pending\n", NPROC - completed);
  185. }
  186. qunlock(&rl);
  187. if(verbose)
  188. print("main: waiting all sleepers to wakeup... done\n");
  189. average = 0;
  190. for(i = 0; i < NPROC; ++i){
  191. average += elapsed[i];
  192. }
  193. average = average / NPROC / (1000 * 1000);
  194. if(awakened == NPROC && average > 900 && average < 5000)
  195. {
  196. print("PASS\n");
  197. exits("PASS");
  198. }
  199. print("FAIL: average timeout too long %lld ms\n", average);
  200. exits("FAIL");
  201. }