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