source: benchmark/io/http/protocol.cfa @ e54d0c3

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since e54d0c3 was e54d0c3, checked in by Peter A. Buhr <pabuhr@…>, 6 months ago

Fixed benchmarks after another change to getTimeNsec()

  • Property mode set to 100644
File size: 6.9 KB
Line 
1#include "protocol.hfa"
2
3#define _GNU_SOURCE
4extern "C" {
5        #include <fcntl.h>
6}
7
8#include <fstream.hfa>
9#include <iofwd.hfa>
10
11#include <assert.h>
12// #include <stdio.h> // Don't use stdio.h, too slow to compile
13extern "C" {
14      int snprintf ( char * s, size_t n, const char * format, ... );
15        // #include <linux/io_uring.h>
16}
17#include <string.h>
18#include <errno.h>
19
20#include "options.hfa"
21
22#define PLAINTEXT_1WRITE
23#define PLAINTEXT_NOCOPY
24
25struct https_msg_str {
26        char msg[512];
27        size_t len;
28};
29
30const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 };
31
32_Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0])));
33
34const int http_codes[KNOWN_CODES] = {
35        200,
36        200,
37        400,
38        404,
39        405,
40        408,
41        413,
42        414,
43};
44
45_Static_assert( KNOWN_CODES == (sizeof(http_codes) / sizeof(http_codes[0])));
46
47int code_val(HttpCode code) {
48        return http_codes[code];
49}
50
51static inline int answer( int fd, const char * it, int len) {
52        while(len > 0) {
53                // Call write
54                int ret = cfa_send(fd, it, len, 0, CFA_IO_LAZY);
55                if( ret < 0 ) {
56                        if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET;
57                        if( errno == EAGAIN || errno == EWOULDBLOCK) return -EAGAIN;
58
59                        abort( "'answer error' error: (%d) %s\n", (int)errno, strerror(errno) );
60                }
61
62                // update it/len
63                it  += ret;
64                len -= ret;
65        }
66        return 0;
67}
68
69int answer_error( int fd, HttpCode code ) {
70        /* paranoid */ assert( code < KNOWN_CODES && code != OK200 );
71        int idx = (int)code;
72        return answer( fd, http_msgs[idx]->msg, http_msgs[idx]->len );
73}
74
75int answer_header( int fd, size_t size ) {
76        char buffer[512];
77        char * it = buffer;
78        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
79        it += http_msgs[OK200]->len;
80        int len = http_msgs[OK200]->len;
81        len += snprintf(it, 512 - len, "%d \n\n", size);
82        return answer( fd, buffer, len );
83}
84
85#if defined(PLAINTEXT_NOCOPY)
86int answer_plaintext( int fd ) {
87        return answer(fd, http_msgs[OK200_PlainText]->msg, http_msgs[OK200_PlainText]->len + 1); // +1 cause snprintf doesn't count nullterminator
88}
89#elif defined(PLAINTEXT_1WRITE)
90int answer_plaintext( int fd ) {
91        char text[] = "Hello, World!\n";
92        char buffer[512 + sizeof(text)];
93        char * it = buffer;
94        memcpy(it, http_msgs[OK200]->msg, http_msgs[OK200]->len);
95        it += http_msgs[OK200]->len;
96        int len = http_msgs[OK200]->len;
97        int r = snprintf(it, 512 - len, "%d \n\n", sizeof(text));
98        it += r;
99        len += r;
100        memcpy(it, text, sizeof(text));
101        return answer(fd, buffer, len + sizeof(text));
102}
103#else
104int answer_plaintext( int fd ) {
105        char text[] = "Hello, World!\n";
106        int ret = answer_header(fd, sizeof(text));
107        if( ret < 0 ) return ret;
108        return answer(fd, text, sizeof(text));
109}
110#endif
111
112int answer_empty( int fd ) {
113        return answer_header(fd, 0);
114}
115
116
117[HttpCode code, bool closed, * const char file, size_t len] http_read(int fd, []char buffer, size_t len) {
118        char * it = buffer;
119        size_t count = len - 1;
120        int rlen = 0;
121        READ:
122        for() {
123                int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY);
124                // int ret = read(fd, (void*)it, count);
125                if(ret == 0 ) return [OK200, true, 0, 0];
126                if(ret < 0 ) {
127                        if( errno == EAGAIN || errno == EWOULDBLOCK) continue READ;
128                        if( errno == ECONNRESET ) return [E408, true, 0, 0];
129                        if( errno == EPIPE ) return [E408, true, 0, 0];
130                        abort( "read error: (%d) %s\n", (int)errno, strerror(errno) );
131                }
132                it[ret + 1] = '\0';
133                rlen += ret;
134
135                if( strstr( it, "\r\n\r\n" ) ) break;
136
137                it += ret;
138                count -= ret;
139
140                if( count < 1 ) return [E414, false, 0, 0];
141        }
142
143        if( options.log ) {
144                write(sout, buffer, rlen);
145                sout | nl;
146        }
147
148        it = buffer;
149        int ret = memcmp(it, "GET /", 5);
150        if( ret != 0 ) return [E400, false, 0, 0];
151        it += 5;
152
153        char * end = strstr( it, " " );
154        return [OK200, false, it, end - it];
155}
156
157int sendfile( int pipe[2], int fd, int ans_fd, size_t count ) {
158        unsigned sflags = SPLICE_F_MOVE; // | SPLICE_F_MORE;
159        off_t offset = 0;
160        ssize_t ret;
161        SPLICE1: while(count > 0) {
162                ret = cfa_splice(ans_fd, &offset, pipe[1], 0p, count, sflags, CFA_IO_LAZY);
163                if( ret < 0 ) {
164                        if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1;
165                        if( errno == ECONNRESET ) return -ECONNRESET;
166                        if( errno == EPIPE ) return -EPIPE;
167                        abort( "splice [0] error: (%d) %s\n", (int)errno, strerror(errno) );
168                }
169
170                count -= ret;
171                offset += ret;
172                size_t in_pipe = ret;
173                SPLICE2: while(in_pipe > 0) {
174                        ret = cfa_splice(pipe[0], 0p, fd, 0p, in_pipe, sflags, CFA_IO_LAZY);
175                        if( ret < 0 ) {
176                                if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2;
177                                if( errno == ECONNRESET ) return -ECONNRESET;
178                                if( errno == EPIPE ) return -EPIPE;
179                                abort( "splice [1] error: (%d) %s\n", (int)errno, strerror(errno) );
180                        }
181                        in_pipe -= ret;
182                }
183
184        }
185        return count;
186}
187
188//=============================================================================================
189
190#include <clock.hfa>
191#include <time.hfa>
192#include <thread.hfa>
193
194const char * original_http_msgs[] = {
195        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: ",
196        "HTTP/1.1 200 OK\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 15\n\nHello, World!\n",
197        "HTTP/1.1 400 Bad Request\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
198        "HTTP/1.1 404 Not Found\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
199        "HTTP/1.1 405 Method Not Allowed\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
200        "HTTP/1.1 408 Request Timeout\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
201        "HTTP/1.1 413 Payload Too Large\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
202        "HTTP/1.1 414 URI Too Long\nServer: HttoForall\nDate: %s \nContent-Type: text/plain\nContent-Length: 0 \n\n",
203};
204
205struct date_buffer {
206        https_msg_str strs[KNOWN_CODES];
207};
208
209thread DateFormater {
210        int idx;
211        date_buffer buffers[2];
212};
213
214void ?{}( DateFormater & this ) {
215        ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] };
216        this.idx = 0;
217        memset( &this.buffers[0], 0, sizeof(this.buffers[0]) );
218        memset( &this.buffers[1], 0, sizeof(this.buffers[1]) );
219}
220
221void main(DateFormater & this) {
222        LOOP: for() {
223                waitfor( ^?{} : this) {
224                        break LOOP;
225                }
226                or else {}
227
228
229                char buff[100];
230                Time now = timeHiRes();
231                strftime( buff, 100, "%a, %d %b %Y %H:%M:%S %Z", now );
232                sout | "Updated date to '" | buff | "'";
233
234                for(i; KNOWN_CODES) {
235                        size_t len = snprintf( this.buffers[this.idx].strs[i].msg, 512, original_http_msgs[i], buff );
236                        this.buffers[this.idx].strs[i].len = len;
237                }
238
239                for(i; KNOWN_CODES) {
240                        https_msg_str * next = &this.buffers[this.idx].strs[i];
241                        __atomic_exchange_n((https_msg_str * volatile *)&http_msgs[i], next, __ATOMIC_SEQ_CST);
242                }
243                this.idx = (this.idx + 1) % 2;
244
245                sout | "Date thread sleeping";
246
247                sleep(1`s);
248        }
249}
250
251//=============================================================================================
252DateFormater * the_date_formatter;
253
254void init_protocol(void) {
255        the_date_formatter = alloc();
256        (*the_date_formatter){};
257}
258
259void deinit_protocol(void) {
260        ^(*the_date_formatter){};
261        free( the_date_formatter );
262}
Note: See TracBrowser for help on using the repository browser.