[0aec496] | 1 | #define __USE_GNU |
---|
| 2 | |
---|
| 3 | #include <errno.h> |
---|
| 4 | #include <stdio.h> |
---|
| 5 | #include <string.h> |
---|
| 6 | #include <unistd.h> |
---|
| 7 | extern "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] | 24 | extern void register_fixed_files( cluster &, int *, unsigned count ); |
---|
| 25 | |
---|
| 26 | Duration default_preemption() { |
---|
| 27 | return 0; |
---|
| 28 | } |
---|
| 29 | |
---|
[153dc387] | 30 | //============================================================================================= |
---|
| 31 | // Stats Printer |
---|
| 32 | //=============================================================================================' |
---|
| 33 | |
---|
| 34 | thread StatsPrinter {}; |
---|
| 35 | |
---|
[348f81d5] | 36 | void ?{}( StatsPrinter & this, cluster & cl ) { |
---|
| 37 | ((thread&)this){ "Stats Printer Thread", cl }; |
---|
[153dc387] | 38 | } |
---|
| 39 | |
---|
| 40 | void main(StatsPrinter & this) { |
---|
| 41 | LOOP: for() { |
---|
| 42 | waitfor( ^?{} : this) { |
---|
| 43 | break LOOP; |
---|
| 44 | } |
---|
| 45 | or else {} |
---|
| 46 | |
---|
| 47 | sleep(10`s); |
---|
| 48 | |
---|
[348f81d5] | 49 | print_stats_now( *active_cluster(), CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
| 50 | } |
---|
| 51 | } |
---|
| 52 | |
---|
| 53 | //============================================================================================= |
---|
| 54 | // Globals |
---|
| 55 | //============================================================================================= |
---|
| 56 | // struct ServerProc { |
---|
| 57 | // processor self; |
---|
| 58 | // }; |
---|
| 59 | |
---|
| 60 | // void ?{}( ServerProc & this ) { |
---|
| 61 | // /* paranoid */ assert( options.clopts.instance != 0p ); |
---|
| 62 | // (this.self){ "Benchmark Processor", *options.clopts.instance }; |
---|
| 63 | |
---|
| 64 | // #if !defined(__CFA_NO_STATISTICS__) |
---|
| 65 | // if( options.clopts.procstats ) { |
---|
| 66 | // print_stats_at_exit( this.self, options.clopts.instance->print_stats ); |
---|
| 67 | // } |
---|
| 68 | // if( options.clopts.viewhalts ) { |
---|
| 69 | // print_halts( this.self ); |
---|
| 70 | // } |
---|
| 71 | // #endif |
---|
| 72 | // } |
---|
| 73 | |
---|
| 74 | // struct ServerIoContext { |
---|
| 75 | // io_context self; |
---|
| 76 | // }; |
---|
| 77 | |
---|
| 78 | // void ?{}( ServerIoContext & this ) { |
---|
| 79 | // /* paranoid */ assert( options.clopts.instance != 0p ); |
---|
| 80 | // (this.self){ *options.clopts.instance }; |
---|
| 81 | // } |
---|
| 82 | |
---|
| 83 | struct ServerCluster { |
---|
| 84 | cluster self; |
---|
| 85 | processor * procs; |
---|
| 86 | io_context * ctxs; |
---|
| 87 | StatsPrinter * prnt; |
---|
| 88 | |
---|
| 89 | }; |
---|
| 90 | |
---|
| 91 | void ?{}( ServerCluster & this ) { |
---|
| 92 | (this.self){ "Server Cluster", options.clopts.params }; |
---|
| 93 | |
---|
| 94 | this.procs = alloc(options.clopts.nprocs); |
---|
| 95 | for(i; options.clopts.nprocs) { |
---|
| 96 | (this.procs[i]){ "Benchmark Processor", this.self }; |
---|
| 97 | |
---|
| 98 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 99 | if( options.clopts.procstats ) { |
---|
| 100 | print_stats_at_exit( *this.procs, this.self.print_stats ); |
---|
| 101 | } |
---|
| 102 | if( options.clopts.viewhalts ) { |
---|
| 103 | print_halts( *this.procs ); |
---|
| 104 | } |
---|
| 105 | #endif |
---|
| 106 | } |
---|
| 107 | |
---|
| 108 | this.ctxs = alloc(options.clopts.nprocs); |
---|
| 109 | for(i; options.clopts.nprocs) { |
---|
| 110 | (this.ctxs[i]){ this.self }; |
---|
[153dc387] | 111 | } |
---|
[348f81d5] | 112 | |
---|
| 113 | this.prnt = alloc(options.clopts.nprocs); |
---|
| 114 | for(i; options.clopts.nprocs) { |
---|
| 115 | (this.prnt[i]){ this.self }; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 119 | print_stats_at_exit( this.self, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
| 120 | #endif |
---|
| 121 | |
---|
| 122 | options.clopts.instance[options.clopts.cltr_cnt] = &this.self; |
---|
| 123 | options.clopts.cltr_cnt++; |
---|
[153dc387] | 124 | } |
---|
| 125 | |
---|
[348f81d5] | 126 | void ^?{}( ServerCluster & this ) { |
---|
| 127 | for(i; options.clopts.nprocs) { |
---|
| 128 | ^(this.prnt[i]){}; |
---|
| 129 | } |
---|
| 130 | free(this.prnt); |
---|
| 131 | |
---|
| 132 | for(i; options.clopts.nprocs) { |
---|
| 133 | ^(this.ctxs[i]){}; |
---|
| 134 | } |
---|
| 135 | free(this.ctxs); |
---|
| 136 | |
---|
| 137 | for(i; options.clopts.nprocs) { |
---|
| 138 | ^(this.procs[i]){}; |
---|
| 139 | } |
---|
| 140 | free(this.procs); |
---|
| 141 | |
---|
| 142 | ^(this.self){}; |
---|
| 143 | } |
---|
| 144 | |
---|
| 145 | extern void init_protocol(void); |
---|
| 146 | extern void deinit_protocol(void); |
---|
| 147 | |
---|
[0aec496] | 148 | //============================================================================================= |
---|
| 149 | // Main |
---|
| 150 | //=============================================================================================' |
---|
| 151 | int main( int argc, char * argv[] ) { |
---|
[c2df3031] | 152 | __sighandler_t s = 1p; |
---|
| 153 | signal(SIGPIPE, s); |
---|
| 154 | |
---|
[0aec496] | 155 | //=================== |
---|
| 156 | // Parse args |
---|
[b57db73] | 157 | parse_options(argc, argv); |
---|
[0aec496] | 158 | |
---|
| 159 | //=================== |
---|
| 160 | // Open Files |
---|
[b57db73] | 161 | if( options.file_cache.path ) { |
---|
| 162 | sout | "Filling cache from" | options.file_cache.path; |
---|
| 163 | fill_cache( options.file_cache.path ); |
---|
| 164 | } |
---|
[0aec496] | 165 | |
---|
| 166 | //=================== |
---|
| 167 | // Open Socket |
---|
[8c43d05] | 168 | sout | getpid() | ": Listening on port" | options.socket.port; |
---|
[0aec496] | 169 | int server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 170 | if(server_fd < 0) { |
---|
| 171 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 172 | } |
---|
| 173 | |
---|
[7f389a5c] | 174 | int ret = 0; |
---|
[0aec496] | 175 | struct sockaddr_in address; |
---|
| 176 | int addrlen = sizeof(address); |
---|
| 177 | memset( (char *)&address, '\0' ); |
---|
| 178 | address.sin_family = AF_INET; |
---|
| 179 | address.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
[2ecbd7b] | 180 | address.sin_port = htons( options.socket.port ); |
---|
[0aec496] | 181 | |
---|
[ee913e0a] | 182 | int waited = 0; |
---|
| 183 | for() { |
---|
| 184 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
| 185 | if(ret < 0) { |
---|
[c3ee5f3] | 186 | if(errno == EADDRINUSE) { |
---|
[ee913e0a] | 187 | if(waited == 0) { |
---|
[8c43d05] | 188 | sout | "Waiting for port"; |
---|
[ee913e0a] | 189 | } else { |
---|
[8c43d05] | 190 | sout | "\r" | waited | nonl; |
---|
| 191 | flush( sout ); |
---|
[ee913e0a] | 192 | } |
---|
| 193 | waited ++; |
---|
| 194 | sleep( 1`s ); |
---|
| 195 | continue; |
---|
| 196 | } |
---|
| 197 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 198 | } |
---|
| 199 | break; |
---|
[0aec496] | 200 | } |
---|
| 201 | |
---|
[2ecbd7b] | 202 | ret = listen( server_fd, options.socket.backlog ); |
---|
[0aec496] | 203 | if(ret < 0) { |
---|
| 204 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 205 | } |
---|
| 206 | |
---|
| 207 | //=================== |
---|
| 208 | // Run Server Cluster |
---|
| 209 | { |
---|
[d9c2284] | 210 | int pipe_cnt = options.clopts.nworkers * 2; |
---|
| 211 | int pipe_off; |
---|
| 212 | int * fds; |
---|
| 213 | [fds, pipe_off] = filefds( pipe_cnt ); |
---|
| 214 | for(i; 0 ~ pipe_cnt ~ 2) { |
---|
| 215 | int ret = pipe(&fds[pipe_off + i]); |
---|
| 216 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
| 217 | } |
---|
| 218 | |
---|
[4f762d3] | 219 | // if(options.file_cache.path && options.file_cache.fixed_fds) { |
---|
| 220 | // register_fixed_files(cl, fds, pipe_off); |
---|
| 221 | // } |
---|
[d11d6eb] | 222 | |
---|
[0aec496] | 223 | { |
---|
[348f81d5] | 224 | ServerCluster cl[options.clopts.nclusters]; |
---|
[ece0e80] | 225 | |
---|
| 226 | init_protocol(); |
---|
[0aec496] | 227 | { |
---|
[2ecbd7b] | 228 | Worker workers[options.clopts.nworkers]; |
---|
[d9c2284] | 229 | for(i; options.clopts.nworkers) { |
---|
[d11d6eb] | 230 | // if( options.file_cache.fixed_fds ) { |
---|
| 231 | // workers[i].pipe[0] = pipe_off + (i * 2) + 0; |
---|
| 232 | // workers[i].pipe[1] = pipe_off + (i * 2) + 1; |
---|
| 233 | // } |
---|
| 234 | // else |
---|
| 235 | { |
---|
[d9c2284] | 236 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
| 237 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
[8e3034d] | 238 | workers[i].sockfd = server_fd; |
---|
| 239 | workers[i].addr = (struct sockaddr *)&address; |
---|
| 240 | workers[i].addrlen = (socklen_t*)&addrlen; |
---|
| 241 | workers[i].flags = 0; |
---|
[d9c2284] | 242 | } |
---|
[e235429] | 243 | unpark( workers[i] ); |
---|
[d9c2284] | 244 | } |
---|
[348f81d5] | 245 | sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors /" | options.clopts.nclusters | "clusters"; |
---|
| 246 | for(i; options.clopts.nclusters) { |
---|
| 247 | sout | options.clopts.thrd_cnt[i] | nonl; |
---|
| 248 | } |
---|
| 249 | sout | nl; |
---|
[0aec496] | 250 | { |
---|
[e95a117] | 251 | char buffer[128]; |
---|
[4f762d3] | 252 | while(int ret = cfa_read(0, buffer, 128, 0); ret != 0) { |
---|
[153dc387] | 253 | if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
[e95a117] | 254 | } |
---|
| 255 | |
---|
[8c43d05] | 256 | sout | "Shutdown received"; |
---|
[0aec496] | 257 | } |
---|
[ece0e80] | 258 | |
---|
[b57db73] | 259 | sout | "Notifying connections..." | nonl; flush( sout ); |
---|
[ece0e80] | 260 | for(i; options.clopts.nworkers) { |
---|
[481ee28] | 261 | workers[i].done = true; |
---|
[ece0e80] | 262 | } |
---|
[b57db73] | 263 | sout | "done"; |
---|
[ece0e80] | 264 | |
---|
[b57db73] | 265 | sout | "Shutting down socket..." | nonl; flush( sout ); |
---|
[ece0e80] | 266 | int ret = shutdown( server_fd, SHUT_RD ); |
---|
[b57db73] | 267 | if( ret < 0 ) { |
---|
| 268 | abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 269 | } |
---|
| 270 | sout | "done"; |
---|
[ece0e80] | 271 | |
---|
| 272 | //=================== |
---|
| 273 | // Close Socket |
---|
[b57db73] | 274 | sout | "Closing Socket..." | nonl; flush( sout ); |
---|
[ece0e80] | 275 | ret = close( server_fd ); |
---|
| 276 | if(ret < 0) { |
---|
| 277 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 278 | } |
---|
[0197418] | 279 | sout | "done"; |
---|
| 280 | |
---|
[b57db73] | 281 | sout | "Stopping connection threads..." | nonl; flush( sout ); |
---|
[0aec496] | 282 | } |
---|
[8c43d05] | 283 | sout | "done"; |
---|
[ece0e80] | 284 | |
---|
[b57db73] | 285 | sout | "Stopping protocol threads..." | nonl; flush( sout ); |
---|
[ece0e80] | 286 | deinit_protocol(); |
---|
[8c43d05] | 287 | sout | "done"; |
---|
| 288 | |
---|
[348f81d5] | 289 | sout | "Stopping processors/clusters..." | nonl; flush( sout ); |
---|
[0aec496] | 290 | } |
---|
[8c43d05] | 291 | sout | "done"; |
---|
[d9c2284] | 292 | |
---|
[b57db73] | 293 | sout | "Closing splice fds..." | nonl; flush( sout ); |
---|
[d9c2284] | 294 | for(i; pipe_cnt) { |
---|
| 295 | ret = close( fds[pipe_off + i] ); |
---|
| 296 | if(ret < 0) { |
---|
| 297 | abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 298 | } |
---|
| 299 | } |
---|
| 300 | free(fds); |
---|
[8c43d05] | 301 | sout | "done"; |
---|
[c3ee5f3] | 302 | |
---|
[b57db73] | 303 | sout | "Stopping processors..." | nonl; flush( sout ); |
---|
[0aec496] | 304 | } |
---|
[8c43d05] | 305 | sout | "done"; |
---|
[0aec496] | 306 | |
---|
| 307 | //=================== |
---|
| 308 | // Close Files |
---|
[b57db73] | 309 | if( options.file_cache.path ) { |
---|
| 310 | sout | "Closing open files..." | nonl; flush( sout ); |
---|
| 311 | close_cache(); |
---|
| 312 | sout | "done"; |
---|
| 313 | } |
---|
[0aec496] | 314 | } |
---|