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 <thread.hfa> |
---|
15 | |
---|
16 | #include "channel.hfa" |
---|
17 | #include "filecache.hfa" |
---|
18 | #include "options.hfa" |
---|
19 | #include "worker.hfa" |
---|
20 | |
---|
21 | //============================================================================================= |
---|
22 | // Globals |
---|
23 | //============================================================================================= |
---|
24 | channel & wait_connect; |
---|
25 | |
---|
26 | struct ServerProc { |
---|
27 | processor self; |
---|
28 | }; |
---|
29 | |
---|
30 | void ?{}( ServerProc & this ) { |
---|
31 | /* paranoid */ assert( options.clopts.instance != 0p ); |
---|
32 | (this.self){ "Benchmark Processor", *options.clopts.instance }; |
---|
33 | |
---|
34 | #if !defined(__CFA_NO_STATISTICS__) |
---|
35 | if( options.clopts.procstats ) { |
---|
36 | print_stats_at_exit( this.self, options.clopts.instance->print_stats ); |
---|
37 | } |
---|
38 | if( options.clopts.viewhalts ) { |
---|
39 | print_halts( this.self ); |
---|
40 | } |
---|
41 | #endif |
---|
42 | } |
---|
43 | |
---|
44 | //============================================================================================= |
---|
45 | // Main |
---|
46 | //=============================================================================================' |
---|
47 | int main( int argc, char * argv[] ) { |
---|
48 | //=================== |
---|
49 | // Parse args |
---|
50 | const char * path = parse_options(argc, argv); |
---|
51 | |
---|
52 | //=================== |
---|
53 | // Open Files |
---|
54 | printf("Filling cache from %s\n", path); |
---|
55 | fill_cache( path ); |
---|
56 | |
---|
57 | //=================== |
---|
58 | // Open Socket |
---|
59 | printf("Listening on port %d\n", options.socket.port); |
---|
60 | int server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
61 | if(server_fd < 0) { |
---|
62 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
63 | } |
---|
64 | |
---|
65 | int ret = 0; |
---|
66 | struct sockaddr_in address; |
---|
67 | int addrlen = sizeof(address); |
---|
68 | memset( (char *)&address, '\0' ); |
---|
69 | address.sin_family = AF_INET; |
---|
70 | address.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
71 | address.sin_port = htons( options.socket.port ); |
---|
72 | |
---|
73 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
74 | if(ret < 0) { |
---|
75 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
76 | } |
---|
77 | |
---|
78 | ret = listen( server_fd, options.socket.backlog ); |
---|
79 | if(ret < 0) { |
---|
80 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
81 | } |
---|
82 | |
---|
83 | //=================== |
---|
84 | // Run Server Cluster |
---|
85 | { |
---|
86 | cluster cl = { "Server Cluster", options.clopts.flags }; |
---|
87 | #if !defined(__CFA_NO_STATISTICS__) |
---|
88 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
89 | #endif |
---|
90 | options.clopts.instance = &cl; |
---|
91 | |
---|
92 | channel chan = { options.clopts.chan_size }; |
---|
93 | &wait_connect = &chan; |
---|
94 | |
---|
95 | { |
---|
96 | ServerProc procs[options.clopts.nprocs]; |
---|
97 | { |
---|
98 | Worker workers[options.clopts.nworkers]; |
---|
99 | printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs); |
---|
100 | { |
---|
101 | Acceptor acceptor = { server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen, 0 }; |
---|
102 | |
---|
103 | char buffer[128]; |
---|
104 | while(!feof(stdin)) { |
---|
105 | fgets(buffer, 128, stdin); |
---|
106 | } |
---|
107 | |
---|
108 | printf("Shutting Down\n"); |
---|
109 | } |
---|
110 | printf("Acceptor Closed\n"); |
---|
111 | |
---|
112 | // Clean-up the workers |
---|
113 | for(options.clopts.nworkers) { |
---|
114 | put( wait_connect, -1 ); |
---|
115 | } |
---|
116 | } |
---|
117 | printf("Workers Closed\n"); |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | //=================== |
---|
122 | // Close Socket |
---|
123 | printf("Closing Socket\n"); |
---|
124 | ret = close( server_fd ); |
---|
125 | if(ret < 0) { |
---|
126 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
127 | } |
---|
128 | |
---|
129 | //=================== |
---|
130 | // Close Files |
---|
131 | printf("Closing Files\n"); |
---|
132 | close_cache(); |
---|
133 | } |
---|