[2c7eee0] | 1 | |
---|
| 2 | #include "rq_bench.hpp" |
---|
[69d1748] | 3 | #include <libfibre/fibre.h> |
---|
[2c7eee0] | 4 | |
---|
| 5 | struct Partner { |
---|
| 6 | unsigned long long count = 0; |
---|
| 7 | unsigned long long blocks = 0; |
---|
| 8 | bench_sem self; |
---|
| 9 | bench_sem * next; |
---|
| 10 | }; |
---|
| 11 | |
---|
| 12 | void partner_main( Partner * self ) { |
---|
| 13 | self->count = 0; |
---|
| 14 | for(;;) { |
---|
| 15 | self->blocks += self->self.wait(); |
---|
| 16 | self->next->post(); |
---|
| 17 | self->count ++; |
---|
| 18 | if( clock_mode && stop) break; |
---|
| 19 | if(!clock_mode && self->count >= stop_count) break; |
---|
| 20 | } |
---|
| 21 | |
---|
| 22 | __atomic_fetch_add(&threads_left, -1, __ATOMIC_SEQ_CST); |
---|
| 23 | } |
---|
| 24 | |
---|
| 25 | int main(int argc, char * argv[]) { |
---|
| 26 | unsigned ring_size = 2; |
---|
| 27 | option_t opt[] = { |
---|
| 28 | BENCH_OPT, |
---|
| 29 | { 'r', "ringsize", "Number of threads in a cycle", ring_size } |
---|
| 30 | }; |
---|
[c4241b6] | 31 | BENCH_OPT_PARSE("libfibre cycle benchmark"); |
---|
[2c7eee0] | 32 | |
---|
| 33 | { |
---|
| 34 | unsigned long long global_counter = 0; |
---|
| 35 | unsigned long long global_blocks = 0; |
---|
| 36 | unsigned tthreads = nthreads * ring_size; |
---|
| 37 | uint64_t start, end; |
---|
| 38 | FibreInit(1, nprocs); |
---|
| 39 | { |
---|
| 40 | threads_left = tthreads; |
---|
[1f950c3b] | 41 | Fibre ** threads = new Fibre *[tthreads](); |
---|
| 42 | Partner* thddata = new Partner[tthreads](); |
---|
[ebb6158] | 43 | for(unsigned i = 0; i < tthreads; i++) { |
---|
[2c7eee0] | 44 | unsigned pi = (i + nthreads) % tthreads; |
---|
| 45 | thddata[i].next = &thddata[pi].self; |
---|
| 46 | } |
---|
[ebb6158] | 47 | for(unsigned i = 0; i < tthreads; i++) { |
---|
[3b80db8] | 48 | threads[i] = new Fibre(); |
---|
| 49 | threads[i]->run( partner_main, &thddata[i] ); |
---|
[2c7eee0] | 50 | } |
---|
| 51 | printf("Starting\n"); |
---|
| 52 | |
---|
| 53 | bool is_tty = isatty(STDOUT_FILENO); |
---|
[e54d0c3] | 54 | start = timeHiRes(); |
---|
[2c7eee0] | 55 | |
---|
[ebb6158] | 56 | for(unsigned i = 0; i < nthreads; i++) { |
---|
[2c7eee0] | 57 | thddata[i].self.post(); |
---|
| 58 | } |
---|
[69d1748] | 59 | wait<Fibre>(start, is_tty); |
---|
[2c7eee0] | 60 | |
---|
| 61 | stop = true; |
---|
[e54d0c3] | 62 | end = timeHiRes(); |
---|
[2c7eee0] | 63 | printf("\nDone\n"); |
---|
| 64 | |
---|
[ebb6158] | 65 | for(unsigned i = 0; i < tthreads; i++) { |
---|
[69d1748] | 66 | thddata[i].self.post(); |
---|
[2c7eee0] | 67 | fibre_join( threads[i], nullptr ); |
---|
| 68 | global_counter += thddata[i].count; |
---|
| 69 | global_blocks += thddata[i].blocks; |
---|
| 70 | } |
---|
[8fe35be] | 71 | |
---|
| 72 | delete[](threads); |
---|
| 73 | delete[](thddata); |
---|
[2c7eee0] | 74 | } |
---|
| 75 | |
---|
| 76 | printf("Duration (ms) : %'ld\n", to_miliseconds(end - start)); |
---|
| 77 | printf("Number of processors : %'d\n", nprocs); |
---|
| 78 | printf("Number of threads : %'d\n", tthreads); |
---|
| 79 | printf("Cycle size (# thrds) : %'d\n", ring_size); |
---|
| 80 | printf("Total Operations(ops): %'15llu\n", global_counter); |
---|
| 81 | printf("Total blocks : %'15llu\n", global_blocks); |
---|
| 82 | printf("Ops per second : %'18.2lf\n", ((double)global_counter) / to_fseconds(end - start)); |
---|
| 83 | printf("ns per ops : %'18.2lf\n", ((double)(end - start)) / global_counter); |
---|
| 84 | printf("Ops per threads : %'15llu\n", global_counter / tthreads); |
---|
| 85 | printf("Ops per procs : %'15llu\n", global_counter / nprocs); |
---|
| 86 | printf("Ops/sec/procs : %'18.2lf\n", (((double)global_counter) / nprocs) / to_fseconds(end - start)); |
---|
| 87 | printf("ns per ops/procs : %'18.2lf\n", ((double)(end - start)) / (global_counter / nprocs)); |
---|
| 88 | fflush(stdout); |
---|
| 89 | } |
---|
| 90 | |
---|
| 91 | return 0; |
---|
[e54d0c3] | 92 | } |
---|