qlockt0.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  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 qlockt returns 0 on timeout */
  21. #define NPROC 50
  22. QLock alwaysLocked; /* held by main process, waiters will timeout */
  23. int killerProc; /* pid, will kill the other processes if starved */
  24. int64_t 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, "%r\n");
  51. exits("sys_rfork fails");
  52. default:
  53. killerProc = pid;
  54. atexit(killKiller);
  55. }
  56. }
  57. int
  58. handletimeout(void *v, char *s)
  59. {
  60. /* just not exit, please */
  61. if(strcmp(s, "timedout") == 0){
  62. if(verbose)
  63. print("%d: noted: %s\n", getpid(), s);
  64. exits("FAIL");
  65. }
  66. return 0;
  67. }
  68. int
  69. handlefail(void *v, char *s)
  70. {
  71. /* just not exit, please */
  72. if(strncmp(s, "fail", 4) == 0){
  73. if(verbose)
  74. print("%d: noted: %s\n", getpid(), s);
  75. print("FAIL");
  76. exits("FAIL");
  77. }
  78. return 0;
  79. }
  80. char *
  81. waiter(int index)
  82. {
  83. int64_t start, end;
  84. /* wait for the alwaysLocked to be locked by the main process */
  85. qlock(&rl);
  86. while(alwaysLocked.locked == 0)
  87. rsleep(&rStart);
  88. qunlock(&rl);
  89. /* try to lock for ~1000 ms */
  90. start = nsec();
  91. end = start;
  92. if(verbose)
  93. print("waiter %d: started\n", getpid());
  94. if(qlockt(&alwaysLocked, 1000)){
  95. if(verbose)
  96. print("waiter %d failed: got the qlock\n", getpid());
  97. qunlock(&alwaysLocked);
  98. postnote(PNGROUP, getpid(), smprint("fail: waiter %d got the qlock", getpid()));
  99. } else {
  100. end = nsec();
  101. if(verbose)
  102. print("waiter %d: qlockt timedout in %lld ms\n", getpid(), (end - start) / (1000*1000));
  103. }
  104. /* notify the main process that we have completed */
  105. qlock(&rl);
  106. elapsed[index] = end - start;
  107. ++completed;
  108. rwakeup(&rCompleted);
  109. qunlock(&rl);
  110. return end != start ? nil : "FAIL";
  111. }
  112. void
  113. spawnWaiter(int index)
  114. {
  115. int pid;
  116. char * res;
  117. switch((pid = sys_rfork(RFMEM|RFPROC|RFNOWAIT)))
  118. {
  119. case 0:
  120. res = waiter(index);
  121. exits(res);
  122. break;
  123. case -1:
  124. print("spawnWaiter: %r\n");
  125. exits("sys_rfork fails");
  126. break;
  127. default:
  128. if(verbose)
  129. print("spawn waiter %d\n", pid);
  130. break;
  131. }
  132. }
  133. void
  134. main(int argc, char* argv[])
  135. {
  136. int i;
  137. int64_t average;
  138. ARGBEGIN{
  139. }ARGEND;
  140. sys_rfork(RFNOTEG|RFREND);
  141. rStart.l = &rl;
  142. rCompleted.l = &rl;
  143. if(verbose)
  144. print("main: started with pid %d\n", getpid());
  145. for(i = 0; i < NPROC; ++i){
  146. elapsed[i] = 0;
  147. spawnWaiter(i);
  148. }
  149. stopAllAfter(30);
  150. if (!atnotify(handletimeout, 1)){
  151. fprint(2, "%r\n");
  152. exits("atnotify fails");
  153. }
  154. if (!atnotify(handlefail, 1)){
  155. fprint(2, "%r\n");
  156. exits("atnotify fails");
  157. }
  158. qlock(&rl);
  159. qlock(&alwaysLocked);
  160. if(verbose)
  161. print("main: got the alwaysLocked: wakeup all waiters...\n");
  162. rwakeupall(&rStart);
  163. if(verbose)
  164. print("main: got the alwaysLocked: wakeup all waiters... done\n");
  165. qunlock(&rl);
  166. qlock(&rl);
  167. if(verbose)
  168. print("main: waiting all waiters to timeout...\n");
  169. while(completed < NPROC){
  170. rsleep(&rCompleted);
  171. if(verbose && completed < NPROC)
  172. print("main: awaked, but %d waiters are still pending\n", NPROC - completed);
  173. }
  174. qunlock(&alwaysLocked);
  175. qunlock(&rl);
  176. if(verbose)
  177. print("main: waiting all waiters to timeout... done\n");
  178. average = 0;
  179. for(i = 0; i < NPROC; ++i){
  180. if(verbose)
  181. print("elapsed[%d] = %lld, average = %lld nanosec \n", i, elapsed[i], average);
  182. average += elapsed[i];
  183. }
  184. average = average / NPROC / (1000 * 1000);
  185. if(average > 900 && average < 3000)
  186. {
  187. print("PASS\n");
  188. exits("PASS");
  189. }
  190. print("FAIL: %s: average timeout too long %lld ms\n", argv0, average);
  191. exits("FAIL");
  192. }