| 1 | #pragma once
 | 
|---|
| 2 | #include <assert.h>
 | 
|---|
| 3 | #include <stdint.h>
 | 
|---|
| 4 | 
 | 
|---|
| 5 | #ifdef __cforall
 | 
|---|
| 6 |         #include <kernel.hfa>
 | 
|---|
| 7 |         #include <thread.hfa>
 | 
|---|
| 8 |         #include <stats.hfa>
 | 
|---|
| 9 | #else
 | 
|---|
| 10 | #include <time.h>                                                                               // timespec
 | 
|---|
| 11 | #include <sys/time.h>                                                                   // timeval
 | 
|---|
| 12 | 
 | 
|---|
| 13 | enum { TIMEGRAN = 1000000000LL };                                       // nanosecond granularity, except for timeval
 | 
|---|
| 14 | #endif
 | 
|---|
| 15 | 
 | 
|---|
| 16 | #define BENCH_OPT_SHORT "d:p:t:SPV"
 | 
|---|
| 17 | #define BENCH_OPT_LONG \
 | 
|---|
| 18 |         {"duration",     required_argument, 0, 'd'}, \
 | 
|---|
| 19 |         {"nthreads",     required_argument, 0, 't'}, \
 | 
|---|
| 20 |         {"nprocs",       required_argument, 0, 'p'}, \
 | 
|---|
| 21 |         {"nostats",      no_argument      , 0, 'S'}, \
 | 
|---|
| 22 |         {"procstat",     no_argument      , 0, 'P'}, \
 | 
|---|
| 23 |         {"viewhalts",    no_argument      , 0, 'V'},
 | 
|---|
| 24 | 
 | 
|---|
| 25 | #define BENCH_OPT_CASE \
 | 
|---|
| 26 |         case 'd': \
 | 
|---|
| 27 |                 duration = strtod(arg, &end); \
 | 
|---|
| 28 |                 if(*end != '\0') { \
 | 
|---|
| 29 |                         fprintf(stderr, "Duration must be a valid double, was %s\n", arg); \
 | 
|---|
| 30 |                         goto usage; \
 | 
|---|
| 31 |                 } \
 | 
|---|
| 32 |                 break; \
 | 
|---|
| 33 |         case 't': \
 | 
|---|
| 34 |                 nthreads = strtoul(arg, &end, 10); \
 | 
|---|
| 35 |                 if(*end != '\0' || nthreads < 1) { \
 | 
|---|
| 36 |                         fprintf(stderr, "Number of threads must be a positive integer, was %s\n", arg); \
 | 
|---|
| 37 |                         goto usage; \
 | 
|---|
| 38 |                 } \
 | 
|---|
| 39 |                 break; \
 | 
|---|
| 40 |         case 'p': \
 | 
|---|
| 41 |                 nprocs = strtoul(arg, &end, 10); \
 | 
|---|
| 42 |                 if(*end != '\0' || nprocs < 1) { \
 | 
|---|
| 43 |                         fprintf(stderr, "Number of processors must be a positive integer, was %s\n", arg); \
 | 
|---|
| 44 |                         goto usage; \
 | 
|---|
| 45 |                 } \
 | 
|---|
| 46 |                 break; \
 | 
|---|
| 47 |         case 'S': \
 | 
|---|
| 48 |                 silent = true; \
 | 
|---|
| 49 |                 break; \
 | 
|---|
| 50 |         case 'P': \
 | 
|---|
| 51 |                 procstats = true; \
 | 
|---|
| 52 |                 break; \
 | 
|---|
| 53 |         case 'V': \
 | 
|---|
| 54 |                 viewhalts = true; \
 | 
|---|
| 55 |                 break;
 | 
|---|
| 56 | 
 | 
|---|
| 57 | double duration = 5;
 | 
|---|
| 58 | int nprocs = 1;
 | 
|---|
| 59 | int nthreads = 1;
 | 
|---|
| 60 | bool silent = false;
 | 
|---|
| 61 | bool continuous = false;
 | 
|---|
| 62 | bool procstats = false;
 | 
|---|
| 63 | bool viewhalts = false;
 | 
|---|
| 64 | 
 | 
|---|
| 65 | #define BENCH_OPT_CFA \
 | 
|---|
| 66 |         {'d', "duration",  "Duration of the experiments in seconds", duration }, \
 | 
|---|
| 67 |         {'t', "nthreads",  "Number of threads to use", nthreads }, \
 | 
|---|
| 68 |         {'p', "nprocs",    "Number of processors to use", nprocs }, \
 | 
|---|
| 69 |         {'S', "nostats",   "Don't print statistics", silent, parse_settrue }, \
 | 
|---|
| 70 |         {'C', "constats",  "Regularly print statistics", continuous, parse_settrue }, \
 | 
|---|
| 71 |         {'P', "procstat",  "Print statistics for each processors", procstats, parse_settrue }, \
 | 
|---|
| 72 |         {'V', "viewhalts", "Visualize halts, prints timestamp and Processor id for each halt.", viewhalts, parse_settrue },
 | 
|---|
| 73 | 
 | 
|---|
| 74 | #ifdef __cforall
 | 
|---|
| 75 | #include <parseargs.hfa>
 | 
|---|
| 76 | 
 | 
|---|
| 77 | struct cluster * the_benchmark_cluster = 0p;
 | 
|---|
| 78 | struct BenchCluster {
 | 
|---|
| 79 |         cluster self;
 | 
|---|
| 80 | };
 | 
|---|
| 81 | 
 | 
