#include "options.hfa" #define _GNU_SOURCE #define __USE_GNU extern "C" { #include #include #include } #include #include #include #include #include #include #include Options options @= { false, // log false, // stats true, // interactive 0, // redirect 0, // redirect { // file_cache 0, // path 0, // open_flags; 42u, // hash_seed; 0, // size; false, // list; false // fixed_fds }, { // socket 8080, // port 10, // backlog 1024, // buflen false // reuseport }, { // cluster 1, // nclusters; 1, // nprocs; 1, // nworkers; {}, // params; false, // procstats false, // viewhalts 0, // instance; } }; void parse_options( int argc, char * argv[] ) { unsigned nentries = 0; bool isolate = false; static cfa_option opt[] = { { 'p', "port", "Port the server will listen on", options.socket.port}, { 'c', "cpus", "Number of processors to use", options.clopts.nprocs}, { 't', "threads", "Number of worker threads to use", options.clopts.nworkers}, {'\0', "isolate", "Create one cluster per processor", isolate, parse_settrue}, {'\0', "log", "Enable logs", options.log, parse_settrue}, {'\0', "sout", "Redirect standard out to file", options.reopen_stdout}, {'\0', "serr", "Redirect standard error to file", options.reopen_stderr}, {'\0', "stats", "Enable statistics", options.stats, parse_settrue}, {'\0', "shell", "Disable interactive mode", options.interactive, parse_setfalse}, {'\0', "accept-backlog", "Maximum number of pending accepts", options.socket.backlog}, {'\0', "reuseport", "Use acceptor threads with reuse port SO_REUSEPORT", options.socket.reuseport, parse_settrue}, {'\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}, {'\0', "seed", "seed to use for hashing", options.file_cache.hash_seed }, {'\0', "cache-size", "Size of the cache to use, if set to small, will uses closes power of 2", options.file_cache.size }, {'\0', "list-files", "List the files in the specified path and exit", options.file_cache.list, parse_settrue }, // { 'f', "fixed-fds", "If set, files are open eagerly and pre-registered with the cluster", fixedfd, parse_settrue}, // { 'k', "kpollsubmit", "If set, cluster uses IORING_SETUP_SQPOLL, implies -f", sqkpoll, parse_settrue }, // { 'i', "kpollcomplete", "If set, cluster uses IORING_SETUP_IOPOLL", iokpoll, parse_settrue }, {'e', "numentries", "Number of I/O entries", nentries }, }; int opt_cnt = sizeof(opt) / sizeof(cfa_option); char **left; parse_args( argc, argv, opt, opt_cnt, "[OPTIONS]... [PATH]\ncforall http server", left ); if( nentries != 0 && !is_pow2(nentries) ) { unsigned v = nentries; v--; v |= v >> 1; v |= v >> 2; v |= v >> 4; v |= v >> 8; v |= v >> 16; v++; serr | "Warning: num_entries not a power of 2" | '(' | nentries | ')' | "raising to " | v; nentries = v; } if(isolate) { options.clopts.nclusters = options.clopts.nprocs; options.clopts.nprocs = 1; } options.clopts.params.num_entries = nentries; options.clopts.instance = alloc(options.clopts.nclusters); options.clopts.thrd_cnt = alloc(options.clopts.nclusters); options.clopts.cltr_cnt = 0; for(i; options.clopts.nclusters) { options.clopts.thrd_cnt[i] = 0; } // if( fixedfd ) { // options.file_cache.fixed_fds = true; // } // if( sqkpoll ) { // options.file_cache.fixed_fds = true; // } // if( iokpoll ) { // options.file_cache.open_flags |= O_DIRECT; // } if( left[0] == 0p ) { return; } const char * path = left[0]; left++; if( left[0] != 0p ) { serr | "Too many trailing arguments!" | '\'' | path | '\''; while(left[0] != 0p) { serr | " - " | left[0]; left++; } exit(EXIT_FAILURE); } options.file_cache.path = path; if( options.reopen_stdout && options.reopen_stderr && 0 == strcmp(options.reopen_stdout, options.reopen_stderr) ) { serr | "Redirect sout and serr to the same file is not supported"; exit(EXIT_FAILURE); } if( options.reopen_stdout ) { sout | "redirecting sout to '" | options.reopen_stdout | "'"; FILE * ret = freopen( options.reopen_stdout, "w", stdout); if( ret == 0p ) { serr | "Failed to redirect sout to '" | options.reopen_stdout | "'"; exit(EXIT_FAILURE); } } if( options.reopen_stderr ) { sout | "redirecting serr to '" | options.reopen_stderr | "'"; FILE * ret = freopen( options.reopen_stderr, "w", stderr); if( ret == 0p ) { serr | "Failed to redirect serr to '" | options.reopen_stderr | "'"; exit(EXIT_FAILURE); } } }