| [20ab637] | 1 | #define _GNU_SOURCE
 | 
|---|
 | 2 | 
 | 
|---|
| [5847d35] | 3 | #include <stdlib.h>
 | 
|---|
 | 4 | #include <stdio.h>
 | 
|---|
 | 5 | #include <string.h>
 | 
|---|
 | 6 | 
 | 
|---|
 | 7 | extern "C" {
 | 
|---|
 | 8 |         #include <locale.h>
 | 
|---|
 | 9 |         #include <getopt.h>
 | 
|---|
 | 10 |         #include <fcntl.h>
 | 
|---|
 | 11 |         #include <sys/uio.h>
 | 
|---|
 | 12 | }
 | 
|---|
 | 13 | 
 | 
|---|
| [920dca3] | 14 | #include <errno.h>
 | 
|---|
| [5847d35] | 15 | #include <unistd.h>
 | 
|---|
 | 16 | 
 | 
|---|
 | 17 | #include <clock.hfa>
 | 
|---|
| [920dca3] | 18 | #include <iofwd.hfa>
 | 
|---|
| [5847d35] | 19 | #include <kernel.hfa>
 | 
|---|
 | 20 | #include <thread.hfa>
 | 
|---|
 | 21 | #include <time.hfa>
 | 
|---|
| [566fde0] | 22 | #include <stats.hfa>
 | 
|---|
| [5847d35] | 23 | 
 | 
|---|
| [9791ab5] | 24 | #include "../benchcltr.hfa"
 | 
|---|
 | 25 | 
 | 
|---|
| [1bcdeff] | 26 | extern bool traceHeapOn();
 | 
|---|
| [5847d35] | 27 | 
 | 
|---|
 | 28 | int fd;
 | 
|---|
 | 29 | volatile bool run = false;
 | 
|---|
 | 30 | volatile size_t count = 0;
 | 
|---|
 | 31 | 
 | 
|---|
| [920dca3] | 32 | unsigned long int buflen = 512;
 | 
|---|
| [20ab637] | 33 | bool fixed_file = false;
 | 
|---|
| [5847d35] | 34 | 
 | 
|---|
| [9791ab5] | 35 | thread __attribute__((aligned(128))) Reader {};
 | 
|---|
| [4069faad] | 36 | void ?{}( Reader & this ) {
 | 
|---|
| [9791ab5] | 37 |         ((thread&)this){ "Reader Thread", *the_benchmark_cluster };
 | 
|---|
| [4069faad] | 38 | }
 | 
|---|
 | 39 | 
 | 
