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 | printf("Filling cache from %s\n", path); |
---|
62 | fill_cache( path ); |
---|
63 | |
---|
64 | //=================== |
---|
65 | // Open Socket |
---|
66 | printf("%ld : Listening on port %d\n", getpid(), options.socket.port); |
---|
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 | |
---|
72 | int ret = 0; |
---|
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); |
---|
78 | address.sin_port = htons( options.socket.port ); |
---|
79 | |
---|
80 | int waited = 0; |
---|
81 | for() { |
---|
82 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
83 | if(ret < 0) { |
---|
84 | if(errno == EADDRINUSE) { |
---|
85 | if(waited == 0) { |
---|
86 | printf("Waiting for port\n"); |
---|
87 | } else { |
---|
88 | printf("\r%d", waited); |
---|
89 | fflush(stdout); |
---|
90 | } |
---|
91 | waited ++; |
---|
92 | sleep( 1`s ); |
---|
93 | continue; |
---|
94 | } |
---|
95 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
96 | } |
---|
97 | break; |
---|
98 | } |
---|
99 | |
---|
100 | ret = listen( server_fd, options.socket.backlog ); |
---|
101 | if(ret < 0) { |
---|
102 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
103 | } |
---|
104 | |
---|
105 | //=================== |
---|
106 | // Run Server Cluster |
---|
107 | { |
---|
108 | cluster cl = { "Server Cluster", options.clopts.params }; |
---|
109 | #if !defined(__CFA_NO_STATISTICS__) |
---|
110 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
111 | #endif |
---|
112 | options.clopts.instance = &cl; |
---|
113 | |
---|
114 | |
---|
115 | int pipe_cnt = options.clopts.nworkers * 2; |
---|
116 | int pipe_off; |
---|
117 | int * fds; |
---|
118 | [fds, pipe_off] = filefds( pipe_cnt ); |
---|
119 | for(i; 0 ~ pipe_cnt ~ 2) { |
---|
120 | int ret = pipe(&fds[pipe_off + i]); |
---|
121 | if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
122 | } |
---|
123 | |
---|
124 | if(options.file_cache.fixed_fds) { |
---|
125 | register_fixed_files(cl, fds, pipe_off); |
---|
126 | } |
---|
127 | |
---|
128 | { |
---|
129 | ServerProc procs[options.clopts.nprocs]; |
---|
130 | |
---|
131 | init_protocol(); |
---|
132 | { |
---|
133 | Worker workers[options.clopts.nworkers]; |
---|
134 | for(i; options.clopts.nworkers) { |
---|
135 | // if( options.file_cache.fixed_fds ) { |
---|
136 | // workers[i].pipe[0] = pipe_off + (i * 2) + 0; |
---|
137 | // workers[i].pipe[1] = pipe_off + (i * 2) + 1; |
---|
138 | // } |
---|
139 | // else |
---|
140 | { |
---|
141 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
142 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
143 | workers[i].sockfd = server_fd; |
---|
144 | workers[i].addr = (struct sockaddr *)&address; |
---|
145 | workers[i].addrlen = (socklen_t*)&addrlen; |
---|
146 | workers[i].flags = 0; |
---|
147 | } |
---|
148 | unpark( workers[i] ); |
---|
149 | } |
---|
150 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
151 | { |
---|
152 | char buffer[128]; |
---|
153 | while(!feof(stdin)) { |
---|
154 | fgets(buffer, 128, stdin); |
---|
155 | } |
---|
156 | |
---|
157 | printf("Shutting Down\n"); |
---|
158 | } |
---|
159 | |
---|
160 | for(i; options.clopts.nworkers) { |
---|
161 | printf("Cancelling %p\n", (void*)workers[i].cancel.target); |
---|
162 | workers[i].done = true; |
---|
163 | cancel(workers[i].cancel); |
---|
164 | } |
---|
165 | |
---|
166 | printf("Shutting down socket\n"); |
---|
167 | int ret = shutdown( server_fd, SHUT_RD ); |
---|
168 | if( ret < 0 ) { abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) ); } |
---|
169 | |
---|
170 | //=================== |
---|
171 | // Close Socket |
---|
172 | printf("Closing Socket\n"); |
---|
173 | ret = close( server_fd ); |
---|
174 | if(ret < 0) { |
---|
175 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
176 | } |
---|
177 | } |
---|
178 | printf("Workers Closed\n"); |
---|
179 | |
---|
180 | deinit_protocol(); |
---|
181 | } |
---|
182 | |
---|
183 | for(i; pipe_cnt) { |
---|
184 | ret = close( fds[pipe_off + i] ); |
---|
185 | if(ret < 0) { |
---|
186 | abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
187 | } |
---|
188 | } |
---|
189 | free(fds); |
---|
190 | |
---|
191 | } |
---|
192 | |
---|
193 | //=================== |
---|
194 | // Close Files |
---|
195 | printf("Closing Files\n"); |
---|
196 | close_cache(); |
---|
197 | } |
---|