| 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();
 | 
|---|
| 57 |                                 threads[i]->run(churner_main, &thddata[i]);
 | 
|---|
| 58 |                         }
 | 
|---|
| 59 |                         printf("Starting\n");
 | 
|---|
| 60 | 
 | 
|---|
| 61 |                         bool is_tty = isatty(STDOUT_FILENO);
 | 
|---|
| 62 |                         start = timeHiRes();
 | 
|---|
| 63 | 
 | 
|---|
| 64 |                         for(unsigned i = 0; i < nthreads; i++ ) {
 | 
|---|
| 65 |                                 fibre_unpark( threads[i] );
 | 
|---|
| 66 |                         }
 | 
|---|
| 67 |                         wait<Fibre>(start, is_tty);
 | 
|---|
| 68 | 
 | 
|---|
| 69 |                         stop = true;
 | 
|---|
| 70 |                         end = timeHiRes();
 | 
|---|
| 71 |                         printf("\nDone\n");
 | 
|---|
| 72 | 
 | 
|---|
| 73 |                         for(unsigned i = 0; i < spot_cnt; i++) {
 | 
|---|
| 74 |                                 for(int j = 0; j < 10000; j++) spots[i].V();
 | 
|---|
| 75 |                         }
 | 
|---|
| 76 | 
 | 
|---|
| 77 |                         for(unsigned i = 0; i < nthreads; i++ ) {
 | 
|---|
| 78 |                                 fibre_join( threads[i], nullptr );
 | 
|---|
| 79 |                                 global_counter += thddata[i].count;
 | 
|---|
| 80 |                                 global_blocks  += thddata[i].blocks;
 | 
|---|
| 81 |                         }
 | 
|---|
| 82 | 
 | 
|---|
| 83 |                         delete[](spots);
 | 
|---|
| 84 |                         delete[](thddata);
 | 
|---|
| 85 |                 }
 | 
|---|
| 86 | 
 | 
|---|
| 87 |                 printf("\nDone2\n");
 | 
|---|
| 88 | 
 | 
|---|
| 89 |                 printf("Duration (ms)        : %'ld\n", to_miliseconds(end - start));
 | 
|---|
| 90 |                 printf("Number of processors : %'d\n", nprocs);
 | 
|---|
| 91 |                 printf("Number of threads    : %'d\n", nthreads);
 | 
|---|
| 92 |                 printf("Number of spots      : %'d\n", spot_cnt);
 | 
|---|
| 93 |                 printf("Total Operations(ops): %'15llu\n", global_counter);
 | 
|---|
| 94 |                 printf("Total blocks         : %'15llu\n", global_blocks);
 | 
|---|
| 95 |                 printf("Ops per second       : %'18.2lf\n", ((double)global_counter) / to_fseconds(end - start));
 | 
|---|
| 96 |                 printf("ns per ops           : %'18.2lf\n", ((double)(end - start)) / global_counter);
 | 
|---|
| 97 |                 printf("Ops per threads      : %'15llu\n", global_counter / nthreads);
 | 
|---|
| 98 |                 printf("Ops per procs        : %'15llu\n", global_counter / nprocs);
 | 
|---|
| 99 |                 printf("Ops/sec/procs        : %'18.2lf\n", (((double)global_counter) / nprocs) / to_fseconds(end - start));
 | 
|---|
| 100 |                 printf("ns per ops/procs     : %'18.2lf\n", ((double)(end - start)) / (global_counter / nprocs));
 | 
|---|
| 101 |                 fflush(stdout);
 | 
|---|
| 102 |         }
 | 
|---|
| 103 | 
 | 
|---|
| 104 |         return 0;
 | 
|---|
| 105 | 
 | 
|---|
| 106 | }
 | 
|---|