Ignore:
Timestamp:
Jun 3, 2022, 6:54:31 PM (2 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, ast-experimental, master, pthread-emulation, qualifiedEnum
Children:
c4b10e2
Parents:
7affcda
Message:

Server now cleanly closes in non interactive mode

File:
1 edited

Legend:

Unmodified
Added
Removed
  • benchmark/io/http/main.cfa

    r7affcda r153570d  
    22
    33#include <errno.h>
     4#include <signal.h>
    45#include <stdio.h>
    56#include <string.h>
     
    89        #include <sched.h>
    910        #include <signal.h>
     11        #include <sys/eventfd.h>
    1012        #include <sys/socket.h>
    1113        #include <netinet/in.h>
     
    177179
    178180//=============================================================================================
     181// Termination
     182//=============================================================================================
     183
     184int closefd;
     185void cleanstop(int) {
     186        eventfd_t buffer = 1;
     187        char * buffer_s = (char*)&buffer;
     188        int ret = write(closefd, buffer_s, sizeof(buffer));
     189        if(ret < 0) abort( "eventfd write error: (%d) %s\n", (int)errno, strerror(errno) );
     190        return;
     191}
     192
     193//=============================================================================================
    179194// Main
    180195//============================================================================================='
     
    186201        // Parse args
    187202        parse_options(argc, argv);
     203
     204        //===================
     205        // Setup non-interactive termination
     206        if(!options.interactive) {
     207                closefd = eventfd(0, 0);
     208                if(closefd < 0) abort( "eventfd error: (%d) %s\n", (int)errno, strerror(errno) );
     209
     210                sighandler_t prev = signal(SIGTERM, cleanstop);
     211                intptr_t prev_workaround = (intptr_t) prev;
     212                // can't use SIG_ERR it crashes the compiler
     213                if(prev_workaround == -1) abort( "signal setup error: (%d) %s\n", (int)errno, strerror(errno) );
     214
     215                sout | "Signal termination ready";
     216        }
    188217
    189218        //===================
     
    257286
    258287                {
     288                        // Stats printer makes a copy so this needs to persist longer than normal
     289                        Worker * workers;
    259290                        ServerCluster cl[options.clopts.nclusters];
    260291
    261292                        init_protocol();
    262293                        {
    263                                 Worker * workers = anew(options.clopts.nworkers);
     294                                workers = anew(options.clopts.nworkers);
    264295                                cl[0].prnt->workers = workers;
    265296                                cl[0].prnt->worker_cnt = options.clopts.nworkers;
     
    285316                                }
    286317                                sout | nl;
    287                                 if(!options.interactive) park();
    288318                                {
    289                                         char buffer[128];
    290                                         for() {
    291                                                 int ret = cfa_read(0, buffer, 128, 0);
    292                                                 if(ret == 0) break;
     319                                        if(options.interactive) {
     320                                                char buffer[128];
     321                                                for() {
     322                                                        int ret = cfa_read(0, buffer, 128, 0);
     323                                                        if(ret == 0) break;
     324                                                        if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
     325                                                        sout | "User wrote '" | "" | nonl;
     326                                                        write(sout, buffer, ret - 1);
     327                                                        sout | "'";
     328                                                }
     329                                        }
     330                                        else {
     331                                                char buffer[sizeof(eventfd_t)];
     332                                                int ret = cfa_read(closefd, buffer, sizeof(eventfd_t), 0);
    293333                                                if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
    294                                                 sout | "User wrote '" | "" | nonl;
    295                                                 write(sout, buffer, ret - 1);
    296                                                 sout | "'";
    297334                                        }
    298335
     
    323360
    324361                                sout | "Stopping connection threads..." | nonl; flush( sout );
    325                                 adelete(workers);
     362                                for(i; options.clopts.nworkers) {
     363                                        join(workers[i]);
     364                                }
    326365                        }
    327366                        sout | "done";
     
    330369                        deinit_protocol();
    331370                        sout | "done";
     371
     372                        sout | "Stopping printer threads..." | nonl; flush( sout );
     373                        for(i; options.clopts.nclusters) {
     374                                StatsPrinter * p = cl[i].prnt;
     375                                if(p) join(*p);
     376                        }
     377                        sout | "done";
     378
     379                        // Now that the stats printer is stopped, we can reclaim this
     380                        adelete(workers);
    332381
    333382                        sout | "Stopping processors/clusters..." | nonl; flush( sout );
Note: See TracChangeset for help on using the changeset viewer.