rsleept1.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233
  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 = sys_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("sys_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: %s timedout\n", argv0);
  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 = sys_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("sys_rfork fails");
  133. break;
  134. default:
  135. if(verbose)
  136. print("spawn sleeper %d\n", pid);
  137. break;
  138. }
  139. }
  140. void
  141. main(int argc, char* argv[])
  142. {
  143. int i, awakened;
  144. int64_t average, end;
  145. ARGBEGIN{
  146. }ARGEND;
  147. sys_rfork(RFNOTEG|RFREND);
  148. rStart.l = &rl;
  149. rCompleted.l = &rl;
  150. if(verbose)
  151. print("main: started with pid %d\n", getpid());
  152. for(i = 0; i < NPROC; ++i){
  153. spawnWaiter(i);
  154. }
  155. stopAllAfter(30);
  156. if (!atnotify(handletimeout, 1)){
  157. fprint(2, "%r\n");
  158. exits("atnotify fails");
  159. }
  160. if (!atnotify(handlefail, 1)){
  161. fprint(2, "%r\n");
  162. exits("atnotify fails");
  163. }
  164. qlock(&rl);
  165. afterAWhile.l = &l;
  166. if(verbose)
  167. print("main: wakeup all sleepers...\n");
  168. rwakeupall(&rStart);
  169. if(verbose)
  170. print("main: wakeup all sleepers... done\n");
  171. end = nsec() + 1000*1000*1000;
  172. qunlock(&rl);
  173. /* wait for ~1 sec */
  174. do { sleep(1000); } while(nsec() < end);
  175. qlock(&l);
  176. awakened = rwakeupall(&afterAWhile);
  177. qunlock(&l);
  178. if(verbose)
  179. print("main: released the afterAWhile\n");
  180. qlock(&rl);
  181. if(verbose)
  182. print("main: waiting all sleepers to wakeup...\n");
  183. while(completed < NPROC){
  184. rsleep(&rCompleted);
  185. if(verbose && completed < NPROC)
  186. print("main: awaked, but %d sleepers are still pending\n", NPROC - completed);
  187. }
  188. qunlock(&rl);
  189. if(verbose)
  190. print("main: waiting all sleepers to wakeup... done\n");
  191. average = 0;
  192. for(i = 0; i < NPROC; ++i){
  193. average += elapsed[i];
  194. }
  195. average = average / NPROC / (1000 * 1000);
  196. if(awakened == NPROC && average > 900 && average < 5000)
  197. {
  198. print("PASS\n");
  199. exits("PASS");
  200. }
  201. print("FAIL: %s: average timeout too long %lld ms\n", argv0, average);
  202. exits("FAIL");
  203. }