source: benchmark/io/readv.cfa @ b6f2b21

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

Added the option to dynamically (at cluster creation time) enable/disable the user thread polling of I/O

  • Property mode set to 100644
File size: 4.0 KB
Line 
1#include <stdlib.h>
2#include <stdio.h>
3#include <string.h>
4
5extern "C" {
6        #include <locale.h>
7        #include <getopt.h>
8        #include <fcntl.h>
9        #include <sys/uio.h>
10}
11
12#include <unistd.h>
13
14#include <clock.hfa>
15#include <kernel.hfa>
16#include <thread.hfa>
17#include <time.hfa>
18
19extern bool traceHeapOn();
20extern ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
21
22int fd;
23volatile bool run = false;
24volatile size_t count = 0;
25
26unsigned long int buflen = 50;
27
28cluster * the_cluster;
29
30thread Reader {};
31void ?{}( Reader & this ) {
32        ((thread&)this){ "Reader Thread", *the_cluster };
33}
34
35struct my_processor {
36        processor p;
37};
38
39void ?{}( my_processor & this ) {
40        (this.p){ "I/O Processor", *the_cluster };
41}
42
43void main( Reader & ) {
44        while(!__atomic_load_n(&run, __ATOMIC_RELAXED)) yield();
45
46        char data[buflen];
47        struct iovec iov = { data, buflen };
48
49        while(__atomic_load_n(&run, __ATOMIC_RELAXED)) {
50                int r = cfa_preadv2(fd, &iov, 1, 0, 0);
51                if(r < 0) abort(strerror(-r));
52
53                __atomic_fetch_add( &count, 1, __ATOMIC_SEQ_CST );
54        }
55}
56
57int main(int argc, char * argv[]) {
58        double duration   = 5.0;
59        unsigned long int nthreads = 2;
60        unsigned long int nprocs   = 1;
61        int flags = 0;
62
63        printf("Setting local\n");
64        setlocale(LC_NUMERIC, "");
65
66        arg_loop:
67        for(;;) {
68                static struct option options[] = {
69                        {"duration",   required_argument, 0, 'd'},
70                        {"nthreads",   required_argument, 0, 't'},
71                        {"nprocs",     required_argument, 0, 'p'},
72                        {"bufsize",    required_argument, 0, 'b'},
73                        {"userthread", no_argument      , 0, 'u'},
74                        {0, 0, 0, 0}
75                };
76
77                int idx = 0;
78                int opt = getopt_long(argc, argv, "d:t:p:b:u", options, &idx);
79
80                const char * arg = optarg ? optarg : "";
81                char * end;
82                switch(opt) {
83                        // Exit Case
84                        case -1:
85                                break arg_loop;
86                        // Numeric Arguments
87                        case 'd':
88                                duration = strtod(arg, &end);
89                                if(*end != '\0') {
90                                        fprintf(stderr, "Duration must be a valid double, was %s\n", arg);
91                                        goto usage;
92                                }
93                                break;
94                        case 't':
95                                nthreads = strtoul(arg, &end, 10);
96                                if(*end != '\0' || nthreads < 1) {
97                                        fprintf(stderr, "Number of threads must be a positive integer, was %s\n", arg);
98                                        goto usage;
99                                }
100                                break;
101                        case 'p':
102                                nprocs = strtoul(arg, &end, 10);
103                                if(*end != '\0' || nprocs < 1) {
104                                        fprintf(stderr, "Number of processors must be a positive integer, was %s\n", arg);
105                                        goto usage;
106                                }
107                                break;
108                        case 'b':
109                                buflen = strtoul(arg, &end, 10);
110                                if(*end != '\0' && buflen < 10) {
111                                        fprintf(stderr, "Buffer size must be at least 10, was %s\n", arg);
112                                        goto usage;
113                                }
114                                break;
115                        case 'u':
116                                flags |= CFA_CLUSTER_IO_POLLER_USER_THREAD;
117                                break;
118                        // Other cases
119                        default: /* ? */
120                                fprintf(stderr, "%d\n", opt);
121                        usage:
122                                fprintf(stderr, "Usage: %s : [options]\n", argv[0]);
123                                fprintf(stderr, "\n");
124                                fprintf(stderr, "  -d, --duration=DURATION  Duration of the experiment, in seconds\n");
125                                fprintf(stderr, "  -t, --nthreads=NTHREADS  Number of user threads\n");
126                                fprintf(stderr, "  -p, --nprocs=NPROCS      Number of kernel threads\n");
127                                fprintf(stderr, "  -b, --buflen=SIZE        Number of bytes to read per request\n");
128                                exit(EXIT_FAILURE);
129                }
130        }
131
132        fd = open(__FILE__, 0);
133        if(fd < 0) {
134                fprintf(stderr, "Could not open source file\n");
135                exit(EXIT_FAILURE);
136        }
137
138        printf("Running %lu threads over %lu processors for %lf seconds\n", nthreads, nprocs, duration);
139
140        {
141                Time start, end;
142                cluster cl = { "IO Cluster", flags };
143                the_cluster = &cl;
144                #if !defined(__CFA_NO_STATISTICS__)
145                        print_stats_at_exit( cl );
146                #endif
147                {
148                        my_processor procs[nprocs];
149                        {
150                                Reader threads[nthreads];
151
152                                printf("Starting\n");
153                                start = getTime();
154                                run = true;
155                                do {
156                                        sleep(500`ms);
157                                        end = getTime();
158                                } while( (end - start) < duration`s );
159                                run = false;
160                                end = getTime();
161                                printf("Done\n");
162                        }
163                }
164                printf("Took %ld ms\n", (end - start)`ms);
165                printf("Total reads:      %'zu\n", count);
166                printf("Reads per second: %'lf\n", ((double)count) / (end - start)`s);
167        }
168
169        close(fd);
170}
Note: See TracBrowser for help on using the repository browser.