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