source: benchmark/io/http/worker.cfa@ c4072d8e

ADT ast-experimental pthread-emulation qualifiedEnum
Last change on this file since c4072d8e was 07997cd, checked in by Thierry Delisle <tdelisle@…>, 3 years ago

Fixed errors with the accept many version

  • Property mode set to 100644
File size: 8.6 KB
Line 
1#include "worker.hfa"
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7
8#include <fstream.hfa>
9#include <iofwd.hfa>
10#include <mutex_stmt.hfa>
11
12#include "options.hfa"
13#include "protocol.hfa"
14#include "filecache.hfa"
15
16//=============================================================================================
17// Generic connection handling
18//=============================================================================================
19static void handle_connection( connection & this, volatile int & fd, char * buffer, size_t len, io_future_t * f, unsigned long long & last ) {
20 REQUEST:
21 for() {
22 bool closed;
23 HttpCode code;
24 const char * file;
25 size_t name_size;
26
27 // Read the http request
28 if( options.log ) mutex(sout) sout | "=== Reading request ===";
29 [code, closed, file, name_size] = http_read(fd, buffer, len, f);
30 f = 0p;
31
32 // if we are done, break out of the loop
33 if( closed ) break REQUEST;
34
35 // If this wasn't a request retrun 400
36 if( code != OK200 ) {
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 }
62
63 if( options.log ) {
64 sout | "=== Request for file " | nonl;
65 write(sout, file, name_size);
66 sout | " ===";
67 }
68
69 if( !options.file_cache.path ) {
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;
77 }
78
79 // Get the fd from the file cache
80 int ans_fd;
81 size_t count;
82 [ans_fd, count] = get_file( file, name_size );
83
84 // If we can't find the file, return 404
85 if( ans_fd < 0 ) {
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;
93 }
94
95 // Send the desired file
96 int ret = answer_sendfile( this.pipe, fd, ans_fd, count, this.stats.sendfile );
97 if( ret == -ECONNRESET ) break REQUEST;
98
99 if( options.log ) mutex(sout) sout | "=== Answer sent ===";
100 }
101
102 if (stats_thrd) {
103 unsigned long long next = rdtscl();
104 if(next > (last + 500000000)) {
105 if(try_lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2)) {
106 push(this.stats.sendfile, stats_thrd->stats.send);
107 unlock(stats_thrd->stats.lock);
108 last = next;
109 }
110 }
111 }
112}
113
114//=============================================================================================
115// Self Accepting Worker Thread
116//=============================================================================================
117void ?{}( AcceptWorker & this ) {
118 size_t cli = rand() % options.clopts.cltr_cnt;
119 ((thread&)this){ "Server Worker Thread", *options.clopts.instance[cli], 64000 };
120 options.clopts.thrd_cnt[cli]++;
121 this.done = false;
122}
123
124void main( AcceptWorker & this ) {
125 park();
126 unsigned long long last = rdtscl();
127 /* paranoid */ assert( this.conn.pipe[0] != -1 );
128 /* paranoid */ assert( this.conn.pipe[1] != -1 );
129 for() {
130 if( options.log ) mutex(sout) sout | "=== Accepting connection ===";
131 int fd = cfa_accept4( this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY );
132 if(fd < 0) {
133 if( errno == ECONNABORTED ) break;
134 if( this.done && (errno == EINVAL || errno == EBADF) ) break;
135 abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
136 }
137 if(this.done) break;
138
139 if( options.log ) mutex(sout) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
140 size_t len = options.socket.buflen;
141 char buffer[len];
142 handle_connection( this.conn, fd, buffer, len, 0p, last );
143
144 if( options.log ) mutex(sout) sout | "=== Connection closed ===";
145 }
146}
147
148
149//=============================================================================================
150// Channel Worker Thread
151//=============================================================================================
152void ?{}( ChannelWorker & this ) {
153 size_t cli = rand() % options.clopts.cltr_cnt;
154 ((thread&)this){ "Server Worker Thread", *options.clopts.instance[cli], 64000 };
155 options.clopts.thrd_cnt[cli]++;
156 this.done = false;
157}
158
159void main( ChannelWorker & this ) {
160 park();
161 unsigned long long last = rdtscl();
162 /* paranoid */ assert( this.conn.pipe[0] != -1 );
163 /* paranoid */ assert( this.conn.pipe[1] != -1 );
164 for() {
165 size_t len = options.socket.buflen;
166 char buffer[len];
167 PendingRead p;
168 p.next = 0p;
169 p.in.buf = (void*)buffer;
170 p.in.len = len;
171 push(*this.queue, &p);
172
173 if( options.log ) mutex(sout) sout | "=== Waiting new connection ===";
174 handle_connection( this.conn, p.out.fd, buffer, len, &p.f, last );
175
176 if( options.log ) mutex(sout) sout | "=== Connection closed ===";
177 if(this.done) break;
178 }
179}
180
181extern "C" {
182extern int accept4(int sockfd, struct sockaddr *addr, socklen_t *addrlen, int flags);
183}
184
185void ?{}( Acceptor & this, int cli ) {
186 ((thread&)this){ "Server Acceptor Thread", *options.clopts.instance[cli], 64000 };
187 options.clopts.thrd_cnt[cli]++;
188 this.done = false;
189}
190
191static inline __s32 get_res( io_future_t & this ) {
192 if( this.result < 0 ) {{
193 errno = -this.result;
194 return -1;
195 }}
196 return this.result;
197}
198
199static inline void push_connection( Acceptor & this, int fd ) {
200 PendingRead * p = 0p;
201 for() {
202 if(this.done) return;
203 p = pop(*this.queue);
204 if(p) break;
205 yield();
206 this.stats.creates++;
207 };
208
209 p->out.fd = fd;
210 async_recv(p->f, p->out.fd, p->in.buf, p->in.len, 0, CFA_IO_LAZY);
211}
212
213// #define ACCEPT_SPIN
214#define ACCEPT_MANY
215
216void main( Acceptor & this ) {
217 park();
218 unsigned long long last = rdtscl();
219
220#if defined(ACCEPT_SPIN)
221 if( options.log ) sout | "=== Accepting connection ===";
222 for() {
223 int fd = accept4(this.sockfd, this.[addr, addrlen, flags]);
224 if(fd < 0) {
225 if( errno == EWOULDBLOCK) {
226 this.stats.eagains++;
227 yield();
228 continue;
229 }
230 if( errno == ECONNABORTED ) break;
231 if( this.done && (errno == EINVAL || errno == EBADF) ) break;
232 abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
233 }
234 this.stats.accepts++;
235
236 if(this.done) return;
237
238 if( options.log ) sout | "=== New connection" | fd | "" | ", waiting for requests ===";
239
240 if(fd) push_connection(this, fd);
241
242 if (stats_thrd) {
243 unsigned long long next = rdtscl();
244 if(next > (last + 500000000)) {
245 if(try_lock(stats_thrd->stats.lock)) {
246 push(this.stats, stats_thrd->stats.accpt);
247 unlock(stats_thrd->stats.lock);
248 last = next;
249 }
250 }
251 }
252
253 if( options.log ) sout | "=== Accepting connection ===";
254 }
255
256#elif defined(ACCEPT_MANY)
257 const int nacc = 10;
258 io_future_t results[nacc];
259
260 for(i; nacc) {
261 io_future_t & res = results[i];
262 reset(res);
263 /* paranoid */ assert(!available(res));
264 if( options.log ) mutex(sout) sout | "=== Re-arming accept no" | i | " ===";
265 async_accept4(res, this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY);
266 }
267
268 for() {
269 if (stats_thrd) {
270 unsigned long long next = rdtscl();
271 if(next > (last + 500000000)) {
272 if(try_lock(stats_thrd->stats.lock __cfaabi_dbg_ctx2)) {
273 push(this.stats, stats_thrd->stats.accpt);
274 unlock(stats_thrd->stats.lock);
275 last = next;
276 }
277 }
278 }
279
280 for(i; nacc) {
281 io_future_t & res = results[i];
282 if(available(res)) {
283 if( options.log ) mutex(sout) sout | "=== Accept no " | i | "completed with result" | res.result | "===";
284 int fd = get_res(res);
285 reset(res);
286 this.stats.accepts++;
287 if(fd < 0) {
288 if( errno == ECONNABORTED ) continue;
289 if( this.done && (errno == EINVAL || errno == EBADF) ) continue;
290 abort( "accept error: (%d) %s\n", (int)errno, strerror(errno) );
291 }
292 push_connection( this, fd );
293
294 /* paranoid */ assert(!available(res));
295 if( options.log ) mutex(sout) sout | "=== Re-arming accept no" | i | " ===";
296 async_accept4(res, this.sockfd, this.[addr, addrlen, flags], CFA_IO_LAZY);
297 }
298 }
299 if(this.done) return;
300
301 if( options.log ) mutex(sout) sout | "=== Waiting for any accept ===";
302 this.stats.eagains++;
303 wait_any(results, nacc);
304
305 if( options.log ) mutex(sout) {
306 sout | "=== Acceptor wake-up ===";
307 for(i; nacc) {
308 io_future_t & res = results[i];
309 sout | i | "available:" | available(res);
310 }
311 }
312
313 }
314
315 for(i; nacc) {
316 wait(results[i]);
317 }
318#else
319#error no accept algorithm specified
320#endif
321}
Note: See TracBrowser for help on using the repository browser.