[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 | |
---|
| 19 | extern ssize_t async_preadv2(int fd, const struct iovec *iov, int iovcnt, off_t offset, int flags); |
---|
| 20 | |
---|
| 21 | int fd; |
---|
| 22 | volatile bool run = false; |
---|
| 23 | volatile size_t count = 0; |
---|
| 24 | |
---|
| 25 | unsigned long int buflen = 50; |
---|
| 26 | |
---|
| 27 | thread Reader {}; |
---|
| 28 | void main( Reader & ) { |
---|
| 29 | while(!__atomic_load_n(&run, __ATOMIC_RELAXED)) yield(); |
---|
| 30 | |
---|
| 31 | char data[buflen]; |
---|
| 32 | struct iovec iov = { data, buflen }; |
---|
| 33 | |
---|
| 34 | while(__atomic_load_n(&run, __ATOMIC_RELAXED)) { |
---|
| 35 | async_preadv2(fd, &iov, 1, 0, 0); |
---|
| 36 | __atomic_fetch_add( &count, 1, __ATOMIC_SEQ_CST ); |
---|
| 37 | } |
---|
| 38 | } |
---|
| 39 | |
---|
| 40 | Time now(void); |
---|
| 41 | |
---|
| 42 | int main(int argc, char * argv[]) { |
---|
| 43 | double duration = 5.0; |
---|
| 44 | unsigned long int nthreads = 2; |
---|
| 45 | unsigned long int nprocs = 1; |
---|
| 46 | |
---|
| 47 | setlocale(LC_NUMERIC, ""); |
---|
| 48 | |
---|
| 49 | arg_loop: |
---|
| 50 | for(;;) { |
---|
| 51 | static struct option options[] = { |
---|
| 52 | {"duration", required_argument, 0, 'd'}, |
---|
| 53 | {"nthreads", required_argument, 0, 't'}, |
---|
| 54 | {"nprocs", required_argument, 0, 'p'}, |
---|
| 55 | {"bufsize", required_argument, 0, 'b'}, |
---|
| 56 | {0, 0, 0, 0} |
---|
| 57 | }; |
---|
| 58 | |
---|
| 59 | int idx = 0; |
---|
| 60 | int opt = getopt_long(argc, argv, "d:t:p:b:", options, &idx); |
---|
| 61 | |
---|
| 62 | const char * arg = optarg ? optarg : ""; |
---|
| 63 | char * end; |
---|
| 64 | switch(opt) { |
---|
| 65 | // Exit Case |
---|
| 66 | case -1: |
---|
| 67 | break arg_loop; |
---|
| 68 | // Numeric Arguments |
---|
| 69 | case 'd': |
---|
| 70 | duration = strtod(arg, &end); |
---|
| 71 | if(*end != '\0') { |
---|
| 72 | fprintf(stderr, "Duration must be a valid double, was %s\n", arg); |
---|
| 73 | goto usage; |
---|
| 74 | } |
---|
| 75 | break; |
---|
| 76 | case 't': |
---|
| 77 | nthreads = strtoul(arg, &end, 10); |
---|
| 78 | if(*end != '\0') { |
---|
| 79 | fprintf(stderr, "Number of threads must be a positive integer, was %s\n", arg); |
---|
| 80 | goto usage; |
---|
| 81 | } |
---|
| 82 | break; |
---|
| 83 | case 'p': |
---|
| 84 | nprocs = strtoul(arg, &end, 10); |
---|
| 85 | if(*end != '\0') { |
---|
| 86 | fprintf(stderr, "Number of processors must be a positive integer, was %s\n", arg); |
---|
| 87 | goto usage; |
---|
| 88 | } |
---|
| 89 | break; |
---|
| 90 | case 'b': |
---|
| 91 | buflen = strtoul(arg, &end, 10); |
---|
| 92 | if(*end != '\0' && buflen < 10) { |
---|
| 93 | fprintf(stderr, "Buffer size must be at least 10, was %s\n", arg); |
---|
| 94 | goto usage; |
---|
| 95 | } |
---|
| 96 | break; |
---|
| 97 | // Other cases |
---|
| 98 | default: /* ? */ |
---|
| 99 | fprintf(stderr, "%d\n", opt); |
---|
| 100 | usage: |
---|
| 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, " -b, --buflen=SIZE Number of bytes to read per request\n"); |
---|
| 107 | exit(EXIT_FAILURE); |
---|
| 108 | } |
---|
| 109 | } |
---|
| 110 | |
---|
| 111 | int fd = open(__FILE__, 0); |
---|
| 112 | if(fd < 0) { |
---|
| 113 | fprintf(stderr, "Could not open source file\n"); |
---|
| 114 | exit(EXIT_FAILURE); |
---|
| 115 | } |
---|
| 116 | |
---|
| 117 | printf("Running %lu threads over %lu processors for %lf seconds\n", nthreads, nprocs, duration); |
---|
| 118 | |
---|
| 119 | Time start, end; |
---|
| 120 | { |
---|
| 121 | processor procs[nprocs]; |
---|
| 122 | { |
---|
| 123 | Reader threads[nthreads]; |
---|
| 124 | |
---|
| 125 | printf("Starting\n"); |
---|
| 126 | start = getTime(); |
---|
| 127 | run = true; |
---|
| 128 | do { |
---|
| 129 | sleep(500`ms); |
---|
| 130 | end = getTime(); |
---|
| 131 | } while( (end - start) < duration`s ); |
---|
| 132 | run = false; |
---|
| 133 | end = getTime(); |
---|
| 134 | } |
---|
| 135 | } |
---|
| 136 | printf("Done\n"); |
---|
| 137 | printf("Took %ld ms\n", (end - start)`ms); |
---|
| 138 | printf("Total reads: %'zu\n", count); |
---|
| 139 | printf("Reads per second: %'lf\n", ((double)count) / (end - start)`s); |
---|
| 140 | |
---|
| 141 | close(fd); |
---|
| 142 | } |
---|