Changeset 5407cdc for benchmark/io/http/protocol.cfa
- Timestamp:
- Apr 28, 2021, 4:56:50 PM (5 years ago)
- 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. - File:
-
- 1 edited
-
benchmark/io/http/protocol.cfa (modified) (11 diffs)
Legend:
- Unmodified
- Added
- Removed
-
benchmark/io/http/protocol.cfa
rfeacef9 r5407cdc 5 5 #include <fcntl.h> 6 6 } 7 8 #define xstr(s) str(s) 9 #define str(s) #s 7 10 8 11 #include <fstream.hfa> … … 20 23 #include "options.hfa" 21 24 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 29 struct https_msg_str { 30 char msg[512]; 31 size_t len; 32 }; 33 34 const https_msg_str * volatile http_msgs[KNOWN_CODES] = { 0 }; 33 35 34 36 _Static_assert( KNOWN_CODES == (sizeof(http_msgs ) / sizeof(http_msgs [0]))); 35 37 36 const int http_codes[] = { 38 const int http_codes[KNOWN_CODES] = { 39 200, 37 40 200, 38 41 400, … … 53 56 while(len > 0) { 54 57 // 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); 57 59 if( ret < 0 ) { 58 60 if( errno == ECONNRESET || errno == EPIPE ) return -ECONNRESET; … … 72 74 /* paranoid */ assert( code < KNOWN_CODES && code != OK200 ); 73 75 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 ); 75 77 } 76 78 77 79 int 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); 82 86 return answer( fd, buffer, len ); 83 87 } 84 88 85 int answer_plain( int fd, char buffer[], size_t size ) { 86 int ret = answer_header(fd, size); 89 #if defined(PLAINTEXT_NOCOPY) 90 int 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 95 int 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) 111 int 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 125 int answer_plaintext( int fd ) { 126 char text[] = "Hello, World!\n\n"; 127 int ret = answer_header(fd, sizeof(text)); 87 128 if( ret < 0 ) return ret; 88 return answer(fd, buffer, size); 89 } 129 return answer(fd, text, sizeof(text)); 130 } 131 #endif 90 132 91 133 int answer_empty( int fd ) { … … 94 136 95 137 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) { 97 139 char * it = buffer; 98 140 size_t count = len - 1; … … 100 142 READ: 101 143 for() { 102 int ret = cfa_re ad(fd, (void*)it, count, 0, -1`s, cancel, 0p);144 int ret = cfa_recv(fd, (void*)it, count, 0, CFA_IO_LAZY); 103 145 // int ret = read(fd, (void*)it, count); 104 146 if(ret == 0 ) return [OK200, true, 0, 0]; … … 139 181 ssize_t ret; 140 182 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); 143 184 if( ret < 0 ) { 144 185 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE1; … … 152 193 size_t in_pipe = ret; 153 194 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); 156 196 if( ret < 0 ) { 157 197 if( errno != EAGAIN && errno != EWOULDBLOCK) continue SPLICE2; … … 173 213 #include <thread.hfa> 174 214 215 const 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 175 226 struct date_buffer { 176 char buff[100];227 https_msg_str strs[KNOWN_CODES]; 177 228 }; 178 229 … … 183 234 184 235 void ?{}( DateFormater & this ) { 185 ((thread&)this){ "Server Date Thread", *options.clopts.instance };236 ((thread&)this){ "Server Date Thread", *options.clopts.instance[0] }; 186 237 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]) ); 189 240 } 190 241 … … 196 247 or else {} 197 248 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 } 204 264 this.idx = (this.idx + 1) % 2; 265 266 sout | "Date thread sleeping"; 205 267 206 268 sleep(1`s);
Note:
See TracChangeset
for help on using the changeset viewer.