void semaphore_test(void) { int producer_th; int consumer_th; int producer_ret; int consumer_ret; /* allocate the empty signal semaphores */ /* initialize the empty as 1 so we can insert an item immediately. */ sem_empty = thinkos_sem_alloc(1); /* allocate the full signal semaphores */ /* initialize the full as 0 as we don't have produced anything yet. */ sem_full = thinkos_sem_alloc(0); /* create the producer thread */ producer_th = thinkos_thread_create(producer_task, NULL, producer_stack, sizeof(producer_stack)); /* create the consuer thread */ consumer_th = thinkos_thread_create(consumer_task, NULL, consumer_stack, sizeof(consumer_stack)); printf(" * Empty semaphore: %d\n", sem_empty); printf(" * Full semaphore: %d\n", sem_full); printf(" * Producer thread: %d\n", producer_th); printf(" * Consumer thread: %d\n", consumer_th); printf("\n"); thinkos_sleep(100); /* number of items to be produced */ prod_count = 100; /* wait for the production thread to finish */ producer_ret = thinkos_join(producer_th); /* wait for the consumer thread to finish */ consumer_ret = thinkos_join(consumer_th); printf(" * Production return = %d\n", producer_ret); printf(" * Consumer return = %d\n", consumer_ret); /* release the semaphores */ thinkos_sem_free(sem_empty); thinkos_sem_free(sem_full); printf("\n"); };
void busy_test(void) { int th[4]; int x[4]; int y[4]; int d[4]; int sum; int ref; int ret; int i; printf("---------------------------------------------------------\n"); printf(" - Scheduler test\n"); for (i = 0; i < 4; i++) { dev[i].enabled = true; dev[i].cnt = 0; } th[0] = thinkos_thread_create(busy_task, (void *)&dev[0], stack[0], STACK_SIZE, THINKOS_OPT_PRIORITY(2) | THINKOS_OPT_ID(8)); th[1] = thinkos_thread_create(busy_task, (void *)&dev[1], stack[1], STACK_SIZE, THINKOS_OPT_PRIORITY(4) | THINKOS_OPT_ID(8)); th[2] = thinkos_thread_create(busy_task, (void *)&dev[2], stack[2], STACK_SIZE, THINKOS_OPT_PRIORITY(6) | THINKOS_OPT_ID(8)); th[3] = thinkos_thread_create(busy_task, (void *)&dev[3], stack[3], STACK_SIZE, THINKOS_OPT_PRIORITY(8) | THINKOS_OPT_ID(8)); printf(" * Threads: %d, %d, %d, %d\n", th[0], th[1], th[2], th[3]); for (i = 0; i < 8; i++) { x[0] = dev[0].cnt; x[1] = dev[1].cnt; x[2] = dev[2].cnt; x[3] = dev[3].cnt; thinkos_sleep(1000); y[0] = dev[0].cnt; y[1] = dev[1].cnt; y[2] = dev[2].cnt; y[3] = dev[3].cnt; d[0] = y[0] - x[0]; d[1] = y[1] - x[1]; d[2] = y[2] - x[2]; d[3] = y[3] - x[3]; sum = d[0] + d[1] + d[2] + d[3]; ref = 100 * d[3]; printf(" %8d + %8d + %8d + %8d = %8d (%d idle)\n", d[0], d[1], d[2], d[3], sum, thinkos_idle_val()); printf(" %d%% %d%% %d%% %d%%\n", ref / d[0], ref / d[1], ref / d[2], ref / d[3]); } for (i = 0; i < 4; i++) { dev[i].enabled = false; printf(" * join(%d) ...", th[i]); ret = thinkos_join(th[i]); printf(" %d\n", ret); } thinkos_sleep(1); printf("\n"); }
void sched_speed_test(void) { // uint32_t stack[4][STACK_SIZE / 4]; // volatile struct my_arg ctrl[4]; int th[4]; int x[4]; int y[4]; int d[4]; int ret; int i; printf("---------------------------------------------------------\n"); printf(" - Scheduler speed test\n"); th[0] = thinkos_thread_create(yield_task, (void *)&dev[0], stack[0], STACK_SIZE, THINKOS_OPT_PRIORITY(1) | THINKOS_OPT_ID(2) | THINKOS_OPT_PAUSED); th[1] = thinkos_thread_create(yield_task, (void *)&dev[1], stack[1], STACK_SIZE, THINKOS_OPT_PRIORITY(1) | THINKOS_OPT_ID(2) | THINKOS_OPT_PAUSED); th[2] = thinkos_thread_create(yield_task, (void *)&dev[2], stack[2], STACK_SIZE, THINKOS_OPT_PRIORITY(2) | THINKOS_OPT_ID(0) | THINKOS_OPT_PAUSED); th[3] = thinkos_thread_create(yield_task, (void *)&dev[3], stack[3], STACK_SIZE, THINKOS_OPT_PRIORITY(2) | THINKOS_OPT_ID(0) | THINKOS_OPT_PAUSED); printf(" * Threads: %d, %d, %d, %d\n", th[0], th[1], th[2], th[3]); /* The threads where paused on creation, start them */ for (i = 0; i < 4; i++) { thinkos_resume(th[i]); } for (i = 0; i < 5; i++) { x[0] = dev[0].cnt; x[1] = dev[1].cnt; x[2] = dev[2].cnt; x[3] = dev[3].cnt; thinkos_sleep(1000); y[0] = dev[0].cnt; y[1] = dev[1].cnt; y[2] = dev[2].cnt; y[3] = dev[3].cnt; d[0] = y[0] - x[0]; d[1] = y[1] - x[1]; d[2] = y[2] - x[2]; d[3] = y[3] - x[3]; printf(" %8d + %8d + %8d + %8d = %8d (%d idle)\n", d[0], d[1], d[2], d[3], d[0] + d[1] + d[2] + d[3], thinkos_idle_val()); } for (i = 0; i < 4; i++) { thinkos_cancel(th[i], i + 30); printf(" * join(%d) ...", th[i]); ret = thinkos_join(th[i]); printf(" %d\n", ret); } printf("\n"); };
void busy_test(void) { int th[4]; uint32_t x[4]; uint32_t y[4]; uint32_t d[4]; uint32_t sum; uint32_t ref; uint32_t wrk; struct thinkos_rt rt; uint32_t cycsum; int ret; int i; int j; printf("---------------------------------------------------------\n"); printf(" - Scheduler test\n"); thinkos_rt_snapshot(&rt); thinkos_sleep(1000); thinkos_rt_snapshot(&rt); cycsum = 0; for (j = 0; j < THINKOS_THREADS_MAX + 1; ++j) { uint32_t cyc = rt.cyccnt[j]; cycsum += cyc; if (cyc > 0) { printf(" * Thread %d: %9d cycles.\n", j, cyc); } } printf(" * Total: %9d cycles.\n", cycsum); for (i = 0; i < 4; i++) { dev[i].enabled = true; dev[i].cnt = 0; } th[0] = thinkos_thread_create(busy_task, (void *)&dev[0], stack[0], STACK_SIZE | THINKOS_OPT_PRIORITY(2) | THINKOS_OPT_ID(8)); th[1] = thinkos_thread_create(busy_task, (void *)&dev[1], stack[1], STACK_SIZE | THINKOS_OPT_PRIORITY(4) | THINKOS_OPT_ID(8)); th[2] = thinkos_thread_create(busy_task, (void *)&dev[2], stack[2], STACK_SIZE | THINKOS_OPT_PRIORITY(6) | THINKOS_OPT_ID(8)); th[3] = thinkos_thread_create(busy_task, (void *)&dev[3], stack[3], STACK_SIZE | THINKOS_OPT_PRIORITY(8) | THINKOS_OPT_ID(8)); printf(" * Thread: %8d %8d %8d %8d\n", th[0], th[1], th[2], th[3]); for (i = 0; i < 8; i++) { x[0] = dev[0].cnt; x[1] = dev[1].cnt; x[2] = dev[2].cnt; x[3] = dev[3].cnt; thinkos_rt_snapshot(&rt); thinkos_sleep(1000); thinkos_rt_snapshot(&rt); y[0] = dev[0].cnt; y[1] = dev[1].cnt; y[2] = dev[2].cnt; y[3] = dev[3].cnt; d[0] = y[0] - x[0]; d[1] = y[1] - x[1]; d[2] = y[2] - x[2]; d[3] = y[3] - x[3]; sum = d[0] + d[1] + d[2] + d[3]; /* XXX: reusing x, to scale down the deltas. This is to avoid overflowing the percentage calculations */ x[0] = (d[0] + 50) / 100; x[1] = (d[1] + 50) / 100; x[2] = (d[2] + 50) / 100; x[3] = (d[3] + 50) / 100; /* scale down the total sum */ wrk = sum / 100; ref = d[3]; printf("%d.\n", i); printf(" * Oper: %8d + %8d + %8d + %8d = %8d (%d idle)\n", d[0], d[1], d[2], d[3], sum, thinkos_idle_val()); printf(" * Work: %7d%% %7d%% %7d%% %7d%% = %7d%%\n", (d[0] + wrk / 2) / wrk, (d[1] + wrk / 2) / wrk, (d[2] + wrk / 2) / wrk, (d[3] + wrk / 2) / wrk, (sum + wrk / 2) / wrk); printf(" * Nice: %7d%% %7d%% %7d%% %7d%%\n", (ref + x[0] / 2) / x[0], (ref + x[1] / 2) / x[1], (ref + x[2] / 2) / x[2], (ref + x[3] / 2) / x[3]); cycsum = 0; for (j = 0; j < THINKOS_THREADS_MAX + 1; ++j) cycsum += rt.cyccnt[j]; printf(" * Cycles: %8d + %8d + %8d + %8d = %8d (%d idle)\n", rt.cyccnt[th[0]], rt.cyccnt[th[1]], rt.cyccnt[th[2]], rt.cyccnt[th[3]], cycsum, rt.cyccnt[THINKOS_THREADS_MAX]); } printf("\n"); for (i = 0; i < 4; i++) { dev[i].enabled = false; printf(" * join(%d) ...", th[i]); ret = thinkos_join(th[i]); printf(" %d\n", ret); } printf("---------------------------------------------------------\n"); printf("\n"); }