synchtest.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /*
  2. * Copyright (c) 2000, 2001, 2002, 2003, 2004, 2005, 2008, 2009
  3. * The President and Fellows of Harvard College.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the University nor the names of its contributors
  14. * may be used to endorse or promote products derived from this software
  15. * without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE UNIVERSITY AND CONTRIBUTORS ``AS IS'' AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  19. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  20. * ARE DISCLAIMED. IN NO EVENT SHALL THE UNIVERSITY OR CONTRIBUTORS BE LIABLE
  21. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  22. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  23. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  24. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  25. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  26. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  27. * SUCH DAMAGE.
  28. */
  29. /*
  30. * Synchronization test code.
  31. */
  32. #include <types.h>
  33. #include <lib.h>
  34. #include <clock.h>
  35. #include <thread.h>
  36. #include <synch.h>
  37. #include <test.h>
  38. #define NSEMLOOPS 63
  39. #define NLOCKLOOPS 120
  40. #define NCVLOOPS 5
  41. #define NTHREADS 32
  42. static volatile unsigned long testval1;
  43. static volatile unsigned long testval2;
  44. static volatile unsigned long testval3;
  45. #ifdef UW
  46. static struct semaphore *testsem = 0;
  47. static struct lock *testlock = 0;
  48. static struct cv *testcv = 0;
  49. static struct semaphore *donesem = 0;
  50. #else
  51. static struct semaphore *testsem;
  52. static struct lock *testlock;
  53. static struct cv *testcv;
  54. static struct semaphore *donesem;
  55. #endif
  56. #ifdef UW
  57. static
  58. void
  59. cleanitems(void)
  60. {
  61. kprintf("cleanitems: Destroying sems, locks, and cvs\n");
  62. sem_destroy(testsem);
  63. lock_destroy(testlock);
  64. cv_destroy(testcv);
  65. sem_destroy(donesem);
  66. }
  67. #endif
  68. static
  69. void
  70. inititems(void)
  71. {
  72. if (testsem==NULL) {
  73. testsem = sem_create("testsem", 2);
  74. if (testsem == NULL) {
  75. panic("synchtest: sem_create failed\n");
  76. }
  77. }
  78. if (testlock==NULL) {
  79. testlock = lock_create("testlock");
  80. if (testlock == NULL) {
  81. panic("synchtest: lock_create failed\n");
  82. }
  83. }
  84. if (testcv==NULL) {
  85. #ifdef UW
  86. testcv = cv_create("testcv");
  87. #else
  88. testcv = cv_create("testlock");
  89. #endif
  90. if (testcv == NULL) {
  91. panic("synchtest: cv_create failed\n");
  92. }
  93. }
  94. if (donesem==NULL) {
  95. donesem = sem_create("donesem", 0);
  96. if (donesem == NULL) {
  97. panic("synchtest: sem_create failed\n");
  98. }
  99. }
  100. }
  101. static
  102. void
  103. semtestthread(void *junk, unsigned long num)
  104. {
  105. int i;
  106. (void)junk;
  107. /*
  108. * Only one of these should print at a time.
  109. */
  110. P(testsem);
  111. kprintf("Thread %2lu: ", num);
  112. for (i=0; i<NSEMLOOPS; i++) {
  113. kprintf("%c", (int)num+64);
  114. }
  115. kprintf("\n");
  116. V(donesem);
  117. #ifdef UW
  118. thread_exit();
  119. #endif
  120. }
  121. int
  122. semtest(int nargs, char **args)
  123. {
  124. int i, result;
  125. (void)nargs;
  126. (void)args;
  127. inititems();
  128. kprintf("Starting semaphore test...\n");
  129. kprintf("If this hangs, it's broken: ");
  130. P(testsem);
  131. P(testsem);
  132. kprintf("ok\n");
  133. for (i=0; i<NTHREADS; i++) {
  134. result = thread_fork("semtest", NULL, semtestthread, NULL, i);
  135. if (result) {
  136. panic("semtest: thread_fork failed: %s\n",
  137. strerror(result));
  138. }
  139. }
  140. for (i=0; i<NTHREADS; i++) {
  141. V(testsem);
  142. P(donesem);
  143. }
  144. /* so we can run it again */
  145. V(testsem);
  146. V(testsem);
  147. #ifdef UW
  148. cleanitems();
  149. #endif
  150. kprintf("Semaphore test done.\n");
  151. return 0;
  152. }
  153. static
  154. void
  155. fail(unsigned long num, const char *msg)
  156. {
  157. kprintf("thread %lu: Mismatch on %s\n", num, msg);
  158. kprintf("Test failed\n");
  159. lock_release(testlock);
  160. V(donesem);
  161. thread_exit();
  162. }
  163. static
  164. void
  165. locktestthread(void *junk, unsigned long num)
  166. {
  167. int i;
  168. (void)junk;
  169. for (i=0; i<NLOCKLOOPS; i++) {
  170. lock_acquire(testlock);
  171. testval1 = num;
  172. testval2 = num*num;
  173. testval3 = num%3;
  174. if (testval2 != testval1*testval1) {
  175. fail(num, "testval2/testval1");
  176. }
  177. if (testval2%3 != (testval3*testval3)%3) {
  178. fail(num, "testval2/testval3");
  179. }
  180. if (testval3 != testval1%3) {
  181. fail(num, "testval3/testval1");
  182. }
  183. if (testval1 != num) {
  184. fail(num, "testval1/num");
  185. }
  186. if (testval2 != num*num) {
  187. fail(num, "testval2/num");
  188. }
  189. if (testval3 != num%3) {
  190. fail(num, "testval3/num");
  191. }
  192. lock_release(testlock);
  193. }
  194. V(donesem);
  195. #ifdef UW
  196. thread_exit();
  197. #endif
  198. }
  199. int
  200. locktest(int nargs, char **args)
  201. {
  202. int i, result;
  203. (void)nargs;
  204. (void)args;
  205. inititems();
  206. kprintf("Starting lock test...\n");
  207. for (i=0; i<NTHREADS; i++) {
  208. result = thread_fork("synchtest", NULL, locktestthread,
  209. NULL, i);
  210. if (result) {
  211. panic("locktest: thread_fork failed: %s\n",
  212. strerror(result));
  213. }
  214. }
  215. for (i=0; i<NTHREADS; i++) {
  216. P(donesem);
  217. }
  218. #ifdef UW
  219. cleanitems();
  220. #endif
  221. kprintf("Lock test done.\n");
  222. return 0;
  223. }
  224. static
  225. void
  226. cvtestthread(void *junk, unsigned long num)
  227. {
  228. int i;
  229. volatile int j;
  230. time_t secs1, secs2;
  231. uint32_t nsecs1, nsecs2;
  232. (void)junk;
  233. for (i=0; i<NCVLOOPS; i++) {
  234. lock_acquire(testlock);
  235. while (testval1 != num) {
  236. gettime(&secs1, &nsecs1);
  237. cv_wait(testcv, testlock);
  238. gettime(&secs2, &nsecs2);
  239. if (nsecs2 < nsecs1) {
  240. secs2--;
  241. nsecs2 += 1000000000;
  242. }
  243. nsecs2 -= nsecs1;
  244. secs2 -= secs1;
  245. /* Require at least 2000 cpu cycles (we're 25mhz) */
  246. if (secs2==0 && nsecs2 < 40*2000) {
  247. kprintf("cv_wait took only %u ns\n", nsecs2);
  248. kprintf("That's too fast... you must be "
  249. "busy-looping\n");
  250. V(donesem);
  251. thread_exit();
  252. }
  253. }
  254. kprintf("Thread %lu\n", num);
  255. testval1 = (testval1 + NTHREADS - 1)%NTHREADS;
  256. /*
  257. * loop a little while to make sure we can measure the
  258. * time waiting on the cv.
  259. */
  260. for (j=0; j<3000; j++);
  261. cv_broadcast(testcv, testlock);
  262. lock_release(testlock);
  263. }
  264. V(donesem);
  265. #ifdef UW
  266. thread_exit();
  267. #endif
  268. }
  269. int
  270. cvtest(int nargs, char **args)
  271. {
  272. int i, result;
  273. (void)nargs;
  274. (void)args;
  275. inititems();
  276. kprintf("Starting CV test...\n");
  277. #ifdef UW
  278. kprintf("%d threads should print out in reverse order %d times.\n", NTHREADS, NCVLOOPS);
  279. #else
  280. kprintf("Threads should print out in reverse order.\n");
  281. #endif
  282. testval1 = NTHREADS-1;
  283. for (i=0; i<NTHREADS; i++) {
  284. result = thread_fork("synchtest", NULL, cvtestthread, NULL, i);
  285. if (result) {
  286. panic("cvtest: thread_fork failed: %s\n",
  287. strerror(result));
  288. }
  289. }
  290. for (i=0; i<NTHREADS; i++) {
  291. P(donesem);
  292. }
  293. #ifdef UW
  294. cleanitems();
  295. #endif
  296. kprintf("CV test done\n");
  297. return 0;
  298. }