self-check benchmarks
This commit is contained in:
parent
d2f3970b79
commit
42cdc839d9
|
@ -1,3 +1,5 @@
|
|||
// TODO: we do not need this if we have klib.
|
||||
|
||||
#ifndef __BENCHLIB_H__
|
||||
#define __BENCHLIB_H__
|
||||
|
||||
|
@ -9,15 +11,9 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
void klib_init();
|
||||
|
||||
// printk
|
||||
void printk(const char *fmt, ...);
|
||||
|
||||
// memory allocation
|
||||
void* kalloc(size_t);
|
||||
void kfree(void*);
|
||||
|
||||
#define assert(cond) \
|
||||
do { \
|
||||
if (!(cond)) { \
|
||||
|
|
|
@ -8,17 +8,21 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define REPEAT 1
|
||||
|
||||
// memory usages
|
||||
#define MB * 1024 * 1024
|
||||
#define KB * 1024
|
||||
|
||||
#define SMALL
|
||||
#define LARGE
|
||||
|
||||
// the list of benchmarks
|
||||
#define BENCHMARK_LIST(V) \
|
||||
V(qsort, "qsort", 1 MB, true, "quick sort") \
|
||||
V(queen, "queen", 0 MB, true, "queen placement problem") \
|
||||
V( bf, "bf", 1 MB, true, "branf**k interpreter") \
|
||||
// Name | Mem | Enable | Desc |
|
||||
#define BENCHMARK_LIST(def) \
|
||||
def(qsort, "qsort", 640 KB, true, "quick sort") \
|
||||
def(queen, "queen", 0 KB, true, "queen placement") \
|
||||
def( bf, "bf", 32 KB, true, "branf**k interpreter") \
|
||||
|
||||
// Each benchmark will run REPEAT times
|
||||
#define REPEAT 1
|
||||
|
||||
#define DECL(name, sname, mlim, enabled, desc) \
|
||||
void bench_##name##_prepare(); \
|
||||
|
@ -53,6 +57,9 @@ void bench_reset();
|
|||
void srand(int seed);
|
||||
int rand(); // return a random number between 0..32767
|
||||
|
||||
// checksum
|
||||
u32 checksum(void *start, void *end);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
|
|
@ -2,6 +2,8 @@
|
|||
#include <benchmark.h>
|
||||
#include <benchlib.h>
|
||||
|
||||
Benchmark *current;
|
||||
|
||||
// The benchmark list
|
||||
|
||||
#define ENTRY(nm, snm, ml, en, des) \
|
||||
|
@ -40,6 +42,7 @@ const char *bench_check(Benchmark &bench) {
|
|||
}
|
||||
|
||||
bool run_once(Benchmark &bench, Result &res) {
|
||||
current = &bench;
|
||||
bench_reset(); // reset malloc state
|
||||
bench.prepare(); // call bechmark's prepare function
|
||||
bench_prepare(&res); // clean everything, start timer
|
||||
|
@ -71,7 +74,7 @@ int main() {
|
|||
for (int i = 0; i < REPEAT; i ++) {
|
||||
Result res;
|
||||
bool s = run_once(bench, res);
|
||||
printk(s ? "." : "x");
|
||||
printk(s ? "*" : "X");
|
||||
succ &= s;
|
||||
tsc += res.tsc;
|
||||
msec += res.msec;
|
||||
|
@ -80,7 +83,6 @@ int main() {
|
|||
tsc /= REPEAT;
|
||||
msec /= REPEAT; // TODO: handle overflow
|
||||
|
||||
|
||||
if (succ) {
|
||||
printk(" Passed.");
|
||||
} else {
|
||||
|
@ -107,6 +109,8 @@ void* bench_alloc(size_t size) {
|
|||
if (start >= _heap.end) {
|
||||
return NULL;
|
||||
}
|
||||
ulong use = (ulong)start - (ulong)_heap.start;
|
||||
assert(use <= current->mlim);
|
||||
return old;
|
||||
}
|
||||
|
||||
|
@ -127,3 +131,18 @@ int rand() {
|
|||
seed = (seed * 214013L + 2531011L) >> 16;
|
||||
return seed & 0x7fff;
|
||||
}
|
||||
|
||||
// FNV hash
|
||||
u32 checksum(void *start, void *end) {
|
||||
const int x = 16777619;
|
||||
int hash = 2166136261;
|
||||
for (char *p = (char*)start; p != (char*)end; p ++) {
|
||||
hash = (hash ^ *p) * x;
|
||||
}
|
||||
hash += hash << 13;
|
||||
hash ^= hash >> 7;
|
||||
hash += hash << 3;
|
||||
hash ^= hash >> 17;
|
||||
hash += hash << 5;
|
||||
return hash;
|
||||
}
|
||||
|
|
|
@ -27,6 +27,7 @@
|
|||
#define CODE ">>+>>>>>,[>+>>,]>+[--[+<<<-]<[<+>-]<[<[->[<<<+>>>>+<-]<<[>>+>[->]<<[<]" \
|
||||
"<-]>]>>>+<[[-]<[>+<-]<]>[[>>>]+<<<-<[<<[<<<]>>+>[>>>]<-]<<[<<<]>[>>[>>" \
|
||||
">]<+<<[<<<]>-]]+<<<]+[->>>]>>]>>[.>>>]"
|
||||
#define CHECKSUM 0x25c64801
|
||||
|
||||
#define OP_END 0
|
||||
#define OP_INC_DP 1
|
||||
|
@ -43,7 +44,7 @@
|
|||
|
||||
#define PROGRAM_SIZE 4096
|
||||
#define STACK_SIZE 512
|
||||
#define DATA_SIZE 65535
|
||||
#define DATA_SIZE 4096
|
||||
|
||||
#define STACK_PUSH(A) (STACK[SP++] = A)
|
||||
#define STACK_POP() (STACK[--SP])
|
||||
|
@ -100,8 +101,10 @@ int compile_bf() {
|
|||
return SUCCESS;
|
||||
}
|
||||
|
||||
unsigned short *data;
|
||||
int execute_bf() {
|
||||
unsigned short *data, *output;
|
||||
int noutput;
|
||||
|
||||
u32 execute_bf() {
|
||||
unsigned int pc = 0, ptr = 0;
|
||||
while (PROGRAM[pc].operator != OP_END && ptr < DATA_SIZE) {
|
||||
switch (PROGRAM[pc].operator) {
|
||||
|
@ -109,7 +112,7 @@ int execute_bf() {
|
|||
case OP_DEC_DP: ptr--; break;
|
||||
case OP_INC_VAL: data[ptr]++; break;
|
||||
case OP_DEC_VAL: data[ptr]--; break;
|
||||
case OP_OUT: _putc(data[ptr]); break; // TODO: check output: data[ptr]
|
||||
case OP_OUT: output[noutput ++] = data[ptr]; break;
|
||||
case OP_IN: data[ptr] = *(input ++); break;
|
||||
case OP_JMP_FWD: if(!data[ptr]) { pc = PROGRAM[pc].operand; } break;
|
||||
case OP_JMP_BCK: if(data[ptr]) { pc = PROGRAM[pc].operand; } break;
|
||||
|
@ -117,7 +120,7 @@ int execute_bf() {
|
|||
}
|
||||
pc++;
|
||||
}
|
||||
return ptr != DATA_SIZE ? SUCCESS : FAILURE;
|
||||
return checksum(output, output + noutput);
|
||||
}
|
||||
|
||||
|
||||
|
@ -128,17 +131,22 @@ void bench_bf_prepare() {
|
|||
data = bench_alloc(sizeof(data[0]) * DATA_SIZE);
|
||||
code = CODE;
|
||||
input = bench_alloc(ARR_SIZE + 1);
|
||||
output = bench_alloc(DATA_SIZE);
|
||||
noutput = 0;
|
||||
|
||||
srand(1);
|
||||
for (int i = 0; i < ARR_SIZE; i ++) {
|
||||
input[i] = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"[rand() % 62];
|
||||
}
|
||||
}
|
||||
|
||||
u32 retval;
|
||||
|
||||
void bench_bf_run() {
|
||||
compile_bf();
|
||||
execute_bf();
|
||||
retval = execute_bf();
|
||||
}
|
||||
|
||||
const char * bench_bf_validate() {
|
||||
return NULL;
|
||||
return (noutput == ARR_SIZE && retval == CHECKSUM) ? NULL : "wrong answer";
|
||||
}
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
#include <benchmark.h>
|
||||
|
||||
#define N 100000
|
||||
#define CHECKSUM 0xf9404918
|
||||
int *data;
|
||||
|
||||
void bench_qsort_prepare() {
|
||||
|
@ -37,8 +38,5 @@ void bench_qsort_run() {
|
|||
}
|
||||
|
||||
const char * bench_qsort_validate() {
|
||||
for (int i = 1; i < N; i ++) {
|
||||
if (data[i - 1] > data[i]) return "Order violation";
|
||||
}
|
||||
return NULL;
|
||||
return checksum(data, data + N) == CHECKSUM ? NULL : "wrong checksum";
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue