source: benchmark/io/http/main.cfa@ 80d16f8

ADT ast-experimental pthread-emulation
Last change on this file since 80d16f8 was 3f95dab, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Committing hopefully last version of the webserver

  • Property mode set to 100644
File size: 11.4 KB
RevLine 
[1e8b4b49]1#define _GNU_SOURCE
[0aec496]2
3#include <errno.h>
[153570d]4#include <signal.h>
[0aec496]5#include <stdio.h>
6#include <string.h>
7#include <unistd.h>
8extern "C" {
[6117fc0]9 #include <sched.h>
[c2df3031]10 #include <signal.h>
[153570d]11 #include <sys/eventfd.h>
[0aec496]12 #include <sys/socket.h>
13 #include <netinet/in.h>
14}
15
[8c43d05]16#include <fstream.hfa>
[0aec496]17#include <kernel.hfa>
[ce98816]18#include <locks.hfa>
[153dc387]19#include <iofwd.hfa>
[0aec496]20#include <stats.hfa>
[d11d6eb]21#include <time.hfa>
[0aec496]22#include <thread.hfa>
23
24#include "filecache.hfa"
25#include "options.hfa"
[c4b10e2]26#include "socket.hfa"
[137974ae]27#include "printer.hfa"
[0aec496]28#include "worker.hfa"
29
[d11d6eb]30extern void register_fixed_files( cluster &, int *, unsigned count );
31
32Duration default_preemption() {
33 return 0;
34}
35
[348f81d5]36//=============================================================================================
37// Globals
38//=============================================================================================
39void ?{}( ServerCluster & this ) {
40 (this.self){ "Server Cluster", options.clopts.params };
41
[6117fc0]42 cpu_set_t fullset;
43 CPU_ZERO(&fullset);
44 int ret = sched_getaffinity(getpid(), sizeof(fullset), &fullset);
45 if( ret != 0 ) abort | "sched_getaffinity failed with" | errno | strerror( errno );
46 int cnt = CPU_COUNT(&fullset);
47
[348f81d5]48 this.procs = alloc(options.clopts.nprocs);
49 for(i; options.clopts.nprocs) {
50 (this.procs[i]){ "Benchmark Processor", this.self };
51
[329e26a]52 // int c = 0;
53 // int n = 1 + (i % cnt);
54 // for(int j = 0; j < CPU_SETSIZE; j++) {
55 // if(CPU_ISSET(j, &fullset)) n--;
56 // if(n == 0) {
57 // c = j;
58 // break;
59 // }
60 // }
61 // cpu_set_t localset;
62 // CPU_ZERO(&localset);
63 // CPU_SET(c, &localset);
64 // ret = pthread_setaffinity_np(this.procs[i].kernel_thread, sizeof(localset), &localset);
65 // if( ret != 0 ) abort | "sched_getaffinity failed with" | ret | strerror( ret );
[6117fc0]66
[348f81d5]67 #if !defined(__CFA_NO_STATISTICS__)
68 if( options.clopts.procstats ) {
69 print_stats_at_exit( *this.procs, this.self.print_stats );
70 }
71 if( options.clopts.viewhalts ) {
72 print_halts( *this.procs );
73 }
74 #endif
75 }
76
77 #if !defined(__CFA_NO_STATISTICS__)
78 print_stats_at_exit( this.self, CFA_STATS_READY_Q | CFA_STATS_IO );
79 #endif
80
[329e26a]81 options.clopts.instance[options.clopts.cltr_cnt] = &this.self;
82 options.clopts.cltr_cnt++;
[153dc387]83}
84
[348f81d5]85void ^?{}( ServerCluster & this ) {
86 for(i; options.clopts.nprocs) {
87 ^(this.procs[i]){};
88 }
89 free(this.procs);
90
91 ^(this.self){};
92}
93
94extern void init_protocol(void);
95extern void deinit_protocol(void);
96
[7f0ac12]97//=============================================================================================
98// REUSEPORT
99//=============================================================================================
100
101size_t sockarr_size;
102struct __attribute__((aligned(128))) Q {
103 mpsc_queue(PendingRead) q;
104};
105
[153570d]106//=============================================================================================
107// Termination
108//=============================================================================================
109
110int closefd;
111void cleanstop(int) {
112 eventfd_t buffer = 1;
113 char * buffer_s = (char*)&buffer;
114 int ret = write(closefd, buffer_s, sizeof(buffer));
115 if(ret < 0) abort( "eventfd write error: (%d) %s\n", (int)errno, strerror(errno) );
116 return;
117}
118
[0aec496]119//=============================================================================================
120// Main
121//============================================================================================='
122int main( int argc, char * argv[] ) {
[c4b10e2]123 int ret;
[c2df3031]124 __sighandler_t s = 1p;
125 signal(SIGPIPE, s);
126
[0aec496]127 //===================
128 // Parse args
[b57db73]129 parse_options(argc, argv);
[0aec496]130
[153570d]131 //===================
132 // Setup non-interactive termination
133 if(!options.interactive) {
134 closefd = eventfd(0, 0);
135 if(closefd < 0) abort( "eventfd error: (%d) %s\n", (int)errno, strerror(errno) );
136
137 sighandler_t prev = signal(SIGTERM, cleanstop);
138 intptr_t prev_workaround = (intptr_t) prev;
139 // can't use SIG_ERR it crashes the compiler
140 if(prev_workaround == -1) abort( "signal setup error: (%d) %s\n", (int)errno, strerror(errno) );
141
142 sout | "Signal termination ready";
143 }
144
[0aec496]145 //===================
146 // Open Files
[b57db73]147 if( options.file_cache.path ) {
148 sout | "Filling cache from" | options.file_cache.path;
149 fill_cache( options.file_cache.path );
150 }
[0aec496]151
152 //===================
153 // Open Socket
[8c43d05]154 sout | getpid() | ": Listening on port" | options.socket.port;
[0aec496]155
156 struct sockaddr_in address;
[c4b10e2]157 int addrlen = prepaddr(address);
[0aec496]158
[86c12d65]159 int server_fd;
[0aec496]160
161 //===================
162 // Run Server Cluster
163 {
[d9c2284]164 int pipe_cnt = options.clopts.nworkers * 2;
165 int pipe_off;
166 int * fds;
167 [fds, pipe_off] = filefds( pipe_cnt );
168 for(i; 0 ~ pipe_cnt ~ 2) {
169 int ret = pipe(&fds[pipe_off + i]);
170 if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
171 }
172
[4f762d3]173 // if(options.file_cache.path && options.file_cache.fixed_fds) {
174 // register_fixed_files(cl, fds, pipe_off);
175 // }
[d11d6eb]176
[0aec496]177 {
[153570d]178 // Stats printer makes a copy so this needs to persist longer than normal
[7f0ac12]179 connection ** conns;
180 AcceptWorker * aworkers = 0p;
181 ChannelWorker * cworkers = 0p;
182 Acceptor * acceptors = 0p;
183 Q * queues = 0p;
[329e26a]184 ServerCluster cl[options.clopts.nclusters];
[ece0e80]185
[137974ae]186 if(options.stats) {
187 stats_thrd = alloc();
[329e26a]188 (*stats_thrd){ cl };
[137974ae]189 } else {
190 stats_thrd = 0p;
191 }
192
[ece0e80]193 init_protocol();
[0aec496]194 {
[329e26a]195 int nacceptors = options.clopts.nprocs * options.clopts.nclusters;
[7f0ac12]196 conns = alloc(options.clopts.nworkers);
197 if(options.socket.reuseport) {
[329e26a]198 queues = alloc(nacceptors);
199 acceptors = alloc(nacceptors);
200 sout | "Creating" | nacceptors | "Acceptors";
201 for(i; nacceptors) {
202 (acceptors[i]){ i % options.clopts.nclusters };
203 }
204 for(i; nacceptors) {
[7f0ac12]205 (queues[i]){};
206 {
207 acceptors[i].sockfd = listener(address, addrlen);
208 acceptors[i].addr = (struct sockaddr *)&address;
209 acceptors[i].addrlen = (socklen_t*)&addrlen;
210 acceptors[i].flags = 0;
211 acceptors[i].queue = &queues[i].q;
212 }
213 unpark( acceptors[i] );
214 }
215
216 cworkers = anew(options.clopts.nworkers);
217 for(i; options.clopts.nworkers) {
218 {
219 cworkers[i].conn.pipe[0] = fds[pipe_off + (i * 2) + 0];
220 cworkers[i].conn.pipe[1] = fds[pipe_off + (i * 2) + 1];
[329e26a]221 cworkers[i].queue = &queues[i % nacceptors].q;
[7f0ac12]222 conns[i] = &cworkers[i].conn;
223 }
224 unpark( cworkers[i] );
225 }
226 }
227 else {
228 server_fd = listener(address, addrlen);
229 aworkers = anew(options.clopts.nworkers);
230 for(i; options.clopts.nworkers) {
231 // if( options.file_cache.fixed_fds ) {
232 // workers[i].pipe[0] = pipe_off + (i * 2) + 0;
233 // workers[i].pipe[1] = pipe_off + (i * 2) + 1;
234 // }
235 // else
236 {
237 aworkers[i].conn.pipe[0] = fds[pipe_off + (i * 2) + 0];
238 aworkers[i].conn.pipe[1] = fds[pipe_off + (i * 2) + 1];
239 aworkers[i].sockfd = server_fd;
240 aworkers[i].addr = (struct sockaddr *)&address;
241 aworkers[i].addrlen = (socklen_t*)&addrlen;
242 aworkers[i].flags = 0;
243 conns[i] = &aworkers[i].conn;
244 }
245 unpark( aworkers[i] );
[d9c2284]246 }
247 }
[329e26a]248 sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors /" | options.clopts.nclusters | "clusters";
249 for(i; options.clopts.nclusters) {
250 sout | options.clopts.thrd_cnt[i] | nonl;
251 }
[348f81d5]252 sout | nl;
[0aec496]253 {
[153570d]254 if(options.interactive) {
255 char buffer[128];
256 for() {
257 int ret = cfa_read(0, buffer, 128, 0);
258 if(ret == 0) break;
259 if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
260 sout | "User wrote '" | "" | nonl;
261 write(sout, buffer, ret - 1);
262 sout | "'";
263 }
264 }
265 else {
266 char buffer[sizeof(eventfd_t)];
267 int ret = cfa_read(closefd, buffer, sizeof(eventfd_t), 0);
[153dc387]268 if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
[e95a117]269 }
270
[8c43d05]271 sout | "Shutdown received";
[0aec496]272 }
[ece0e80]273
[7f0ac12]274 //===================
275 // Close Socket and join
276 if(options.socket.reuseport) {
277 sout | "Notifying connections..." | nonl; flush( sout );
[329e26a]278 for(i; nacceptors) {
[7f0ac12]279 acceptors[i].done = true;
280 }
281 for(i; options.clopts.nworkers) {
282 cworkers[i].done = true;
283 }
284 sout | "done";
285
286 sout | "Shutting down Socket..." | nonl; flush( sout );
[329e26a]287 for(i; nacceptors) {
[7f0ac12]288 ret = shutdown( acceptors[i].sockfd, SHUT_RD );
289 if( ret < 0 ) {
290 abort( "shutdown1 error: (%d) %s\n", (int)errno, strerror(errno) );
291 }
292 }
293 sout | "done";
[ece0e80]294
[7f0ac12]295 sout | "Closing Socket..." | nonl; flush( sout );
[329e26a]296 for(i; nacceptors) {
[7f0ac12]297 ret = close( acceptors[i].sockfd );
298 if( ret < 0) {
299 abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
300 }
301 }
302 sout | "done";
303
[3f95dab]304 //===================
305 // Close Files
306 if( options.file_cache.path ) {
307 sout | "Closing open files..." | nonl; flush( sout );
308 close_cache();
309 sout | "done";
310 }
311
[7f0ac12]312 sout | "Stopping accept threads..." | nonl; flush( sout );
[329e26a]313 for(i; nacceptors) {
[7f0ac12]314 join(acceptors[i]);
315 }
316 sout | "done";
317
318 sout | "Draining worker queues..." | nonl; flush( sout );
[329e26a]319 for(i; nacceptors) {
[7f0ac12]320 PendingRead * p = 0p;
321 while(p = pop(queues[i].q)) {
322 fulfil(p->f, -ECONNRESET);
323 }
324 }
325 sout | "done";
326
327 sout | "Stopping worker threads..." | nonl; flush( sout );
[86c12d65]328 for(i; options.clopts.nworkers) {
[7f0ac12]329 for(j; 2) {
330 ret = close(cworkers[i].conn.pipe[j]);
331 if(ret < 0) abort( "close pipe %d error: (%d) %s\n", j, (int)errno, strerror(errno) );
332 }
333 join(cworkers[i]);
[86c12d65]334 }
335 }
336 else {
[7f0ac12]337 sout | "Notifying connections..." | nonl; flush( sout );
338 for(i; options.clopts.nworkers) {
339 aworkers[i].done = true;
340 }
341 sout | "done";
342
343 sout | "Shutting down Socket..." | nonl; flush( sout );
[86c12d65]344 ret = shutdown( server_fd, SHUT_RD );
345 if( ret < 0 ) {
[7f0ac12]346 abort( "shutdown2 error: (%d) %s\n", (int)errno, strerror(errno) );
[86c12d65]347 }
[7f0ac12]348 sout | "done";
[ece0e80]349
[7f0ac12]350 sout | "Closing Socket..." | nonl; flush( sout );
[86c12d65]351 ret = close( server_fd );
352 if(ret < 0) {
353 abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
354 }
[7f0ac12]355 sout | "done";
[0197418]356
[3f95dab]357 //===================
358 // Close Files
359 if( options.file_cache.path ) {
360 sout | "Closing open files..." | nonl; flush( sout );
361 close_cache();
362 sout | "done";
363 }
364
[7f0ac12]365 sout | "Stopping connection threads..." | nonl; flush( sout );
366 for(i; options.clopts.nworkers) {
367 for(j; 2) {
368 ret = close(aworkers[i].conn.pipe[j]);
369 if(ret < 0) abort( "close pipe %d error: (%d) %s\n", j, (int)errno, strerror(errno) );
370 }
371 join(aworkers[i]);
[c4b10e2]372 }
[153570d]373 }
[0aec496]374 }
[8c43d05]375 sout | "done";
[ece0e80]376
[b57db73]377 sout | "Stopping protocol threads..." | nonl; flush( sout );
[ece0e80]378 deinit_protocol();
[8c43d05]379 sout | "done";
380
[153570d]381 sout | "Stopping printer threads..." | nonl; flush( sout );
[137974ae]382 if(stats_thrd) {
383 notify_one(stats_thrd->var);
[153570d]384 }
[137974ae]385 delete(stats_thrd);
[153570d]386 sout | "done";
387
388 // Now that the stats printer is stopped, we can reclaim this
[7f0ac12]389 adelete(aworkers);
390 adelete(cworkers);
391 adelete(acceptors);
392 adelete(queues);
393 free(conns);
[153570d]394
[348f81d5]395 sout | "Stopping processors/clusters..." | nonl; flush( sout );
[0aec496]396 }
[8c43d05]397 sout | "done";
[d9c2284]398
399 free(fds);
[c3ee5f3]400
[b57db73]401 sout | "Stopping processors..." | nonl; flush( sout );
[0aec496]402 }
[8c43d05]403 sout | "done";
[ef3c383]404}
405
406const size_t zipf_sizes[] = { 102, 204, 307, 409, 512, 614, 716, 819, 921, 1024, 2048, 3072, 4096, 5120, 6144, 7168, 8192, 9216, 10240, 20480, 30720, 40960, 51200, 61440, 71680, 81920, 92160, 102400, 204800, 307200, 409600, 512000, 614400, 716800, 819200, 921600 };
407static_assert(zipf_cnts == sizeof(zipf_sizes) / sizeof(zipf_sizes[0]));
Note: See TracBrowser for help on using the repository browser.