[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 "channel.hfa" |
---|
| 18 | #include "filecache.hfa" |
---|
| 19 | #include "options.hfa" |
---|
| 20 | #include "worker.hfa" |
---|
| 21 | |
---|
[d11d6eb] | 22 | extern void register_fixed_files( cluster &, int *, unsigned count ); |
---|
| 23 | |
---|
| 24 | Duration default_preemption() { |
---|
| 25 | return 0; |
---|
| 26 | } |
---|
| 27 | |
---|
[0aec496] | 28 | //============================================================================================= |
---|
| 29 | // Globals |
---|
| 30 | //============================================================================================= |
---|
| 31 | channel & wait_connect; |
---|
| 32 | |
---|
| 33 | struct ServerProc { |
---|
| 34 | processor self; |
---|
| 35 | }; |
---|
| 36 | |
---|
| 37 | void ?{}( 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 | |
---|
| 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 |
---|
| 61 | printf("Filling cache from %s\n", path); |
---|
| 62 | fill_cache( path ); |
---|
| 63 | |
---|
| 64 | //=================== |
---|
| 65 | // Open Socket |
---|
[2ecbd7b] | 66 | printf("Listening on port %d\n", options.socket.port); |
---|
[0aec496] | 67 | int server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 68 | if(server_fd < 0) { |
---|
| 69 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 70 | } |
---|
| 71 | |
---|
[7f389a5c] | 72 | int ret = 0; |
---|
[0aec496] | 73 | struct sockaddr_in address; |
---|
| 74 | int addrlen = sizeof(address); |
---|
| 75 | memset( (char *)&address, '\0' ); |
---|
| 76 | address.sin_family = AF_INET; |
---|
| 77 | address.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
[2ecbd7b] | 78 | address.sin_port = htons( options.socket.port ); |
---|
[0aec496] | 79 | |
---|
| 80 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
| 81 | if(ret < 0) { |
---|
| 82 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 83 | } |
---|
| 84 | |
---|
[2ecbd7b] | 85 | ret = listen( server_fd, options.socket.backlog ); |
---|
[0aec496] | 86 | if(ret < 0) { |
---|
| 87 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 88 | } |
---|
| 89 | |
---|
| 90 | //=================== |
---|
| 91 | // Run Server Cluster |
---|
| 92 | { |
---|
[d11d6eb] | 93 | cluster cl = { "Server Cluster", options.clopts.params }; |
---|
[0aec496] | 94 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 95 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
| 96 | #endif |
---|
[2ecbd7b] | 97 | options.clopts.instance = &cl; |
---|
[0aec496] | 98 | |
---|
[2ecbd7b] | 99 | channel chan = { options.clopts.chan_size }; |
---|
[0aec496] | 100 | &wait_connect = &chan; |
---|
| 101 | |
---|
[d9c2284] | 102 | int pipe_cnt = options.clopts.nworkers * 2; |
---|
| 103 | int pipe_off; |
---|
| 104 | int * fds; |
---|
| 105 | [fds, pipe_off] = filefds( pipe_cnt ); |
---|
| 106 | for(i; 0 ~ pipe_cnt ~ 2) { |
---|
| 107 | int ret = pipe(&fds[pipe_off + i]); |
---|
| 108 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
| 109 | } |
---|
| 110 | |
---|
[d11d6eb] | 111 | if(options.file_cache.fixed_fds) { |
---|
| 112 | register_fixed_files(cl, fds, pipe_off); |
---|
| 113 | } |
---|
| 114 | |
---|
[0aec496] | 115 | { |
---|
[2ecbd7b] | 116 | ServerProc procs[options.clopts.nprocs]; |
---|
[0aec496] | 117 | { |
---|
[2ecbd7b] | 118 | Worker workers[options.clopts.nworkers]; |
---|
[d9c2284] | 119 | for(i; options.clopts.nworkers) { |
---|
[d11d6eb] | 120 | // if( options.file_cache.fixed_fds ) { |
---|
| 121 | // workers[i].pipe[0] = pipe_off + (i * 2) + 0; |
---|
| 122 | // workers[i].pipe[1] = pipe_off + (i * 2) + 1; |
---|
| 123 | // } |
---|
| 124 | // else |
---|
| 125 | { |
---|
[d9c2284] | 126 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
| 127 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
| 128 | } |
---|
| 129 | unpark( workers[i] __cfaabi_dbg_ctx2 ); |
---|
| 130 | } |
---|
[2ecbd7b] | 131 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
[0aec496] | 132 | { |
---|
| 133 | Acceptor acceptor = { server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen, 0 }; |
---|
[e95a117] | 134 | |
---|
| 135 | char buffer[128]; |
---|
| 136 | while(!feof(stdin)) { |
---|
| 137 | fgets(buffer, 128, stdin); |
---|
| 138 | } |
---|
| 139 | |
---|
| 140 | printf("Shutting Down\n"); |
---|
[0aec496] | 141 | } |
---|
[e95a117] | 142 | printf("Acceptor Closed\n"); |
---|
[0aec496] | 143 | |
---|
| 144 | // Clean-up the workers |
---|
[2ecbd7b] | 145 | for(options.clopts.nworkers) { |
---|
[0aec496] | 146 | put( wait_connect, -1 ); |
---|
| 147 | } |
---|
| 148 | } |
---|
[e95a117] | 149 | printf("Workers Closed\n"); |
---|
[0aec496] | 150 | } |
---|
[d9c2284] | 151 | |
---|
| 152 | for(i; pipe_cnt) { |
---|
| 153 | ret = close( fds[pipe_off + i] ); |
---|
| 154 | if(ret < 0) { |
---|
| 155 | abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 156 | } |
---|
| 157 | } |
---|
| 158 | free(fds); |
---|
[0aec496] | 159 | } |
---|
| 160 | |
---|
| 161 | //=================== |
---|
| 162 | // Close Socket |
---|
| 163 | printf("Closing Socket\n"); |
---|
| 164 | ret = close( server_fd ); |
---|
| 165 | if(ret < 0) { |
---|
| 166 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 167 | } |
---|
| 168 | |
---|
| 169 | //=================== |
---|
| 170 | // Close Files |
---|
| 171 | printf("Closing Files\n"); |
---|
| 172 | close_cache(); |
---|
| 173 | } |
---|