source: benchmark/io/http/main.cfa@ 4b1c8da

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 4b1c8da was 0197418, checked in by Thierry Delisle <tdelisle@…>, 5 years ago

Fixed missing print

  • Property mode set to 100644
File size: 6.2 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
[8c43d05]12#include <fstream.hfa>
[0aec496]13#include <kernel.hfa>
[153dc387]14#include <iofwd.hfa>
[0aec496]15#include <stats.hfa>
[d11d6eb]16#include <time.hfa>
[0aec496]17#include <thread.hfa>
18
19#include "filecache.hfa"
20#include "options.hfa"
21#include "worker.hfa"
22
[d11d6eb]23extern void register_fixed_files( cluster &, int *, unsigned count );
24
25Duration default_preemption() {
26 return 0;
27}
28
[0aec496]29//=============================================================================================
30// Globals
31//=============================================================================================
32struct ServerProc {
33 processor self;
34};
35
36void ?{}( ServerProc & this ) {
[2ecbd7b]37 /* paranoid */ assert( options.clopts.instance != 0p );
38 (this.self){ "Benchmark Processor", *options.clopts.instance };
[0aec496]39
40 #if !defined(__CFA_NO_STATISTICS__)
[2ecbd7b]41 if( options.clopts.procstats ) {
42 print_stats_at_exit( this.self, options.clopts.instance->print_stats );
[0aec496]43 }
[2ecbd7b]44 if( options.clopts.viewhalts ) {
[0aec496]45 print_halts( this.self );
46 }
47 #endif
48}
49
[c3ee5f3]50extern void init_protocol(void);
51extern void deinit_protocol(void);
52
[153dc387]53//=============================================================================================
54// Stats Printer
55//============================================================================================='
56
57thread StatsPrinter {};
58
59void ?{}( StatsPrinter & this ) {
60 ((thread&)this){ "Stats Printer Thread" };
61}
62
63void main(StatsPrinter & this) {
64 LOOP: for() {
65 waitfor( ^?{} : this) {
66 break LOOP;
67 }
68 or else {}
69
70 sleep(10`s);
71
72 print_stats_now( *options.clopts.instance, CFA_STATS_READY_Q | CFA_STATS_IO );
73 }
74}
75
[0aec496]76//=============================================================================================
77// Main
78//============================================================================================='
79int main( int argc, char * argv[] ) {
80 //===================
81 // Parse args
[b57db73]82 parse_options(argc, argv);
[0aec496]83
84 //===================
85 // Open Files
[b57db73]86 if( options.file_cache.path ) {
87 sout | "Filling cache from" | options.file_cache.path;
88 fill_cache( options.file_cache.path );
89 }
[0aec496]90
91 //===================
92 // Open Socket
[8c43d05]93 sout | getpid() | ": Listening on port" | options.socket.port;
[0aec496]94 int server_fd = socket(AF_INET, SOCK_STREAM, 0);
95 if(server_fd < 0) {
96 abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) );
97 }
98
[7f389a5c]99 int ret = 0;
[0aec496]100 struct sockaddr_in address;
101 int addrlen = sizeof(address);
102 memset( (char *)&address, '\0' );
103 address.sin_family = AF_INET;
104 address.sin_addr.s_addr = htonl(INADDR_ANY);
[2ecbd7b]105 address.sin_port = htons( options.socket.port );
[0aec496]106
[ee913e0a]107 int waited = 0;
108 for() {
109 ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
110 if(ret < 0) {
[c3ee5f3]111 if(errno == EADDRINUSE) {
[ee913e0a]112 if(waited == 0) {
[8c43d05]113 sout | "Waiting for port";
[ee913e0a]114 } else {
[8c43d05]115 sout | "\r" | waited | nonl;
116 flush( sout );
[ee913e0a]117 }
118 waited ++;
119 sleep( 1`s );
120 continue;
121 }
122 abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
123 }
124 break;
[0aec496]125 }
126
[2ecbd7b]127 ret = listen( server_fd, options.socket.backlog );
[0aec496]128 if(ret < 0) {
129 abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
130 }
131
132 //===================
133 // Run Server Cluster
134 {
[d11d6eb]135 cluster cl = { "Server Cluster", options.clopts.params };
[0aec496]136 #if !defined(__CFA_NO_STATISTICS__)
137 print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
138 #endif
[2ecbd7b]139 options.clopts.instance = &cl;
[0aec496]140
[c3ee5f3]141
[d9c2284]142 int pipe_cnt = options.clopts.nworkers * 2;
143 int pipe_off;
144 int * fds;
145 [fds, pipe_off] = filefds( pipe_cnt );
146 for(i; 0 ~ pipe_cnt ~ 2) {
147 int ret = pipe(&fds[pipe_off + i]);
148 if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
149 }
150
[b57db73]151 if(options.file_cache.path && options.file_cache.fixed_fds) {
[d11d6eb]152 register_fixed_files(cl, fds, pipe_off);
153 }
154
[0aec496]155 {
[2ecbd7b]156 ServerProc procs[options.clopts.nprocs];
[153dc387]157 StatsPrinter printer;
[ece0e80]158
159 init_protocol();
[0aec496]160 {
[2ecbd7b]161 Worker workers[options.clopts.nworkers];
[d9c2284]162 for(i; options.clopts.nworkers) {
[d11d6eb]163 // if( options.file_cache.fixed_fds ) {
164 // workers[i].pipe[0] = pipe_off + (i * 2) + 0;
165 // workers[i].pipe[1] = pipe_off + (i * 2) + 1;
166 // }
167 // else
168 {
[d9c2284]169 workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
170 workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
[8e3034d]171 workers[i].sockfd = server_fd;
172 workers[i].addr = (struct sockaddr *)&address;
173 workers[i].addrlen = (socklen_t*)&addrlen;
174 workers[i].flags = 0;
[d9c2284]175 }
[e235429]176 unpark( workers[i] );
[d9c2284]177 }
[8c43d05]178 sout | options.clopts.nworkers | "workers started on" | options.clopts.nprocs | "processors";
[0aec496]179 {
[e95a117]180 char buffer[128];
[7223dbf2]181 while(int ret = cfa_read(0, buffer, 128, 0, -1`s, 0p, 0p); ret != 0) {
[153dc387]182 if(ret < 0) abort( "main read error: (%d) %s\n", (int)errno, strerror(errno) );
[e95a117]183 }
184
[8c43d05]185 sout | "Shutdown received";
[0aec496]186 }
[ece0e80]187
[b57db73]188 sout | "Notifying connections..." | nonl; flush( sout );
[ece0e80]189 for(i; options.clopts.nworkers) {
[481ee28]190 workers[i].done = true;
[ece0e80]191 cancel(workers[i].cancel);
192 }
[b57db73]193 sout | "done";
[ece0e80]194
[b57db73]195 sout | "Shutting down socket..." | nonl; flush( sout );
[ece0e80]196 int ret = shutdown( server_fd, SHUT_RD );
[b57db73]197 if( ret < 0 ) {
198 abort( "shutdown error: (%d) %s\n", (int)errno, strerror(errno) );
199 }
200 sout | "done";
[ece0e80]201
202 //===================
203 // Close Socket
[b57db73]204 sout | "Closing Socket..." | nonl; flush( sout );
[ece0e80]205 ret = close( server_fd );
206 if(ret < 0) {
207 abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
208 }
[0197418]209 sout | "done";
210
[b57db73]211 sout | "Stopping connection threads..." | nonl; flush( sout );
[0aec496]212 }
[8c43d05]213 sout | "done";
[ece0e80]214
[b57db73]215 sout | "Stopping protocol threads..." | nonl; flush( sout );
[ece0e80]216 deinit_protocol();
[8c43d05]217 sout | "done";
218
[b57db73]219 sout | "Stopping processors..." | nonl; flush( sout );
[0aec496]220 }
[8c43d05]221 sout | "done";
[d9c2284]222
[b57db73]223 sout | "Closing splice fds..." | nonl; flush( sout );
[d9c2284]224 for(i; pipe_cnt) {
225 ret = close( fds[pipe_off + i] );
226 if(ret < 0) {
227 abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) );
228 }
229 }
230 free(fds);
[8c43d05]231 sout | "done";
[c3ee5f3]232
[b57db73]233 sout | "Stopping processors..." | nonl; flush( sout );
[0aec496]234 }
[8c43d05]235 sout | "done";
[0aec496]236
237 //===================
238 // Close Files
[b57db73]239 if( options.file_cache.path ) {
240 sout | "Closing open files..." | nonl; flush( sout );
241 close_cache();
242 sout | "done";
243 }
[0aec496]244}
Note: See TracBrowser for help on using the repository browser.