|---|
| [20ab637] | 40 | int do_read(int fd, struct iovec * iov) {
 | 
|---|
| [920dca3] | 41 |         // extern ssize_t cfa_preadv2(int, const struct iovec *, int, off_t, int, int = 0, Duration = -1`s, io_cancellation * = 0p, io_context * = 0p);
 | 
|---|
| [01c6256] | 42 |         int sflags = 0
 | 
|---|
 | 43 |         #if defined(CFA_HAVE_IOSQE_ASYNC)
 | 
|---|
 | 44 |                 | CFA_IO_ASYNC
 | 
|---|
 | 45 |         #else
 | 
|---|
 | 46 |         #warning no CFA_IO_ASYNC support
 | 
|---|
 | 47 |         #endif
 | 
|---|
 | 48 |         ;
 | 
|---|
| [20ab637] | 49 |         if(fixed_file) {
 | 
|---|
| [920dca3] | 50 |                 sflags |= CFA_IO_FIXED_FD1;
 | 
|---|
| [20ab637] | 51 |         }
 | 
|---|
| [920dca3] | 52 |         return cfa_preadv2(fd, iov, 1, 0, 0, sflags, -1`s, 0p, 0p);
 | 
|---|
| [20ab637] | 53 | }
 | 
|---|
 | 54 | 
 | 
|---|
| [5847d35] | 55 | void main( Reader & ) {
 | 
|---|
| [e235429] | 56 |         park();
 | 
|---|
| [9791ab5] | 57 |         /* paranoid */ assert( true == __atomic_load_n(&run, __ATOMIC_RELAXED) );
 | 
|---|
| [5847d35] | 58 | 
 | 
|---|
| [920dca3] | 59 |         __attribute__((aligned(512)))  char data[buflen];
 | 
|---|
| [5847d35] | 60 |         struct iovec iov = { data, buflen };
 | 
|---|
 | 61 | 
 | 
|---|
 | 62 |         while(__atomic_load_n(&run, __ATOMIC_RELAXED)) {
 | 
|---|
| [20ab637] | 63 |                 int r = do_read(fd, &iov);
 | 
|---|
| [920dca3] | 64 |                 if(r < 0) abort("%s\n", strerror(errno));
 | 
|---|
| [4069faad] | 65 | 
 | 
|---|
| [5847d35] | 66 |                 __atomic_fetch_add( &count, 1, __ATOMIC_SEQ_CST );
 | 
|---|
 | 67 |         }
 | 
|---|
 | 68 | }
 | 
|---|
 | 69 | 
 | 
|---|
 | 70 | int main(int argc, char * argv[]) {
 | 
|---|
| [20ab637] | 71 |         int file_flags = 0;
 | 
|---|
| [080ee15] | 72 |         unsigned num_io = 1;
 | 
|---|
| [dd4e2d7] | 73 |         unsigned sublen = 16;
 | 
|---|
| [080ee15] | 74 |         unsigned nentries = 0;
 | 
|---|
| [5847d35] | 75 | 
 | 
|---|
| [d9265a2] | 76 |         bool subthrd = false;
 | 
|---|
 | 77 |         bool subeagr = false;
 | 
|---|
 | 78 |         bool odirect = false;
 | 
|---|
 | 79 |         bool kpollsb = false;
 | 
|---|
 | 80 |         bool kpollcp = false;
 | 
|---|
 | 81 | 
 | 
|---|
 | 82 |         cfa_option opt[] = {
 | 
|---|
 | 83 |                 BENCH_OPT_CFA
 | 
|---|
 | 84 |                 {'b', "bufsize",       "Number of bytes to read per request", buflen},
 | 
|---|
 | 85 |                 {'s', "submitthread",  "If set, cluster uses polling thread to submit I/O", subthrd, parse_settrue},
 | 
|---|
 | 86 |                 {'e', "eagersubmit",   "If set, cluster submits I/O eagerly but still aggregates submits", subeagr, parse_settrue},
 | 
|---|
 | 87 |                 {'f', "fixed-files",   "Pre-register files with the io_contexts", fixed_file, parse_settrue},
 | 
|---|
 | 88 |                 {'o', "open-direct",   "Open files with O_DIRECT flag, bypassing the file cache", odirect, parse_settrue},
 | 
|---|
 | 89 |                 {'k', "kpollsubmit",   "If set, cluster uses an in kernel thread to poll submission, implies -f, requires elevated permissions", kpollsb, parse_settrue},
 | 
|---|
 | 90 |                 {'i', "kpollcomplete", "If set, cluster polls fds for completions instead of relying on interrupts to get notifications, implies -o", kpollcp, parse_settrue},
 | 
|---|
 | 91 |                 {'l', "submitlength",  "Size of the buffer that stores ready submissions", sublen},
 | 
|---|
| [080ee15] | 92 |                 {'r', "numentries",    "Number of entries each of the io_context have", nentries},
 | 
|---|
| [d9265a2] | 93 |                 {'n', "numcontexts",   "Number of io_contexts to the cluster", num_io},
 | 
|---|
 | 94 |         };
 | 
|---|
 | 95 |         int opt_cnt = sizeof(opt) / sizeof(cfa_option);
 | 
|---|
 | 96 | 
 | 
|---|
 | 97 |         char **left;
 | 
|---|
| [77fde9d5] | 98 |         parse_args( opt, opt_cnt, "[OPTIONS]...\ncforall readv benchmark", left );
 | 
|---|
| [d9265a2] | 99 | 
 | 
|---|
 | 100 |         if(kpollcp || odirect) {
 | 
|---|
 | 101 |                 if( (buflen % 512) != 0 ) {
 | 
|---|
 | 102 |                         fprintf(stderr, "Buffer length must be a multiple of 512 when using O_DIRECT, was %lu\n\n", buflen);
 | 
|---|
| [77fde9d5] | 103 |                         print_args_usage(opt, opt_cnt, "[OPTIONS]...\ncforall readv benchmark", true);
 | 
|---|
| [5847d35] | 104 |                 }
 | 
|---|
 | 105 |         }
 | 
|---|
 | 106 | 
 | 
|---|
| [d9265a2] | 107 |         io_context_params params;
 | 
|---|
 | 108 | 
 | 
|---|
 | 109 |         if( subthrd ) params.poller_submits = true;
 | 
|---|
 | 110 |         if( subeagr ) params.eager_submits  = true;
 | 
|---|
 | 111 |         if( kpollsb ) params.poll_submit    = true;
 | 
|---|
 | 112 |         if( kpollcp ) params.poll_complete  = true;
 | 
|---|
 | 113 | 
 | 
|---|
| [01c6256] | 114 |         if(params.poll_submit  ) fixed_file = true;
 | 
|---|
| [2b5be17] | 115 |         if(params.poll_complete) odirect    = true;
 | 
|---|
| [d9265a2] | 116 | 
 | 
|---|
| [080ee15] | 117 |         params.num_ready = sublen;
 | 
|---|
 | 118 |         params.num_entries = nentries;
 | 
|---|
 | 119 | 
 | 
|---|
| [d9265a2] | 120 |         if(odirect) file_flags |= O_DIRECT;
 | 
|---|
| [01c6256] | 121 | 
 | 
|---|
| [20ab637] | 122 |         int lfd = open(__FILE__, file_flags);
 | 
|---|
 | 123 |         if(lfd < 0) {
 | 
|---|
| [5847d35] | 124 |                 fprintf(stderr, "Could not open source file\n");
 | 
|---|
 | 125 |                 exit(EXIT_FAILURE);
 | 
|---|
 | 126 |         }
 | 
|---|
 | 127 | 
 | 
|---|
| [cb85603] | 128 |         printf("Running %d threads, reading %lu bytes each, over %d processors for %f seconds\n", nthreads, buflen, nprocs, duration);
 | 
|---|
| [5847d35] | 129 | 
 | 
|---|
 | 130 |         {
 | 
|---|
| [4069faad] | 131 |                 Time start, end;
 | 
|---|
| [920dca3] | 132 |                 BenchCluster cl = { num_io, params, CFA_STATS_READY_Q | CFA_STATS_IO };
 | 
|---|
| [20ab637] | 133 | 
 | 
|---|
 | 134 |                 if(fixed_file) {
 | 
|---|
 | 135 |                         fd = 0;
 | 
|---|
 | 136 |                         register_fixed_files( cl.self, &lfd, 1 );
 | 
|---|
 | 137 |                 }
 | 
|---|
 | 138 |                 else {
 | 
|---|
 | 139 |                         fd = lfd;
 | 
|---|
 | 140 |                 }
 | 
|---|
 | 141 | 
 | 
|---|
| [5847d35] | 142 |                 {
 | 
|---|
| [9791ab5] | 143 |                         BenchProc procs[nprocs];
 | 
|---|
| [4069faad] | 144 |                         {
 | 
|---|
 | 145 |                                 Reader threads[nthreads];
 | 
|---|
 | 146 | 
 | 
|---|
 | 147 |                                 printf("Starting\n");
 | 
|---|
| [9791ab5] | 148 |                                 bool is_tty = isatty(STDOUT_FILENO);
 | 
|---|
| [e54d0c3] | 149 |                                 start = timeHiRes();
 | 
|---|
| [4069faad] | 150 |                                 run = true;
 | 
|---|
| [9791ab5] | 151 | 
 | 
|---|
 | 152 |                                 for(i; nthreads) {
 | 
|---|
| [e235429] | 153 |                                         unpark( threads[i] );
 | 
|---|
| [9791ab5] | 154 |                                 }
 | 
|---|
 | 155 |                                 wait(duration, start, end, is_tty);
 | 
|---|
 | 156 | 
 | 
|---|
| [4069faad] | 157 |                                 run = false;
 | 
|---|
| [e54d0c3] | 158 |                                 end = timeHiRes();
 | 
|---|
| [9791ab5] | 159 |                                 printf("\nDone\n");
 | 
|---|
| [4069faad] | 160 |                         }
 | 
|---|
| [920dca3] | 161 |                         printf("Readers closed\n");
 | 
|---|
| [5847d35] | 162 |                 }
 | 
|---|
| [b1ac7dd] | 163 |                 printf("Took %'ld ms\n", (end - start)`ms);
 | 
|---|
| [cbabfd4] | 164 |                 printf("Total reads      : %'15zu\n", count);
 | 
|---|
 | 165 |                 printf("Reads per second : %'18.2lf\n", ((double)count) / (end - start)`s);
 | 
|---|
 | 166 |                 printf("Total read size  : %'15zu\n", buflen * count);
 | 
|---|
 | 167 |                 printf("Bytes per second : %'18.2lf\n", ((double)count * buflen) / (end - start)`s);
 | 
|---|
| [5847d35] | 168 |         }
 | 
|---|
 | 169 | 
 | 
|---|
| [20ab637] | 170 |         close(lfd);
 | 
|---|
| [e54d0c3] | 171 | }
 | 
|---|