Ignore:
File:
1 edited

Legend:

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

    r0f1336c r07997cd  
    1414#include "filecache.hfa"
    1515
    16 static const unsigned long long period = 5_000_000;
    17 
    1816//=============================================================================================
    1917// Generic connection handling
     
    3331
    3432                // if we are done, break out of the loop
    35                 if( closed ) {
    36                         if( code != OK200 ) this.stats.sendfile.error++;
    37                         break REQUEST;
    38                 }
     33                if( closed ) break REQUEST;
    3934
    4035                // If this wasn't a request retrun 400
    4136                if( code != OK200 ) {
    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                 // }
     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                }
    6762
    6863                if( options.log ) {
     
    7368
    7469                if( !options.file_cache.path ) {
    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;
     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;
    8377                }
    8478
     
    9084                // If we can't find the file, return 404
    9185                if( ans_fd < 0 ) {
    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;
     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;
    10093                }
    10194
    10295                // Send the desired file
    10396                int ret = answer_sendfile( this.pipe, fd, ans_fd, count, this.stats.sendfile );
    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                 }
     97                if( ret == -ECONNRESET ) break REQUEST;
    11198
    11299                if( options.log ) mutex(sout) sout | "=== Answer sent ===";
    113100        }
    114101
    115         this.stats.sendfile.close++;
    116 
    117102        if (stats_thrd) {
    118                 // unsigned long long next = rdtscl();
    119                 // if(next > (last + period)) {
     103                unsigned long long next = rdtscl();
     104                if(next > (last + 500000000)) {
    120105                        if(try_lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2)) {
    121106                                push(this.stats.sendfile, stats_thrd->stats.send);
    122107                                unlock(stats_thrd->stats.lock);
    123                                 // last = next;
    124                         }
    125                 // }
     108                                last = next;
     109                        }
     110                }
    126111        }
    127112}
     
    145130                if( options.log ) mutex(sout) sout | "=== Accepting connection ===";
    146131                int fd = cfa_accept4( this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY );
    147                 if(fd <= 0) {
     132                if(fd < 0) {
    148133                        if( errno == ECONNABORTED ) break;
    149134                        if( this.done && (errno == EINVAL || errno == EBADF) ) break;
    150                         abort( "accept error %d: (%d) %s\n", fd, (int)errno, strerror(errno) );
     135                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
    151136                }
    152137                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                 }
    159138
    160139                if( options.log ) mutex(sout) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
     
    162141                char buffer[len];
    163142                handle_connection( this.conn, fd, buffer, len, 0p, last );
    164                 this.conn.stats.sendfile.maxfd = max(this.conn.stats.sendfile.maxfd, fd);
    165143
    166144                if( options.log ) mutex(sout) sout | "=== Connection closed ===";
     
    184162        /* paranoid */ assert( this.conn.pipe[0] != -1 );
    185163        /* 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;
    188164        for() {
    189165                size_t len = options.socket.buflen;
     
    197173                if( options.log ) mutex(sout) sout | "=== Waiting new connection ===";
    198174                handle_connection( this.conn, p.out.fd, buffer, len, &p.f, last );
     175
     176                if( options.log ) mutex(sout) sout | "=== Connection closed ===";
    199177                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++;
    202 
    203                 if( options.log ) mutex(sout) sout | "=== Connection closed ===";
    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);
     178        }
    209179}
    210180
     
    228198
    229199static inline void push_connection( Acceptor & this, int fd ) {
    230         this.stats.accepts++;
    231200        PendingRead * p = 0p;
    232201        for() {
     
    234203                p = pop(*this.queue);
    235204                if(p) break;
    236                 // abort( "Too few threads" );
    237205                yield();
    238206                this.stats.creates++;
     
    244212
    245213// #define ACCEPT_SPIN
    246 #define ACCEPT_ONE
    247 // #define ACCEPT_MANY
     214#define ACCEPT_MANY
    248215
    249216void main( Acceptor & this ) {
     
    265232                        abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
    266233                }
     234                this.stats.accepts++;
    267235
    268236                if(this.done) return;
     
    274242                if (stats_thrd) {
    275243                        unsigned long long next = rdtscl();
    276                         if(next > (last + period)) {
     244                        if(next > (last + 500000000)) {
    277245                                if(try_lock(stats_thrd->stats.lock)) {
    278246                                        push(this.stats, stats_thrd->stats.accpt);
     
    281249                                }
    282250                        }
    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                         // }
    313251                }
    314252
     
    331269                if (stats_thrd) {
    332270                        unsigned long long next = rdtscl();
    333                         if(next > (last + period)) {
     271                        if(next > (last + 500000000)) {
    334272                                if(try_lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2)) {
    335273                                        push(this.stats, stats_thrd->stats.accpt);
     
    346284                                int fd = get_res(res);
    347285                                reset(res);
     286                                this.stats.accepts++;
    348287                                if(fd < 0) {
    349288                                        if( errno == ECONNABORTED ) continue;
     
    380319#error no accept algorithm specified
    381320#endif
    382         lock(stats_thrd->stats.lock);
    383         push(this.stats, stats_thrd->stats.accpt);
    384         unlock(stats_thrd->stats.lock);
    385 }
     321}
Note: See TracChangeset for help on using the changeset viewer.