[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 |
|
---|
| 77 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
|
---|
| 78 | if(ret < 0) {
|
---|
| 79 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
|
---|
| 80 | }
|
---|
| 81 |
|
---|
[2ecbd7b] | 82 | ret = listen( server_fd, options.socket.backlog );
|
---|
[0aec496] | 83 | if(ret < 0) {
|
---|
| 84 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
|
---|
| 85 | }
|
---|
| 86 |
|
---|
| 87 | //===================
|
---|
| 88 | // Run Server Cluster
|
---|
| 89 | {
|
---|
[d11d6eb] | 90 | cluster cl = { "Server Cluster", options.clopts.params };
|
---|
[0aec496] | 91 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
| 92 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
|
---|
| 93 | #endif
|
---|
[2ecbd7b] | 94 | options.clopts.instance = &cl;
|
---|
[0aec496] | 95 |
|
---|
[d9c2284] | 96 | int pipe_cnt = options.clopts.nworkers * 2;
|
---|
| 97 | int pipe_off;
|
---|
| 98 | int * fds;
|
---|
| 99 | [fds, pipe_off] = filefds( pipe_cnt );
|
---|
| 100 | for(i; 0 ~ pipe_cnt ~ 2) {
|
---|
| 101 | int ret = pipe(&fds[pipe_off + i]);
|
---|
| 102 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[d11d6eb] | 105 | if(options.file_cache.fixed_fds) {
|
---|
| 106 | register_fixed_files(cl, fds, pipe_off);
|
---|
| 107 | }
|
---|
| 108 |
|
---|
[0aec496] | 109 | {
|
---|
[2ecbd7b] | 110 | ServerProc procs[options.clopts.nprocs];
|
---|
[0aec496] | 111 | {
|
---|
[2ecbd7b] | 112 | Worker workers[options.clopts.nworkers];
|
---|
[d9c2284] | 113 | for(i; options.clopts.nworkers) {
|
---|
[d11d6eb] | 114 | // if( options.file_cache.fixed_fds ) {
|
---|
| 115 | // workers[i].pipe[0] = pipe_off + (i * 2) + 0;
|
---|
| 116 | // workers[i].pipe[1] = pipe_off + (i * 2) + 1;
|
---|
| 117 | // }
|
---|
| 118 | // else
|
---|
| 119 | {
|
---|
[d9c2284] | 120 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
|
---|
| 121 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
|
---|
[8e3034d] | 122 | workers[i].sockfd = server_fd;
|
---|
| 123 | workers[i].addr = (struct sockaddr *)&address;
|
---|
| 124 | workers[i].addrlen = (socklen_t*)&addrlen;
|
---|
| 125 | workers[i].flags = 0;
|
---|
[d9c2284] | 126 | }
|
---|
[e235429] | 127 | unpark( workers[i] );
|
---|
[d9c2284] | 128 | }
|
---|
[2ecbd7b] | 129 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs);
|
---|
[0aec496] | 130 | {
|
---|
[e95a117] | 131 | char buffer[128];
|
---|
| 132 | while(!feof(stdin)) {
|
---|
| 133 | fgets(buffer, 128, stdin);
|
---|
| 134 | }
|
---|
| 135 |
|
---|
| 136 | printf("Shutting Down\n");
|
---|
[0aec496] | 137 | }
|
---|
| 138 | }
|
---|
[e95a117] | 139 | printf("Workers Closed\n");
|
---|
[0aec496] | 140 | }
|
---|
[d9c2284] | 141 |
|
---|
| 142 | for(i; pipe_cnt) {
|
---|
| 143 | ret = close( fds[pipe_off + i] );
|
---|
| 144 | if(ret < 0) {
|
---|
| 145 | abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) );
|
---|
| 146 | }
|
---|
| 147 | }
|
---|
| 148 | free(fds);
|
---|
[0aec496] | 149 | }
|
---|
| 150 |
|
---|
| 151 | //===================
|
---|
| 152 | // Close Socket
|
---|
| 153 | printf("Closing Socket\n");
|
---|
| 154 | ret = close( server_fd );
|
---|
| 155 | if(ret < 0) {
|
---|
| 156 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
|
---|
| 157 | }
|
---|
| 158 |
|
---|
| 159 | //===================
|
---|
| 160 | // Close Files
|
---|
| 161 | printf("Closing Files\n");
|
---|
| 162 | close_cache();
|
---|
| 163 | }
|
---|