1
0

rsleept2.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 = 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("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: timedout\n");
  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. void
  118. spawnsleeper(int index)
  119. {
  120. int pid;
  121. char * res;
  122. switch((pid = rfork(RFMEM|RFPROC|RFNOWAIT)))
  123. {
  124. case 0:
  125. res = sleeper(index);
  126. exits(res);
  127. break;
  128. case -1:
  129. print("spawnsleeper: %r\n");
  130. exits("rfork fails");
  131. break;
  132. default:
  133. if(verbose)
  134. print("spawn sleeper %d\n", pid);
  135. break;
  136. }
  137. }
  138. void
  139. main(void)
  140. {
  141. int i;
  142. int64_t average;
  143. rfork(RFNOTEG|RFREND);
  144. rStart.l = &rl;
  145. rCompleted.l = &rl;
  146. if(verbose)
  147. print("main: started with pid %d\n", getpid());
  148. for(i = 0; i < NPROC; ++i){
  149. spawnsleeper(i);
  150. }
  151. stopAllAfter(30);
  152. if (!atnotify(handletimeout, 1)){
  153. fprint(2, "%r\n");
  154. exits("atnotify fails");
  155. }
  156. if (!atnotify(handlefail, 1)){
  157. fprint(2, "%r\n");
  158. exits("atnotify fails");
  159. }
  160. qlock(&rl);
  161. neverAwakened.l = &al;
  162. if(verbose)
  163. print("main: wakeup all sleepers...\n");
  164. rwakeupall(&rStart);
  165. if(verbose)
  166. print("main: wakeup all sleepers... done\n");
  167. qunlock(&rl);
  168. qlock(&rl);
  169. if(verbose)
  170. print("main: waiting all sleepers to timeout...\n");
  171. while(completed < NPROC){
  172. rsleep(&rCompleted);
  173. if(verbose && completed < NPROC)
  174. print("main: awaked, but %d sleepers are still pending\n", NPROC - completed);
  175. }
  176. qunlock(&rl);
  177. if(verbose)
  178. print("main: waiting all sleepers to timeout... done\n");
  179. average = 0;
  180. for(i = 0; i < NPROC; ++i){
  181. average += elapsed[i];
  182. }
  183. average = average / NPROC / (1000 * 1000);
  184. if(average < 500) /* we asked for 1ms... we are dumb, after all */
  185. {
  186. print("PASS\n");
  187. exits("PASS");
  188. }
  189. print("FAIL: average timeout too long %lld ms\n", average);
  190. exits("FAIL");
  191. }