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>
|
---|
14 | #include <time.hfa>
|
---|
15 | #include <thread.hfa>
|
---|
16 |
|
---|
17 | #include "filecache.hfa"
|
---|
18 | #include "options.hfa"
|
---|
19 | #include "worker.hfa"
|
---|
20 |
|
---|
21 | extern void register_fixed_files( cluster &, int *, unsigned count );
|
---|
22 |
|
---|
23 | Duration default_preemption() {
|
---|
24 | return 0;
|
---|
25 | }
|
---|
26 |
|
---|
27 | //=============================================================================================
|
---|
28 | // Globals
|
---|
29 | //=============================================================================================
|
---|
30 | struct ServerProc {
|
---|
31 | processor self;
|
---|
32 | };
|
---|
33 |
|
---|
34 | void ?{}( ServerProc & this ) {
|
---|
35 | /* paranoid */ assert( options.clopts.instance != 0p );
|
---|
36 | (this.self){ "Benchmark Processor", *options.clopts.instance };
|
---|
37 |
|
---|
38 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
39 | if( options.clopts.procstats ) {
|
---|
40 | print_stats_at_exit( this.self, options.clopts.instance->print_stats );
|
---|
41 | }
|
---|
42 | if( options.clopts.viewhalts ) {
|
---|
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
|
---|
54 | const char * path = parse_options(argc, argv);
|
---|
55 |
|
---|
56 | //===================
|
---|
57 | // Open Files
|
---|
58 | printf("Filling cache from %s\n", path);
|
---|
59 | fill_cache( path );
|
---|
60 |
|
---|
61 | //===================
|
---|
62 | // Open Socket
|
---|
63 | printf("Listening on port %d\n", options.socket.port);
|
---|
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 |
|
---|
69 | int ret = 0;
|
---|
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);
|
---|
75 | address.sin_port = htons( options.socket.port );
|
---|
76 |
|
---|
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;
|
---|
95 | }
|
---|
96 |
|
---|
97 | ret = listen( server_fd, options.socket.backlog );
|
---|
98 | if(ret < 0) {
|
---|
99 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
|
---|
100 | }
|
---|
101 |
|
---|
102 | //===================
|
---|
103 | // Run Server Cluster
|
---|
104 | {
|
---|
105 | cluster cl = { "Server Cluster", options.clopts.params };
|
---|
106 | #if !defined(__CFA_NO_STATISTICS__)
|
---|
107 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
|
---|
108 | #endif
|
---|
109 | options.clopts.instance = &cl;
|
---|
110 |
|
---|
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 |
|
---|
120 | if(options.file_cache.fixed_fds) {
|
---|
121 | register_fixed_files(cl, fds, pipe_off);
|
---|
122 | }
|
---|
123 |
|
---|
124 | {
|
---|
125 | ServerProc procs[options.clopts.nprocs];
|
---|
126 | {
|
---|
127 | Worker workers[options.clopts.nworkers];
|
---|
128 | for(i; options.clopts.nworkers) {
|
---|
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 | {
|
---|
135 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
|
---|
136 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
|
---|
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;
|
---|
141 | }
|
---|
142 | unpark( workers[i] );
|
---|
143 | }
|
---|
144 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs);
|
---|
145 | {
|
---|
146 | char buffer[128];
|
---|
147 | while(!feof(stdin)) {
|
---|
148 | fgets(buffer, 128, stdin);
|
---|
149 | }
|
---|
150 |
|
---|
151 | printf("Shutting Down\n");
|
---|
152 | }
|
---|
153 | }
|
---|
154 | printf("Workers Closed\n");
|
---|
155 | }
|
---|
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);
|
---|
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 | }
|
---|