-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkqueueserver2.c
275 lines (249 loc) · 6.53 KB
/
kqueueserver2.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
// (1) First make sure you update the EXPECTED_HTTP_REQUEST variable as instructed below.
// (2) Set NUM_WORKERS to the number of cores you have (or maybe more if you want?)
// (3) Set NUM_CLIENTS to be the argument given to -c of weighttp.
// (4) Compile with: gcc -O2 kqueueserver2.c -lpthread -Wall -o kqueueserver2
// (5) To run: ./kqueueserver2
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/tcp.h>
#include <stdint.h>
#include <sys/event.h>
#include <sys/time.h>
// prototypes
void acceptLoop(void);
void startWorkers(void);
void startWorkerThread(int);
void *workerLoop(void *);
void receiveLoop(int, int, int, char []);
void setNonBlocking(int);
void socketCheck(void);
// constants
#define NUM_WORKERS 2
#define PORT_NUM (8080)
#define BACKLOG 600
#define MAX_EVENTS 500
#define NUM_CLIENTS 10 // 500 // comes from the -c argument of weighttp
// Fill this in with the http request that your
// weighttp client sends to the server. This is the
// request that I get. You can also find out what weighttp
// is sending by first setting SHOW_REQUEST (see below) and leaving
// this as is. Then when you run the program it will print the request
// it received.
char EXPECTED_HTTP_REQUEST[] =
"GET / HTTP/1.1\r\nHost: 10.12.0.1:8080\r\n"
"User-Agent: weighttp/0.3\r\nConnection: keep-alive\r\n\r\n";
// Define this and the program will print the request made
// by the http client and then exit.
// #define SHOW_REQUEST
int EXPECTED_RECV_LEN;
char RESPONSE[] =
"HTTP/1.1 200 OK\r\n"
"Date: Tue, 09 Oct 2012 16:36:18 GMT\r\n"
"Content-Length: 145\r\n"
"Server: Fake\r\n"
"Last-Modified: Mon, 09 Jul 2012 03:42:33 GMT\r\n"
"Content-Type: text/html\r\n\r\n"
"<html>\n<head>\n<title>Welcome to foo</title>\n</head>\n"
"<body bgcolor=\"white\" text=\"black\">\n"
"<center><h1>Welcome to foo</h1></center>\n</body>\n</html>\n";
size_t RESPONSE_LEN;
// global variables
int sockets[NUM_CLIENTS];
int socketAssignments[NUM_CLIENTS];
int socketRequestCounts[NUM_CLIENTS];
int main(void) {
EXPECTED_RECV_LEN = strlen(EXPECTED_HTTP_REQUEST);
RESPONSE_LEN = strlen(RESPONSE);
acceptLoop();
startWorkers();
socketCheck();
return 0;
}
void startWorkers(void) {
int i;
for (i=0; i < NUM_WORKERS; i++) {
startWorkerThread(i);
}
}
void startWorkerThread(int w) {
pthread_t thread;
if (pthread_create(&thread, NULL, workerLoop, (void *)(unsigned long) w)) {
perror("pthread_create");
exit(-1);
}
return;
}
void *workerLoop(void * arg) {
int w = (int)(unsigned long) arg;
int epfd;
int n;
int i;
int sock;
struct kevent64_s *events;
char recvbuf[1000];
int j;
events = calloc (MAX_EVENTS, sizeof (struct kevent64_s));
if (-1==(epfd = kqueue())) {
perror("worker kqueue");
exit(-1);
}
for (j=0; j<NUM_CLIENTS; j++) {
if (socketAssignments[j] == w) {
struct kevent64_s event;
EV_SET64(&event, sockets[j], EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0, 0, 0);
if (kevent64(epfd, &event, 1, NULL, 0, 0, NULL)!=0) {
perror("initial registration");
}
}
}
while(1) {
n = kevent64(epfd,NULL,0,events,MAX_EVENTS,0,NULL);
for (i=0; i < n; i++) {
sock = events[i].ident;
#ifdef SHOW_REQUEST
int m;
m = recv(sock, recvbuf, 200, 0);
recvbuf[m]='\0';
printf("http request: %s\n", recvbuf);
exit(0);
#endif
receiveLoop(sock, epfd, w, recvbuf);
}
}
pthread_exit(NULL);
}
void
incSocketRequestCount(int sock) {
int i;
for (i=0; i < NUM_CLIENTS; i++) {
if (sockets[i]==sock) {
socketRequestCounts[i]++;
}
}
}
void receiveLoop(int sock, int epfd, int w, char recvbuf[]) {
ssize_t m;
int numSent;
struct kevent64_s event;
while(1) {
m = recv(sock, recvbuf, EXPECTED_RECV_LEN, 0);
if (m==0) break;
if (m > 0) {
if (m == EXPECTED_RECV_LEN) {
incSocketRequestCount(sock);
numSent = send(sock, RESPONSE, RESPONSE_LEN, 0);
if (numSent == -1) {
perror("send failed");
exit(-1);
}
if (numSent != RESPONSE_LEN) {
perror("partial send");
exit(-1);
}
} else {
perror("partial recv");
exit(-1);
}
} else {
if (m==-1) {
if (errno==EAGAIN) {
// re-arm the socket with epoll.
EV_SET64(&event, sock, EVFILT_READ, EV_ADD | EV_ONESHOT, 0, 0, 0, 0, 0);
if (kevent64(epfd, &event, 1, NULL, 0, 0, NULL)!=0) {
perror("rearm");
exit(-1);
}
// printf("rearmed %d on %d\n", sock, w);
break;
} else {
perror("recv");
exit(-1);
}
} else {
perror("unexpected");
exit(-1);
}
}
}
}
void socketCheck() {
int i, bytesAvailable;
sleep(10);
for (i = 0; i < NUM_CLIENTS; i++) {
if (ioctl(sockets[i], FIONREAD, &bytesAvailable) < 0) {
perror("ioctl");
exit(-1);
}
if (bytesAvailable > 0) {
printf("socket %d with index %d assigned to worker %d has %d bytes of data ready and completed %d requests\n",
sockets[i],
i,
socketAssignments[i],
bytesAvailable,
socketRequestCounts[i]);
}
}
}
void acceptLoop(void)
{
int sd;
struct sockaddr_in addr;
socklen_t alen = sizeof(addr);
short port = PORT_NUM;
int sock_tmp;
int current_worker = 0;
int current_client = 0;
int optval;
int j;
if (-1 == (sd = socket(PF_INET, SOCK_STREAM, 0))) {
printf("socket: error: %d\n",errno);
exit(-1);
}
addr.sin_family = AF_INET;
addr.sin_addr.s_addr = INADDR_ANY;
addr.sin_port = htons(port);
optval = 1;
setsockopt(sd, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof optval);
if (bind(sd, (struct sockaddr*)&addr, sizeof(addr))) {
printf("bind error: %d\n",errno);
exit(-1);
}
if (listen(sd, BACKLOG)) {
printf("listen error: %d\n",errno);
exit(-1);
}
for (j=0; j<NUM_CLIENTS; j++) {
if (-1 == (sock_tmp = accept(sd, (struct sockaddr*)&addr, &alen))) {
printf("Error %d doing accept", errno);
perror("accept");
exit(-1);
}
sockets[current_client] = sock_tmp;
setNonBlocking(sock_tmp);
socketAssignments[current_client] = current_worker;
printf("current_client: %d\n", current_client);
current_client++;
current_worker = (current_worker + 1) % NUM_WORKERS;
}
}
void setNonBlocking(int fd) {
int flags;
if (-1 == (flags = fcntl(fd, F_GETFL, 0))) {
perror("Getting NONBLOCKING failed.\n");
exit(-1);
}
if ( fcntl(fd, F_SETFL, flags | O_NONBLOCK ) < 0 ) {
perror("Setting NONBLOCKING failed.\n");
exit(-1);
}
return;
}