Synchronization testing changes. Tests now know when they succeed or fail and are ready for automation.

This commit is contained in:
Geoffrey Challen 2015-12-31 11:44:46 -05:00
parent 54c11ab660
commit e7a585a298

View File

@ -37,7 +37,8 @@
#include <thread.h> #include <thread.h>
#include <synch.h> #include <synch.h>
#include <test.h> #include <test.h>
#include <kern/overwrite.h> #include <kern/secret.h>
#include <spinlock.h>
#define SUCCESS 0 #define SUCCESS 0
#define FAIL 1 #define FAIL 1
@ -50,12 +51,16 @@
static volatile unsigned long testval1; static volatile unsigned long testval1;
static volatile unsigned long testval2; static volatile unsigned long testval2;
static volatile unsigned long testval3; static volatile unsigned long testval3;
static volatile int32_t testval4;
static struct semaphore *testsem; static struct semaphore *testsem;
static struct lock *testlock; static struct lock *testlock;
static struct cv *testcv; static struct cv *testcv;
static struct semaphore *donesem; static struct semaphore *donesem;
static bool semtest_status; struct spinlock status_lock;
static bool test_status;
static unsigned long semtest_current; static unsigned long semtest_current;
static static
@ -86,6 +91,17 @@ inititems(void)
panic("synchtest: sem_create failed\n"); panic("synchtest: sem_create failed\n");
} }
} }
spinlock_init(&status_lock);
}
static
void
success(bool status, const char *msg) {
if (status == SUCCESS) {
skprintf("%s: SUCCESS\n", msg);
} else {
skprintf("%s: FAIL\n", msg);
}
} }
static static
@ -101,15 +117,19 @@ semtestthread(void *junk, unsigned long num)
random_yielder(4); random_yielder(4);
P(testsem); P(testsem);
semtest_current = num; semtest_current = num;
kprintf("Thread %2lu: ", num); tkprintf("Thread %2lu: ", num);
for (i=0; i<NSEMLOOPS; i++) { for (i=0; i<NSEMLOOPS; i++) {
kprintf("%c", (int)num+64); tkprintf("%c", (int)num+64);
random_yielder(4); random_yielder(4);
if (semtest_current != num) { if (semtest_current != num) {
semtest_status = FAIL; spinlock_acquire(&status_lock);
test_status = FAIL;
spinlock_release(&status_lock);
} }
} }
kprintf("\n");
tkprintf("\n");
V(donesem); V(donesem);
} }
@ -122,12 +142,12 @@ semtest(int nargs, char **args)
(void)args; (void)args;
inititems(); inititems();
semtest_status = SUCCESS; test_status = SUCCESS;
kprintf("Starting semaphore test...\n"); tkprintf("Starting semaphore test...\n");
kprintf("If this hangs, it's broken: "); tkprintf("If this hangs, it's broken: ");
P(testsem); P(testsem);
P(testsem); P(testsem);
kprintf("ok\n"); tkprintf("ok\n");
for (i=0; i<NTHREADS; i++) { for (i=0; i<NTHREADS; i++) {
result = thread_fork("semtest", NULL, semtestthread, NULL, i); result = thread_fork("semtest", NULL, semtestthread, NULL, i);
@ -146,13 +166,9 @@ semtest(int nargs, char **args)
V(testsem); V(testsem);
V(testsem); V(testsem);
if (semtest_status == SUCCESS) { tkprintf("Semaphore test done.\n");
kprintf("SUCCESS: %llu\n", KERNEL_SECRET); success(test_status, "semtest");
} else {
kprintf("FAIL: %llu\n", KERNEL_SECRET);
}
kprintf("Semaphore test done.\n");
return 0; return 0;
} }
@ -160,11 +176,15 @@ static
void void
fail(unsigned long num, const char *msg) fail(unsigned long num, const char *msg)
{ {
kprintf("thread %lu: Mismatch on %s\n", num, msg); tkprintf("thread %lu: Mismatch on %s\n", num, msg);
kprintf("Test failed\n"); tkprintf("Test failed\n");
lock_release(testlock); lock_release(testlock);
spinlock_acquire(&status_lock);
test_status = FAIL;
spinlock_release(&status_lock);
V(donesem); V(donesem);
thread_exit(); thread_exit();
} }
@ -176,10 +196,12 @@ locktestthread(void *junk, unsigned long num)
int i; int i;
(void)junk; (void)junk;
for (i=0; i<NLOCKLOOPS; i++) { random_yielder(4);
lock_acquire(testlock);
thread_yield(); for (i=0; i<NLOCKLOOPS; i++) {
random_yielder(4);
lock_acquire(testlock);
random_yielder(4);
testval1 = num; testval1 = num;
testval2 = num*num; testval2 = num*num;
@ -188,38 +210,32 @@ locktestthread(void *junk, unsigned long num)
if (testval2 != testval1*testval1) { if (testval2 != testval1*testval1) {
fail(num, "testval2/testval1"); fail(num, "testval2/testval1");
} }
random_yielder(4);
thread_yield();
if (testval2%3 != (testval3*testval3)%3) { if (testval2%3 != (testval3*testval3)%3) {
fail(num, "testval2/testval3"); fail(num, "testval2/testval3");
} }
random_yielder(4);
thread_yield();
if (testval3 != testval1%3) { if (testval3 != testval1%3) {
fail(num, "testval3/testval1"); fail(num, "testval3/testval1");
} }
random_yielder(4);
thread_yield();
if (testval1 != num) { if (testval1 != num) {
fail(num, "testval1/num"); fail(num, "testval1/num");
} }
random_yielder(4);
thread_yield();
if (testval2 != num*num) { if (testval2 != num*num) {
fail(num, "testval2/num"); fail(num, "testval2/num");
} }
random_yielder(4);
thread_yield();
if (testval3 != num%3) { if (testval3 != num%3) {
fail(num, "testval3/num"); fail(num, "testval3/num");
} }
random_yielder(4);
thread_yield();
lock_release(testlock); lock_release(testlock);
} }
@ -236,21 +252,21 @@ locktest(int nargs, char **args)
(void)args; (void)args;
inititems(); inititems();
kprintf("Starting lock test...\n"); test_status = SUCCESS;
tkprintf("Starting lock test...\n");
for (i=0; i<NTHREADS; i++) { for (i=0; i<NTHREADS; i++) {
result = thread_fork("synchtest", NULL, locktestthread, result = thread_fork("synchtest", NULL, locktestthread, NULL, i);
NULL, i);
if (result) { if (result) {
panic("locktest: thread_fork failed: %s\n", panic("locktest: thread_fork failed: %s\n", strerror(result));
strerror(result));
} }
} }
for (i=0; i<NTHREADS; i++) { for (i=0; i<NTHREADS; i++) {
P(donesem); P(donesem);
} }
kprintf("Lock test done.\n"); tkprintf("Lock test done.\n");
success(test_status, "locktest");
return 0; return 0;
} }
@ -278,10 +294,11 @@ cvtestthread(void *junk, unsigned long num)
/* Require at least 2000 cpu cycles (we're 25mhz) */ /* Require at least 2000 cpu cycles (we're 25mhz) */
if (ts2.tv_sec == 0 && ts2.tv_nsec < 40*2000) { if (ts2.tv_sec == 0 && ts2.tv_nsec < 40*2000) {
kprintf("cv_wait took only %u ns\n", tkprintf("cv_wait took only %u ns\n", ts2.tv_nsec);
ts2.tv_nsec); tkprintf("That's too fast... you must be " "busy-looping\n");
kprintf("That's too fast... you must be " spinlock_acquire(&status_lock);
"busy-looping\n"); test_status = FAIL;
spinlock_release(&status_lock);
V(donesem); V(donesem);
thread_exit(); thread_exit();
} }
@ -296,10 +313,18 @@ cvtestthread(void *junk, unsigned long num)
*/ */
for (j=0; j<3000; j++); for (j=0; j<3000; j++);
random_yielder(4);
cv_broadcast(testcv, testlock); cv_broadcast(testcv, testlock);
thread_yield(); random_yielder(4);
kprintf("Thread %lu\n", testval2);
testval1 = (testval1 + NTHREADS - 1)%NTHREADS; spinlock_acquire(&status_lock);
if (testval1 != testval2) {
test_status = FAIL;
}
spinlock_release(&status_lock);
tkprintf("Thread %lu\n", testval2);
testval1 = (testval1 + NTHREADS - 1) % NTHREADS;
lock_release(testlock); lock_release(testlock);
} }
V(donesem); V(donesem);
@ -314,23 +339,23 @@ cvtest(int nargs, char **args)
(void)args; (void)args;
inititems(); inititems();
kprintf("Starting CV test...\n"); tkprintf("Starting CV test...\n");
kprintf("Threads should print out in reverse order.\n"); tkprintf("Threads should print out in reverse order.\n");
testval1 = NTHREADS-1; testval1 = NTHREADS-1;
for (i=0; i<NTHREADS; i++) { for (i=0; i<NTHREADS; i++) {
result = thread_fork("synchtest", NULL, cvtestthread, NULL, (long unsigned) i); result = thread_fork("synchtest", NULL, cvtestthread, NULL, (long unsigned) i);
if (result) { if (result) {
panic("cvtest: thread_fork failed: %s\n", panic("cvtest: thread_fork failed: %s\n", strerror(result));
strerror(result));
} }
} }
for (i=0; i<NTHREADS; i++) { for (i=0; i<NTHREADS; i++) {
P(donesem); P(donesem);
} }
kprintf("CV test done\n"); tkprintf("CV test done\n");
success(test_status, "cvtest");
return 0; return 0;
} }
@ -361,14 +386,22 @@ sleepthread(void *junk1, unsigned long junk2)
(void)junk1; (void)junk1;
(void)junk2; (void)junk2;
random_yielder(4);
for (j=0; j<NLOOPS; j++) { for (j=0; j<NLOOPS; j++) {
for (i=0; i<NCVS; i++) { for (i=0; i<NCVS; i++) {
lock_acquire(testlocks[i]); lock_acquire(testlocks[i]);
random_yielder(4);
V(gatesem); V(gatesem);
random_yielder(4);
spinlock_acquire(&status_lock);
testval1++;
spinlock_release(&status_lock);
cv_wait(testcvs[i], testlocks[i]); cv_wait(testcvs[i], testlocks[i]);
random_yielder(4);
lock_release(testlocks[i]); lock_release(testlocks[i]);
} }
kprintf("sleepthread: %u\n", j); tkprintf("sleepthread: %u\n", j);
} }
V(exitsem); V(exitsem);
} }
@ -382,14 +415,26 @@ wakethread(void *junk1, unsigned long junk2)
(void)junk1; (void)junk1;
(void)junk2; (void)junk2;
random_yielder(4);
for (j=0; j<NLOOPS; j++) { for (j=0; j<NLOOPS; j++) {
for (i=0; i<NCVS; i++) { for (i=0; i<NCVS; i++) {
random_yielder(4);
P(gatesem); P(gatesem);
random_yielder(4);
lock_acquire(testlocks[i]); lock_acquire(testlocks[i]);
random_yielder(4);
spinlock_acquire(&status_lock);
testval4--;
if (testval4 != 0) {
test_status = FAIL;
}
spinlock_release(&status_lock);
cv_signal(testcvs[i], testlocks[i]); cv_signal(testcvs[i], testlocks[i]);
random_yielder(4);
lock_release(testlocks[i]); lock_release(testlocks[i]);
} }
kprintf("wakethread: %u\n", j); tkprintf("wakethread: %u\n", j);
} }
V(exitsem); V(exitsem);
} }
@ -403,6 +448,9 @@ cvtest2(int nargs, char **args)
(void)nargs; (void)nargs;
(void)args; (void)args;
inititems();
test_status = SUCCESS;
for (i=0; i<NCVS; i++) { for (i=0; i<NCVS; i++) {
testlocks[i] = lock_create("cvtest2 lock"); testlocks[i] = lock_create("cvtest2 lock");
testcvs[i] = cv_create("cvtest2 cv"); testcvs[i] = cv_create("cvtest2 cv");
@ -410,7 +458,7 @@ cvtest2(int nargs, char **args)
gatesem = sem_create("gatesem", 0); gatesem = sem_create("gatesem", 0);
exitsem = sem_create("exitsem", 0); exitsem = sem_create("exitsem", 0);
kprintf("cvtest2...\n"); tkprintf("cvtest2...\n");
result = thread_fork("cvtest2", NULL, sleepthread, NULL, 0); result = thread_fork("cvtest2", NULL, sleepthread, NULL, 0);
if (result) { if (result) {
@ -434,6 +482,8 @@ cvtest2(int nargs, char **args)
testcvs[i] = NULL; testcvs[i] = NULL;
} }
kprintf("cvtest2 done\n"); tkprintf("cvtest2 done\n");
success(test_status, "cvtest2");
return 0; return 0;
} }