|---|
| 82 | void ?{}( BenchCluster & this, int num_io, const io_context_params & io_params, int stats ) {
 | 
|---|
| 83 |         (this.self){ "Benchmark Cluster", num_io, io_params };
 | 
|---|
| 84 | 
 | 
|---|
| 85 |         assert( the_benchmark_cluster == 0p );
 | 
|---|
| 86 |         the_benchmark_cluster = &this.self;
 | 
|---|
| 87 | 
 | 
|---|
| 88 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 89 |                 if( !silent ) {
 | 
|---|
| 90 |                         print_stats_at_exit( this.self, stats );
 | 
|---|
| 91 |                 }
 | 
|---|
| 92 |         #endif
 | 
|---|
| 93 | }
 | 
|---|
| 94 | 
 | 
|---|
| 95 | struct BenchProc {
 | 
|---|
| 96 |         processor self;
 | 
|---|
| 97 | };
 | 
|---|
| 98 | 
 | 
|---|
| 99 | void ?{}( BenchProc & this ) {
 | 
|---|
| 100 |         assert( the_benchmark_cluster != 0p );
 | 
|---|
| 101 |         (this.self){ "Benchmark Processor", *the_benchmark_cluster };
 | 
|---|
| 102 | 
 | 
|---|
| 103 |         #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
| 104 |                 if( procstats ) {
 | 
|---|
| 105 |                         print_stats_at_exit( this.self, the_benchmark_cluster->print_stats );
 | 
|---|
| 106 |                 }
 | 
|---|
| 107 |                 if( viewhalts ) {
 | 
|---|
| 108 |                         print_halts( this.self );
 | 
|---|
| 109 |                 }
 | 
|---|
| 110 |         #endif
 | 
|---|
| 111 | }
 | 
|---|
| 112 | 
 | 
|---|
| 113 | void wait(double duration, Time & start, Time & end, bool is_tty) {
 | 
|---|
| 114 |         for() {
 | 
|---|
| 115 |                 sleep(100`ms);
 | 
|---|
| 116 |                 end = timeHiRes();
 | 
|---|
| 117 |                 Duration delta = end - start;
 | 
|---|
| 118 |                 /*if(is_tty)*/ {
 | 
|---|
| 119 |                         printf(" %.1f\r", delta`ds);
 | 
|---|
| 120 |                         fflush(stdout);
 | 
|---|
| 121 |                 }
 | 
|---|
| 122 |                 if( delta >= duration`s ) {
 | 
|---|
| 123 |                         break;
 | 
|---|
| 124 |                 }
 | 
|---|
| 125 |         }
 | 
|---|
| 126 | }
 | 
|---|
| 127 | #else
 | 
|---|
| 128 | uint64_t timeHiRes() {
 | 
|---|
| 129 |         timespec curr;
 | 
|---|
| 130 |         clock_gettime( CLOCK_REALTIME, &curr );
 | 
|---|
| 131 |         return (int64_t)curr.tv_sec * TIMEGRAN + curr.tv_nsec;
 | 
|---|
| 132 | }
 | 
|---|
| 133 | 
 | 
|---|
| 134 | uint64_t to_miliseconds( uint64_t durtn ) { return durtn / (TIMEGRAN / 1000LL); }
 | 
|---|
| 135 | double to_fseconds(uint64_t durtn ) { return durtn / (double)TIMEGRAN; }
 | 
|---|
| 136 | uint64_t from_fseconds(double sec) { return sec * TIMEGRAN; }
 | 
|---|
| 137 | 
 | 
|---|
| 138 | 
 | 
|---|
| 139 | void wait_duration(double duration, uint64_t & start, uint64_t & end, bool is_tty) {
 | 
|---|
| 140 |         for(;;) {
 | 
|---|
| 141 |                 usleep(100000);
 | 
|---|
| 142 |                 end = timeHiRes();
 | 
|---|
| 143 |                 uint64_t delta = end - start;
 | 
|---|
| 144 |                 /*if(is_tty)*/ {
 | 
|---|
| 145 |                         printf(" %.1f\r", to_fseconds(delta));
 | 
|---|
| 146 |                         fflush(stdout);
 | 
|---|
| 147 |                 }
 | 
|---|
| 148 |                 if( delta >= from_fseconds(duration) ) {
 | 
|---|
| 149 |                         break;
 | 
|---|
| 150 |                 }
 | 
|---|
| 151 |         }
 | 
|---|
| 152 | }
 | 
|---|
| 153 | #endif
 | 
|---|
| 154 | 
 | 
|---|
| 155 | 
 | 
|---|
| 156 | void bench_usage( char * argv [] ) {
 | 
|---|
| 157 |         fprintf( stderr, "Usage: %s : [options]\n", argv[0] );
 | 
|---|
| 158 |         fprintf( stderr, "\n" );
 | 
|---|
| 159 |         fprintf( stderr, "  -d, --duration=DURATION  Duration of the experiment, in seconds\n" );
 | 
|---|
| 160 |         fprintf( stderr, "  -t, --nthreads=NTHREADS  Number of user threads\n" );
 | 
|---|
| 161 |         fprintf( stderr, "  -p, --nprocs=NPROCS      Number of kernel threads\n" );
 | 
|---|
| 162 |         fprintf( stderr, "  -S, --nostats            Don't print cluster stats\n" );
 | 
|---|
| 163 |         fprintf( stderr, "  -P, --procstat           Print processor stats" );
 | 
|---|
| 164 | }
 | 
|---|