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