[2649ff9] | 1 | #include <stdlib.h> |
---|
| 2 | #include <stdio.h> |
---|
| 3 | #include <string.h> |
---|
| 4 | #include <limits.h> |
---|
| 5 | |
---|
| 6 | extern "C" { |
---|
| 7 | #include <locale.h> |
---|
| 8 | #include <getopt.h> |
---|
| 9 | } |
---|
| 10 | |
---|
| 11 | #include <unistd.h> |
---|
| 12 | |
---|
| 13 | #include <clock.hfa> |
---|
| 14 | #include <time.hfa> |
---|
| 15 | |
---|
| 16 | #include "../benchcltr.hfa" |
---|
| 17 | |
---|
| 18 | extern bool traceHeapOn(); |
---|
| 19 | |
---|
| 20 | |
---|
| 21 | volatile bool run = false; |
---|
| 22 | volatile unsigned long long global_counter; |
---|
| 23 | |
---|
[04b5cef] | 24 | thread __attribute__((aligned(128))) Yielder { |
---|
[2649ff9] | 25 | unsigned long long counter; |
---|
| 26 | }; |
---|
| 27 | void ?{}( Yielder & this ) { |
---|
| 28 | this.counter = 0; |
---|
| 29 | ((thread&)this){ "Yielder Thread", *the_benchmark_cluster }; |
---|
| 30 | } |
---|
| 31 | |
---|
| 32 | void main( Yielder & this ) { |
---|
| 33 | park( __cfaabi_dbg_ctx ); |
---|
| 34 | /* paranoid */ assert( true == __atomic_load_n(&run, __ATOMIC_RELAXED) ); |
---|
| 35 | |
---|
| 36 | while(__atomic_load_n(&run, __ATOMIC_RELAXED)) { |
---|
| 37 | yield(); |
---|
| 38 | this.counter++; |
---|
| 39 | } |
---|
| 40 | __atomic_fetch_add(&global_counter, this.counter, __ATOMIC_SEQ_CST); |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int main(int argc, char * argv[]) { |
---|
| 44 | double duration = 5; |
---|
| 45 | int nprocs = 1; |
---|
| 46 | int nthreads = 1; |
---|
| 47 | |
---|
| 48 | |
---|
| 49 | |
---|
| 50 | for(;;) { |
---|
| 51 | static struct option options[] = { |
---|
| 52 | {"duration", required_argument, 0, 'd'}, |
---|
| 53 | {"nprocs", required_argument, 0, 'p'}, |
---|
| 54 | {"nthreads", required_argument, 0, 't'}, |
---|
| 55 | {0, 0, 0, 0} |
---|
| 56 | }; |
---|
| 57 | |
---|
| 58 | int idx = 0; |
---|
| 59 | int opt = getopt_long(argc, argv, "d:p:t:", options, &idx); |
---|
| 60 | |
---|
| 61 | char * arg = optarg ? optarg : ""; |
---|
| 62 | size_t len = 0; |
---|
| 63 | char * end; |
---|
| 64 | switch(opt) { |
---|
| 65 | case -1: |
---|
| 66 | goto run; |
---|
| 67 | // Numeric Arguments |
---|
| 68 | case 'd': |
---|
| 69 | duration = strtod(arg, &end); |
---|
| 70 | if(*end != '\0') { |
---|
| 71 | fprintf(stderr, "Duration must be a valid double, was %s\n", arg); |
---|
| 72 | goto usage; |
---|
| 73 | } |
---|
| 74 | break; |
---|
| 75 | case 't': |
---|
| 76 | nthreads = strtoul(arg, &end, 10); |
---|
| 77 | if(*end != '\0' || nthreads < 1) { |
---|
| 78 | fprintf(stderr, "Number of threads must be a positive integer, was %s\n", arg); |
---|
| 79 | goto usage; |
---|
| 80 | } |
---|
| 81 | break; |
---|
| 82 | case 'p': |
---|
| 83 | nprocs = strtoul(arg, &end, 10); |
---|
| 84 | if(*end != '\0' || nprocs < 1) { |
---|
| 85 | fprintf(stderr, "Number of processors must be a positive integer, was %s\n", arg); |
---|
| 86 | goto usage; |
---|
| 87 | } |
---|
| 88 | break; |
---|
| 89 | // Other cases |
---|
| 90 | default: /* ? */ |
---|
| 91 | fprintf( stderr, "Unkown option '%c'\n", opt); |
---|
| 92 | usage: |
---|
| 93 | fprintf( stderr, "Usage: %s [options]\n", argv[0]); |
---|
| 94 | fprintf( stderr, "\n" ); |
---|
| 95 | fprintf( stderr, " -d, --duration=DURATION Duration of the experiment, in seconds\n" ); |
---|
| 96 | fprintf( stderr, " -t, --nthreads=NTHREADS Number of kernel threads\n" ); |
---|
| 97 | fprintf( stderr, " -q, --nqueues=NQUEUES Number of queues per threads\n" ); |
---|
| 98 | exit(1); |
---|
| 99 | } |
---|
| 100 | } |
---|
| 101 | run: |
---|
| 102 | |
---|
| 103 | { |
---|
| 104 | printf("Running %d threads on %d processors for %lf seconds\n", nthreads, nprocs, duration); |
---|
| 105 | |
---|
| 106 | Time start, end; |
---|
| 107 | BenchCluster cl; |
---|
| 108 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 109 | print_stats_at_exit( cl.self ); |
---|
| 110 | #endif |
---|
| 111 | { |
---|
| 112 | BenchProc procs[nprocs]; |
---|
| 113 | { |
---|
| 114 | Yielder threads[nthreads]; |
---|
[04b5cef] | 115 | bool is_tty = isatty(STDOUT_FILENO); |
---|
[2649ff9] | 116 | printf("Starting\n"); |
---|
| 117 | start = getTime(); |
---|
| 118 | run = true; |
---|
| 119 | |
---|
| 120 | for(i; nthreads) { |
---|
| 121 | unpark( threads[i] __cfaabi_dbg_ctx2 ); |
---|
| 122 | } |
---|
[2073d207] | 123 | for() { |
---|
[2649ff9] | 124 | sleep(500`ms); |
---|
| 125 | end = getTime(); |
---|
[2073d207] | 126 | if( (end - start) >= duration`s ) { |
---|
| 127 | break; |
---|
| 128 | } |
---|
| 129 | if(is_tty) { |
---|
| 130 | printf("\r%.4lf", (double)(end - start)`s); |
---|
| 131 | fflush(stdout); |
---|
| 132 | } |
---|
| 133 | } |
---|
[2649ff9] | 134 | |
---|
| 135 | run = false; |
---|
| 136 | end = getTime(); |
---|
| 137 | printf("Done\n"); |
---|
| 138 | } |
---|
| 139 | } |
---|
| 140 | |
---|
| 141 | printf("Took %'ld ms\n", (end - start)`ms); |
---|
[2073d207] | 142 | printf("Total yields : %'15llu\n", global_counter); |
---|
| 143 | printf("Yields per procs : %'15llu\n", global_counter / nprocs); |
---|
| 144 | printf("Yields per second : %'18.2lf\n", ((double)global_counter) / (end - start)`s); |
---|
| 145 | printf("Yields/sec/procs : %'18.2lf\n", (((double)global_counter) / nprocs) / (end - start)`s); |
---|
| 146 | printf("ns per yields : %'18.2lf\n", ((double)(end - start)`ns) / global_counter); |
---|
| 147 | printf("ns per yields/procs : %'18.2lf\n", ((double)(end - start)`ns) / (global_counter / nprocs)); |
---|
| 148 | |
---|
[2649ff9] | 149 | } |
---|
| 150 | } |
---|