source: benchmark/io/http/protocol.cfa @ 187fdb8

arm-ehjacob/cs343-translationnew-ast-unique-expr
Last change on this file since 187fdb8 was 187fdb8, checked in by Thierry Delisle <tdelisle@…>, 9 months ago

Optim 60% to plaintext webserver by doing 1 write instead of 2.

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