[9f1c286] | 1 | #include <assert.h> |
---|
| 2 | #include <stdlib.h> |
---|
| 3 | #include <stdio.h> |
---|
| 4 | #include <string.h> |
---|
| 5 | |
---|
| 6 | extern "C" { |
---|
| 7 | #include <locale.h> |
---|
| 8 | #include <getopt.h> |
---|
| 9 | #include <fcntl.h> |
---|
| 10 | #include <sys/uio.h> |
---|
| 11 | } |
---|
| 12 | |
---|
| 13 | #include <unistd.h> |
---|
| 14 | |
---|
| 15 | #include <pthread.h> |
---|
| 16 | |
---|
| 17 | #include "../benchcltr.hfa" |
---|
| 18 | |
---|
| 19 | int fd; |
---|
| 20 | volatile bool run = false; |
---|
| 21 | volatile size_t count = 0; |
---|
| 22 | |
---|
| 23 | unsigned long int buflen = 50; |
---|
| 24 | |
---|
| 25 | void * reader_main( void * arg ) { |
---|
| 26 | pthread_barrier_wait( (pthread_barrier_t*) arg ); |
---|
| 27 | /* paranoid */ assert( true == __atomic_load_n(&run, __ATOMIC_RELAXED) ); |
---|
| 28 | |
---|
| 29 | char data[buflen]; |
---|
| 30 | struct iovec iov = { data, buflen }; |
---|
| 31 | |
---|
| 32 | while(__atomic_load_n(&run, __ATOMIC_RELAXED)) { |
---|
| 33 | int r = preadv2(fd, &iov, 1, 0, 0); |
---|
| 34 | if(r < 0) { |
---|
| 35 | fprintf(stderr, "%s\n", strerror(-r)); |
---|
| 36 | abort(); |
---|
| 37 | } |
---|
| 38 | |
---|
| 39 | __atomic_fetch_add( &count, 1, __ATOMIC_SEQ_CST ); |
---|
| 40 | } |
---|
| 41 | |
---|
| 42 | return NULL; |
---|
| 43 | } |
---|
| 44 | |
---|
| 45 | int main(int argc, char * argv[]) { |
---|
| 46 | BENCH_DECL |
---|
| 47 | unsigned flags = 0; |
---|
| 48 | unsigned sublen = 16; |
---|
| 49 | |
---|
| 50 | setlocale(LC_ALL, ""); |
---|
| 51 | |
---|
| 52 | for(;;) { |
---|
| 53 | static struct option options[] = { |
---|
| 54 | BENCH_OPT_LONG |
---|
| 55 | {"polled-io", required_argument, 0, 'i'}, |
---|
| 56 | {"bufsize", required_argument, 0, 'b'}, |
---|
| 57 | {0, 0, 0, 0} |
---|
| 58 | }; |
---|
| 59 | |
---|
| 60 | int idx = 0; |
---|
| 61 | int opt = getopt_long(argc, argv, BENCH_OPT_SHORT "ib:", options, &idx); |
---|
| 62 | |
---|
| 63 | const char * arg = optarg ? optarg : ""; |
---|
| 64 | char * end; |
---|
| 65 | switch(opt) { |
---|
| 66 | // Exit Case |
---|
| 67 | case -1: |
---|
| 68 | goto arg_loop; |
---|
| 69 | BENCH_OPT_CASE |
---|
| 70 | case 'i': |
---|
| 71 | flags |= O_DIRECT; |
---|
| 72 | break; |
---|
| 73 | case 'b': |
---|
| 74 | buflen = strtoul(arg, &end, 10); |
---|
| 75 | if(*end != '\0' && buflen < 10) { |
---|
| 76 | fprintf(stderr, "Buffer size must be at least 10, was %s\n", arg); |
---|
| 77 | goto usage; |
---|
| 78 | } |
---|
| 79 | break; |
---|
| 80 | default: /* ? */ |
---|
| 81 | fprintf(stderr, "%d\n", opt); |
---|
| 82 | usage: |
---|
| 83 | bench_usage( argv ); |
---|
| 84 | fprintf( stderr, " -i, --polled-io If set opens the file with O_DIRECT\n" ); |
---|
| 85 | fprintf( stderr, " -b, --buflen=SIZE Number of bytes to read per request\n" ); |
---|
| 86 | exit(EXIT_FAILURE); |
---|
| 87 | } |
---|
| 88 | } |
---|
| 89 | arg_loop: |
---|
| 90 | |
---|
| 91 | fd = open(__FILE__, flags); |
---|
| 92 | if(fd < 0) { |
---|
| 93 | fprintf(stderr, "Could not open source file\n"); |
---|
| 94 | exit(EXIT_FAILURE); |
---|
| 95 | } |
---|
| 96 | |
---|
| 97 | printf("Running %d threads, reading %lu bytes each, over %d processors for %f seconds\n", nthreads, buflen, nprocs, duration); |
---|
| 98 | |
---|
| 99 | { |
---|
| 100 | uint64_t start, end; |
---|
| 101 | { |
---|
| 102 | pthread_barrier_t barrier; |
---|
| 103 | pthread_barrier_init(&barrier, NULL, nthreads + 1); |
---|
| 104 | { |
---|
| 105 | pthread_t threads[nthreads]; |
---|
| 106 | for(int i = 0; i < nthreads; i++) { |
---|
| 107 | pthread_attr_t attr; |
---|
| 108 | pthread_attr_init( &attr ); |
---|
| 109 | pthread_create( &threads[i], &attr, reader_main, &barrier ); |
---|
| 110 | } |
---|
| 111 | |
---|
| 112 | printf("Starting\n"); |
---|
| 113 | bool is_tty = isatty(STDOUT_FILENO); |
---|
| 114 | start = getTimeNsec(); |
---|
| 115 | run = true; |
---|
| 116 | |
---|
| 117 | pthread_barrier_wait( &barrier ); |
---|
| 118 | wait_duration(duration, start, end, is_tty); |
---|
| 119 | |
---|
| 120 | run = false; |
---|
| 121 | end = getTimeNsec(); |
---|
| 122 | printf("\nDone\n"); |
---|
| 123 | |
---|
| 124 | for(int i = 0; i < nthreads; i++) { |
---|
| 125 | void * ret; |
---|
| 126 | pthread_join( threads[i], &ret ); |
---|
| 127 | } |
---|
| 128 | } |
---|
| 129 | pthread_barrier_destroy(&barrier); |
---|
| 130 | } |
---|
| 131 | printf("Took %'ld ms\n", to_miliseconds(end - start)); |
---|
| 132 | printf("Total reads : %'15zu\n", count); |
---|
| 133 | printf("Reads per second : %'18.2lf\n", ((double)count) / to_fseconds(end - start)); |
---|
| 134 | printf("Total read size : %'15zu\n", buflen * count); |
---|
| 135 | printf("Bytes per second : %'18.2lf\n", ((double)count * buflen) / to_fseconds(end - start)); |
---|
| 136 | } |
---|
| 137 | |
---|
| 138 | close(fd); |
---|
| 139 | } |
---|