1
0

rlockt1.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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 rlockt 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 readers 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 = sys_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("sys_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: %s timedout\n", argv0);
  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.writer == 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("reader %d: started\n", getpid());
  97. if(rlockt(&afterAWhile, 100000)){
  98. end = nsec();
  99. if(verbose)
  100. print("reader %d: got the rlock in %lld ms\n", getpid(), (end - start) / (1000*1000));
  101. runlock(&afterAWhile);
  102. if((end - start) / (1000*1000) > 3000)
  103. postnote(PNGROUP, getpid(), smprint("fail: reader %d got the rlock after %lld ms", getpid(), (end - start) / (1000*1000)));
  104. } else {
  105. if(verbose)
  106. print("reader %d failed: rlockt timedout in %lld ms\n", getpid(), (nsec() - start) / (1000*1000));
  107. postnote(PNGROUP, getpid(), smprint("fail: reader %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) < 3000 ? nil : "FAIL";
  116. }
  117. void
  118. spawnWaiter(int index)
  119. {
  120. int pid;
  121. char * res;
  122. switch((pid = sys_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("sys_rfork fails");
  131. break;
  132. default:
  133. if(verbose)
  134. print("spawn reader %d\n", pid);
  135. break;
  136. }
  137. }
  138. void
  139. main(int argc, char* argv[])
  140. {
  141. int i;
  142. int64_t average, end;
  143. ARGBEGIN{
  144. }ARGEND;
  145. sys_rfork(RFNOTEG|RFREND);
  146. rStart.l = &rl;
  147. rCompleted.l = &rl;
  148. if(verbose)
  149. print("main: started with pid %d\n", getpid());
  150. for(i = 0; i < NPROC; ++i){
  151. spawnWaiter(i);
  152. }
  153. if (!atnotify(handletimeout, 1)){
  154. fprint(2, "atnotify(handletimeout): %r\n");
  155. exits("atnotify fails");
  156. }
  157. if (!atnotify(handlefail, 1)){
  158. fprint(2, "atnotify(handlefail): %r\n");
  159. exits("atnotify fails");
  160. }
  161. stopAllAfter(60); /* ensure we exit */
  162. qlock(&rl);
  163. wlock(&afterAWhile);
  164. if(verbose)
  165. print("main: got the afterAWhile: wakeup all readers...\n");
  166. rwakeupall(&rStart);
  167. if(verbose)
  168. print("main: got the afterAWhile: wakeup all readers... done\n");
  169. end = nsec() + 1000*1000*1000;
  170. qunlock(&rl);
  171. /* wait for ~1 sec */
  172. do { sleep(100); } while(nsec() < end);
  173. wunlock(&afterAWhile);
  174. if(verbose)
  175. print("main: released the afterAWhile\n");
  176. qlock(&rl);
  177. if(verbose)
  178. print("main: waiting all readers to take the lock...\n");
  179. while(completed < NPROC){
  180. rsleep(&rCompleted);
  181. if(verbose && completed < NPROC)
  182. print("main: awaked, but %d readers are still pending\n", NPROC - completed);
  183. }
  184. qunlock(&rl);
  185. if(verbose)
  186. print("main: waiting all readers to take the lock... done\n");
  187. average = 0;
  188. for(i = 0; i < NPROC; ++i){
  189. average += elapsed[i];
  190. }
  191. average = average / NPROC / (1000 * 1000);
  192. if(average > 900 && average < 5000)
  193. {
  194. print("PASS\n");
  195. exits("PASS");
  196. }
  197. print("FAIL: %s: average timeout too long %lld ms\n", argv0, average);
  198. exits("FAIL");
  199. }