source: benchmark/io/http/options.cfa @ bbf61838

ADTast-experimentalpthread-emulationqualifiedEnum
Last change on this file since bbf61838 was 7f0ac12, checked in by Thierry Delisle <tdelisle@…>, 2 years ago

First draft at acceptor thread webserver

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