source: benchmark/readyQ/yield.cfa @ 8834751

ADTarm-ehast-experimentalenumforall-pointer-decayjacob/cs343-translationnew-astnew-ast-unique-exprpthread-emulationqualifiedEnum
Last change on this file since 8834751 was 2649ff9, checked in by Thierry Delisle <tdelisle@…>, 4 years ago

Added simple yielding benchmark

  • Property mode set to 100644
File size: 3.2 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4#include <limits.h>
5
6extern "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
18extern bool traceHeapOn();
19
20
21volatile bool run = false;
22volatile unsigned long long global_counter;
23
24thread Yielder {
25        unsigned long long counter;
26};
27void ?{}( Yielder & this ) {
28        this.counter = 0;
29        ((thread&)this){ "Yielder Thread", *the_benchmark_cluster };
30}
31
32void 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
43int 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];
115                                printf("Starting\n");
116                                start = getTime();
117                                run = true;
118
119                                for(i; nthreads) {
120                                        unpark( threads[i] __cfaabi_dbg_ctx2 );
121                                }
122                                do {
123                                        sleep(500`ms);
124                                        end = getTime();
125                                } while( (end - start) < duration`s );
126
127                                run = false;
128                                end = getTime();
129                                printf("Done\n");
130                        }
131                }
132
133                printf("Took %'ld ms\n", (end - start)`ms);
134                printf("Total yields      : %'15llu\n", global_counter);
135                printf("yields per second : %'18.2lf\n", ((double)global_counter) / (end - start)`s);
136                printf("ns per yields     : %'18.2lf\n", ((double)(end - start)`ns) / global_counter);
137        }
138}
Note: See TracBrowser for help on using the repository browser.