tt3.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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. * More thread test code.
  31. */
  32. #include <types.h>
  33. #include <lib.h>
  34. #include <wchan.h>
  35. #include <thread.h>
  36. #include <synch.h>
  37. #include <test.h>
  38. #include "opt-synchprobs.h"
  39. /* dimension of matrices (cannot be too large or will overflow stack) */
  40. #if OPT_SYNCHPROBS
  41. #define DIM 10
  42. #else
  43. #define DIM 70
  44. #endif
  45. /* number of iterations for sleepalot threads */
  46. #define SLEEPALOT_PRINTS 20 /* number of printouts */
  47. #define SLEEPALOT_ITERS 4 /* iterations per printout */
  48. /* polling frequency of waker thread */
  49. #define WAKER_WAKES 100
  50. /* number of iterations per compute thread */
  51. #define COMPUTE_ITERS 10
  52. #define NWAITCHANS 12
  53. static struct wchan *waitchans[NWAITCHANS]; /* N distinct wait channels */
  54. static volatile int wakerdone;
  55. static struct semaphore *wakersem;
  56. static struct semaphore *donesem;
  57. static
  58. void
  59. setup(void)
  60. {
  61. char tmp[16];
  62. int i;
  63. if (wakersem == NULL) {
  64. wakersem = sem_create("wakersem", 1);
  65. donesem = sem_create("donesem", 0);
  66. for (i=0; i<NWAITCHANS; i++) {
  67. snprintf(tmp, sizeof(tmp), "wc%d", i);
  68. waitchans[i] = wchan_create(kstrdup(tmp));
  69. }
  70. }
  71. wakerdone = 0;
  72. }
  73. static
  74. void
  75. sleepalot_thread(void *junk, unsigned long num)
  76. {
  77. int i, j;
  78. (void)junk;
  79. for (i=0; i<SLEEPALOT_PRINTS; i++) {
  80. for (j=0; j<SLEEPALOT_ITERS; j++) {
  81. struct wchan *w;
  82. w = waitchans[random()%NWAITCHANS];
  83. wchan_lock(w);
  84. wchan_sleep(w);
  85. }
  86. kprintf("[%lu]", num);
  87. }
  88. V(donesem);
  89. }
  90. static
  91. void
  92. waker_thread(void *junk1, unsigned long junk2)
  93. {
  94. int i, done;
  95. (void)junk1;
  96. (void)junk2;
  97. while (1) {
  98. P(wakersem);
  99. done = wakerdone;
  100. V(wakersem);
  101. if (done) {
  102. break;
  103. }
  104. for (i=0; i<WAKER_WAKES; i++) {
  105. struct wchan *w;
  106. w = waitchans[random()%NWAITCHANS];
  107. wchan_wakeall(w);
  108. thread_yield();
  109. }
  110. }
  111. V(donesem);
  112. }
  113. static
  114. void
  115. make_sleepalots(int howmany)
  116. {
  117. char name[16];
  118. int i, result;
  119. for (i=0; i<howmany; i++) {
  120. snprintf(name, sizeof(name), "sleepalot%d", i);
  121. result = thread_fork(name, NULL, sleepalot_thread, NULL, i);
  122. if (result) {
  123. panic("thread_fork failed: %s\n", strerror(result));
  124. }
  125. }
  126. result = thread_fork("waker", NULL, waker_thread, NULL, 0);
  127. if (result) {
  128. panic("thread_fork failed: %s\n", strerror(result));
  129. }
  130. }
  131. static
  132. void
  133. compute_thread(void *junk1, unsigned long num)
  134. {
  135. struct matrix {
  136. char m[DIM][DIM];
  137. };
  138. struct matrix *m1, *m2, *m3;
  139. unsigned char tot;
  140. int i, j, k, m;
  141. uint32_t rand;
  142. (void)junk1;
  143. m1 = kmalloc(sizeof(struct matrix));
  144. KASSERT(m1 != NULL);
  145. m2 = kmalloc(sizeof(struct matrix));
  146. KASSERT(m2 != NULL);
  147. m3 = kmalloc(sizeof(struct matrix));
  148. KASSERT(m3 != NULL);
  149. for (m=0; m<COMPUTE_ITERS; m++) {
  150. for (i=0; i<DIM; i++) {
  151. for (j=0; j<DIM; j++) {
  152. rand = random();
  153. m1->m[i][j] = rand >> 16;
  154. m2->m[i][j] = rand & 0xffff;
  155. }
  156. }
  157. for (i=0; i<DIM; i++) {
  158. for (j=0; j<DIM; j++) {
  159. tot = 0;
  160. for (k=0; k<DIM; k++) {
  161. tot += m1->m[i][k] * m2->m[k][j];
  162. }
  163. m3->m[i][j] = tot;
  164. }
  165. }
  166. tot = 0;
  167. for (i=0; i<DIM; i++) {
  168. tot += m3->m[i][i];
  169. }
  170. kprintf("{%lu: %u}", num, (unsigned) tot);
  171. thread_yield();
  172. }
  173. kfree(m1);
  174. kfree(m2);
  175. kfree(m3);
  176. V(donesem);
  177. }
  178. static
  179. void
  180. make_computes(int howmany)
  181. {
  182. char name[16];
  183. int i, result;
  184. for (i=0; i<howmany; i++) {
  185. snprintf(name, sizeof(name), "compute%d", i);
  186. result = thread_fork(name, NULL, compute_thread, NULL, i);
  187. if (result) {
  188. panic("thread_fork failed: %s\n", strerror(result));
  189. }
  190. }
  191. }
  192. static
  193. void
  194. finish(int howmanytotal)
  195. {
  196. int i;
  197. for (i=0; i<howmanytotal; i++) {
  198. P(donesem);
  199. }
  200. P(wakersem);
  201. wakerdone = 1;
  202. V(wakersem);
  203. P(donesem);
  204. }
  205. static
  206. void
  207. runtest3(int nsleeps, int ncomputes)
  208. {
  209. setup();
  210. kprintf("Starting thread test 3 (%d [sleepalots], %d {computes}, "
  211. "1 waker)\n",
  212. nsleeps, ncomputes);
  213. make_sleepalots(nsleeps);
  214. make_computes(ncomputes);
  215. finish(nsleeps+ncomputes);
  216. kprintf("\nThread test 3 done\n");
  217. }
  218. int
  219. threadtest3(int nargs, char **args)
  220. {
  221. if (nargs==1) {
  222. runtest3(5, 2);
  223. }
  224. else if (nargs==3) {
  225. runtest3(atoi(args[1]), atoi(args[2]));
  226. }
  227. else {
  228. kprintf("Usage: tt3 [sleepthreads computethreads]\n");
  229. return 1;
  230. }
  231. return 0;
  232. }