[2ecbd7b] | 1 | #include "options.hfa" |
---|
| 2 | |
---|
| 3 | #define _GNU_SOURCE |
---|
| 4 | #define __USE_GNU |
---|
| 5 | extern "C" { |
---|
| 6 | #include <sys/types.h> |
---|
| 7 | #include <sys/stat.h> |
---|
| 8 | #include <fcntl.h> |
---|
| 9 | } |
---|
| 10 | |
---|
[a6b587f] | 11 | #include <bitmanip.hfa> |
---|
[b57db73] | 12 | #include <fstream.hfa> |
---|
[2ecbd7b] | 13 | #include <kernel.hfa> |
---|
[42f1d739] | 14 | #include <parseargs.hfa> |
---|
[348f81d5] | 15 | #include <stdlib.hfa> |
---|
[2ecbd7b] | 16 | |
---|
[b57db73] | 17 | #include <stdlib.h> |
---|
[5ad381b] | 18 | #include <string.h> |
---|
| 19 | |
---|
[2ecbd7b] | 20 | Options options @= { |
---|
[481ee28] | 21 | false, // log |
---|
[2cd784a] | 22 | false, // stats |
---|
[40a64f78] | 23 | true, // interactive |
---|
| 24 | 0, // redirect |
---|
| 25 | 0, // redirect |
---|
[5ad381b] | 26 | |
---|
[2ecbd7b] | 27 | { // file_cache |
---|
[b57db73] | 28 | 0, // path |
---|
[2ecbd7b] | 29 | 0, // open_flags; |
---|
| 30 | 42u, // hash_seed; |
---|
| 31 | 0, // size; |
---|
| 32 | false, // list; |
---|
| 33 | false // fixed_fds |
---|
| 34 | }, |
---|
| 35 | |
---|
| 36 | { // socket |
---|
| 37 | 8080, // port |
---|
[03ed863] | 38 | 10, // backlog |
---|
| 39 | 1024 // buflen |
---|
[2ecbd7b] | 40 | }, |
---|
| 41 | |
---|
| 42 | { // cluster |
---|
[348f81d5] | 43 | 1, // nclusters; |
---|
[2ecbd7b] | 44 | 1, // nprocs; |
---|
| 45 | 1, // nworkers; |
---|
[b57db73] | 46 | {}, // params; |
---|
[2ecbd7b] | 47 | false, // procstats |
---|
| 48 | false, // viewhalts |
---|
| 49 | 0, // instance; |
---|
| 50 | } |
---|
| 51 | }; |
---|
| 52 | |
---|
[b57db73] | 53 | void parse_options( int argc, char * argv[] ) { |
---|
[4f762d3] | 54 | // bool fixedfd = false; |
---|
| 55 | // bool sqkpoll = false; |
---|
| 56 | // bool iokpoll = false; |
---|
[8a039be] | 57 | unsigned nentries = 0; |
---|
[348f81d5] | 58 | bool isolate = false; |
---|
[a6b587f] | 59 | |
---|
[2ecbd7b] | 60 | |
---|
| 61 | static cfa_option opt[] = { |
---|
[bdbc24d] | 62 | { 'p', "port", "Port the server will listen on", options.socket.port}, |
---|
| 63 | { 'c', "cpus", "Number of processors to use", options.clopts.nprocs}, |
---|
| 64 | { 't', "threads", "Number of worker threads to use", options.clopts.nworkers}, |
---|
[348f81d5] | 65 | {'\0', "isolate", "Create one cluster per processor", isolate, parse_settrue}, |
---|
[bdbc24d] | 66 | {'\0', "log", "Enable logs", options.log, parse_settrue}, |
---|
[40a64f78] | 67 | {'\0', "sout", "Redirect standard out to file", options.reopen_stdout}, |
---|
| 68 | {'\0', "serr", "Redirect standard error to file", options.reopen_stderr}, |
---|
[2cd784a] | 69 | {'\0', "stats", "Enable statistics", options.stats, parse_settrue}, |
---|
[40a64f78] | 70 | {'\0', "shell", "Disable interactive mode", options.interactive, parse_setfalse}, |
---|
[bdbc24d] | 71 | {'\0', "accept-backlog", "Maximum number of pending accepts", options.socket.backlog}, |
---|
| 72 | {'\0', "request_len", "Maximum number of bytes in the http request, requests with more data will be answered with Http Code 414", options.socket.buflen}, |
---|
| 73 | {'\0', "seed", "seed to use for hashing", options.file_cache.hash_seed }, |
---|
| 74 | {'\0', "cache-size", "Size of the cache to use, if set to small, will uses closes power of 2", options.file_cache.size }, |
---|
| 75 | {'\0', "list-files", "List the files in the specified path and exit", options.file_cache.list, parse_settrue }, |
---|
[4f762d3] | 76 | // { 'f', "fixed-fds", "If set, files are open eagerly and pre-registered with the cluster", fixedfd, parse_settrue}, |
---|
| 77 | // { 'k', "kpollsubmit", "If set, cluster uses IORING_SETUP_SQPOLL, implies -f", sqkpoll, parse_settrue }, |
---|
| 78 | // { 'i', "kpollcomplete", "If set, cluster uses IORING_SETUP_IOPOLL", iokpoll, parse_settrue }, |
---|
[18a7594] | 79 | {'e', "numentries", "Number of I/O entries", nentries }, |
---|
[2ecbd7b] | 80 | |
---|
| 81 | }; |
---|
| 82 | int opt_cnt = sizeof(opt) / sizeof(cfa_option); |
---|
| 83 | |
---|
| 84 | char **left; |
---|
| 85 | parse_args( argc, argv, opt, opt_cnt, "[OPTIONS]... [PATH]\ncforall http server", left ); |
---|
| 86 | |
---|
[8a039be] | 87 | if( nentries != 0 && !is_pow2(nentries) ) { |
---|
[a6b587f] | 88 | unsigned v = nentries; |
---|
| 89 | v--; |
---|
| 90 | v |= v >> 1; |
---|
| 91 | v |= v >> 2; |
---|
| 92 | v |= v >> 4; |
---|
| 93 | v |= v >> 8; |
---|
| 94 | v |= v >> 16; |
---|
| 95 | v++; |
---|
| 96 | serr | "Warning: num_entries not a power of 2" | '(' | nentries | ')' | "raising to " | v; |
---|
| 97 | nentries = v; |
---|
| 98 | } |
---|
[348f81d5] | 99 | if(isolate) { |
---|
| 100 | options.clopts.nclusters = options.clopts.nprocs; |
---|
| 101 | options.clopts.nprocs = 1; |
---|
| 102 | } |
---|
[a6b587f] | 103 | options.clopts.params.num_entries = nentries; |
---|
[348f81d5] | 104 | options.clopts.instance = alloc(options.clopts.nclusters); |
---|
| 105 | options.clopts.thrd_cnt = alloc(options.clopts.nclusters); |
---|
| 106 | options.clopts.cltr_cnt = 0; |
---|
| 107 | for(i; options.clopts.nclusters) { |
---|
| 108 | options.clopts.thrd_cnt[i] = 0; |
---|
| 109 | } |
---|
| 110 | |
---|
[a6b587f] | 111 | |
---|
[4f762d3] | 112 | // if( fixedfd ) { |
---|
| 113 | // options.file_cache.fixed_fds = true; |
---|
| 114 | // } |
---|
[2ecbd7b] | 115 | |
---|
[4f762d3] | 116 | // if( sqkpoll ) { |
---|
| 117 | // options.file_cache.fixed_fds = true; |
---|
| 118 | // } |
---|
[2ecbd7b] | 119 | |
---|
[4f762d3] | 120 | // if( iokpoll ) { |
---|
| 121 | // options.file_cache.open_flags |= O_DIRECT; |
---|
| 122 | // } |
---|
[2ecbd7b] | 123 | |
---|
[a6b587f] | 124 | if( left[0] == 0p ) { return; } |
---|
[2ecbd7b] | 125 | |
---|
| 126 | const char * path = left[0]; |
---|
| 127 | left++; |
---|
| 128 | |
---|
| 129 | if( left[0] != 0p ) { |
---|
[a6b587f] | 130 | serr | "Too many trailing arguments!" | '\'' | path | '\''; |
---|
| 131 | while(left[0] != 0p) { |
---|
| 132 | serr | " - " | left[0]; |
---|
| 133 | left++; |
---|
| 134 | } |
---|
[b57db73] | 135 | exit(EXIT_FAILURE); |
---|
[2ecbd7b] | 136 | } |
---|
| 137 | |
---|
[b57db73] | 138 | options.file_cache.path = path; |
---|
[40a64f78] | 139 | |
---|
| 140 | if( options.reopen_stdout && options.reopen_stderr && 0 == strcmp(options.reopen_stdout, options.reopen_stderr) ) { |
---|
| 141 | serr | "Redirect sout and serr to the same file is not supported"; |
---|
| 142 | exit(EXIT_FAILURE); |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | if( options.reopen_stdout ) { |
---|
| 146 | sout | "redirecting sout to '" | options.reopen_stdout | "'"; |
---|
| 147 | FILE * ret = freopen( options.reopen_stdout, "w", stdout); |
---|
| 148 | if( ret == 0p ) { |
---|
| 149 | serr | "Failed to redirect sout to '" | options.reopen_stdout | "'"; |
---|
| 150 | exit(EXIT_FAILURE); |
---|
| 151 | } |
---|
| 152 | } |
---|
| 153 | |
---|
| 154 | if( options.reopen_stderr ) { |
---|
| 155 | sout | "redirecting serr to '" | options.reopen_stderr | "'"; |
---|
| 156 | FILE * ret = freopen( options.reopen_stderr, "w", stderr); |
---|
| 157 | if( ret == 0p ) { |
---|
| 158 | serr | "Failed to redirect serr to '" | options.reopen_stderr | "'"; |
---|
| 159 | exit(EXIT_FAILURE); |
---|
| 160 | } |
---|
| 161 | } |
---|
[2ecbd7b] | 162 | } |
---|