-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcppsp_standalone.C
More file actions
416 lines (403 loc) · 11.6 KB
/
cppsp_standalone.C
File metadata and controls
416 lines (403 loc) · 11.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
* */
#include <cpoll/cpoll.H>
#include <unistd.h>
#include <iostream>
#include <signal.h>
#include <cppsp/page.H>
#include <cppsp/cppsp_cpoll.H>
#include <cppsp/common.H>
#include <assert.h>
#include <sys/wait.h>
#include <sys/syscall.h> //SYS_gettid
#include "server.C"
#define PRINTSIZE(x) printf("sizeof("#x") = %i\n",sizeof(x))
#define printerr(x, ...) fprintf(stderr, "\x1B[41;1;33m" x "\x1B[0;0;0m\n", ##__VA_ARGS__)
#define printinfo(x, ...) fprintf(stderr, "\x1B[1;1;1m" x "\x1B[0;0;0m\n", ##__VA_ARGS__)
#ifndef SO_REUSEPORT
#define SO_REUSEPORT 15
#endif
#define CPPSP_LISTEN_BACKLOG 256
#define tprintf(s,...) fprintf(stderr,"[thread %i] " s,curThreadID,##__VA_ARGS__)
using namespace std;
using namespace CP;
using namespace cppsp;
using namespace RGC;
string rootDir;
void parseArgs(int argc, char** argv, const function<void(char*, const function<char*()>&)>& cb);
struct moduleLoad
{
string path;
bool host;
bool loaded;
};
struct workerThread
{
Poll p;
cppspServer::Server srv; //host
vector<moduleLoad> modules;
Ref<CP::Socket> listenSock;
union {
pid_t pid;
pthread_t thread;
};
int cpu; //id of cpu to pin to, or -1
workerThread(Socket& sock): srv(&p,rootDir),
listenSock(sock),cpu(-1){
}
};
class handler1: public RGC::Allocator
{
public:
Socket sock;
cppspServer::handler h;
handler1(cppspServer::Server& thr,CP::Poll& poll,HANDLE s,int d,int t,int p):
sock(s,d,t,p),h(thr,poll,sock) {
h.allocator=this;
}
void* alloc(int s) { return NULL; }
void dealloc(void* ptr) {
sock.~Socket();
if(allocator==NULL)free(this);
else allocator->dealloc(this);
}
};
void pinToCPU(int cpu) {
cpu_set_t s;
CPU_ZERO(&s);
CPU_SET(cpu,&s);
if(sched_setaffinity((pid_t)syscall(SYS_gettid),sizeof(s),&s)!=0)
perror("sched_setaffinity");
}
void* thread1(void* v) {
workerThread& thr=*(workerThread*)v;
cppspServer::Server& srv=thr.srv;
int curThreadID=thr.srv.threadID;
srv.loadDefaultMimeDB();
Poll& p=thr.p;
if(thr.cpu>=0) pinToCPU(thr.cpu);
/*
p.add(thr->efd);
struct {
Poll& p;
serverThread* thr;
void operator()(eventfd_t eventcount) {
cppsp_request* req;
while((req=thr->req_queue.beginDequeue())!=NULL) {
Socket* sock=new Socket(req->fd,listensock.addressFamily,
listensock.type,listensock.protocol);
//printf("new socket: %p\n",sock);
p.add(*sock);
processRequest(*thr,p,*sock);
sock->release();
thr->req_queue.endDequeue();
}
}
} cb {p, thr};
thr->efd.repeatGetEvent(&cb);*/
MemoryPool handlerPool(sizeof(handler1),256);
struct {
Poll& p;
workerThread& thr;
MemoryPool& handlerPool;
int reqn;
void operator()(HANDLE sock) {
handler1* hdlr=new (handlerPool.alloc())
handler1(thr.srv,p,sock,thr.listenSock->addressFamily,
thr.listenSock->type,thr.listenSock->protocol);
hdlr->allocator=&handlerPool;
if(++reqn>10) {
reqn=0;
sched_yield();
}
}
} cb {p, thr, handlerPool, 0};
p.add(*thr.listenSock);
int nextModule=0;
struct ModuleLoader {
int& nextModule;
vector<moduleLoad>& modules;
Server* server;
Host* host;
int curThreadID;
Delegate<void(bool,exception*)> _cb;
void onError(const char* mod,exception& ex) {
tprintf("error loading module %s: %s\n",mod,ex.what());
cppsp::CompileException* ce = dynamic_cast<cppsp::CompileException*>(&ex);
if (ce != NULL) {
tprintf("compiler output:\n%s\n",ce->compilerOutput.c_str());
}
}
void printSuccess(ModuleInstance inst) {
if (inst.origin->info.name.length() == 0)
tprintf("module %s loaded\n", inst.origin->path.c_str());
else tprintf("module %s (%s) loaded\n", inst.origin->path.c_str(),
inst.origin->info.name.c_str());
}
void loadCB(ModuleInstance inst, exception* ex) {
if(ex!=nullptr) {
onError(modules[nextModule].path.c_str(),*ex);
_cb(false,ex);
return;
}
printSuccess(inst);
nextModule++;
bool b;
try {
b=doLoad();
} catch(exception& ex) {
_cb(false,&ex);
return;
}
if(b)_cb(true,nullptr);
}
bool doLoad() {
AsyncValue<ModuleInstance> tmp;
repeat:
if(nextModule>=(int)modules.size()) return true;
try {
if(modules[nextModule].host)
tmp=host->loadModule(modules[nextModule].path.c_str());
else tmp=server->loadModule(modules[nextModule].path.c_str());
} catch(exception& ex) {
onError(modules[nextModule].path.c_str(),ex);
throw;
}
if(tmp) {
printSuccess(tmp());
nextModule++;
goto repeat;
}
tmp.wait({&ModuleLoader::loadCB,this});
return false;
}
AsyncValue<bool> start() {
if(doLoad())return true;
return Future<bool>(&_cb);
}
} moduleLoader {nextModule,thr.modules,thr.srv.defaultServer,&thr.srv,curThreadID};
if(thr.modules.size()>0) {
tprintf("loading modules...\n");
}
auto moduleLoadCB=[&](bool b, exception* ex){
if(!b)exit(1);
if(thr.modules.size()>0) {
tprintf("...done. starting listening socket.\n");
}
thr.listenSock->repeatAcceptHandle(&cb);
};
auto val=moduleLoader.start();
if(val) moduleLoadCB(val(),nullptr);
else val.wait(&moduleLoadCB);
p.loop();
return NULL;
}
CP::Socket listensock;
int main(int argc, char** argv) {
{
char cwd[255];
if(getcwd(cwd,255)==NULL) throw runtime_error(strerror(errno));
rootDir=cwd;
}
string listen="0.0.0.0:80";
string tmpDir;
int threads=-1;
vector<string> cxxopts;
vector<moduleLoad> modules;
bool f0rk=false;
bool reusePort=true;
bool setAffinity=false;
bool debug=false;
try {
parseArgs(argc, argv,
[&](char* name, const std::function<char*()>& getvalue)
{
if(name==NULL) goto help;
if(strcmp(name,"r")==0) {
rootDir=getvalue();
} else if(strcmp(name,"c")==0) {
cxxopts.push_back(getvalue());
} else if(strcmp(name,"g")==0) {
cppsp::gxx=getvalue();
} else if(strcmp(name,"l")==0) {
listen=getvalue();
} else if(strcmp(name,"t")==0) {
threads=atoi(getvalue());
} else if(strcmp(name,"m")==0) {
modules.push_back({getvalue(),false,false});
} else if(strcmp(name,"M")==0) {
modules.push_back({getvalue(),true,false});
} else if(strcmp(name,"f")==0) {
f0rk=true;
} else if(strcmp(name,"s")==0) {
reusePort=false;
} else if(strcmp(name,"a")==0) {
setAffinity=true;
} else if(strcmp(name,"b")==0) {
tmpDir=getvalue();
} else if(strcmp(name,"d")==0) {
debug=true;
} else {
help:
fprintf(stderr,"usage: %s [options]...\noptions:\n"
"\t-l <host:port>: listen on specified host:port (default: 0.0.0.0:80)\n"
"\t-g <option>: specify the C++ compiler (default: g++)\n"
"\t-c <option>: specify a compiler option to be passed to g++\n"
"\t-m <path>: load a cppsp module (path is relative to root)\n"
"\t-M <path>: load a cppsp module into the host (path is absolute or relative to CWD; if relative then \"./\" must be used)\n"
"\t-r <root>: set root directory (must be absolute) (default: $(pwd))\n"
"\t-t <threads>: # of worker processes/threads to start up (default: sysconf(_SC_NPROCESSORS_CONF))\n"
"\t-f: use multi-processing (forking) instead of multi-threading (pthreads)\n"
"\t-a: automatically set cpu affinity of the created worker threads/processes\n"
"\t-b <path>: the directory in which temporary binaries are stored\n",argv[0]);
exit(1);
}
});
} catch(exception& ex) {
printerr("error: %s\nspecify -? for help",ex.what());
return 1;
}
printinfo("specify -? for help");
auto i=listen.rfind(':');
if(i==string::npos) throw runtime_error("expected \":\" in listen");
int cpus=(int)sysconf(_SC_NPROCESSORS_CONF);
if(threads<0)threads=cpus;
if(setAffinity) {
if(threads > cpus && (threads%(int)sysconf(_SC_NPROCESSORS_CONF) != 0)) {
printerr("warning: cpu affinity is to be set; thread count larger than and not divisible by cpu count");
}
}
EndPoint* ep=NULL;
struct {
bool& reusePort;
void operator()(int s) {
int optval = 1;
reusePort=(setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval))==0);
}
} initsock {reusePort};
if(reusePort)
listensock.bind(listen.substr(0,i).c_str(),
listen.substr(i + 1, listen.length() - i - 1).c_str(), AF_UNSPEC, SOCK_STREAM,0,0,&initsock);
else
listensock.bind(listen.substr(0,i).c_str(),
listen.substr(i + 1, listen.length() - i - 1).c_str(), AF_UNSPEC, SOCK_STREAM);
if(reusePort) {
printinfo("using SO_REUSEPORT");
ep=listensock.getLocalEndPoint();
} else {
printerr("NOT using SO_REUSEPORT");
listensock.listen(CPPSP_LISTEN_BACKLOG);
}
//p.add(listensock);
PRINTSIZE(CP::Socket);
PRINTSIZE(cppspServer::handler);
PRINTSIZE(handler1);
if(f0rk) printinfo("starting %i processes",threads);
else printinfo("starting %i threads",threads);
workerThread* th=(workerThread*)new char[sizeof(workerThread)*threads];
for(int i=0;i<threads;i++) {
int cpu=i%cpus;
Socket* newsock;
if(reusePort) {
newsock=new Socket(listensock.addressFamily, listensock.type, listensock.protocol);
int optval = 1;
assert(setsockopt(newsock->handle, SOL_SOCKET, SO_REUSEPORT, &optval, sizeof(optval))==0);
newsock->bind(*ep);
newsock->listen(CPPSP_LISTEN_BACKLOG);
} else {
newsock=new Socket(f0rk ? listensock.handle : dup(listensock.handle),
listensock.addressFamily, listensock.type, listensock.protocol);
}
workerThread& tmp=*(new (th+i) workerThread(*newsock));
tmp.cpu=(setAffinity?cpu:-1);
newsock->release();
tmp.srv.mgr->cxxopts=cxxopts;
tmp.srv.mgr->tmpDir=tmpDir;
tmp.srv.mgr->debug=debug;
tmp.modules=modules;
tmp.srv.threadID=i;
if(threads==1) {
thread1(&tmp);
return 0;
}
if(f0rk) {
pid_t pid=fork();
if(pid==0) {
tmp.pid=getpid();
srand(int(tmp.pid)^(int)time(NULL));
thread1(&tmp);
return 0;
} else if(pid>0) {
tmp.pid=pid;
//delete newsock;
} else {
perror("fork");
return 1;
}
} else {
if (pthread_create(&tmp.thread, NULL, thread1, &tmp) != 0) {
throw runtime_error(strerror(errno));
}
}
}
if(f0rk) {
static workerThread* _threads;
static int _threadcount;
struct sig_handler
{
static void a(int sig) {
for(int i=0;i<_threadcount;i++) {
kill(_threads[i].pid, 9);
}
exit(0);
}
};
_threads=th;
_threadcount=threads;
struct sigaction sa;
sa.sa_handler = &sig_handler::a;
sigemptyset(&sa.sa_mask);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGSEGV, &sa, NULL);
}
union {
int status1;
void* status2;
};
int ret=0;
if(f0rk) while(true) {
pid_t child=wait(&status1);
if(child<=0)return ret;
printf("child %i exited with code %i\n",(int)child,status1);
if(status1!=0)ret=status1;
} else for(int i=0;i<threads;i++)
pthread_join(th[i].thread,&status2);
}
void parseArgs(int argc, char** argv, const function<void(char*, const function<char*()>&)>& cb) {
int i = 1;
function<char*()> func = [&]()->char*
{
if(i+1>=argc)throw logic_error(string(argv[i])+" requires an argument");
return argv[(++i)];
};
for (; i < argc; i++) {
if (argv[i][0] == '\x00') continue;
if (argv[i][0] == '-') {
cb(argv[i] + 1, func);
} else {
cb(NULL, [argv,i]()
{ return argv[i];});
}
}
}