david.git  about / heads / tags
slow client simulator to bring prefork/threading servers to their knees
blob bda462416e814b8fd8fbd8d74de2a170dec95090 7492 bytes (raw)
$ git show HEAD:david.c	# shows this blob on the CLI

  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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
 
/*
 * This file is licensed under the GPL v2, or a later version
 * at the discretion of Eric Wong <normalperson@yhbt.net>.
 */

/*
 * The real number of connections is MAX_CHILDREN * NR_CONN, we fork
 * children to get around file descriptor limits and to take advantage
 * of multiple processors/cores
 *
 * NR_CONN needs to be <= (FD_SETSIZE - 4)
 */
static int MAX_CHILDREN = 1;
static int NR_CONN = 1000;

/*
 * the delay between subsequent send/recv() calls to each socket,
 * assuming the socket is write/readable
 */
static int DELAY_SECONDS = 1;

/* the maximum number of bytes we write at once */
static int WR_BYTES = 8;

/* the maximum number of bytes we read at once */
static int RD_BYTES = 8;

/*
 * set this to 1 to enable printing of requests and responses to stdout
 */
static int DEBUG = 0;

/* -------- end of configuration section --------- */


#include <stdio.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <netdb.h>
#include <time.h>
#include <errno.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <stdarg.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>

static char *http_request;
static size_t http_request_size;
static struct addrinfo *ap;
static int max_fd;
#define CONNECT_OFFSET 0x7fffffff

#ifdef TCP_NODELAY
static int can_tcp_nodelay = 1;
#else
static int can_tcp_nodelay;
#define TCP_NODELAY 0 /* just to get it to compile */
#endif

struct interface {
	int fd;

	/* offset:
	 *  CONNECT_OFFSET indicates connect()
	 *  < 0 indicates writing
	 *  >= 0 indicates reading
	 */
	off_t offset;

	time_t last;
};

static struct interface *interfaces;
static pid_t *children_pids;

static void die(const char *fmt, ...)
{
	va_list args;
	va_start(args, fmt);
	fprintf(stderr, fmt, args);
	va_end(args);
	fputc('\n', stderr);
	exit(EXIT_FAILURE);
}

static void mmap_request(const char *path)
{
	struct stat sb;
	int fd = open(path, O_RDONLY);

	if (fd < 0 || fstat(fd, &sb) < 0)
		die("Couldn't open or stat: %s", strerror(errno));

	http_request_size = sb.st_size;
	http_request = mmap(NULL, http_request_size,
	                    PROT_READ, MAP_PRIVATE, fd, 0);
}

static void setup_addrinfo(void)
{
	struct addrinfo *ans;
	struct addrinfo hints;
	char *http_host = getenv("HTTP_HOST");
	char *http_port = getenv("HTTP_PORT");

	if (!http_host)
		die("HTTP_HOST must be defined in the environment");
	if (!http_port)
		http_port = "80";

	hints.ai_flags = 0;
	hints.ai_family = PF_UNSPEC;
	hints.ai_socktype = SOCK_STREAM;
	hints.ai_protocol = IPPROTO_TCP;
	hints.ai_addrlen = 0;
	hints.ai_addr = NULL;
	hints.ai_canonname = NULL;
	hints.ai_next = NULL;

	if (getaddrinfo(http_host, http_port, &hints, &ans) != 0)
		die("Couldn't get address");

	for (ap = ans; ap != NULL; ap = ap->ai_next) {
		int fd;
		fd = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
		if (fd < 0)
			die("Couldn't connect: %s", strerror(errno));
		if (connect(fd, ap->ai_addr, ap->ai_addrlen) >= 0
		    || errno == EINPROGRESS) {
			close(fd);
			break;
		}
		close(fd);
	}
}

static void init_interface(const int i)
{
	int fd;
	socklen_t optval;
	fd = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol);
	if (fd > max_fd)
		max_fd = fd;
	if (fd < 0) {
		fprintf(stderr,
			"Couldn't get socket: %s", strerror(errno));
		return;
	}
	if (fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK) < 0)
		die("Couldn't set nonblocking: %s", strerror(errno));
	if (connect(fd, ap->ai_addr, ap->ai_addrlen) < 0) {
		if (errno != EINPROGRESS)
			die("connect error: %s", strerror(errno));
	}

	/* we want buffers to be as small as possible,
	 * and disable Nagle algorithm buffering, too. */
	optval = 1;
	if (setsockopt(fd, SOL_SOCKET, SO_SNDBUF,
		       &optval, sizeof(optval)) < 0)
		die("[%d] Couldn't set SNDBUF: %s", i, strerror(errno));
	optval = 1;
	if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF,
		       &optval, sizeof(optval)) < 0)
		die("[%d] Couldn't set RCVBUF: %s", i, strerror(errno));
	if (can_tcp_nodelay) {
		optval = 1;
		if (setsockopt(fd, IPPROTO_TCP, TCP_NODELAY,
			       &optval, sizeof(optval)) < 0) {
			can_tcp_nodelay = 0;
			fprintf(stderr, "Couldn't set TCP_NODELAY: %s\n",
			        strerror(errno));
		}
	}
	interfaces[i].fd = fd;
	interfaces[i].offset = CONNECT_OFFSET;
	/* fprintf(stderr, "[%d] ready fd: %d\n", i, fd); */
}

