Changeset 2caed18


Ignore:
Timestamp:
Feb 24, 2021, 1:41:19 PM (7 months ago)
Author:
Thierry Delisle <tdelisle@…>
Branches:
arm-eh, jacob/cs343-translation, master, new-ast-unique-expr
Children:
187fdb8
Parents:
b664af2
Message:

Changed how messages is handled to avoid long printf.

File:
1 edited

Legend:

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

    rb664af2 r2caed18  
    2020#include "options.hfa"
    2121
    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 };
     22struct https_msg_str {
     23        char msg[512];
     24        size_t len;
     25};
     26
     27const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
    3328
    3429_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
    3530
    36 const int http_codes[] = {
     31const int http_codes[KNOWN_CODES] = {
    3732        200,
    3833        400,
     
    7166        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
    7267        int idx = (int)code;
    73         return answer( fd, http_msgs[idx], strlen( http_msgs[idx] ) );
     68        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
    7469}
    7570
    7671int answer_header( int fd, size_t size ) {
    77         const char * fmt = http_msgs[OK200];
    78         int len = 200;
    79         char buffer[len];
    80         len = snprintf(buffer, len, fmt, date, size);
     72        char buffer[512];
     73        char * it = buffer;
     74        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
     75        it += http_msgs[OK200]->len;
     76        int len = http_msgs[OK200]->len;
     77        len += snprintf(it, 512 - len, "%d \n\n", size);
    8178        return answer( fd, buffer, len );
    8279}
     
    172169#include <thread.hfa>
    173170
     171const char * original_http_msgs[] = {
     172        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
     173        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     174        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     175        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     176        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     177        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     178        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
     179};
     180
    174181struct date_buffer {
    175         char buff[100];
     182        https_msg_str strs[KNOWN_CODES];
    176183};
    177184
     
    184191        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
    185192        this.idx = 0;
    186         memset( this.buffers[0].buff, 0, sizeof(this.buffers[0]) );
    187         memset( this.buffers[1].buff, 0, sizeof(this.buffers[1]) );
     193        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
     194        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
    188195}
    189196
     
    195202                or else {}
    196203
     204
     205                char buff[100];
    197206                Time now = getTimeNsec();
    198 
    199                 strftime( this.buffers[this.idx].buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
    200 
    201                 char * next = this.buffers[this.idx].buff;
    202                 __atomic_exchange_n((char * volatile *)&date, next, __ATOMIC_SEQ_CST);
     207                strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
     208
     209                for(i; KNOWN_CODES) {
     210                        size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
     211                        this.buffers[this.idx].strs[i].len = len;
     212                }
     213
     214                for(i; KNOWN_CODES) {
     215                        https_msg_str * next = &this.buffers[this.idx].strs[i];
     216                        __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
     217                }
    203218                this.idx = (this.idx + 1) % 2;
    204219
Note: See TracChangeset for help on using the changeset viewer.