source: benchmark/io/http/main.cfa @ c8f5f7d

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

Stats printer now uses a condition variable to sleep so it exists immediately.

  • Property mode set to 100644
File size: 10.4 KB
Line 
1#define _GNU_SOURCE
2
3#include <errno.h>
4#include <signal.h>
5#include <stdio.h>
6#include <string.h>
7#include <unistd.h>
8extern "C" {
9        #include <sched.h>
10        #include <signal.h>
11        #include <sys/eventfd.h>
12        #include <sys/socket.h>
13        #include <netinet/in.h>
14}
15
16#include <fstream.hfa>
17#include <kernel.hfa>
18#include <locks.hfa>
19#include <iofwd.hfa>
20#include <stats.hfa>
21#include <time.hfa>
22#include <thread.hfa>
23
24#include "filecache.hfa"
25#include "options.hfa"
26#include "socket.hfa"
27#include "worker.hfa"
28
29extern void register_fixed_files( cluster &, int *, unsigned count );
30
31Duration default_preemption() {
32        return 0;
33}
34
35//=============================================================================================
36// Stats Printer
37//============================================================================================='
38
39thread StatsPrinter {
40        Worker * workers;
41        int worker_cnt;
42        condition_variable(fast_block_lock) var;
43};
44
45void ?{}( StatsPrinter & this, cluster & cl ) {
46        ((thread&)this){ "Stats Printer Thread", cl };
47        this.worker_cnt = 0;
48}
49
50void ^?{}( StatsPrinter & mutex this ) {}
51
52#define eng3(X) (ws(3, 3, unit(eng( X ))))
53
54void main(StatsPrinter & this) {
55        LOOP: for() {
56                waitfor( ^?{} : this) {
57                        break LOOP;
58                }
59                or else {}
60
61                wait(this.var, 10`s);
62
63                print_stats_now( *active_cluster(), CFA_STATS_READY_Q | CFA_STATS_IO );
64                if(this.worker_cnt != 0) {
65                        uint64_t tries = 0;
66                        uint64_t calls = 0;
67                        uint64_t header = 0;
68                        uint64_t splcin = 0;
69                        uint64_t splcot = 0;
70                        struct {
71                                volatile uint64_t calls;
72                                volatile uint64_t bytes;
73                        } avgrd[zipf_cnts];
74                        memset(avgrd, 0, sizeof(avgrd));
75
76                        for(i; this.worker_cnt) {
77                                tries += this.workers[i].stats.sendfile.tries;
78                                calls += this.workers[i].stats.sendfile.calls;
79                                header += this.workers[i].stats.sendfile.header;
80                                splcin += this.workers[i].stats.sendfile.splcin;
81                                splcot += this.workers[i].stats.sendfile.splcot;
82                                for(j; zipf_cnts) {
83                                        avgrd[j].calls += this.workers[i].stats.sendfile.avgrd[j].calls;
84                                        avgrd[j].bytes += this.workers[i].stats.sendfile.avgrd[j].bytes;
85                                }
86                        }
87
88                        double ratio = ((double)tries) / calls;
89
90                        sout | "----- Worker Stats -----";
91                        sout | "sendfile  : " | calls | "calls," | tries | "tries (" | ratio | " try/call)";
92                        sout | "            " | header | "header," | splcin | "splice in," | splcot | "splice out";
93                        sout | " - zipf sizes:";
94                        for(i; zipf_cnts) {
95                                double written = avgrd[i].calls > 0 ? ((double)avgrd[i].bytes) / avgrd[i].calls : 0;
96                                sout | "        " | zipf_sizes[i] | "bytes," | avgrd[i].calls | "shorts," | written | "written";
97                        }
98                }
99                else {
100                        sout | "No Workers!";
101                }
102        }
103}
104
105//=============================================================================================
106// Globals
107//=============================================================================================
108struct ServerCluster {
109        cluster self;
110        processor    * procs;
111        // io_context   * ctxs;
112        StatsPrinter * prnt;
113
114};
115
116void ?{}( ServerCluster & this ) {
117        (this.self){ "Server Cluster", options.clopts.params };
118
119        cpu_set_t fullset;
120        CPU_ZERO(&fullset);
121        int ret = sched_getaffinity(getpid(), sizeof(fullset), &fullset);
122        if( ret != 0 ) abort | "sched_getaffinity failed with" | errno | strerror( errno );
123        int cnt = CPU_COUNT(&fullset);
124
125        this.procs = alloc(options.clopts.nprocs);
126        for(i; options.clopts.nprocs) {
127                (this.procs[i]){ "Benchmark Processor", this.self };
128
129                int c = 0;
130                int n = 1 + (i % cnt);
131                for(int j = 0; j < CPU_SETSIZE; j++) {
132                        if(CPU_ISSET(j, &fullset)) n--;
133                        if(n == 0) {
134                                c = j;
135                                break;
136                        }
137                }
138                cpu_set_t localset;
139                CPU_ZERO(&localset);
140                CPU_SET(c, &localset);
141                ret = pthread_setaffinity_np(this.procs[i].kernel_thread, sizeof(localset), &localset);
142                if( ret != 0 ) abort | "sched_getaffinity failed with" | ret | strerror( ret );
143
144                #if !defined(__CFA_NO_STATISTICS__)
145                        if( options.clopts.procstats ) {
146                                print_stats_at_exit( *this.procs, this.self.print_stats );
147                        }
148                        if( options.clopts.viewhalts ) {
149                                print_halts( *this.procs );
150                        }
151                #endif
152        }
153
154        if(options.stats) {
155                this.prnt = alloc();
156                (*this.prnt){ this.self };
157        } else {
158                this.prnt = 0p;
159        }
160
161        #if !defined(__CFA_NO_STATISTICS__)
162                print_stats_at_exit( this.self, CFA_STATS_READY_Q | CFA_STATS_IO );
163        #endif
164
165        options.clopts.instance[options.clopts.cltr_cnt] = &this.self;
166        options.clopts.cltr_cnt++;
167}
168
169void ^?{}( ServerCluster & this ) {
170        delete(this.prnt);
171
172        for(i; options.clopts.nprocs) {
173                ^(this.procs[i]){};
174        }
175        free(this.procs);
176
177        ^(this.self){};
178}
179
180extern void init_protocol(void);
181extern void deinit_protocol(void);
182
183//=============================================================================================
184// Termination
185//=============================================================================================
186
187int closefd;
188void cleanstop(int) {
189        eventfd_t buffer = 1;
190        char * buffer_s = (char*)&buffer;
191        int ret = write(closefd, buffer_s, sizeof(buffer));
192        if(ret < 0) abort( "eventfd write error: (%d) %s\n", (int)errno, strerror(errno) );
193        return;
194}
195
196//=============================================================================================
197// Main
198//============================================================================================='
199int main( int argc, char * argv[] ) {
200        int ret;
201        __sighandler_t s = 1p;
202        signal(SIGPIPE, s);
203
204        //===================
205        // Parse args
206        parse_options(argc, argv);
207
208        //===================
209        // Setup non-interactive termination
210        if(!options.interactive) {
211                closefd = eventfd(0, 0);
212                if(closefd < 0) abort( "eventfd error: (%d) %s\n", (int)errno, strerror(errno) );
213
214                sighandler_t prev = signal(SIGTERM, cleanstop);
215                intptr_t prev_workaround = (intptr_t) prev;
216                // can't use SIG_ERR it crashes the compiler
217                if(prev_workaround == -1) abort( "signal setup error: (%d) %s\n", (int)errno, strerror(errno) );
218
219                sout | "Signal termination ready";
220        }
221
222        //===================
223        // Open Files
224        if( options.file_cache.path ) {
225                sout | "Filling cache from" | options.file_cache.path;
226                fill_cache( options.file_cache.path );
227        }
228
229        //===================
230        // Open Socket
231        sout | getpid() | ": Listening on port" | options.socket.port;
232
233        struct sockaddr_in address;
234        int addrlen = prepaddr(address);
235
236        int server_fd = listener(address, addrlen);
237
238        //===================
239        // Run Server Cluster
240        {
241                int pipe_cnt = options.clopts.nworkers * 2;
242                int pipe_off;
243                int * fds;
244                [fds, pipe_off] = filefds( pipe_cnt );
245                for(i; 0 ~ pipe_cnt ~ 2) {
246                        int ret = pipe(&fds[pipe_off + i]);
247                        if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
248                }
249
250                // if(options.file_cache.path && options.file_cache.fixed_fds) {
251                //      register_fixed_files(cl, fds, pipe_off);
252                // }
253
254                {
255                        // Stats printer makes a copy so this needs to persist longer than normal
256                        Worker * workers;
257                        ServerCluster cl[options.clopts.nclusters];
258
259                        init_protocol();
260                        {
261                                workers = anew(options.clopts.nworkers);
262                                cl[0].prnt->workers = workers;
263                                cl[0].prnt->worker_cnt = options.clopts.nworkers;
264                                for(i; options.clopts.nworkers) {
265                                        // if( options.file_cache.fixed_fds ) {
266                                        //      workers[i].pipe[0] = pipe_off + (i * 2) + 0;
267                                        //      workers[i].pipe[1] = pipe_off + (i * 2) + 1;
268                                        // }
269                                        // else
270                                        {
271                                                workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
272                                                workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
273                                                workers[i].sockfd  = server_fd;
274                                                workers[i].addr    = (struct sockaddr *)&address;
275                                                workers[i].addrlen = (socklen_t*)&addrlen;
276                                                workers[i].flags   = 0;
277                                        }
278                                        unpark( workers[i] );
279                                }
280                                sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors /" | options.clopts.nclusters | "clusters";
281                                for(i; options.clopts.nclusters) {
282                                        sout | options.clopts.thrd_cnt[i] | nonl;
283                                }
284                                sout | nl;
285                                {
286                                        if(options.interactive) {
287                                                char buffer[128];
288                                                for() {
289                                                        int ret = cfa_read(0, buffer, 128, 0);
290                                                        if(ret == 0) break;
291                                                        if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
292                                                        sout | "User wrote '" | "" | nonl;
293                                                        write(sout, buffer, ret - 1);
294                                                        sout | "'";
295                                                }
296                                        }
297                                        else {
298                                                char buffer[sizeof(eventfd_t)];
299                                                int ret = cfa_read(closefd, buffer, sizeof(eventfd_t), 0);
300                                                if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
301                                        }
302
303                                        sout | "Shutdown received";
304                                }
305
306                                sout | "Notifying connections..." | nonl; flush( sout );
307                                for(i; options.clopts.nworkers) {
308                                        workers[i].done = true;
309                                }
310                                sout | "done";
311
312                                sout | "Shutting down socket..." | nonl; flush( sout );
313                                int ret = shutdown( server_fd, SHUT_RD );
314                                if( ret < 0 ) {
315                                        abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) );
316                                }
317                                sout | "done";
318
319                                //===================
320                                // Close Socket
321                                sout | "Closing Socket..." | nonl; flush( sout );
322                                ret = close( server_fd );
323                                if(ret < 0) {
324                                        abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
325                                }
326                                sout | "done";
327
328                                sout | "Stopping connection threads..." | nonl; flush( sout );
329                                for(i; options.clopts.nworkers) {
330                                        for(j; 2) {
331                                                ret = close(workers[i].pipe[j]);
332                                                if(ret < 0) abort( "close pipe %d error: (%d) %s\n", j, (int)errno, strerror(errno) );
333                                        }
334                                        join(workers[i]);
335                                }
336                        }
337                        sout | "done";
338
339                        sout | "Stopping protocol threads..." | nonl; flush( sout );
340                        deinit_protocol();
341                        sout | "done";
342
343                        sout | "Stopping printer threads..." | nonl; flush( sout );
344                        for(i; options.clopts.nclusters) {
345                                StatsPrinter * p = cl[i].prnt;
346                                if(p) {
347                                        notify_one(p->var);
348                                        join(*p);
349                                }
350                        }
351                        sout | "done";
352
353                        // Now that the stats printer is stopped, we can reclaim this
354                        adelete(workers);
355
356                        sout | "Stopping processors/clusters..." | nonl; flush( sout );
357                }
358                sout | "done";
359
360                // sout | "Closing splice fds..." | nonl; flush( sout );
361                // for(i; pipe_cnt) {
362                //      ret = close( fds[pipe_off + i] );
363                //      if(ret < 0) {
364                //              abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) );
365                //      }
366                // }
367                free(fds);
368                sout | "done";
369
370                sout | "Stopping processors..." | nonl; flush( sout );
371        }
372        sout | "done";
373
374        //===================
375        // Close Files
376        if( options.file_cache.path ) {
377                sout | "Closing open files..." | nonl; flush( sout );
378                close_cache();
379                sout | "done";
380        }
381}
382
383const 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 };
384static_assert(zipf_cnts == sizeof(zipf_sizes) / sizeof(zipf_sizes[0]));
Note: See TracBrowser for help on using the repository browser.