[12bb5ab1] | 1 | #include "rq_bench.hpp" |
---|
| 2 | #include <libfibre/fibre.h> |
---|
| 3 | |
---|
| 4 | unsigned spot_cnt = 2; |
---|
| 5 | FredSemaphore * spots; |
---|
| 6 | |
---|
| 7 | struct Churner { |
---|
| 8 | unsigned long long count = 0; |
---|
| 9 | unsigned long long blocks = 0; |
---|
| 10 | bool skip = false; |
---|
| 11 | __lehmer64_state_t seed; |
---|
| 12 | bench_sem self; |
---|
| 13 | }; |
---|
| 14 | |
---|
| 15 | void churner_main( Churner * self ) { |
---|
| 16 | fibre_park(); |
---|
| 17 | for(;;) { |
---|
| 18 | unsigned r = __lehmer64( self->seed ); |
---|
| 19 | FredSemaphore & sem = spots[r % spot_cnt]; |
---|
| 20 | if(!self->skip) sem.V(); |
---|
| 21 | self->blocks += sem.P() == SemaphoreWasOpen ? 1 : 0; |
---|
| 22 | self->skip = false; |
---|
| 23 | |
---|
| 24 | self->count ++; |
---|
| 25 | if( clock_mode && stop) break; |
---|
| 26 | if(!clock_mode && self->count >= stop_count) break; |
---|
| 27 | } |
---|
| 28 | |
---|
| 29 | __atomic_fetch_add(&threads_left, -1, __ATOMIC_SEQ_CST); |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | int main(int argc, char * argv[]) { |
---|
| 33 | option_t opt[] = { |
---|
| 34 | BENCH_OPT, |
---|
| 35 | { 's', "spots", "Number of spots in the system", spot_cnt } |
---|
| 36 | }; |
---|
| 37 | BENCH_OPT_PARSE("libfibre churn benchmark"); |
---|
| 38 | |
---|
| 39 | { |
---|
| 40 | unsigned long long global_counter = 0; |
---|
| 41 | unsigned long long global_blocks = 0; |
---|
| 42 | uint64_t start, end; |
---|
| 43 | FibreInit(1, nprocs ); |
---|
| 44 | { |
---|
| 45 | spots = new FredSemaphore[spot_cnt](); |
---|
| 46 | |
---|
| 47 | threads_left = nthreads; |
---|
| 48 | Churner * thddata = new Churner[nthreads](); |
---|
| 49 | for(unsigned i = 0; i < nthreads; i++ ) { |
---|
| 50 | Churner & t = thddata[i]; |
---|
| 51 | t.skip = i < spot_cnt; |
---|
| 52 | t.seed = rand(); |
---|
| 53 | } |
---|
| 54 | Fibre * threads[nthreads]; |
---|
| 55 | for(unsigned i = 0; i < nthreads; i++) { |
---|
| 56 | threads[i] = new Fibre( reinterpret_cast<void (*)(void *)>(churner_main), &thddata[i] ); |
---|
| 57 | } |
---|
| 58 | printf("Starting\n"); |
---|
| 59 | |
---|
| 60 | bool is_tty = isatty(STDOUT_FILENO); |
---|
| 61 | start = timeHiRes(); |
---|
| 62 | |
---|
| 63 | for(unsigned i = 0; i < nthreads; i++ ) { |
---|
| 64 | fibre_unpark( threads[i] ); |
---|
| 65 | } |
---|
| 66 | wait<Fibre>(start, is_tty); |
---|
| 67 | |
---|
| 68 | stop = true; |
---|
| 69 | end = timeHiRes(); |
---|
| 70 | printf("\nDone\n"); |
---|
| 71 | |
---|
| 72 | for(unsigned i = 0; i < spot_cnt; i++) { |
---|
| 73 | for(int j = 0; j < 10000; j++) spots[i].V(); |
---|
| 74 | } |
---|
| 75 | |
---|
| 76 | for(unsigned i = 0; i < nthreads; i++ ) { |
---|
| 77 | fibre_join( threads[i], nullptr ); |
---|
| 78 | global_counter += thddata[i].count; |
---|
| 79 | global_blocks += thddata[i].blocks; |
---|
| 80 | } |
---|
| 81 | |
---|
| 82 | delete[](spots); |
---|
| 83 | delete[](thddata); |
---|
| 84 | } |
---|
| 85 | |
---|
| 86 | printf("\nDone2\n"); |
---|
| 87 | |
---|
| 88 | printf("Duration (ms) : %'ld\n", to_miliseconds(end - start)); |
---|
| 89 | printf("Number of processors : %'d\n", nprocs); |
---|
| 90 | printf("Number of threads : %'d\n", nthreads); |
---|
| 91 | printf("Number of spots : %'d\n", spot_cnt); |
---|
| 92 | printf("Total Operations(ops): %'15llu\n", global_counter); |
---|
| 93 | printf("Total blocks : %'15llu\n", global_blocks); |
---|
| 94 | printf("Ops per second : %'18.2lf\n", ((double)global_counter) / to_fseconds(end - start)); |
---|
| 95 | printf("ns per ops : %'18.2lf\n", ((double)(end - start)) / global_counter); |
---|
| 96 | printf("Ops per threads : %'15llu\n", global_counter / nthreads); |
---|
| 97 | printf("Ops per procs : %'15llu\n", global_counter / nprocs); |
---|
| 98 | printf("Ops/sec/procs : %'18.2lf\n", (((double)global_counter) / nprocs) / to_fseconds(end - start)); |
---|
| 99 | printf("ns per ops/procs : %'18.2lf\n", ((double)(end - start)) / (global_counter / nprocs)); |
---|
| 100 | fflush(stdout); |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | return 0; |
---|
| 104 | |
---|
| 105 | } |
---|