static void open_interfaces(void)
{
	int i;

	interfaces = malloc(sizeof(struct interface) * NR_CONN);
	for (i = 0; i < NR_CONN; ++i)
		init_interface(i);
}

static void do_io(void)
{
	struct timeval tv = { 1, 0 };
	time_t now;
	fd_set wfds;
	fd_set rfds;
	FD_ZERO(&wfds);
	FD_ZERO(&rfds);
	char buf[RD_BYTES];
	ssize_t nr;
	int i;

	now = time(NULL);
	for (i = 0; i < NR_CONN; ++i) {
		if (interfaces[i].fd < 0)
			continue;
		if (DELAY_SECONDS &&
		    (now - interfaces[i].last) <= DELAY_SECONDS)
			continue;
		if (interfaces[i].offset < 0 ||
		    interfaces[i].offset == CONNECT_OFFSET) {
			FD_SET(interfaces[i].fd, &wfds);
		} else {
			FD_SET(interfaces[i].fd, &rfds);
		}
	}

	i = select(max_fd + 1, &rfds, &wfds, NULL, &tv);
	if (i < 0)
		die("select returned %i: %s %d", i, strerror(errno), NR_CONN);

	for (i = 0; i < NR_CONN; ++i) {
		if (interfaces[i].fd < 0)
			continue;
		if (FD_ISSET(interfaces[i].fd, &wfds)) {
			ssize_t wr_bytes;
			if (interfaces[i].offset == CONNECT_OFFSET) {
				int err;
				size_t err_len = sizeof(err);
				getsockopt(interfaces[i].fd, SOL_SOCKET,
				           SO_ERROR, &err, &err_len);
				if (err) {
					close(interfaces[i].fd);
					init_interface(i);
					continue;
				}
				interfaces[i].offset = -http_request_size;
			}
			/* if offset == -2 and WR_BYTES == 8,
			 * only write 2 bytes here: */
			if (interfaces[i].offset > -WR_BYTES)
				wr_bytes = -interfaces[i].offset;
			else
				wr_bytes = WR_BYTES;

			nr = send(interfaces[i].fd,
			          http_request +
			            http_request_size + interfaces[i].offset,
			          wr_bytes, MSG_DONTWAIT);
			if (DEBUG && nr > 0) {
				printf("W%d:", i);
				fwrite(http_request +
				         http_request_size +
				         interfaces[i].offset,
				       1, wr_bytes, stdout);
				puts("");
			}
		} else if (FD_ISSET(interfaces[i].fd, &rfds)) {
			nr = recv(interfaces[i].fd, buf,
			          RD_BYTES, MSG_DONTWAIT);
			if (DEBUG && nr > 0) {
				printf("R%d:", i);
				fwrite(buf, 1, nr, stdout);
				puts("");
			}
		} else
			continue;

		if (nr > 0) {
			now = interfaces[i].last = time(NULL);
			interfaces[i].offset += nr;
		} else {
			switch (errno) {
			case EAGAIN:
			case EINTR:
			case EINPROGRESS:
				break;
			default:
				fprintf(stderr, "[%d] closing for error: %s\n",
					i, strerror(errno));
				close(interfaces[i].fd);
				init_interface(i);
			}
		}
	}
}

static void kill_children(void)
{
	int i;
	for (i = 0; i < MAX_CHILDREN; ++i)
		kill(children_pids[i], SIGTERM);
}

int main(int argc, const char *argv[])
{
	int i;

	if (argc < 2)
		return -1;

	mmap_request(argv[1]);
	setup_addrinfo();
	children_pids = malloc(sizeof(pid_t) * MAX_CHILDREN);

	for (i = 0; i < MAX_CHILDREN; ++i) {
		fflush(NULL);
		pid_t pid = fork();
		if (!pid) {
			open_interfaces();
			while (1)
				do_io();
			return 0;
		}
		if (pid < 0)
			die("Couldn't fork: %s", strerror(errno));
		children_pids[i] = pid;
	}
	atexit(kill_children);

	{
		int too_lazy_to_check;
		for (i = 0; i < MAX_CHILDREN; ++i)
			wait(&too_lazy_to_check);
	}

	return 0;
}

git clone https://yhbt.net/david.git