rlockt2.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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 0 even on tight timeouts */
  21. #define NPROC 50
  22. RWLock alwaysLocked; /* held by main process, readers will timeout */
  23. int killerProc; /* pid, will kill the other processes if starved */
  24. int elapsed[NPROC];
  25. int32_t completed;
  26. QLock rl;
  27. Rendez rStart;
  28. Rendez rCompleted;
  29. int verbose = 0;
  30. void
  31. killKiller(void)
  32. {
  33. postnote(PNPROC, killerProc, "interrupt");
  34. }
  35. void
  36. stopAllAfter(int seconds)
  37. {
  38. int pid;
  39. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  40. {
  41. case 0:
  42. if(verbose)
  43. print("killer proc started: pid %d\n", getpid());
  44. sleep(seconds * 1000);
  45. postnote(PNGROUP, killerProc, "timedout");
  46. if(verbose)
  47. print("killer proc timedout: pid %d\n", getpid());
  48. exits("FAIL");
  49. case -1:
  50. fprint(2, "stopAllAfter: %r\n");
  51. exits("sys_rfork fails");
  52. default:
  53. if(verbose)
  54. print("killer proc spawn: pid %d\n", getpid());
  55. killerProc = pid;
  56. atexit(killKiller);
  57. }
  58. }
  59. int
  60. handletimeout(void *v, char *s)
  61. {
  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. if(strncmp(s, "fail", 4) == 0){
  74. if(verbose)
  75. print("%d: noted: %s\n", getpid(), s);
  76. print("FAIL: %s\n", s);
  77. exits("FAIL");
  78. }
  79. return 0;
  80. }
  81. char *
  82. waiter(int index)
  83. {
  84. int64_t start, end;
  85. /* wait for the alwaysLocked to be locked by the main process */
  86. qlock(&rl);
  87. while(alwaysLocked.writer == 0)
  88. rsleep(&rStart);
  89. qunlock(&rl);
  90. /* try to lock for ~1 ms */
  91. start = nsec();
  92. end = start;
  93. if(verbose)
  94. print("reader %d: started\n", getpid());
  95. if(rlockt(&alwaysLocked, 1)){
  96. if(verbose)
  97. print("reader %d failed: got the rlock\n", getpid());
  98. runlock(&alwaysLocked);
  99. postnote(PNGROUP, getpid(), smprint("fail: reader %d got the rlock", getpid()));
  100. } else {
  101. end = nsec();
  102. if(verbose)
  103. print("reader %d: rlockt timedout in %lld ms\n", getpid(), (end - start) / (1000*1000));
  104. }
  105. /* notify the main process that we have completed */
  106. qlock(&rl);
  107. elapsed[index] = end - start;
  108. ++completed;
  109. rwakeup(&rCompleted);
  110. qunlock(&rl);
  111. return end != start ? nil : "FAIL";
  112. }
  113. int lastspawn;
  114. void
  115. spawnWaiter(int index)
  116. {
  117. int pid, ls = lastspawn;
  118. char * res;
  119. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  120. {
  121. case 0:
  122. ++lastspawn;
  123. res = waiter(index);
  124. exits(res);
  125. break;
  126. case -1:
  127. print("spawnWaiter: %r\n");
  128. exits("sys_rfork fails");
  129. break;
  130. default:
  131. while(ls == lastspawn)
  132. ;
  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;
  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. stopAllAfter(30);
  154. if (!atnotify(handletimeout, 1)){
  155. fprint(2, "atnotify(handletimeout): %r\n");
  156. exits("atnotify fails");
  157. }
  158. if (!atnotify(handlefail, 1)){
  159. fprint(2, "atnotify(handlefail): %r\n");
  160. exits("atnotify fails");
  161. }
  162. if(verbose)
  163. print("main: ready to lock...\n");
  164. qlock(&rl);
  165. wlock(&alwaysLocked);
  166. if(verbose)
  167. print("main: got the alwaysLocked: wakeup all readers...\n");
  168. rwakeupall(&rStart);
  169. if(verbose)
  170. print("main: got the alwaysLocked: wakeup all readers... done\n");
  171. qunlock(&rl);
  172. qlock(&rl);
  173. if(verbose)
  174. print("main: waiting all readers to timeout...\n");
  175. while(completed < NPROC){
  176. rsleep(&rCompleted);
  177. if(verbose && completed < NPROC)
  178. print("main: awaked, but %d readers are still pending\n", NPROC - completed);
  179. }
  180. wunlock(&alwaysLocked);
  181. qunlock(&rl);
  182. if(verbose)
  183. print("main: waiting all readers 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 < 2000) /* we asked for 1ms... we are dumb, after all */
  190. {
  191. print("PASS\n");
  192. exits("PASS");
  193. }
  194. print("FAIL: %s: average timeout too long %lld ms\n", argv0, average);
  195. exits("FAIL");
  196. }