[b35ab2d] | 1 | #include <clock.hfa> |
---|
| 2 | #include <kernel.hfa> |
---|
| 3 | #include <parseargs.hfa> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <stdlib.hfa> |
---|
[1d5deea] | 6 | #include <stats.hfa> |
---|
[b35ab2d] | 7 | #include <thread.hfa> |
---|
| 8 | #include <time.hfa> |
---|
| 9 | #include <unistd.h> |
---|
| 10 | |
---|
| 11 | volatile bool stop = false; |
---|
| 12 | bool clock_mode; |
---|
| 13 | double duration = -1; |
---|
| 14 | unsigned long long stop_count = 0; |
---|
| 15 | unsigned nprocs = 1; |
---|
| 16 | unsigned nthreads = 1; |
---|
| 17 | |
---|
| 18 | volatile unsigned long long threads_left; |
---|
| 19 | |
---|
[0b84b15] | 20 | #define thread_loop for(this.count = 0;; this.count++) |
---|
[b35ab2d] | 21 | |
---|
| 22 | #define BENCH_OPT \ |
---|
| 23 | {'d', "duration", "Duration of the experiments in seconds", duration }, \ |
---|
| 24 | {'i', "iterations", "Number of iterations of the experiments", stop_count }, \ |
---|
| 25 | {'t', "nthreads", "Number of threads to use", nthreads }, \ |
---|
| 26 | {'p', "nprocs", "Number of processors to use", nprocs } |
---|
| 27 | |
---|
| 28 | #define BENCH_OPT_PARSE(name) \ |
---|
| 29 | { \ |
---|
| 30 | int opt_cnt = sizeof(opt) / sizeof(cfa_option); \ |
---|
| 31 | char **left; \ |
---|
| 32 | parse_args( argc, argv, opt, opt_cnt, "[OPTIONS]...\n" name, left ); \ |
---|
| 33 | if(duration > 0 && stop_count > 0) { \ |
---|
| 34 | fprintf(stderr, "--duration and --iterations cannot be used together\n"); \ |
---|
| 35 | print_args_usage(argc, argv, opt, opt_cnt, "[OPTIONS]...\n" name, true); \ |
---|
| 36 | } else if(duration > 0) { \ |
---|
| 37 | clock_mode = true; \ |
---|
| 38 | stop_count = 0xFFFFFFFFFFFFFFFF; \ |
---|
[0b84b15] | 39 | printf("Running for %lf seconds\n", duration); \ |
---|
[b35ab2d] | 40 | } else if(stop_count > 0) { \ |
---|
| 41 | clock_mode = false; \ |
---|
[06573b2] | 42 | printf("Running for %llu iterations\n", stop_count); \ |
---|
[b35ab2d] | 43 | } else { \ |
---|
| 44 | duration = 5; clock_mode = true;\ |
---|
[0b84b15] | 45 | printf("Running for %lf seconds\n", duration); \ |
---|
[b35ab2d] | 46 | } \ |
---|
| 47 | } |
---|
| 48 | |
---|
| 49 | struct cluster & bench_cluster; |
---|
| 50 | |
---|
| 51 | struct BenchCluster { |
---|
| 52 | cluster cl; |
---|
| 53 | processor * procs; |
---|
| 54 | unsigned nprocs; |
---|
| 55 | }; |
---|
| 56 | |
---|
| 57 | void ?{}( BenchCluster & this, unsigned nprocs ) { |
---|
| 58 | (this.cl){ "Benchmark Cluster" }; |
---|
| 59 | &bench_cluster = &this.cl; |
---|
| 60 | this.nprocs = nprocs; |
---|
| 61 | this.procs = alloc( this.nprocs ); |
---|
| 62 | for(i; this.nprocs){ |
---|
| 63 | processor * p = &this.procs[i]; |
---|
| 64 | (*p){ "Benchmark Processor", this.cl }; |
---|
| 65 | } |
---|
[1d5deea] | 66 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 67 | print_stats_at_exit( this.cl, CFA_STATS_READY_Q ); |
---|
| 68 | #endif |
---|
[b35ab2d] | 69 | } |
---|
| 70 | |
---|
| 71 | void ^?{}( BenchCluster & this ) { |
---|
[2c7eee0] | 72 | adelete( this.procs ); |
---|
[b35ab2d] | 73 | ^(this.cl){}; |
---|
| 74 | } |
---|
| 75 | |
---|
[0b84b15] | 76 | void wait(const Time & start, bool is_tty) { |
---|
[b35ab2d] | 77 | for() { |
---|
| 78 | sleep(100`ms); |
---|
[e54d0c3] | 79 | Time end = timeHiRes(); |
---|
[b35ab2d] | 80 | Duration delta = end - start; |
---|
| 81 | if(is_tty) { |
---|
| 82 | printf(" %.1f\r", delta`ds); |
---|
| 83 | fflush(stdout); |
---|
| 84 | } |
---|
| 85 | if( clock_mode && delta >= duration`s ) { |
---|
| 86 | break; |
---|
| 87 | } |
---|
| 88 | else if( !clock_mode && threads_left == 0 ) { |
---|
| 89 | break; |
---|
| 90 | } |
---|
| 91 | } |
---|
[2c7eee0] | 92 | } |
---|
| 93 | |
---|
[b5d51b0] | 94 | struct __attribute__((aligned(128))) bench_sem { |
---|
[2c7eee0] | 95 | struct $thread * volatile ptr; |
---|
| 96 | }; |
---|
| 97 | |
---|
| 98 | static inline { |
---|
| 99 | void ?{}(bench_sem & this) { |
---|
| 100 | this.ptr = 0p; |
---|
| 101 | } |
---|
| 102 | |
---|
| 103 | void ^?{}(bench_sem & this) {} |
---|
| 104 | |
---|
| 105 | bool wait(bench_sem & this) { |
---|
| 106 | for() { |
---|
| 107 | struct $thread * expected = this.ptr; |
---|
| 108 | if(expected == 1p) { |
---|
| 109 | if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
| 110 | return false; |
---|
| 111 | } |
---|
| 112 | } |
---|
| 113 | else { |
---|
| 114 | /* paranoid */ verify( expected == 0p ); |
---|
| 115 | if(__atomic_compare_exchange_n(&this.ptr, &expected, active_thread(), false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
| 116 | park(); |
---|
| 117 | return true; |
---|
| 118 | } |
---|
| 119 | } |
---|
| 120 | |
---|
| 121 | } |
---|
| 122 | } |
---|
| 123 | |
---|
| 124 | bool post(bench_sem & this) { |
---|
| 125 | for() { |
---|
| 126 | struct $thread * expected = this.ptr; |
---|
| 127 | if(expected == 1p) return false; |
---|
| 128 | if(expected == 0p) { |
---|
| 129 | if(__atomic_compare_exchange_n(&this.ptr, &expected, 1p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
| 130 | return false; |
---|
| 131 | } |
---|
| 132 | } |
---|
| 133 | else { |
---|
| 134 | if(__atomic_compare_exchange_n(&this.ptr, &expected, 0p, false, __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST)) { |
---|
| 135 | unpark( expected ); |
---|
| 136 | return true; |
---|
| 137 | } |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | } |
---|
[e54d0c3] | 141 | } |
---|