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 | extern void init_protocol(void); |
---|
49 | extern void deinit_protocol(void); |
---|
50 | |
---|
51 | //============================================================================================= |
---|
52 | // Main |
---|
53 | //=============================================================================================' |
---|
54 | int main( int argc, char * argv[] ) { |
---|
55 | //=================== |
---|
56 | // Parse args |
---|
57 | const char * path = parse_options(argc, argv); |
---|
58 | |
---|
59 | //=================== |
---|
60 | // Open Files |
---|
61 | if (FileExperiment == options.experiment.type) { |
---|
62 | printf("Filling cache from %s\n", path); |
---|
63 | fill_cache( path ); |
---|
64 | } |
---|
65 | |
---|
66 | //=================== |
---|
67 | // Open Socket |
---|
68 | printf("Listening on port %d\n", options.socket.port); |
---|
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 | |
---|
74 | int ret = 0; |
---|
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); |
---|
80 | address.sin_port = htons( options.socket.port ); |
---|
81 | |
---|
82 | int waited = 0; |
---|
83 | for() { |
---|
84 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
85 | if(ret < 0) { |
---|
86 | if(errno == EADDRINUSE) { |
---|
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; |
---|
100 | } |
---|
101 | |
---|
102 | ret = listen( server_fd, options.socket.backlog ); |
---|
103 | if(ret < 0) { |
---|
104 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
105 | } |
---|
106 | |
---|
107 | //=================== |
---|
108 | // Run Server Cluster |
---|
109 | { |
---|
110 | cluster cl = { "Server Cluster", options.clopts.params }; |
---|
111 | #if !defined(__CFA_NO_STATISTICS__) |
---|
112 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
113 | #endif |
---|
114 | options.clopts.instance = &cl; |
---|
115 | |
---|
116 | |
---|
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 | |
---|
126 | if(options.file_cache.fixed_fds) { |
---|
127 | register_fixed_files(cl, fds, pipe_off); |
---|
128 | } |
---|
129 | |
---|
130 | { |
---|
131 | ServerProc procs[options.clopts.nprocs]; |
---|
132 | |
---|
133 | init_protocol(); |
---|
134 | { |
---|
135 | Worker workers[options.clopts.nworkers]; |
---|
136 | for(i; options.clopts.nworkers) { |
---|
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 | { |
---|
143 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
144 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
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; |
---|
149 | } |
---|
150 | unpark( workers[i] ); |
---|
151 | } |
---|
152 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
153 | { |
---|
154 | char buffer[128]; |
---|
155 | while(!feof(stdin)) { |
---|
156 | fgets(buffer, 128, stdin); |
---|
157 | } |
---|
158 | |
---|
159 | printf("Shutting Down\n"); |
---|
160 | } |
---|
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 | } |
---|
178 | } |
---|
179 | printf("Workers Closed\n"); |
---|
180 | |
---|
181 | deinit_protocol(); |
---|
182 | } |
---|
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); |
---|
191 | |
---|
192 | } |
---|
193 | |
---|
194 | //=================== |
---|
195 | // Close Files |
---|
196 | printf("Closing Files\n"); |
---|
197 | close_cache(); |
---|
198 | } |
---|