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 "parseargs.hfa" |
---|
20 | #include "worker.hfa" |
---|
21 | |
---|
22 | //============================================================================================= |
---|
23 | // Globals |
---|
24 | //============================================================================================= |
---|
25 | Options options @= { |
---|
26 | 0, // open_flags; |
---|
27 | 42u, // hash_seed; |
---|
28 | 0, // file_cache_size; |
---|
29 | false, // file_cache_list; |
---|
30 | false, // procstats; |
---|
31 | false, // viewhalts; |
---|
32 | 0 // the_cluster; |
---|
33 | }; |
---|
34 | |
---|
35 | channel & wait_connect; |
---|
36 | |
---|
37 | struct ServerProc { |
---|
38 | processor self; |
---|
39 | }; |
---|
40 | |
---|
41 | void ?{}( ServerProc & this ) { |
---|
42 | /* paranoid */ assert( options.the_cluster != 0p ); |
---|
43 | (this.self){ "Benchmark Processor", *options.the_cluster }; |
---|
44 | |
---|
45 | #if !defined(__CFA_NO_STATISTICS__) |
---|
46 | if( options.procstats ) { |
---|
47 | print_stats_at_exit( this.self, options.the_cluster->print_stats ); |
---|
48 | } |
---|
49 | if( options.viewhalts ) { |
---|
50 | print_halts( this.self ); |
---|
51 | } |
---|
52 | #endif |
---|
53 | } |
---|
54 | |
---|
55 | //============================================================================================= |
---|
56 | // Main |
---|
57 | //=============================================================================================' |
---|
58 | int main( int argc, char * argv[] ) { |
---|
59 | int port = 8080; |
---|
60 | int backlog = 10; |
---|
61 | int nprocs = 1; |
---|
62 | int nworkers = 1; |
---|
63 | int cl_flags = 0; |
---|
64 | int chan_size = 10; |
---|
65 | const char * path = "."; |
---|
66 | //=================== |
---|
67 | // Parse args |
---|
68 | static cfa_option opt[] = { |
---|
69 | {'p', "port", "Port the server will listen on", port}, |
---|
70 | {'c', "cpus", "Number of processors to use", nprocs}, |
---|
71 | {'t', "threads", "Number of worker threads to use", nworkers}, |
---|
72 | {'b', "accept-backlog", "Maximum number of pending accepts", backlog}, |
---|
73 | {'B', "channel-size", "Maximum number of accepted connection pending", chan_size}, |
---|
74 | {'S', "seed", "seed to use for hashing", options.hash_seed }, |
---|
75 | {'C', "cache-size", "Size of the cache to use, if set to small, will uses closes power of 2", options.file_cache_size }, |
---|
76 | {'l', "list-files", "List the files in the specified path and exit", options.file_cache_list, parse_settrue } |
---|
77 | |
---|
78 | }; |
---|
79 | int opt_cnt = sizeof(opt) / sizeof(cfa_option); |
---|
80 | |
---|
81 | char **left; |
---|
82 | parse_args( argc, argv, opt, opt_cnt, "[OPTIONS]... [PATH]\ncforall http server", left ); |
---|
83 | if( left[0] != 0p ) { |
---|
84 | path = left[0]; |
---|
85 | left++; |
---|
86 | } |
---|
87 | if( left[0] != 0p ) { |
---|
88 | abort("Too many trailing arguments!\n"); |
---|
89 | } |
---|
90 | |
---|
91 | |
---|
92 | //=================== |
---|
93 | // Open Files |
---|
94 | printf("Filling cache from %s\n", path); |
---|
95 | fill_cache( path ); |
---|
96 | |
---|
97 | //=================== |
---|
98 | // Open Socket |
---|
99 | printf("Listening on port %d\n", port); |
---|
100 | int server_fd = socket(AF_INET, SOCK_STREAM, 0); |
---|
101 | if(server_fd < 0) { |
---|
102 | abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
103 | } |
---|
104 | |
---|
105 | int ret = 0; |
---|
106 | struct sockaddr_in address; |
---|
107 | int addrlen = sizeof(address); |
---|
108 | memset( (char *)&address, '\0' ); |
---|
109 | address.sin_family = AF_INET; |
---|
110 | address.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
111 | address.sin_port = htons( port ); |
---|
112 | |
---|
113 | ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) ); |
---|
114 | if(ret < 0) { |
---|
115 | abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
116 | } |
---|
117 | |
---|
118 | ret = listen( server_fd, backlog ); |
---|
119 | if(ret < 0) { |
---|
120 | abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
121 | } |
---|
122 | |
---|
123 | //=================== |
---|
124 | // Run Server Cluster |
---|
125 | { |
---|
126 | cluster cl = { "Server Cluster", cl_flags }; |
---|
127 | #if !defined(__CFA_NO_STATISTICS__) |
---|
128 | print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO ); |
---|
129 | #endif |
---|
130 | options.the_cluster = &cl; |
---|
131 | |
---|
132 | channel chan = { chan_size }; |
---|
133 | &wait_connect = &chan; |
---|
134 | |
---|
135 | { |
---|
136 | ServerProc procs[nprocs]; |
---|
137 | { |
---|
138 | Worker workers[nworkers]; |
---|
139 | printf("%d workers started on %d processors\n", nworkers, nprocs); |
---|
140 | { |
---|
141 | Acceptor acceptor = { server_fd, (struct sockaddr *)&address, (socklen_t*)&addrlen, 0 }; |
---|
142 | } |
---|
143 | printf("Shutting Down\n"); |
---|
144 | |
---|
145 | // Clean-up the workers |
---|
146 | for(nworkers) { |
---|
147 | put( wait_connect, -1 ); |
---|
148 | } |
---|
149 | } |
---|
150 | } |
---|
151 | } |
---|
152 | |
---|
153 | //=================== |
---|
154 | // Close Socket |
---|
155 | printf("Closing Socket\n"); |
---|
156 | ret = close( server_fd ); |
---|
157 | if(ret < 0) { |
---|
158 | abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) ); |
---|
159 | } |
---|
160 | |
---|
161 | //=================== |
---|
162 | // Close Files |
---|
163 | printf("Closing Files\n"); |
---|
164 | close_cache(); |
---|
165 | } |
---|