wlockt1.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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 wlockt returns 1 if the lock is released and
  21. * it can take it
  22. */
  23. #define NPROC 50
  24. RWLock afterAWhile; /* held for a while by main, then writers 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, "stopAllAfter: %r\n");
  53. exits("rfork fails");
  54. default:
  55. if(verbose)
  56. print("killer proc spawn: pid %d\n", pid);
  57. killerProc = pid;
  58. atexit(killKiller);
  59. }
  60. }
  61. int
  62. handletimeout(void *v, char *s)
  63. {
  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. if(strncmp(s, "fail", 4) == 0){
  76. if(verbose)
  77. print("%d: noted: %s\n", getpid(), s);
  78. print("FAIL: %s\n", s);
  79. exits("FAIL");
  80. }
  81. return 0;
  82. }
  83. char *
  84. waiter(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._readers == 0)
  90. rsleep(&rStart);
  91. qunlock(&rl);
  92. /* try to lock for ~1000 s, but it will be available after ~1s */
  93. start = nsec();
  94. end = start;
  95. if(verbose)
  96. print("writer %d: started\n", getpid());
  97. if(wlockt(&afterAWhile, 100000)){
  98. end = nsec();
  99. if(verbose)
  100. print("writer %d: got the wlock in %lld ms\n", getpid(), (end - start) / (1000*1000));
  101. wunlock(&afterAWhile);
  102. if((end - start) / (1000*1000) > 1300)
  103. postnote(PNGROUP, getpid(), smprint("fail: writer %d got the wlock after %lld ms", getpid(), (end - start) / (1000*1000)));
  104. } else {
  105. if(verbose)
  106. print("writer %d failed: wlockt timedout in %lld ms\n", getpid(), (nsec() - start) / (1000*1000));
  107. postnote(PNGROUP, getpid(), smprint("fail: writer %d timedout", getpid()));
  108. }
  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) / (1000*1000) < 1300 ? nil : "FAIL";
  116. }
  117. void
  118. spawnWaiter(int index)
  119. {
  120. int pid;
  121. char * res;
  122. switch((pid = rfork(RFMEM|RFPROC|RFNOWAIT)))
  123. {
  124. case 0:
  125. res = waiter(index);
  126. exits(res);
  127. break;
  128. case -1:
  129. print("spawnWaiter: %r\n");
  130. exits("rfork fails");
  131. break;
  132. default:
  133. if(verbose)
  134. print("spawn writer %d\n", pid);
  135. break;
  136. }
  137. }
  138. void
  139. main(void)
  140. {
  141. int i;
  142. int64_t average, end;
  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. spawnWaiter(i);
  150. }
  151. if (!atnotify(handletimeout, 1)){
  152. fprint(2, "atnotify(handletimeout): %r\n");
  153. exits("atnotify fails");
  154. }
  155. if (!atnotify(handlefail, 1)){
  156. fprint(2, "atnotify(handlefail): %r\n");
  157. exits("atnotify fails");
  158. }
  159. stopAllAfter(60); /* ensure we exit */
  160. qlock(&rl);
  161. rlock(&afterAWhile);
  162. if(verbose)
  163. print("main: got the afterAWhile: wakeup all writers...\n");
  164. rwakeupall(&rStart);
  165. if(verbose)
  166. print("main: got the afterAWhile: wakeup all writers... done\n");
  167. end = nsec() + 1000*1000*1000;
  168. qunlock(&rl);
  169. /* wait for ~1 sec */
  170. do { sleep(100); } while(nsec() < end);
  171. runlock(&afterAWhile);
  172. if(verbose)
  173. print("main: released the afterAWhile\n");
  174. qlock(&rl);
  175. if(verbose)
  176. print("main: waiting all writers to take the lock...\n");
  177. while(completed < NPROC){
  178. rsleep(&rCompleted);
  179. if(verbose && completed < NPROC)
  180. print("main: awaked, but %d writers are still pending\n", NPROC - completed);
  181. }
  182. qunlock(&rl);
  183. if(verbose)
  184. print("main: waiting all writers to take the lock... done\n");
  185. average = 0;
  186. for(i = 0; i < NPROC; ++i){
  187. average += elapsed[i];
  188. }
  189. average = average / NPROC / (1000 * 1000);
  190. if(average > 900 && average < 5000)
  191. {
  192. print("PASS\n");
  193. exits("PASS");
  194. }
  195. print("FAIL: average timeout too long %lld ms\n", average);
  196. exits("FAIL");
  197. }