[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 | |
---|
| 48 | //============================================================================================= |
---|
| 49 | // Main |
---|
| 50 | //=============================================================================================' |
---|
| 51 | int main( int argc, char * argv[] ) { |
---|
| 52 | //=================== |
---|
| 53 | // Parse args |
---|
[2ecbd7b] | 54 | const char * path = parse_options(argc, argv); |
---|
[0aec496] | 55 | |
---|
| 56 | //=================== |
---|
| 57 | // Open Files |
---|
| 58 | printf("Filling cache from %s\n", path); |
---|
| 59 | fill_cache( path ); |
---|
| 60 | |
---|
| 61 | //=================== |
---|
| 62 | // Open Socket |
---|
[2ecbd7b] | 63 | printf("Listening on port %d\n", options.socket.port); |
---|
[0aec496] | 64 | int server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
| 65 | if(server_fd < 0) { |
---|
| 66 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 67 | } |
---|
| 68 | |
---|
[7f389a5c] | 69 | int ret = 0; |
---|
[0aec496] | 70 | struct sockaddr_in address; |
---|
| 71 | int addrlen = sizeof(address); |
---|
| 72 | memset( (char *)&address, '\0' ); |
---|
| 73 | address.sin_family = AF_INET; |
---|
| 74 | address.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
[2ecbd7b] | 75 | address.sin_port = htons( options.socket.port ); |
---|
[0aec496] | 76 | |
---|
[ee913e0a] | 77 | int waited = 0; |
---|
| 78 | for() { |
---|
| 79 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
| 80 | if(ret < 0) { |
---|
| 81 | if(errno == 98) { |
---|
| 82 | if(waited == 0) { |
---|
| 83 | printf("Waiting for port\n"); |
---|
| 84 | } else { |
---|
| 85 | printf("\r%d", waited); |
---|
| 86 | fflush(stdout); |
---|
| 87 | } |
---|
| 88 | waited ++; |
---|
| 89 | sleep( 1`s ); |
---|
| 90 | continue; |
---|
| 91 | } |
---|
| 92 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 93 | } |
---|
| 94 | break; |
---|
[0aec496] | 95 | } |
---|
| 96 | |
---|
[2ecbd7b] | 97 | ret = listen( server_fd, options.socket.backlog ); |
---|
[0aec496] | 98 | if(ret < 0) { |
---|
| 99 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 100 | } |
---|
| 101 | |
---|
| 102 | //=================== |
---|
| 103 | // Run Server Cluster |
---|
| 104 | { |
---|
[d11d6eb] | 105 | cluster cl = { "Server Cluster", options.clopts.params }; |
---|
[0aec496] | 106 | #if !defined(__CFA_NO_STATISTICS__) |
---|
| 107 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
| 108 | #endif |
---|
[2ecbd7b] | 109 | options.clopts.instance = &cl; |
---|
[0aec496] | 110 | |
---|
[d9c2284] | 111 | int pipe_cnt = options.clopts.nworkers * 2; |
---|
| 112 | int pipe_off; |
---|
| 113 | int * fds; |
---|
| 114 | [fds, pipe_off] = filefds( pipe_cnt ); |
---|
| 115 | for(i; 0 ~ pipe_cnt ~ 2) { |
---|
| 116 | int ret = pipe(&fds[pipe_off + i]); |
---|
| 117 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
| 118 | } |
---|
| 119 | |
---|
[d11d6eb] | 120 | if(options.file_cache.fixed_fds) { |
---|
| 121 | register_fixed_files(cl, fds, pipe_off); |
---|
| 122 | } |
---|
| 123 | |
---|
[0aec496] | 124 | { |
---|
[2ecbd7b] | 125 | ServerProc procs[options.clopts.nprocs]; |
---|
[0aec496] | 126 | { |
---|
[2ecbd7b] | 127 | Worker workers[options.clopts.nworkers]; |
---|
[d9c2284] | 128 | for(i; options.clopts.nworkers) { |
---|
[d11d6eb] | 129 | // if( options.file_cache.fixed_fds ) { |
---|
| 130 | // workers[i].pipe[0] = pipe_off + (i * 2) + 0; |
---|
| 131 | // workers[i].pipe[1] = pipe_off + (i * 2) + 1; |
---|
| 132 | // } |
---|
| 133 | // else |
---|
| 134 | { |
---|
[d9c2284] | 135 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
| 136 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
[8e3034d] | 137 | workers[i].sockfd = server_fd; |
---|
| 138 | workers[i].addr = (struct sockaddr *)&address; |
---|
| 139 | workers[i].addrlen = (socklen_t*)&addrlen; |
---|
| 140 | workers[i].flags = 0; |
---|
[d9c2284] | 141 | } |
---|
[e235429] | 142 | unpark( workers[i] ); |
---|
[d9c2284] | 143 | } |
---|
[2ecbd7b] | 144 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
[0aec496] | 145 | { |
---|
[e95a117] | 146 | char buffer[128]; |
---|
| 147 | while(!feof(stdin)) { |
---|
| 148 | fgets(buffer, 128, stdin); |
---|
| 149 | } |
---|
| 150 | |
---|
| 151 | printf("Shutting Down\n"); |
---|
[0aec496] | 152 | } |
---|
| 153 | } |
---|
[e95a117] | 154 | printf("Workers Closed\n"); |
---|
[0aec496] | 155 | } |
---|
[d9c2284] | 156 | |
---|
| 157 | for(i; pipe_cnt) { |
---|
| 158 | ret = close( fds[pipe_off + i] ); |
---|
| 159 | if(ret < 0) { |
---|
| 160 | abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 161 | } |
---|
| 162 | } |
---|
| 163 | free(fds); |
---|
[0aec496] | 164 | } |
---|
| 165 | |
---|
| 166 | //=================== |
---|
| 167 | // Close Socket |
---|
| 168 | printf("Closing Socket\n"); |
---|
| 169 | ret = close( server_fd ); |
---|
| 170 | if(ret < 0) { |
---|
| 171 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
| 172 | } |
---|
| 173 | |
---|
| 174 | //=================== |
---|
| 175 | // Close Files |
---|
| 176 | printf("Closing Files\n"); |
---|
| 177 | close_cache(); |
---|
| 178 | } |
---|