| [5847d35] | 1 | #include <stdlib.h>
 | 
|---|
 | 2 | #include <stdio.h>
 | 
|---|
 | 3 | #include <string.h>
 | 
|---|
 | 4 | 
 | 
|---|
 | 5 | extern "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 | 
 | 
|---|
| [1bcdeff] | 19 | extern bool traceHeapOn();
 | 
|---|
| [2489d31] | 20 | extern ssize_t cfa_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags);
 | 
|---|
| [5847d35] | 21 | 
 | 
|---|
 | 22 | int fd;
 | 
|---|
 | 23 | volatile bool run = false;
 | 
|---|
 | 24 | volatile size_t count = 0;
 | 
|---|
 | 25 | 
 | 
|---|
 | 26 | unsigned long int buflen = 50;
 | 
|---|
 | 27 | 
 | 
|---|
| [4069faad] | 28 | cluster * the_cluster;
 | 
|---|
 | 29 | 
 | 
|---|
| [5847d35] | 30 | thread Reader {};
 | 
|---|
| [4069faad] | 31 | void ?{}( Reader & this ) {
 | 
|---|
 | 32 |         ((thread&)this){ "Reader Thread", *the_cluster };
 | 
|---|
 | 33 | }
 | 
|---|
 | 34 | 
 | 
|---|
 | 35 | struct my_processor {
 | 
|---|
 | 36 |         processor p;
 | 
|---|
 | 37 | };
 | 
|---|
 | 38 | 
 | 
|---|
 | 39 | void ?{}( my_processor & this ) {
 | 
|---|
 | 40 |         (this.p){ "I/O Processor", *the_cluster };
 | 
|---|
 | 41 | }
 | 
|---|
 | 42 | 
 | 
|---|
| [5847d35] | 43 | void 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)) {
 | 
|---|
| [4069faad] | 50 |                 int r = cfa_preadv2(fd, &iov, 1, 0, 0);
 | 
|---|
 | 51 |                 if(r < 0) abort(strerror(-r));
 | 
|---|
 | 52 | 
 | 
|---|
| [5847d35] | 53 |                 __atomic_fetch_add( &count, 1, __ATOMIC_SEQ_CST );
 | 
|---|
 | 54 |         }
 | 
|---|
 | 55 | }
 | 
|---|
 | 56 | 
 | 
|---|
 | 57 | int main(int argc, char * argv[]) {
 | 
|---|
 | 58 |         double duration   = 5.0;
 | 
|---|
 | 59 |         unsigned long int nthreads = 2;
 | 
|---|
 | 60 |         unsigned long int nprocs   = 1;
 | 
|---|
| [b6f2b213] | 61 |         int flags = 0;
 | 
|---|
| [5847d35] | 62 | 
 | 
|---|
| [1bcdeff] | 63 |         printf("Setting local\n");
 | 
|---|
| [5847d35] | 64 |         setlocale(LC_NUMERIC, "");
 | 
|---|
 | 65 | 
 | 
|---|
 | 66 |         arg_loop:
 | 
|---|
 | 67 |         for(;;) {
 | 
|---|
 | 68 |                 static struct option options[] = {
 | 
|---|
| [b6f2b213] | 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'},
 | 
|---|
| [5847d35] | 74 |                         {0, 0, 0, 0}
 | 
|---|
 | 75 |                 };
 | 
|---|
 | 76 | 
 | 
|---|
 | 77 |                 int idx = 0;
 | 
|---|
| [b6f2b213] | 78 |                 int opt = getopt_long(argc, argv, "d:t:p:b:u", options, &idx);
 | 
|---|
| [5847d35] | 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);
 | 
|---|
| [1bcdeff] | 96 |                                 if(*end != '\0' || nthreads < 1) {
 | 
|---|
| [5847d35] | 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);
 | 
|---|
| [1bcdeff] | 103 |                                 if(*end != '\0' || nprocs < 1) {
 | 
|---|
| [5847d35] | 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;
 | 
|---|
| [b6f2b213] | 115 |                         case 'u':
 | 
|---|
 | 116 |                                 flags |= CFA_CLUSTER_IO_POLLER_USER_THREAD;
 | 
|---|
 | 117 |                                 break;
 | 
|---|
| [5847d35] | 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 | 
 | 
|---|
| [4069faad] | 132 |         fd = open(__FILE__, 0);
 | 
|---|
| [5847d35] | 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 |         {
 | 
|---|
| [4069faad] | 141 |                 Time start, end;
 | 
|---|
| [b6f2b213] | 142 |                 cluster cl = { "IO Cluster", flags };
 | 
|---|
| [4069faad] | 143 |                 the_cluster = &cl;
 | 
|---|
 | 144 |                 #if !defined(__CFA_NO_STATISTICS__)
 | 
|---|
 | 145 |                         print_stats_at_exit( cl );
 | 
|---|
 | 146 |                 #endif
 | 
|---|
| [5847d35] | 147 |                 {
 | 
|---|
| [4069faad] | 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;
 | 
|---|
| [5847d35] | 160 |                                 end = getTime();
 | 
|---|
| [4069faad] | 161 |                                 printf("Done\n");
 | 
|---|
 | 162 |                         }
 | 
|---|
| [5847d35] | 163 |                 }
 | 
|---|
| [4069faad] | 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);
 | 
|---|
| [5847d35] | 167 |         }
 | 
|---|
 | 168 | 
 | 
|---|
 | 169 |         close(fd);
 | 
|---|
 | 170 | }
 | 
|---|