1
0

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