1
0

rsleept0.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  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. /* just not exit, please */
  62. if(strcmp(s, "timedout") == 0){
  63. if(verbose)
  64. print("%d: noted: %s\n", getpid(), s);
  65. print("FAIL: %s timedout\n", argv0);
  66. exits("FAIL");
  67. }
  68. return 0;
  69. }
  70. int
  71. handlefail(void *v, char *s)
  72. {
  73. /* just not exit, please */
  74. if(strncmp(s, "fail", 4) == 0){
  75. if(verbose)
  76. print("%d: noted: %s\n", getpid(), s);
  77. print("FAIL: %s\n");
  78. exits("FAIL");
  79. }
  80. return 0;
  81. }
  82. char *
  83. sleeper(int index)
  84. {
  85. int64_t start, end;
  86. /* wait for the other sleepers to be ready to start */
  87. qlock(&rl);
  88. while(!neverAwakened.l)
  89. rsleep(&rStart);
  90. qunlock(&rl);
  91. /* try to sleep for ~1000 ms */
  92. start = nsec();
  93. end = start;
  94. if(verbose)
  95. print("sleeper %d: started\n", getpid());
  96. qlock(neverAwakened.l);
  97. if(rsleept(&neverAwakened, 1000)){
  98. if(verbose)
  99. print("sleeper %d failed: awakened by rwakeup\n", getpid());
  100. postnote(PNGROUP, getpid(), smprint("fail: sleeper %d awakened by rwakeup", getpid()));
  101. } else {
  102. end = nsec();
  103. if(verbose)
  104. print("sleeper %d: rsleept timedout in %lld ms\n", getpid(), (end - start) / (1000*1000));
  105. }
  106. qunlock(neverAwakened.l);
  107. /* notify the main process that we have completed */
  108. qlock(&rl);
  109. elapsed[index] = end - start;
  110. ++completed;
  111. rwakeup(&rCompleted);
  112. qunlock(&rl);
  113. return end != start ? nil : "FAIL";
  114. }
  115. void
  116. spawnsleeper(int index)
  117. {
  118. int pid;
  119. char * res;
  120. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  121. {
  122. case 0:
  123. res = sleeper(index);
  124. exits(res);
  125. break;
  126. case -1:
  127. print("spawnsleeper: %r\n");
  128. exits("sys_rfork fails");
  129. break;
  130. default:
  131. if(verbose)
  132. print("spawn sleeper %d\n", pid);
  133. break;
  134. }
  135. }
  136. void
  137. main(int argc, char* argv[])
  138. {
  139. int i;
  140. int64_t average;
  141. ARGBEGIN{
  142. }ARGEND;
  143. sys_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 > 900 && average < 3000)
  185. {
  186. print("PASS\n");
  187. exits("PASS");
  188. }
  189. print("FAIL: %s: average timeout too long %lld ms\n", argv0, average);
  190. exits("FAIL");
  191. }