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

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

Protocol now returns date in answer header

  • Property mode set to 100644
File size: 4.5 KB
Line 
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>
14#include <time.hfa>
15#include <thread.hfa>
16
17#include "filecache.hfa"
18#include "options.hfa"
19#include "worker.hfa"
20
21extern void register_fixed_files( cluster &, int *, unsigned count );
22
23Duration default_preemption() {
24 return 0;
25}
26
27//=============================================================================================
28// Globals
29//=============================================================================================
30struct ServerProc {
31 processor self;
32};
33
34void ?{}( 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
48extern void init_protocol(void);
49extern void deinit_protocol(void);
50
51//=============================================================================================
52// Main
53//============================================================================================='
54int main( int argc, char * argv[] ) {
55 //===================
56 // Parse args
57 const char * path = parse_options(argc, argv);
58
59 //===================
60 // Open Files
61 if (FileExperiment == options.experiment.type) {
62 printf("Filling cache from %s\n", path);
63 fill_cache( path );
64 }
65
66 //===================
67 // Open Socket
68 printf("Listening on port %d\n", options.socket.port);
69 int server_fd = socket(AF_INET, SOCK_STREAM, 0);
70 if(server_fd < 0) {
71 abort( "socket error: (%d) %s\n", (int)errno, strerror(errno) );
72 }
73
74 int ret = 0;
75 struct sockaddr_in address;
76 int addrlen = sizeof(address);
77 memset( (char *)&address, '\0' );
78 address.sin_family = AF_INET;
79 address.sin_addr.s_addr = htonl(INADDR_ANY);
80 address.sin_port = htons( options.socket.port );
81
82 int waited = 0;
83 for() {
84 ret = bind( server_fd, (struct sockaddr *)&address, sizeof(address) );
85 if(ret < 0) {
86 if(errno == EADDRINUSE) {
87 if(waited == 0) {
88 printf("Waiting for port\n");
89 } else {
90 printf("\r%d", waited);
91 fflush(stdout);
92 }
93 waited ++;
94 sleep( 1`s );
95 continue;
96 }
97 abort( "bind error: (%d) %s\n", (int)errno, strerror(errno) );
98 }
99 break;
100 }
101
102 ret = listen( server_fd, options.socket.backlog );
103 if(ret < 0) {
104 abort( "listen error: (%d) %s\n", (int)errno, strerror(errno) );
105 }
106
107 //===================
108 // Run Server Cluster
109 {
110 cluster cl = { "Server Cluster", options.clopts.params };
111 #if !defined(__CFA_NO_STATISTICS__)
112 print_stats_at_exit( cl, CFA_STATS_READY_Q | CFA_STATS_IO );
113 #endif
114 options.clopts.instance = &cl;
115
116 init_protocol();
117
118 int pipe_cnt = options.clopts.nworkers * 2;
119 int pipe_off;
120 int * fds;
121 [fds, pipe_off] = filefds( pipe_cnt );
122 for(i; 0 ~ pipe_cnt ~ 2) {
123 int ret = pipe(&fds[pipe_off + i]);
124 if( ret < 0 ) { abort( "pipe error: (%d) %s\n", (int)errno, strerror(errno) ); }
125 }
126
127 if(options.file_cache.fixed_fds) {
128 register_fixed_files(cl, fds, pipe_off);
129 }
130
131 {
132 ServerProc procs[options.clopts.nprocs];
133 {
134 Worker workers[options.clopts.nworkers];
135 for(i; options.clopts.nworkers) {
136 // if( options.file_cache.fixed_fds ) {
137 // workers[i].pipe[0] = pipe_off + (i * 2) + 0;
138 // workers[i].pipe[1] = pipe_off + (i * 2) + 1;
139 // }
140 // else
141 {
142 workers[i].pipe[0] = fds[pipe_off + (i * 2) + 0];
143 workers[i].pipe[1] = fds[pipe_off + (i * 2) + 1];
144 workers[i].sockfd = server_fd;
145 workers[i].addr = (struct sockaddr *)&address;
146 workers[i].addrlen = (socklen_t*)&addrlen;
147 workers[i].flags = 0;
148 }
149 unpark( workers[i] );
150 }
151 printf("%d workers started on %d processors\n", options.clopts.nworkers, options.clopts.nprocs);
152 {
153 char buffer[128];
154 while(!feof(stdin)) {
155 fgets(buffer, 128, stdin);
156 }
157
158 printf("Shutting Down\n");
159 }
160 }
161 printf("Workers Closed\n");
162 }
163
164 for(i; pipe_cnt) {
165 ret = close( fds[pipe_off + i] );
166 if(ret < 0) {
167 abort( "close pipe error: (%d) %s\n", (int)errno, strerror(errno) );
168 }
169 }
170 free(fds);
171
172 deinit_protocol();
173 }
174
175 //===================
176 // Close Socket
177 printf("Closing Socket\n");
178 ret = close( server_fd );
179 if(ret < 0) {
180 abort( "close socket error: (%d) %s\n", (int)errno, strerror(errno) );
181 }
182
183 //===================
184 // Close Files
185 printf("Closing Files\n");
186 close_cache();
187}
Note: See TracBrowser for help on using the repository browser.