Ignore:
Timestamp:
Apr 28, 2021, 4:56:50 PM (5 years ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
ADT, arm-eh, ast-experimental, enum, forall-pointer-decay, jacob/cs343-translation, master, new-ast-unique-expr, pthread-emulation, qualifiedEnum
Children:
8d66610
Parents:
feacef9 (diff), b7fd2db6 (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the (diff) links above to see all the changes relative to each parent.
Message:

Merge branch 'master' of plg.uwaterloo.ca:software/cfa/cfa-cc

File:
1 edited

Legend:

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

    rfeacef9 r5407cdc  
    55        #include <fcntl.h>
    66}
     7
     8#define xstr(s) str(s)
     9#define str(s) #s
    710
    811#include <fstream.hfa>
     
    2023#include "options.hfa"
    2124
    22 const char * volatile date = 0p;
    23 
    24 const char * http_msgs[] = {
    25         "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: %zu \n\n",
    26         "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    27         "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    28         "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    29         "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    30         "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    31         "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
    32 };
     25#define PLAINTEXT_1WRITE
     26#define PLAINTEXT_MEMCPY
     27#define PLAINTEXT_NOCOPY
     28
     29struct https_msg_str {
     30        char msg[512];
     31        size_t len;
     32};
     33
     34const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
    3335
    3436_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
    3537
    36 const int http_codes[] = {
     38const int http_codes[KNOWN_CODES] = {
     39        200,
    3740        200,
    3841        400,
     
    5356        while(len > 0) {
    5457                // Call write
    55                 int ret = cfa_write(fd, it, len, 0, -1`s, 0p, 0p);
    56                 // int ret = write(fd, it, len);
     58                int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
    5759                if( ret < 0 ) {
    5860                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
     
    7274        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
    7375        int idx = (int)code;
    74         return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
     76        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
    7577}
    7678
    7779int answer_header( int fd, size_t size ) {
    78         const char * fmt = http_msgs[OK200];
    79         int len = 200;
    80         char buffer[len];
    81         len = snprintf(buffer, len, fmt, date, size);
     80        char buffer[512];
     81        char * it = buffer;
     82        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
     83        it += http_msgs[OK200]->len;
     84        int len = http_msgs[OK200]->len;
     85        len += snprintf(it, 512 - len, "%d \n\n", size);
    8286        return answer( fd, buffer, len );
    8387}
    8488
    85 int answer_plain( int fd, char buffer[], size_t size ) {
    86         int ret = answer_header(fd, size);
     89#if defined(PLAINTEXT_NOCOPY)
     90int answer_plaintext( int fd ) {
     91        return answer(fd, http_msgs[OK200_PlainText]->msg, http_msgs[OK200_PlainText]->len); // +1 cause snprintf doesn't count nullterminator
     92}
     93#elif defined(PLAINTEXT_MEMCPY)
     94#define TEXTSIZE 15
     95int answer_plaintext( int fd ) {
     96        char text[] = "Hello, World!\n\n";
     97        char ts[] = xstr(TEXTSIZE) " \n\n";
     98        _Static_assert(sizeof(text) - 1 == TEXTSIZE);
     99        char buffer[512 + TEXTSIZE];
     100        char * it = buffer;
     101        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
     102        it += http_msgs[OK200]->len;
     103        int len = http_msgs[OK200]->len;
     104        memcpy(it, ts, sizeof(ts) - 1);
     105        it += sizeof(ts) - 1;
     106        len += sizeof(ts) - 1;
     107        memcpy(it, text, TEXTSIZE);
     108        return answer(fd, buffer, len + TEXTSIZE);
     109}
     110#elif defined(PLAINTEXT_1WRITE)
     111int answer_plaintext( int fd ) {
     112        char text[] = "Hello, World!\n\n";
     113        char buffer[512 + sizeof(text)];
     114        char * it = buffer;
     115        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
     116        it += http_msgs[OK200]->len;
     117        int len = http_msgs[OK200]->len;
     118        int r = snprintf(it, 512 - len, "%d \n\n", sizeof(text));
     119        it += r;
     120        len += r;
     121        memcpy(it, text, sizeof(text));
     122        return answer(fd, buffer, len + sizeof(text));
     123}
     124#else
     125int answer_plaintext( int fd ) {
     126        char text[] = "Hello, World!\n\n";
     127        int ret = answer_header(fd, sizeof(text));
    87128        if( ret < 0 ) return ret;
    88         return answer(fd, buffer, size);
    89 }
     129        return answer(fd, text, sizeof(text));
     130}
     131#endif
    90132
    91133int answer_empty( int fd ) {
     
    94136
    95137
    96 [HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len, io_cancellation * cancel) {
     138[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
    97139        char * it = buffer;
    98140        size_t count = len - 1;
     
    100142        READ:
    101143        for() {
    102                 int ret = cfa_read(fd, (void*)it, count, 0, -1`s, cancel, 0p);
     144                int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
    103145                // int ret = read(fd, (void*)it, count);
    104146                if(ret == 0 ) return [OK200, true, 0, 0];
     
    139181        ssize_t ret;
    140182        SPLICE1: while(count > 0) {
    141                 ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, 0, -1`s, 0p, 0p);
    142                 // ret = splice(ans_fd, &offset, pipe[1], 0p, count, sflags);
     183                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
    143184                if( ret < 0 ) {
    144185                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
     
    152193                size_t in_pipe = ret;
    153194                SPLICE2: while(in_pipe > 0) {
    154                         ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, 0, -1`s, 0p, 0p);
    155                         // ret = splice(pipe[0], 0p, fd, 0p, in_pipe, sflags);
     195                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
    156196                        if( ret < 0 ) {
    157197                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
     
    173213#include <thread.hfa>
    174214
     215const char * original_http_msgs[] = {
     216        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
     217        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n\n",
     218        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     219        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     220        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     221        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     222        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     223        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     224};
     225
    175226struct date_buffer {
    176         char buff[100];
     227        https_msg_str strs[KNOWN_CODES];
    177228};
    178229
     
    183234
    184235void ?{}( DateFormater & this ) {
    185         ((thread&)this){ "Server Date Thread", *options.clopts.instance };
     236        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
    186237        this.idx = 0;
    187         memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
    188         memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
     238        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
     239        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
    189240}
    190241
     
    196247                or else {}
    197248
    198                 Time now = getTimeNsec();
    199 
    200                 strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
    201 
    202                 char * next = this.buffers[this.idx].buff;
    203                 __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST);
     249
     250                char buff[100];
     251                Time now = timeHiRes();
     252                strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
     253                sout | "Updated date to '" | buff | "'";
     254
     255                for(i; KNOWN_CODES) {
     256                        size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
     257                        this.buffers[this.idx].strs[i].len = len;
     258                }
     259
     260                for(i; KNOWN_CODES) {
     261                        https_msg_str * next = &this.buffers[this.idx].strs[i];
     262                        __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
     263                }
    204264                this.idx = (this.idx + 1) % 2;
     265
     266                sout | "Date thread sleeping";
    205267
    206268                sleep(1`s);
Note: See TracChangeset for help on using the changeset viewer.