source: benchmark/io/http/main.cfa@ ee59ede

ADT arm-eh ast-experimental enum forall-pointer-decay jacob/cs343-translation new-ast-unique-expr pthread-emulation qualifiedEnum
Last change on this file since ee59ede was 075b8fd, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Main now prints pid on startup

  • Property mode set to 100644
File size: 4.9 KB
RevLine 
[0aec496]1#define __USE_GNU
2
3#include <errno.h>
4#include <stdio.h>
5#include <string.h>
6#include <unistd.h>
7extern "C" {
8 #include <sys/socket.h>
9 #include <netinet/in.h>
10}
11
12#include <kernel.hfa>
13#include <stats.hfa>
[d11d6eb]14#include <time.hfa>
[0aec496]15#include <thread.hfa>
16
17#include "filecache.hfa"
18#include "options.hfa"
19#include "worker.hfa"
20
[d11d6eb]21extern void register_fixed_files( cluster &, int *, unsigned count );
22
23Duration default_preemption() {
24 return 0;
25}
26
[0aec496]27//=============================================================================================
28// Globals
29//=============================================================================================
30struct ServerProc {
31 processor self;
32};
33
34void ?{}( ServerProc & this ) {
[2ecbd7b]35 /* paranoid */ assert( options.clopts.instance != 0p );
36 (this.self){ "Benchmark Processor", *options.clopts.instance };
[0aec496]37
38 #if !defined(__CFA_NO_STATISTICS__)
[2ecbd7b]39 if( options.clopts.procstats ) {
40 print_stats_at_exit( this.self, options.clopts.instance->print_stats );
[0aec496]41 }
[2ecbd7b]42 if( options.clopts.viewhalts ) {
[0aec496]43 print_halts( this.self );
44 }
45 #endif
46}
47
[c3ee5f3]48extern void init_protocol(void);
49extern void deinit_protocol(void);
50
[0aec496]51//=============================================================================================
52// Main
53//============================================================================================='
54int main( int argc, char * argv[] ) {
55 //===================
56 // Parse args
[2ecbd7b]57 const char * path = parse_options(argc, argv);
[0aec496]58
59 //===================
60 // Open Files
[481ee28]61 printf("Filling cache from %s\n", path);
62 fill_cache( path );
[0aec496]63
64 //===================
65 // Open Socket
[075b8fd]66 printf("%ld : Listening on port %d\n", getpid(), options.socket.port);
[0aec496]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
[7f389a5c]72 int ret = 0;
[0aec496]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);
[2ecbd7b]78 address.sin_port = htons( options.socket.port );
[0aec496]79
[ee913e0a]80 int waited = 0;
81 for() {
82 ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
83 if(ret < 0) {
[c3ee5f3]84 if(errno == EADDRINUSE) {
[ee913e0a]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;
[0aec496]98 }
99
[2ecbd7b]100 ret = listen( server_fd, options.socket.backlog );
[0aec496]101 if(ret < 0) {
102 abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
103 }
104
105 //===================
106 // Run Server Cluster
107 {
[d11d6eb]108 cluster cl = { "Server Cluster", options.clopts.params };
[0aec496]109 #if !defined(__CFA_NO_STATISTICS__)
110 print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
111 #endif
[2ecbd7b]112 options.clopts.instance = &cl;
[0aec496]113
[c3ee5f3]114
[d9c2284]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
[d11d6eb]124 if(options.file_cache.fixed_fds) {
125 register_fixed_files(cl, fds, pipe_off);
126 }
127
[0aec496]128 {
[2ecbd7b]129 ServerProc procs[options.clopts.nprocs];
[ece0e80]130
131 init_protocol();
[0aec496]132 {
[2ecbd7b]133 Worker workers[options.clopts.nworkers];
[d9c2284]134 for(i; options.clopts.nworkers) {
[d11d6eb]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 {
[d9c2284]141 workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
142 workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
[8e3034d]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;
[d9c2284]147 }
[e235429]148 unpark( workers[i] );
[d9c2284]149 }
[2ecbd7b]150 printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs);
[0aec496]151 {
[e95a117]152 char buffer[128];
153 while(!feof(stdin)) {
154 fgets(buffer, 128, stdin);
155 }
156
157 printf("Shutting Down\n");
[0aec496]158 }
[ece0e80]159
160 for(i; options.clopts.nworkers) {
161 printf("Cancelling %p\n", (void*)workers[i].cancel.target);
[481ee28]162 workers[i].done = true;
[ece0e80]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 }
[0aec496]177 }
[e95a117]178 printf("Workers Closed\n");
[ece0e80]179
180 deinit_protocol();
[0aec496]181 }
[d9c2284]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);
[c3ee5f3]190
[0aec496]191 }
192
193 //===================
194 // Close Files
195 printf("Closing Files\n");
196 close_cache();
197}
Note: See TracBrowser for help on using the repository browser.