uw-tests.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  1. /*
  2. * UW - Synchronization test code.
  3. * Tim Brecht July, 2013
  4. * UW - uwvmstats tests code.
  5. * Tim Brecht January, 2014
  6. */
  7. #include <types.h>
  8. #include <synch.h>
  9. #include <thread.h>
  10. #include <test.h>
  11. #include <uw-vmstats.h>
  12. #define NAME_LEN (30)
  13. static struct lock *testlock = NULL;
  14. static struct semaphore *donesem = NULL;
  15. #define NTESTLOOPS (5000) /* Needs to be evenly divisible by 8 */
  16. #define NTESTTHREADS (8)
  17. #define START_VALUE (0)
  18. static volatile int test_value = START_VALUE;
  19. static int use_locks = 1;
  20. static
  21. void
  22. cleanitems(void)
  23. {
  24. kprintf("cleanitems: Destroying sems and locks\n");
  25. lock_destroy(testlock);
  26. testlock = NULL;
  27. sem_destroy(donesem);
  28. donesem = NULL;
  29. test_value = START_VALUE;
  30. }
  31. static
  32. void
  33. inititems(void)
  34. {
  35. if (testlock==NULL) {
  36. testlock = lock_create("testlock");
  37. if (testlock == NULL) {
  38. panic("synchtest: lock_create failed\n");
  39. }
  40. }
  41. if (donesem==NULL) {
  42. donesem = sem_create("donesem", 0);
  43. if (donesem == NULL) {
  44. panic("synchtest: sem_create failed\n");
  45. }
  46. }
  47. }
  48. /* This thread adds values to a global variable. */
  49. static
  50. void
  51. add_thread(void *junk, unsigned long num)
  52. {
  53. int i;
  54. (void) num;
  55. (void) junk;
  56. for (i=0; i<NTESTLOOPS; i++) {
  57. if (use_locks) {
  58. lock_acquire(testlock);
  59. }
  60. /* This loop is unrolled to possibly avoid optimizations
  61. * and to provide more instructions that could be interrupted.
  62. * This may or may not be necessary.
  63. */
  64. test_value = test_value + 1;
  65. test_value = test_value + 1;
  66. test_value = test_value + 1;
  67. test_value = test_value + 1;
  68. test_value = test_value + 1;
  69. if (use_locks) {
  70. lock_release(testlock);
  71. }
  72. }
  73. V(donesem);
  74. thread_exit();
  75. }
  76. /* This thread substract values from a global variable. */
  77. static
  78. void
  79. sub_thread(void *junk, unsigned long num)
  80. {
  81. int i;
  82. (void)num;
  83. (void)junk;
  84. for (i=0; i<NTESTLOOPS; i++) {
  85. if (use_locks) {
  86. lock_acquire(testlock);
  87. }
  88. /* This loop is unrolled to avoid optimizations
  89. * and to provide more instructions that could be interrupted.
  90. * This may or may not be necessary.
  91. */
  92. test_value = test_value - 1;
  93. test_value = test_value - 1;
  94. test_value = test_value - 1;
  95. test_value = test_value - 1;
  96. test_value = test_value - 1;
  97. if (use_locks) {
  98. lock_release(testlock);
  99. }
  100. }
  101. V(donesem);
  102. thread_exit();
  103. }
  104. int
  105. uwlocktest1(int nargs, char **args)
  106. {
  107. int i, result;
  108. char name[NAME_LEN];
  109. (void)nargs;
  110. (void)args;
  111. inititems();
  112. kprintf("Starting uwlocktest1...\n");
  113. for (i=0; i<NTESTTHREADS; i++) {
  114. snprintf(name, NAME_LEN, "add_thread %d", i);
  115. result = thread_fork(name, NULL, add_thread, NULL, i);
  116. if (result) {
  117. panic("uwlocktest1: thread_fork failed: %s\n",
  118. strerror(result));
  119. }
  120. }
  121. for (i=0; i<NTESTTHREADS; i++) {
  122. snprintf(name, NAME_LEN, "sub_thread %d", i);
  123. result = thread_fork(name, NULL, sub_thread, NULL, i);
  124. if (result) {
  125. panic("uwlocktest1: thread_fork failed: %s\n",
  126. strerror(result));
  127. }
  128. }
  129. for (i=0; i<NTESTTHREADS*2; i++) {
  130. P(donesem);
  131. }
  132. kprintf("value of test_value = %d should be %d\n", test_value, START_VALUE);
  133. if (test_value == START_VALUE) {
  134. kprintf("TEST SUCCEEDED\n");
  135. } else {
  136. kprintf("TEST FAILED\n");
  137. }
  138. KASSERT(test_value == START_VALUE);
  139. cleanitems();
  140. kprintf("uwlocktest1 done.\n");
  141. return 0;
  142. }
  143. /*-----------------------------------------------------------------------*/
  144. /* Each thread makes some calls to vmstats functions */
  145. static
  146. void
  147. vmstats_thread(void *junk, unsigned long num)
  148. {
  149. int i;
  150. int j;
  151. (void)num;
  152. (void)junk;
  153. for (i=0; i<NTESTLOOPS; i++) {
  154. for (j=0; j<VMSTAT_COUNT; j++) {
  155. /* NOTE: The number of calls to vmstats_inc below have been manipulated
  156. * so the checks during printing add up properly and pass the various tests
  157. */
  158. switch(j) {
  159. /* Need twice as many TLB faults */
  160. case VMSTAT_TLB_FAULT:
  161. vmstats_inc(j);
  162. vmstats_inc(j);
  163. break;
  164. case VMSTAT_TLB_FAULT_FREE:
  165. vmstats_inc(j);
  166. break;
  167. case VMSTAT_TLB_FAULT_REPLACE:
  168. vmstats_inc(j);
  169. break;
  170. /* Just reduce these to compare (not necessary) */
  171. case VMSTAT_TLB_INVALIDATE:
  172. if (i % 2 == 0) {
  173. vmstats_inc(j);
  174. }
  175. break;
  176. case VMSTAT_TLB_RELOAD:
  177. vmstats_inc(j);
  178. break;
  179. /* VMSTAT_TLB_FAULT = VMSTAT_TLB_RELOAD + VMSTAT_PAGE_FAULT_DISK + VMSTAT_SWAP_FILE_ZERO */
  180. case VMSTAT_PAGE_FAULT_ZERO:
  181. if (i % 2 == 0) {
  182. vmstats_inc(j);
  183. }
  184. break;
  185. /* VMSTAT_PAGE_FAULT_DISK = VMSTAT_ELF_FILE_READ + VMSTAT_SWAP_FILE_READ */
  186. case VMSTAT_PAGE_FAULT_DISK:
  187. if (i % 2 == 0) {
  188. vmstats_inc(j);
  189. }
  190. break;
  191. case VMSTAT_ELF_FILE_READ:
  192. if (i % 4 == 0) {
  193. vmstats_inc(j);
  194. }
  195. break;
  196. case VMSTAT_SWAP_FILE_READ:
  197. if (i % 4 == 0) {
  198. vmstats_inc(j);
  199. }
  200. break;
  201. case VMSTAT_SWAP_FILE_WRITE:
  202. if (i % 8 == 0) {
  203. vmstats_inc(j);
  204. }
  205. break;
  206. default:
  207. kprintf("Unknown stat %d\n", j);
  208. break;
  209. }
  210. }
  211. }
  212. V(donesem);
  213. thread_exit();
  214. }
  215. int
  216. uwvmstatstest(int nargs, char **args)
  217. {
  218. int i, result;
  219. char name[NAME_LEN];
  220. (void)nargs;
  221. (void)args;
  222. inititems();
  223. kprintf("Starting uwvmstatstest...\n");
  224. kprintf("Initializing vmstats\n");
  225. vmstats_init();
  226. for (i=0; i<NTESTTHREADS; i++) {
  227. snprintf(name, NAME_LEN, "vmstatsthread %d", i);
  228. result = thread_fork(name, NULL, vmstats_thread, NULL, i);
  229. if (result) {
  230. panic("uwvmstatstest: thread_fork failed: %s\n",
  231. strerror(result));
  232. }
  233. }
  234. for (i=0; i<NTESTTHREADS; i++) {
  235. P(donesem);
  236. }
  237. vmstats_print();
  238. cleanitems();
  239. kprintf("uwvmstatstest done.\n");
  240. return 0;
  241. }