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_DECL \ |
---|
26 | double duration = 5; \ |
---|
27 | int nprocs = 1; \ |
---|
28 | int nthreads = 1; |
---|
29 | |
---|
30 | #define BENCH_OPT_CASE \ |
---|
31 | case 'd': \ |
---|
32 | duration = strtod(arg, &end); \ |
---|
33 | if(*end != '\0') { \ |
---|
34 | fprintf(stderr, "Duration must be a valid double, was %s\n", arg); \ |
---|
35 | goto usage; \ |
---|
36 | } \ |
---|
37 | break; \ |
---|
38 | case 't': \ |
---|
39 | nthreads = strtoul(arg, &end, 10); \ |
---|
40 | if(*end != '\0' || nthreads < 1) { \ |
---|
41 | fprintf(stderr, "Number of threads must be a positive integer, was %s\n", arg); \ |
---|
42 | goto usage; \ |
---|
43 | } \ |
---|
44 | break; \ |
---|
45 | case 'p': \ |
---|
46 | nprocs = strtoul(arg, &end, 10); \ |
---|
47 | if(*end != '\0' || nprocs < 1) { \ |
---|
48 | fprintf(stderr, "Number of processors must be a positive integer, was %s\n", arg); \ |
---|
49 | goto usage; \ |
---|
50 | } \ |
---|
51 | break; \ |
---|
52 | case 'S': \ |
---|
53 | silent = true; \ |
---|
54 | break; \ |
---|
55 | case 'P': \ |
---|
56 | procstats = true; \ |
---|
57 | break; \ |
---|
58 | case 'V': \ |
---|
59 | viewhalts = true; \ |
---|
60 | break; |
---|
61 | |
---|
62 | bool silent = false; |
---|
63 | bool procstats = false; |
---|
64 | bool viewhalts = false; |
---|
65 | |
---|
66 | #ifdef __cforall |
---|
67 | struct cluster * the_benchmark_cluster = 0p; |
---|
68 | struct BenchCluster { |
---|
69 | cluster self; |
---|
70 | }; |
---|
71 | |
---|
72 | void ?{}( BenchCluster & this, int num_io, const io_context_params & io_params, int stats ) { |
---|
73 | (this.self){ "Benchmark Cluster", num_io, io_params }; |
---|
74 | |
---|
75 | assert( the_benchmark_cluster == 0p ); |
---|
76 | the_benchmark_cluster = &this.self; |
---|
77 | |
---|
78 | #if !defined(__CFA_NO_STATISTICS__) |
---|
79 | if( !silent ) { |
---|
80 | print_stats_at_exit( this.self, stats ); |
---|
81 | } |
---|
82 | #endif |
---|
83 | } |
---|
84 | |
---|
85 | struct BenchProc { |
---|
86 | processor self; |
---|
87 | }; |
---|
88 | |
---|
89 | void ?{}( BenchProc & this ) { |
---|
90 | assert( the_benchmark_cluster != 0p ); |
---|
91 | (this.self){ "Benchmark Processor", *the_benchmark_cluster }; |
---|
92 | |
---|
93 | #if !defined(__CFA_NO_STATISTICS__) |
---|
94 | if( procstats ) { |
---|
95 | print_stats_at_exit( this.self, the_benchmark_cluster->print_stats ); |
---|
96 | } |
---|
97 | if( viewhalts ) { |
---|
98 | print_halts( this.self ); |
---|
99 | } |
---|
100 | #endif |
---|
101 | } |
---|
102 | |
---|
103 | void wait(double duration, Time & start, Time & end, bool is_tty) { |
---|
104 | for() { |
---|
105 | sleep(100`ms); |
---|
106 | end = getTimeNsec(); |
---|
107 | Duration delta = end - start; |
---|
108 | /*if(is_tty)*/ { |
---|
109 | printf(" %.1f\r", delta`ds); |
---|
110 | fflush(stdout); |
---|
111 | } |
---|
112 | if( delta >= duration`s ) { |
---|
113 | break; |
---|
114 | } |
---|
115 | } |
---|
116 | } |
---|
117 | #else |
---|
118 | uint64_t getTimeNsec() { |
---|
119 | timespec curr; |
---|
120 | clock_gettime( CLOCK_REALTIME, &curr ); |
---|
121 | return (int64_t)curr.tv_sec * TIMEGRAN + curr.tv_nsec; |
---|
122 | } |
---|
123 | |
---|
124 | uint64_t to_miliseconds( uint64_t durtn ) { return durtn / (TIMEGRAN / 1000LL); } |
---|
125 | double to_fseconds(uint64_t durtn ) { return durtn / (double)TIMEGRAN; } |
---|
126 | uint64_t from_fseconds(double sec) { return sec * TIMEGRAN; } |
---|
127 | |
---|
128 | |
---|
129 | void wait_duration(double duration, uint64_t & start, uint64_t & end, bool is_tty) { |
---|
130 | for(;;) { |
---|
131 | usleep(100000); |
---|
132 | end = getTimeNsec(); |
---|
133 | uint64_t delta = end - start; |
---|
134 | /*if(is_tty)*/ { |
---|
135 | printf(" %.1f\r", to_fseconds(delta)); |
---|
136 | fflush(stdout); |
---|
137 | } |
---|
138 | if( delta >= from_fseconds(duration) ) { |
---|
139 | break; |
---|
140 | } |
---|
141 | } |
---|
142 | } |
---|
143 | #endif |
---|
144 | |
---|
145 | |
---|
146 | void bench_usage( char * argv [] ) { |
---|
147 | fprintf( stderr, "Usage: %s : [options]\n", argv[0] ); |
---|
148 | fprintf( stderr, "\n" ); |
---|
149 | fprintf( stderr, " -d, --duration=DURATION Duration of the experiment, in seconds\n" ); |
---|
150 | fprintf( stderr, " -t, --nthreads=NTHREADS Number of user threads\n" ); |
---|
151 | fprintf( stderr, " -p, --nprocs=NPROCS Number of kernel threads\n" ); |
---|
152 | fprintf( stderr, " -S, --nostats Don't print cluster stats\n" ); |
---|
153 | fprintf( stderr, " -P, --procstat Print processor stats" ); |
---|
154 | } |
---|