[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 | |
---|
| 12 | #include <kernel.hfa> |
---|
| 13 | #include <stats.hfa> |
---|
[d11d6eb] | 14 | #include <time.hfa> |
---|
[0aec496] | 15 | #include <thread.hfa> |
---|
| 16 | |
---|
| 17 | #include "filecache.hfa" |
---|
| 18 | #include "options.hfa" |
---|
| 19 | #include "worker.hfa" |
---|
| 20 | |
---|
[d11d6eb] | 21 | extern void register_fixed_files( cluster &, int *, unsigned count ); |
---|
| 22 | |
---|
| 23 | Duration default_preemption() { |
---|
| 24 | return 0; |
---|
| 25 | } |
---|
| 26 | |
---|
[0aec496] | 27 | //============================================================================================= |
---|
| 28 | // Globals |
---|
| 29 | //============================================================================================= |
---|
| 30 | struct ServerProc { |
---|
| 31 | processor self; |
---|
| 32 | }; |
---|
| 33 | |
---|
| 34 | void ?{}( ServerProc & this ) { |
---|
[2ecbd7b] | 35 | /* paranoid */ assert( options.clopts.instance != 0p ); |
---|
| 36 | (this.self){ "Benchmark Processor", *options.clopts.instance }; |
---|
[0aec496] | 37 | |
---|
| 38 | #if !defined(__CFA_NO_STATISTICS__) |
---|
[2ecbd7b] | 39 | if( options.clopts.procstats ) { |
---|
| 40 | print_stats_at_exit( this.self, options.clopts.instance->print_stats ); |
---|
[0aec496] | 41 | } |
---|
[2ecbd7b] | 42 | if( options.clopts.viewhalts ) { |
---|
[0aec496] | 43 | print_halts( this.self ); |
---|
| 44 | } |
---|
| 45 | #endif |
---|
| 46 | } |
---|
| 47 | |
---|
[c3ee5f3] | 48 | extern void init_protocol(void); |
---|
| 49 | extern void deinit_protocol(void); |
---|
| 50 | |
---|
[0aec496] | 51 | //============================================================================================= |
---|
| 52 | // Main |
---|
| 53 | //=============================================================================================' |
---|
| 54 | int main( int argc, char * argv[] ) { |
---|
| 55 | //=================== |
---|
| 56 | // Parse args |
---|
[2ecbd7b] | 57 | const char * path = parse_options(argc, argv); |
---|
[0aec496] | 58 | |
---|
| 59 | //=================== |
---|
| 60 | // Open Files |
---|
[c3ee5f3] | 61 | if (FileExperiment == options.experiment.type) { |
---|
| 62 | printf("Filling cache from %s\n", path); |
---|
| 63 | fill_cache( path ); |
---|
| 64 | } |
---|
[0aec496] | 65 | |
---|
| 66 | //=================== |
---|
| 67 | // Open Socket |
---|
[2ecbd7b] | 68 | printf("Listening on port %d\n", options.socket.port); |
---|
[0aec496] | 69 | int server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 70 | if(server_fd < 0) { |
---|
| 71 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 72 | } |
---|
| 73 | |
---|
[7f389a5c] | 74 | int ret = 0; |
---|
[0aec496] | 75 | struct sockaddr_in address; |
---|
| 76 | int addrlen = sizeof(address); |
---|
| 77 | memset( (char *)&address, '\0' ); |
---|
| 78 | address.sin_family = AF_INET; |
---|
| 79 | address.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
[2ecbd7b] | 80 | address.sin_port = htons( options.socket.port ); |
---|
[0aec496] | 81 | |
---|
[ee913e0a] | 82 | int waited = 0; |
---|
| 83 | for() { |
---|
| 84 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
| 85 | if(ret < 0) { |
---|
[c3ee5f3] | 86 | if(errno == EADDRINUSE) { |
---|
[ee913e0a] | 87 | if(waited == 0) { |
---|
| 88 | printf("Waiting for port\n"); |
---|
| 89 | } else { |
---|
| 90 | printf("\r%d", waited); |
---|
| 91 | fflush(stdout); |
---|
| 92 | } |
---|
| 93 | waited ++; |
---|
| 94 | sleep( 1`s ); |
---|
| 95 | continue; |
---|
| 96 | } |
---|
| 97 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 98 | } |
---|
| 99 | break; |
---|
[0aec496] | 100 | } |
---|
| 101 | |
---|
[2ecbd7b] | 102 | ret = listen( server_fd, options.socket.backlog ); |
---|
[0aec496] | 103 | if(ret < 0) { |
---|
| 104 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 105 | } |
---|
| 106 | |
---|
| 107 | //=================== |
---|
| 108 | // Run Server Cluster |
---|
| 109 | { |
---|
[d11d6eb] | 110 | cluster cl = { "Server Cluster", options.clopts.params }; |
---|
[0aec496] | 111 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 112 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
| 113 | #endif |
---|
[2ecbd7b] | 114 | options.clopts.instance = &cl; |
---|
[0aec496] | 115 | |
---|
[c3ee5f3] | 116 | |
---|
[d9c2284] | 117 | int pipe_cnt = options.clopts.nworkers * 2; |
---|
| 118 | int pipe_off; |
---|
| 119 | int * fds; |
---|
| 120 | [fds, pipe_off] = filefds( pipe_cnt ); |
---|
| 121 | for(i; 0 ~ pipe_cnt ~ 2) { |
---|
| 122 | int ret = pipe(&fds[pipe_off + i]); |
---|
| 123 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
| 124 | } |
---|
| 125 | |
---|
[d11d6eb] | 126 | if(options.file_cache.fixed_fds) { |
---|
| 127 | register_fixed_files(cl, fds, pipe_off); |
---|
| 128 | } |
---|
| 129 | |
---|
[0aec496] | 130 | { |
---|
[2ecbd7b] | 131 | ServerProc procs[options.clopts.nprocs]; |
---|
[ece0e80] | 132 | |
---|
| 133 | init_protocol(); |
---|
[0aec496] | 134 | { |
---|
[2ecbd7b] | 135 | Worker workers[options.clopts.nworkers]; |
---|
[d9c2284] | 136 | for(i; options.clopts.nworkers) { |
---|
[d11d6eb] | 137 | // if( options.file_cache.fixed_fds ) { |
---|
| 138 | // workers[i].pipe[0] = pipe_off + (i * 2) + 0; |
---|
| 139 | // workers[i].pipe[1] = pipe_off + (i * 2) + 1; |
---|
| 140 | // } |
---|
| 141 | // else |
---|
| 142 | { |
---|
[d9c2284] | 143 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
| 144 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
[8e3034d] | 145 | workers[i].sockfd = server_fd; |
---|
| 146 | workers[i].addr = (struct sockaddr *)&address; |
---|
| 147 | workers[i].addrlen = (socklen_t*)&addrlen; |
---|
| 148 | workers[i].flags = 0; |
---|
[d9c2284] | 149 | } |
---|
[e235429] | 150 | unpark( workers[i] ); |
---|
[d9c2284] | 151 | } |
---|
[2ecbd7b] | 152 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
[0aec496] | 153 | { |
---|
[e95a117] | 154 | char buffer[128]; |
---|
| 155 | while(!feof(stdin)) { |
---|
| 156 | fgets(buffer, 128, stdin); |
---|
| 157 | } |
---|
| 158 | |
---|
| 159 | printf("Shutting Down\n"); |
---|
[0aec496] | 160 | } |
---|
[ece0e80] | 161 | |
---|
| 162 | for(i; options.clopts.nworkers) { |
---|
| 163 | printf("Cancelling %p\n", (void*)workers[i].cancel.target); |
---|
| 164 | cancel(workers[i].cancel); |
---|
| 165 | } |
---|
| 166 | |
---|
| 167 | printf("Shutting down socket\n"); |
---|
| 168 | int ret = shutdown( server_fd, SHUT_RD ); |
---|
| 169 | if( ret < 0 ) { abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
| 170 | |
---|
| 171 | //=================== |
---|
| 172 | // Close Socket |
---|
| 173 | printf("Closing Socket\n"); |
---|
| 174 | ret = close( server_fd ); |
---|
| 175 | if(ret < 0) { |
---|
| 176 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 177 | } |
---|
[0aec496] | 178 | } |
---|
[e95a117] | 179 | printf("Workers Closed\n"); |
---|
[ece0e80] | 180 | |
---|
| 181 | deinit_protocol(); |
---|
[0aec496] | 182 | } |
---|
[d9c2284] | 183 | |
---|
| 184 | for(i; pipe_cnt) { |
---|
| 185 | ret = close( fds[pipe_off + i] ); |
---|
| 186 | if(ret < 0) { |
---|
| 187 | abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 188 | } |
---|
| 189 | } |
---|
| 190 | free(fds); |
---|
[c3ee5f3] | 191 | |
---|
[0aec496] | 192 | } |
---|
| 193 | |
---|
| 194 | //=================== |
---|
| 195 | // Close Files |
---|
| 196 | printf("Closing Files\n"); |
---|
| 197 | close_cache(); |
---|
| 198 | } |
---|