source: benchmark/io/http/main.cfa @ 1e8b4b49

ADTast-experimentalenumforall-pointer-decaypthread-emulationqualifiedEnum
Last change on this file since 1e8b4b49 was 1e8b4b49, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Added work around for CFA bug with implicit conversion to unions.

  • Property mode set to 100644
File size: 7.2 KB
RevLine 
[1e8b4b49]1#define _GNU_SOURCE
[0aec496]2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7extern "C" {
[c2df3031]8        #include <signal.h>
[0aec496]9        #include <sys/socket.h>
10        #include <netinet/in.h>
11}
12
[8c43d05]13#include <fstream.hfa>
[0aec496]14#include <kernel.hfa>
[153dc387]15#include <iofwd.hfa>
[0aec496]16#include <stats.hfa>
[d11d6eb]17#include <time.hfa>
[0aec496]18#include <thread.hfa>
19
20#include "filecache.hfa"
21#include "options.hfa"
22#include "worker.hfa"
23
[d11d6eb]24extern void register_fixed_files( cluster &, int *, unsigned count );
25
26Duration default_preemption() {
27        return 0;
28}
29
[153dc387]30//=============================================================================================
31// Stats Printer
32//============================================================================================='
33
34thread StatsPrinter {};
35
[348f81d5]36void ?{}( StatsPrinter & this, cluster & cl ) {
37        ((thread&)this){ "Stats Printer Thread", cl };
[153dc387]38}
39
[2cd784a]40void ^?{}( StatsPrinter & mutex this ) {}
41
[153dc387]42void main(StatsPrinter & this) {
43        LOOP: for() {
44                waitfor( ^?{} : this) {
45                        break LOOP;
46                }
47                or else {}
48
49                sleep(10`s);
50
[348f81d5]51                print_stats_now( *active_cluster(), CFA_STATS_READY_Q | CFA_STATS_IO );
52        }
53}
54
55//=============================================================================================
56// Globals
57//=============================================================================================
58struct ServerCluster {
59        cluster self;
60        processor    * procs;
[2cd784a]61        // io_context   * ctxs;
[348f81d5]62        StatsPrinter * prnt;
63
64};
65
66void ?{}( ServerCluster & this ) {
67        (this.self){ "Server Cluster", options.clopts.params };
68
69        this.procs = alloc(options.clopts.nprocs);
70        for(i; options.clopts.nprocs) {
71                (this.procs[i]){ "Benchmark Processor", this.self };
72
73                #if !defined(__CFA_NO_STATISTICS__)
74                        if( options.clopts.procstats ) {
75                                print_stats_at_exit( *this.procs, this.self.print_stats );
76                        }
77                        if( options.clopts.viewhalts ) {
78                                print_halts( *this.procs );
79                        }
80                #endif
81        }
82
[2cd784a]83        if(options.stats) {
84                this.prnt = alloc();
85                (*this.prnt){ this.self };
86        } else {
87                this.prnt = 0p;
[348f81d5]88        }
89
90        #if !defined(__CFA_NO_STATISTICS__)
91                print_stats_at_exit( this.self, CFA_STATS_READY_Q | CFA_STATS_IO );
92        #endif
93
94        options.clopts.instance[options.clopts.cltr_cnt] = &this.self;
95        options.clopts.cltr_cnt++;
[153dc387]96}
97
[348f81d5]98void ^?{}( ServerCluster & this ) {
[2cd784a]99        delete(this.prnt);
[348f81d5]100
101        for(i; options.clopts.nprocs) {
102                ^(this.procs[i]){};
103        }
104        free(this.procs);
105
106        ^(this.self){};
107}
108
109extern void init_protocol(void);
110extern void deinit_protocol(void);
111
[0aec496]112//=============================================================================================
113// Main
114//============================================================================================='
115int main( int argc, char * argv[] ) {
[c2df3031]116        __sighandler_t s = 1p;
117        signal(SIGPIPE, s);
118
[0aec496]119        //===================
120        // Parse args
[b57db73]121        parse_options(argc, argv);
[0aec496]122
123        //===================
124        // Open Files
[b57db73]125        if( options.file_cache.path ) {
126                sout | "Filling cache from" | options.file_cache.path;
127                fill_cache( options.file_cache.path );
128        }
[0aec496]129
130        //===================
131        // Open Socket
[8c43d05]132        sout | getpid() | ": Listening on port" | options.socket.port;
[0aec496]133        int server_fd = socket(AF_INET, SOCK_STREAM, 0);
134        if(server_fd < 0) {
135                abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) );
136        }
137
[7f389a5c]138        int ret = 0;
[0aec496]139        struct sockaddr_in address;
140        int addrlen = sizeof(address);
141        memset( (char *)&address, '\0' );
142        address.sin_family = AF_INET;
143        address.sin_addr.s_addr = htonl(INADDR_ANY);
[2ecbd7b]144        address.sin_port = htons( options.socket.port );
[0aec496]145
[ee913e0a]146        int waited = 0;
147        for() {
[1e8b4b49]148                int sockfd = server_fd;
149                __CONST_SOCKADDR_ARG addr;
150                addr.__sockaddr__ = (struct sockaddr *)&address;
151                socklen_t addrlen = sizeof(address);
152                ret = bind( sockfd, addr, addrlen );
[ee913e0a]153                if(ret < 0) {
[c3ee5f3]154                        if(errno == EADDRINUSE) {
[ee913e0a]155                                if(waited == 0) {
[3a0ddb6]156                                        if(!options.interactive) abort | "Port already in use in non-interactive mode. Aborting";
[8c43d05]157                                        sout | "Waiting for port";
[ee913e0a]158                                } else {
[8c43d05]159                                        sout | "\r" | waited | nonl;
160                                        flush( sout );
[ee913e0a]161                                }
162                                waited ++;
163                                sleep( 1`s );
164                                continue;
165                        }
166                        abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
167                }
168                break;
[0aec496]169        }
170
[2ecbd7b]171        ret = listen( server_fd, options.socket.backlog );
[0aec496]172        if(ret < 0) {
173                abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
174        }
175
176        //===================
177        // Run Server Cluster
178        {
[d9c2284]179                int pipe_cnt = options.clopts.nworkers * 2;
180                int pipe_off;
181                int * fds;
182                [fds, pipe_off] = filefds( pipe_cnt );
183                for(i; 0 ~ pipe_cnt ~ 2) {
184                        int ret = pipe(&fds[pipe_off + i]);
185                        if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
186                }
187
[4f762d3]188                // if(options.file_cache.path && options.file_cache.fixed_fds) {
189                //      register_fixed_files(cl, fds, pipe_off);
190                // }
[d11d6eb]191
[0aec496]192                {
[348f81d5]193                        ServerCluster cl[options.clopts.nclusters];
[ece0e80]194
195                        init_protocol();
[0aec496]196                        {
[3eb540fb]197                                Worker * workers = anew(options.clopts.nworkers);
[d9c2284]198                                for(i; options.clopts.nworkers) {
[d11d6eb]199                                        // if( options.file_cache.fixed_fds ) {
200                                        //      workers[i].pipe[0] = pipe_off + (i * 2) + 0;
201                                        //      workers[i].pipe[1] = pipe_off + (i * 2) + 1;
202                                        // }
203                                        // else
204                                        {
[d9c2284]205                                                workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
206                                                workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
[8e3034d]207                                                workers[i].sockfd  = server_fd;
208                                                workers[i].addr    = (struct sockaddr *)&address;
209                                                workers[i].addrlen = (socklen_t*)&addrlen;
210                                                workers[i].flags   = 0;
[d9c2284]211                                        }
[e235429]212                                        unpark( workers[i] );
[d9c2284]213                                }
[348f81d5]214                                sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors /" | options.clopts.nclusters | "clusters";
215                                for(i; options.clopts.nclusters) {
216                                        sout | options.clopts.thrd_cnt[i] | nonl;
217                                }
218                                sout | nl;
[4087baf]219                                if(!options.interactive) park();
[0aec496]220                                {
[e95a117]221                                        char buffer[128];
[2cd784a]222                                        for() {
223                                                int ret = cfa_read(0, buffer, 128, 0);
224                                                if(ret == 0) break;
[153dc387]225                                                if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
[2cd784a]226                                                sout | "User wrote '" | "" | nonl;
227                                                write(sout, buffer, ret - 1);
228                                                sout | "'";
[e95a117]229                                        }
230
[8c43d05]231                                        sout | "Shutdown received";
[0aec496]232                                }
[ece0e80]233
[b57db73]234                                sout | "Notifying connections..." | nonl; flush( sout );
[ece0e80]235                                for(i; options.clopts.nworkers) {
[481ee28]236                                        workers[i].done = true;
[ece0e80]237                                }
[b57db73]238                                sout | "done";
[ece0e80]239
[b57db73]240                                sout | "Shutting down socket..." | nonl; flush( sout );
[ece0e80]241                                int ret = shutdown( server_fd, SHUT_RD );
[b57db73]242                                if( ret < 0 ) {
243                                        abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) );
244                                }
245                                sout | "done";
[ece0e80]246
247                                //===================
248                                // Close Socket
[b57db73]249                                sout | "Closing Socket..." | nonl; flush( sout );
[ece0e80]250                                ret = close( server_fd );
251                                if(ret < 0) {
252                                        abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
253                                }
[0197418]254                                sout | "done";
255
[b57db73]256                                sout | "Stopping connection threads..." | nonl; flush( sout );
[3eb540fb]257                                adelete(workers);
[0aec496]258                        }
[8c43d05]259                        sout | "done";
[ece0e80]260
[b57db73]261                        sout | "Stopping protocol threads..." | nonl; flush( sout );
[ece0e80]262                        deinit_protocol();
[8c43d05]263                        sout | "done";
264
[348f81d5]265                        sout | "Stopping processors/clusters..." | nonl; flush( sout );
[0aec496]266                }
[8c43d05]267                sout | "done";
[d9c2284]268
[b57db73]269                sout | "Closing splice fds..." | nonl; flush( sout );
[d9c2284]270                for(i; pipe_cnt) {
271                        ret = close( fds[pipe_off + i] );
272                        if(ret < 0) {
273                                abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) );
274                        }
275                }
276                free(fds);
[8c43d05]277                sout | "done";
[c3ee5f3]278
[b57db73]279                sout | "Stopping processors..." | nonl; flush( sout );
[0aec496]280        }
[8c43d05]281        sout | "done";
[0aec496]282
283        //===================
284        // Close Files
[b57db73]285        if( options.file_cache.path ) {
286                sout | "Closing open files..." | nonl; flush( sout );
287                close_cache();
288                sout | "done";
289        }
[0aec496]290}
Note: See TracBrowser for help on using the repository browser.