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