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 | 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 | |
---|
82 | ret = listen( server_fd, options.socket.backlog ); |
---|
83 | if(ret < 0) { |
---|
84 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
85 | } |
---|
86 | |
---|
87 | //=================== |
---|
88 | // Run Server Cluster |
---|
89 | { |
---|
90 | cluster cl = { "Server Cluster", options.clopts.params }; |
---|
91 | #if !defined(__CFA_NO_STATISTICS__) |
---|
92 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
93 | #endif |
---|
94 | options.clopts.instance = &cl; |
---|
95 | |
---|
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 | |
---|
105 | if(options.file_cache.fixed_fds) { |
---|
106 | register_fixed_files(cl, fds, pipe_off); |
---|
107 | } |
---|
108 | |
---|
109 | { |
---|
110 | ServerProc procs[options.clopts.nprocs]; |
---|
111 | { |
---|
112 | Worker workers[options.clopts.nworkers]; |
---|
113 | for(i; options.clopts.nworkers) { |
---|
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 | { |
---|
120 | workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0]; |
---|
121 | workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1]; |
---|
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; |
---|
126 | } |
---|
127 | unpark( workers[i] __cfaabi_dbg_ctx2 ); |
---|
128 | } |
---|
129 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
130 | { |
---|
131 | char buffer[128]; |
---|
132 | while(!feof(stdin)) { |
---|
133 | fgets(buffer, 128, stdin); |
---|
134 | } |
---|
135 | |
---|
136 | printf("Shutting Down\n"); |
---|
137 | } |
---|
138 | } |
---|
139 | printf("Workers Closed\n"); |
---|
140 | } |
---|
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); |
---|
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 | } |
---|