1 | #pragma once |
---|
2 | |
---|
3 | #include <assert.h> |
---|
4 | #include <kernel.hfa> |
---|
5 | #include <thread.hfa> |
---|
6 | #include <stats.hfa> |
---|
7 | |
---|
8 | #define BENCH_OPT_SHORT "d:p:t:SP" |
---|
9 | #define BENCH_OPT_LONG \ |
---|
10 | {"duration", required_argument, 0, 'd'}, \ |
---|
11 | {"nthreads", required_argument, 0, 't'}, \ |
---|
12 | {"nprocs", required_argument, 0, 'p'}, \ |
---|
13 | {"nostats", no_argument , 0, 'S'}, \ |
---|
14 | {"procstat", no_argument , 0, 'P'}, |
---|
15 | |
---|
16 | #define BENCH_DECL \ |
---|
17 | double duration = 5; \ |
---|
18 | int nprocs = 1; \ |
---|
19 | int nthreads = 1; |
---|
20 | |
---|
21 | #define BENCH_OPT_CASE \ |
---|
22 | case 'd': \ |
---|
23 | duration = strtod(arg, &end); \ |
---|
24 | if(*end != '\0') { \ |
---|
25 | fprintf(stderr, "Duration must be a valid double, was %s\n", arg); \ |
---|
26 | goto usage; \ |
---|
27 | } \ |
---|
28 | break; \ |
---|
29 | case 't': \ |
---|
30 | nthreads = strtoul(arg, &end, 10); \ |
---|
31 | if(*end != '\0' || nthreads < 1) { \ |
---|
32 | fprintf(stderr, "Number of threads must be a positive integer, was %s\n", arg); \ |
---|
33 | goto usage; \ |
---|
34 | } \ |
---|
35 | break; \ |
---|
36 | case 'p': \ |
---|
37 | nprocs = strtoul(arg, &end, 10); \ |
---|
38 | if(*end != '\0' || nprocs < 1) { \ |
---|
39 | fprintf(stderr, "Number of processors must be a positive integer, was %s\n", arg); \ |
---|
40 | goto usage; \ |
---|
41 | } \ |
---|
42 | break; \ |
---|
43 | case 'S': \ |
---|
44 | silent = true; \ |
---|
45 | break; \ |
---|
46 | case 'P': \ |
---|
47 | procstats = true; \ |
---|
48 | break; |
---|
49 | |
---|
50 | bool silent = false; |
---|
51 | bool procstats = false; |
---|
52 | struct cluster * the_benchmark_cluster = 0p; |
---|
53 | struct BenchCluster { |
---|
54 | cluster self; |
---|
55 | }; |
---|
56 | |
---|
57 | void ?{}( BenchCluster & this, int flags, int stats ) { |
---|
58 | (this.self){ "Benchmark Cluster", flags }; |
---|
59 | |
---|
60 | assert( the_benchmark_cluster == 0p ); |
---|
61 | the_benchmark_cluster = &this.self; |
---|
62 | |
---|
63 | #if !defined(__CFA_NO_STATISTICS__) |
---|
64 | if( !silent ) { |
---|
65 | print_stats_at_exit( this.self, stats ); |
---|
66 | } |
---|
67 | #endif |
---|
68 | } |
---|
69 | |
---|
70 | struct BenchProc { |
---|
71 | processor self; |
---|
72 | }; |
---|
73 | |
---|
74 | void ?{}( BenchProc & this ) { |
---|
75 | assert( the_benchmark_cluster != 0p ); |
---|
76 | (this.self){ "Benchmark Processor", *the_benchmark_cluster }; |
---|
77 | |
---|
78 | #if !defined(__CFA_NO_STATISTICS__) |
---|
79 | if( procstats ) { |
---|
80 | print_stats_at_exit( this.self, the_benchmark_cluster->print_stats ); |
---|
81 | } |
---|
82 | #endif |
---|
83 | } |
---|
84 | |
---|
85 | void wait(double duration, Time & start, Time & end, bool is_tty) { |
---|
86 | for() { |
---|
87 | sleep(100`ms); |
---|
88 | end = getTimeNsec(); |
---|
89 | Duration delta = end - start; |
---|
90 | if(is_tty) { |
---|
91 | printf("\r%.1f", delta`ds); |
---|
92 | fflush(stdout); |
---|
93 | } |
---|
94 | if( delta >= duration`s ) { |
---|
95 | break; |
---|
96 | } |
---|
97 | } |
---|
98 | } |
---|
99 | |
---|
100 | void bench_usage( char * argv [] ) { |
---|
101 | fprintf( stderr, "Usage: %s : [options]\n", argv[0] ); |
---|
102 | fprintf( stderr, "\n" ); |
---|
103 | fprintf( stderr, " -d, --duration=DURATION Duration of the experiment, in seconds\n" ); |
---|
104 | fprintf( stderr, " -t, --nthreads=NTHREADS Number of user threads\n" ); |
---|
105 | fprintf( stderr, " -p, --nprocs=NPROCS Number of kernel threads\n" ); |
---|
106 | fprintf( stderr, " -S, --nostats Don't print cluster stats\n" ); |
---|
107 | fprintf( stderr, " -P, --procstat Print processor stats" ); |
---|
108 | } |
---|