void bench_memset(void) { void *buf = malloc(BUFSIZE); uint count = arch_cycle_count(); for (uint i = 0; i < ITER; i++) { memset(buf, 0, BUFSIZE); } count = arch_cycle_count() - count; printf("took %u cycles to memset a buffer of size %u %d times (%u bytes)\n", count, BUFSIZE, ITER, BUFSIZE * ITER); free(buf); }
void bench_memcpy(void) { uint8_t *buf = malloc(BUFSIZE); uint count = arch_cycle_count(); for (uint i = 0; i < ITER; i++) { memcpy(buf, buf + BUFSIZE / 2, BUFSIZE / 2); } count = arch_cycle_count() - count; printf("took %u cycles to memcpy a buffer of size %u %d times (%u bytes)\n", count, BUFSIZE / 2, ITER, BUFSIZE * ITER); free(buf); }
void bench_set_overhead(void) { uint32_t *buf = malloc(BUFSIZE); uint count = arch_cycle_count(); for (uint i = 0; i < ITER; i++) { // for (uint j = 0; j < BUFSIZE / sizeof(*buf); j++) { __asm__ volatile( "nop" ); // } } count = arch_cycle_count() - count; printf("took %u cycles overhead to loop %u times\n", count, ITER); free(buf); }
void bench_cset_stm(void) { uint32_t *buf = malloc(BUFSIZE); uint count = arch_cycle_count(); for (uint i = 0; i < ITER; i++) { for (uint j = 0; j < BUFSIZE / sizeof(*buf) / 8; j++) { __asm__ volatile( "stm %0, {r0-r7};" :: "r" (&buf[j*8]) ); } } count = arch_cycle_count() - count; printf("took %u cycles to manually clear a buffer of size %u %d times 8 words at a time using stm (%u bytes)\n", count, BUFSIZE, ITER, BUFSIZE * ITER); free(buf); }
static int context_switch_tester(void *arg) { int i; uint total_count = 0; const int iter = 100000; int thread_count = (intptr_t)arg; event_wait(&context_switch_event); uint count = arch_cycle_count(); for (i = 0; i < iter; i++) { thread_yield(); } total_count += arch_cycle_count() - count; thread_sleep(1000); printf("took %u cycles to yield %d times, %u per yield, %u per yield per thread\n", total_count, iter, total_count / iter, total_count / iter / thread_count); event_signal(&context_switch_done_event, true); return 0; }
static void spinlock_test(void) { spin_lock_saved_state_t state; spin_lock_t lock; spin_lock_init(&lock); // verify basic functionality (single core) printf("testing spinlock:\n"); ASSERT(!spin_lock_held(&lock)); ASSERT(!arch_ints_disabled()); spin_lock_irqsave(&lock, state); ASSERT(arch_ints_disabled()); ASSERT(spin_lock_held(&lock)); spin_unlock_irqrestore(&lock, state); ASSERT(!spin_lock_held(&lock)); ASSERT(!arch_ints_disabled()); printf("seems to work\n"); #define COUNT (1024*1024) uint32_t c = arch_cycle_count(); for (uint i = 0; i < COUNT; i++) { spin_lock(&lock); spin_unlock(&lock); } c = arch_cycle_count() - c; printf("%u cycles to acquire/release lock %u times (%u cycles per)\n", c, COUNT, c / COUNT); c = arch_cycle_count(); for (uint i = 0; i < COUNT; i++) { spin_lock_irqsave(&lock, state); spin_unlock_irqrestore(&lock, state); } c = arch_cycle_count() - c; printf("%u cycles to acquire/release lock w/irqsave %u times (%u cycles per)\n", c, COUNT, c / COUNT); #undef COUNT }
void bench_cset_wide(void) { uint32_t *buf = malloc(BUFSIZE); uint count = arch_cycle_count(); for (uint i = 0; i < ITER; i++) { for (uint j = 0; j < BUFSIZE / sizeof(*buf) / 8; j++) { buf[j*8] = 0; buf[j*8+1] = 0; buf[j*8+2] = 0; buf[j*8+3] = 0; buf[j*8+4] = 0; buf[j*8+5] = 0; buf[j*8+6] = 0; buf[j*8+7] = 0; } } count = arch_cycle_count() - count; printf("took %u cycles to manually clear a buffer of size %u %d times 8 words at a time (%u bytes)\n", count, BUFSIZE, ITER, BUFSIZE * ITER); free(buf); }
__NO_INLINE static void bench_sincos(void) { printf("touching the floating point unit\n"); __UNUSED volatile double _hole = sin(0); uint count = arch_cycle_count(); __UNUSED double a = sin(2.0); count = arch_cycle_count() - count; printf("took %u cycles for sin()\n", count); count = arch_cycle_count(); a = cos(2.0); count = arch_cycle_count() - count; printf("took %u cycles for cos()\n", count); count = arch_cycle_count(); a = sinf(2.0); count = arch_cycle_count() - count; printf("took %u cycles for sinf()\n", count); count = arch_cycle_count(); a = cosf(2.0); count = arch_cycle_count() - count; printf("took %u cycles for cosf()\n", count); count = arch_cycle_count(); a = sqrt(1234567.0); count = arch_cycle_count() - count; printf("took %u cycles for sqrt()\n", count); count = arch_cycle_count(); a = sqrtf(1234567.0f); count = arch_cycle_count() - count; printf("took %u cycles for sqrtf()\n", count); }
void clock_tests(void) { uint32_t c; lk_time_t t; lk_bigtime_t t2; thread_sleep(100); c = arch_cycle_count(); t = current_time(); c = arch_cycle_count() - c; printf("%u cycles per current_time()\n", c); thread_sleep(100); c = arch_cycle_count(); t2 = current_time_hires(); c = arch_cycle_count() - c; printf("%u cycles per current_time_hires()\n", c); printf("making sure time never goes backwards\n"); { printf("testing current_time()\n"); lk_time_t start = current_time(); lk_time_t last = start; for (;;) { t = current_time(); //printf("%lu %lu\n", last, t); if (TIME_LT(t, last)) { printf("WARNING: time ran backwards: %lu < %lu\n", last, t); } last = t; if (last - start > 5000) break; } } { printf("testing current_time_hires()\n"); lk_bigtime_t start = current_time_hires(); lk_bigtime_t last = start; for (;;) { t2 = current_time_hires(); //printf("%llu %llu\n", last, t2); if (t2 < last) { printf("WARNING: time ran backwards: %llu < %llu\n", last, t2); } last = t2; if (last - start > 5000000) break; } } printf("making sure current_time() and current_time_hires() are always the same base\n"); { lk_time_t start = current_time(); for (;;) { t = current_time(); t2 = current_time_hires(); if (t > (t2 / 1000)) { printf("WARNING: current_time() ahead of current_time_hires() %lu %llu\n", t, t2); } if (t - start > 5000) break; } } printf("counting to 5, in one second intervals\n"); for (int i = 0; i < 5; i++) { thread_sleep(1000); printf("%d\n", i + 1); } printf("measuring cpu clock against current_time_hires()\n"); for (int i = 0; i < 5; i++) { uint cycles = arch_cycle_count(); lk_bigtime_t start = current_time_hires(); while ((current_time_hires() - start) < 1000000) ; cycles = arch_cycle_count() - cycles; printf("%u cycles per second\n", cycles); } }