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