Ignore:
File:
1 edited

Legend:

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

    r07997cd r0f1336c  
    1414#include "filecache.hfa"
    1515
     16static const unsigned long long period = 5_000_000;
     17
    1618//=============================================================================================
    1719// Generic connection handling
     
    3133
    3234                // if we are done, break out of the loop
    33                 if( closed ) break REQUEST;
     35                if( closed ) {
     36                        if( code != OK200 ) this.stats.sendfile.error++;
     37                        break REQUEST;
     38                }
    3439
    3540                // If this wasn't a request retrun 400
    3641                if( code != OK200 ) {
    37                         sout | "=== Invalid Request :" | code_val(code) | "===";
    38                         answer_error(fd, code);
    39                         continue REQUEST;
    40                 }
    41 
    42                 if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
    43                         if( options.log ) mutex(sout) sout | "=== Request for /plaintext ===";
    44 
    45                         int ret = answer_plaintext(fd);
    46                         if( ret == -ECONNRESET ) break REQUEST;
    47 
    48                         if( options.log ) mutex(sout) sout | "=== Answer sent ===";
    49                         continue REQUEST;
    50                 }
    51 
    52                 if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) {
    53                         if( options.log ) mutex(sout) sout | "=== Request for /ping ===";
    54 
    55                         // Send the header
    56                         int ret = answer_empty(fd);
    57                         if( ret == -ECONNRESET ) break REQUEST;
    58 
    59                         if( options.log ) mutex(sout) sout | "=== Answer sent ===";
    60                         continue REQUEST;
    61                 }
     42                        abort | "=== Invalid Request :" | code_val(code) | "===";
     43                        // answer_error(fd, code);
     44                        // continue REQUEST;
     45                }
     46
     47                // if(0 == strncmp(file, "plaintext", min(name_size, sizeof("plaintext") ))) {
     48                //      if( options.log ) mutex(sout) sout | "=== Request for /plaintext ===";
     49
     50                //      int ret = answer_plaintext(fd);
     51                //      if( ret == -ECONNRESET ) { this.stats.sendfile.error++; break REQUEST; }
     52
     53                //      if( options.log ) mutex(sout) sout | "=== Answer sent ===";
     54                //      continue REQUEST;
     55                // }
     56
     57                // if(0 == strncmp(file, "ping", min(name_size, sizeof("ping") ))) {
     58                //      if( options.log ) mutex(sout) sout | "=== Request for /ping ===";
     59
     60                //      // Send the header
     61                //      int ret = answer_empty(fd);
     62                //      if( ret == -ECONNRESET ) { this.stats.sendfile.error++; break REQUEST; }
     63
     64                //      if( options.log ) mutex(sout) sout | "=== Answer sent ===";
     65                //      continue REQUEST;
     66                // }
    6267
    6368                if( options.log ) {
     
    6873
    6974                if( !options.file_cache.path ) {
    70                         if( options.log ) {
    71                                 sout | "=== File Not Found (" | nonl;
    72                                 write(sout, file, name_size);
    73                                 sout | ") ===";
    74                         }
    75                         answer_error(fd, E405);
    76                         continue REQUEST;
     75                        // if( options.log ) {
     76                                serr | "=== File Not Found (" | nonl;
     77                                write(serr, file, name_size);
     78                                serr | ") ===";
     79                                abort();
     80                        // }
     81                        // answer_error(fd, E405);
     82                        // continue REQUEST;
    7783                }
    7884
     
    8490                // If we can't find the file, return 404
    8591                if( ans_fd < 0 ) {
    86                         if( options.log ) {
    87                                 sout | "=== File Not Found (" | nonl;
    88                                 write(sout, file, name_size);
    89                                 sout | ") ===";
    90                         }
    91                         answer_error(fd, E404);
    92                         continue REQUEST;
     92                        // if( options.log ) {
     93                                serr | "=== File Not Found 2 (" | nonl;
     94                                write(serr, file, name_size);
     95                                serr | ") ===";
     96                                abort();
     97                        // }
     98                        // answer_error(fd, E404);
     99                        // continue REQUEST;
    93100                }
    94101
    95102                // Send the desired file
    96103                int ret = answer_sendfile( this.pipe, fd, ans_fd, count, this.stats.sendfile );
    97                 if( ret == -ECONNRESET ) break REQUEST;
     104                if(ret < 0) {
     105                        if( ret == -ECONNABORTED ) { this.stats.sendfile.error++; break REQUEST; }
     106                        if( ret == -ECONNRESET ) { this.stats.sendfile.error++; break REQUEST; }
     107                        if( ret == -EPIPE ) { this.stats.sendfile.error++; break REQUEST; }
     108                        if( ret == -EBADF ) { this.stats.sendfile.error++; break REQUEST; }
     109                        abort( "answer sendfile error: %d (%d) %s\n", ret, (int)errno, strerror(errno) );
     110                }
    98111
    99112                if( options.log ) mutex(sout) sout | "=== Answer sent ===";
    100113        }
    101114
     115        this.stats.sendfile.close++;
     116
    102117        if (stats_thrd) {
    103                 unsigned long long next = rdtscl();
    104                 if(next > (last + 500000000)) {
     118                // unsigned long long next = rdtscl();
     119                // if(next > (last + period)) {
    105120                        if(try_lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2)) {
    106121                                push(this.stats.sendfile, stats_thrd->stats.send);
    107122                                unlock(stats_thrd->stats.lock);
    108                                 last = next;
    109                         }
    110                 }
     123                                // last = next;
     124                        }
     125                // }
    111126        }
    112127}
     
    130145                if( options.log ) mutex(sout) sout | "=== Accepting connection ===";
    131146                int fd = cfa_accept4( this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY );
    132                 if(fd < 0) {
     147                if(fd <= 0) {
    133148                        if( errno == ECONNABORTED ) break;
    134149                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
    135                         abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
     150                        abort( "accept error %d: (%d) %s\n", fd, (int)errno, strerror(errno) );
    136151                }
    137152                if(this.done) break;
     153
     154                this.stats.accepts++;
     155                if (stats_thrd && try_lock(stats_thrd->stats.lock)) {
     156                        push(this.stats, stats_thrd->stats.accpt);
     157                        unlock(stats_thrd->stats.lock);
     158                }
    138159
    139160                if( options.log ) mutex(sout) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
     
    141162                char buffer[len];
    142163                handle_connection( this.conn, fd, buffer, len, 0p, last );
     164                this.conn.stats.sendfile.maxfd = max(this.conn.stats.sendfile.maxfd, fd);
    143165
    144166                if( options.log ) mutex(sout) sout | "=== Connection closed ===";
     
    162184        /* paranoid */ assert( this.conn.pipe[0] != -1 );
    163185        /* paranoid */ assert( this.conn.pipe[1] != -1 );
     186        this.conn.stats.sendfile.maxfd = max(this.conn.pipe[0], this.conn.pipe[1]);
     187        // this.conn.stats.sendfile.maxfd = 0;
    164188        for() {
    165189                size_t len = options.socket.buflen;
     
    173197                if( options.log ) mutex(sout) sout | "=== Waiting new connection ===";
    174198                handle_connection( this.conn, p.out.fd, buffer, len, &p.f, last );
     199                if(this.done) break;
     200                this.conn.stats.sendfile.maxfd = max(this.conn.stats.sendfile.maxfd, p.out.fd);
     201                this.conn.stats.sendfile.close++;
    175202
    176203                if( options.log ) mutex(sout) sout | "=== Connection closed ===";
    177                 if(this.done) break;
    178         }
     204        }
     205
     206        lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2);
     207        push(this.conn.stats.sendfile, stats_thrd->stats.send);
     208        unlock(stats_thrd->stats.lock);
    179209}
    180210
     
    198228
    199229static inline void push_connection( Acceptor & this, int fd ) {
     230        this.stats.accepts++;
    200231        PendingRead * p = 0p;
    201232        for() {
     
    203234                p = pop(*this.queue);
    204235                if(p) break;
     236                // abort( "Too few threads" );
    205237                yield();
    206238                this.stats.creates++;
     
    212244
    213245// #define ACCEPT_SPIN
    214 #define ACCEPT_MANY
     246#define ACCEPT_ONE
     247// #define ACCEPT_MANY
    215248
    216249void main( Acceptor & this ) {
     
    232265                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
    233266                }
    234                 this.stats.accepts++;
    235267
    236268                if(this.done) return;
     
    242274                if (stats_thrd) {
    243275                        unsigned long long next = rdtscl();
    244                         if(next > (last + 500000000)) {
     276                        if(next > (last + period)) {
    245277                                if(try_lock(stats_thrd->stats.lock)) {
    246278                                        push(this.stats, stats_thrd->stats.accpt);
     
    249281                                }
    250282                        }
     283                }
     284
     285                if( options.log ) sout | "=== Accepting connection ===";
     286        }
     287
     288#elif defined(ACCEPT_ONE)
     289        if( options.log ) sout | "=== Accepting connection ===";
     290        for() {
     291                int fd = cfa_accept4(this.sockfd, this.[addr, addrlen, flags], 0);
     292                if(fd < 0) {
     293                        if( errno == ECONNABORTED ) break;
     294                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
     295                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
     296                }
     297
     298                if(this.done) return;
     299
     300                if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
     301
     302                if(fd) push_connection(this, fd);
     303
     304                if (stats_thrd) {
     305                        // unsigned long long next = rdtscl();
     306                        // if(next > (last + period)) {
     307                                if(try_lock(stats_thrd->stats.lock)) {
     308                                        push(this.stats, stats_thrd->stats.accpt);
     309                                        unlock(stats_thrd->stats.lock);
     310                                        // last = next;
     311                                }
     312                        // }
    251313                }
    252314
     
    269331                if (stats_thrd) {
    270332                        unsigned long long next = rdtscl();
    271                         if(next > (last + 500000000)) {
     333                        if(next > (last + period)) {
    272334                                if(try_lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2)) {
    273335                                        push(this.stats, stats_thrd->stats.accpt);
     
    284346                                int fd = get_res(res);
    285347                                reset(res);
    286                                 this.stats.accepts++;
    287348                                if(fd < 0) {
    288349                                        if( errno == ECONNABORTED ) continue;
     
    319380#error no accept algorithm specified
    320381#endif
    321 }
     382        lock(stats_thrd->stats.lock);
     383        push(this.stats, stats_thrd->stats.accpt);
     384        unlock(stats_thrd->stats.lock);
     385}
Note: See TracChangeset for help on using the changeset viewer.