source: benchmark/io/http/main.cfa@ d23c0b2

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since d23c0b2 was c2df3031, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

server now properly ignores SIGPIPE

  • Property mode set to 100644
File size: 6.2 KB
RevLine 
[0aec496]1#define __USE_GNU
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7extern "C" {
[c2df3031]8 #include <signal.h>
[0aec496]9 #include <sys/socket.h>
10 #include <netinet/in.h>
11}
12
[8c43d05]13#include <fstream.hfa>
[0aec496]14#include <kernel.hfa>
[153dc387]15#include <iofwd.hfa>
[0aec496]16#include <stats.hfa>
[d11d6eb]17#include <time.hfa>
[0aec496]18#include <thread.hfa>
19
20#include "filecache.hfa"
21#include "options.hfa"
22#include "worker.hfa"
23
[d11d6eb]24extern void register_fixed_files( cluster &, int *, unsigned count );
25
26Duration default_preemption() {
27 return 0;
28}
29
[0aec496]30//=============================================================================================
31// Globals
32//=============================================================================================
33struct ServerProc {
34 processor self;
35};
36
37void ?{}( ServerProc & this ) {
[2ecbd7b]38 /* paranoid */ assert( options.clopts.instance != 0p );
39 (this.self){ "Benchmark Processor", *options.clopts.instance };
[0aec496]40
41 #if !defined(__CFA_NO_STATISTICS__)
[2ecbd7b]42 if( options.clopts.procstats ) {
43 print_stats_at_exit( this.self, options.clopts.instance->print_stats );
[0aec496]44 }
[2ecbd7b]45 if( options.clopts.viewhalts ) {
[0aec496]46 print_halts( this.self );
47 }
48 #endif
49}
50
[c3ee5f3]51extern void init_protocol(void);
52extern void deinit_protocol(void);
53
[153dc387]54//=============================================================================================
55// Stats Printer
56//============================================================================================='
57
58thread StatsPrinter {};
59
60void ?{}( StatsPrinter & this ) {
61 ((thread&)this){ "Stats Printer Thread" };
62}
63
64void main(StatsPrinter & this) {
65 LOOP: for() {
66 waitfor( ^?{} : this) {
67 break LOOP;
68 }
69 or else {}
70
71 sleep(10`s);
72
73 print_stats_now( *options.clopts.instance, CFA_STATS_READY_Q | CFA_STATS_IO );
74 }
75}
76
[0aec496]77//=============================================================================================
78// Main
79//============================================================================================='
80int main( int argc, char * argv[] ) {
[c2df3031]81 __sighandler_t s = 1p;
82 signal(SIGPIPE, s);
83
[0aec496]84 //===================
85 // Parse args
[b57db73]86 parse_options(argc, argv);
[0aec496]87
88 //===================
89 // Open Files
[b57db73]90 if( options.file_cache.path ) {
91 sout | "Filling cache from" | options.file_cache.path;
92 fill_cache( options.file_cache.path );
93 }
[0aec496]94
95 //===================
96 // Open Socket
[8c43d05]97 sout | getpid() | ": Listening on port" | options.socket.port;
[0aec496]98 int server_fd = socket(AF_INET, SOCK_STREAM, 0);
99 if(server_fd < 0) {
100 abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) );
101 }
102
[7f389a5c]103 int ret = 0;
[0aec496]104 struct sockaddr_in address;
105 int addrlen = sizeof(address);
106 memset( (char *)&address, '\0' );
107 address.sin_family = AF_INET;
108 address.sin_addr.s_addr = htonl(INADDR_ANY);
[2ecbd7b]109 address.sin_port = htons( options.socket.port );
[0aec496]110
[ee913e0a]111 int waited = 0;
112 for() {
113 ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
114 if(ret < 0) {
[c3ee5f3]115 if(errno == EADDRINUSE) {
[ee913e0a]116 if(waited == 0) {
[8c43d05]117 sout | "Waiting for port";
[ee913e0a]118 } else {
[8c43d05]119 sout | "\r" | waited | nonl;
120 flush( sout );
[ee913e0a]121 }
122 waited ++;
123 sleep( 1`s );
124 continue;
125 }
126 abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
127 }
128 break;
[0aec496]129 }
130
[2ecbd7b]131 ret = listen( server_fd, options.socket.backlog );
[0aec496]132 if(ret < 0) {
133 abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
134 }
135
136 //===================
137 // Run Server Cluster
138 {
[d11d6eb]139 cluster cl = { "Server Cluster", options.clopts.params };
[0aec496]140 #if !defined(__CFA_NO_STATISTICS__)
141 print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
142 #endif
[2ecbd7b]143 options.clopts.instance = &cl;
[0aec496]144
[c3ee5f3]145
[d9c2284]146 int pipe_cnt = options.clopts.nworkers * 2;
147 int pipe_off;
148 int * fds;
149 [fds, pipe_off] = filefds( pipe_cnt );
150 for(i; 0 ~ pipe_cnt ~ 2) {
151 int ret = pipe(&fds[pipe_off + i]);
152 if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
153 }
154
[b57db73]155 if(options.file_cache.path && options.file_cache.fixed_fds) {
[d11d6eb]156 register_fixed_files(cl, fds, pipe_off);
157 }
158
[0aec496]159 {
[2ecbd7b]160 ServerProc procs[options.clopts.nprocs];
[153dc387]161 StatsPrinter printer;
[ece0e80]162
163 init_protocol();
[0aec496]164 {
[2ecbd7b]165 Worker workers[options.clopts.nworkers];
[d9c2284]166 for(i; options.clopts.nworkers) {
[d11d6eb]167 // if( options.file_cache.fixed_fds ) {
168 // workers[i].pipe[0] = pipe_off + (i * 2) + 0;
169 // workers[i].pipe[1] = pipe_off + (i * 2) + 1;
170 // }
171 // else
172 {
[d9c2284]173 workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
174 workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
[8e3034d]175 workers[i].sockfd = server_fd;
176 workers[i].addr = (struct sockaddr *)&address;
177 workers[i].addrlen = (socklen_t*)&addrlen;
178 workers[i].flags = 0;
[d9c2284]179 }
[e235429]180 unpark( workers[i] );
[d9c2284]181 }
[8c43d05]182 sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors";
[0aec496]183 {
[e95a117]184 char buffer[128];
[7223dbf2]185 while(int ret = cfa_read(0, buffer, 128, 0, -1`s, 0p, 0p); ret != 0) {
[153dc387]186 if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
[e95a117]187 }
188
[8c43d05]189 sout | "Shutdown received";
[0aec496]190 }
[ece0e80]191
[b57db73]192 sout | "Notifying connections..." | nonl; flush( sout );
[ece0e80]193 for(i; options.clopts.nworkers) {
[481ee28]194 workers[i].done = true;
[ece0e80]195 cancel(workers[i].cancel);
196 }
[b57db73]197 sout | "done";
[ece0e80]198
[b57db73]199 sout | "Shutting down socket..." | nonl; flush( sout );
[ece0e80]200 int ret = shutdown( server_fd, SHUT_RD );
[b57db73]201 if( ret < 0 ) {
202 abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) );
203 }
204 sout | "done";
[ece0e80]205
206 //===================
207 // Close Socket
[b57db73]208 sout | "Closing Socket..." | nonl; flush( sout );
[ece0e80]209 ret = close( server_fd );
210 if(ret < 0) {
211 abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
212 }
[0197418]213 sout | "done";
214
[b57db73]215 sout | "Stopping connection threads..." | nonl; flush( sout );
[0aec496]216 }
[8c43d05]217 sout | "done";
[ece0e80]218
[b57db73]219 sout | "Stopping protocol threads..." | nonl; flush( sout );
[ece0e80]220 deinit_protocol();
[8c43d05]221 sout | "done";
222
[b57db73]223 sout | "Stopping processors..." | nonl; flush( sout );
[0aec496]224 }
[8c43d05]225 sout | "done";
[d9c2284]226
[b57db73]227 sout | "Closing splice fds..." | nonl; flush( sout );
[d9c2284]228 for(i; pipe_cnt) {
229 ret = close( fds[pipe_off + i] );
230 if(ret < 0) {
231 abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) );
232 }
233 }
234 free(fds);
[8c43d05]235 sout | "done";
[c3ee5f3]236
[b57db73]237 sout | "Stopping processors..." | nonl; flush( sout );
[0aec496]238 }
[8c43d05]239 sout | "done";
[0aec496]240
241 //===================
242 // Close Files
[b57db73]243 if( options.file_cache.path ) {
244 sout | "Closing open files..." | nonl; flush( sout );
245 close_cache();
246 sout | "done";
247 }
[0aec496]248}
Note: See TracBrowser for help on using the repository browser.