forked from ColinIanKing/stress-ng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stress-epoll.c
576 lines (517 loc) · 13 KB
/
stress-epoll.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
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
/*
* Copyright (C) 2013-2017 Canonical, Ltd.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* This code is a complete clean re-write of the stress tool by
* Colin Ian King <[email protected]> and attempts to be
* backwardly compatible with the stress tool by Amos Waterland
* <[email protected]> but has more stress tests and more
* functionality.
*
*/
#include "stress-ng.h"
#define MAX_EPOLL_EVENTS (1024)
#define MAX_SERVERS (4)
#if defined(HAVE_LIB_RT) && defined(__linux__) && NEED_GLIBC(2,3,2)
#include <sys/epoll.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#if defined(AF_INET6)
#include <netinet/in.h>
#endif
#if defined(AF_UNIX)
#include <sys/un.h>
#endif
#include <netdb.h>
typedef void (epoll_func_t)(
const args_t *args,
const int child,
const pid_t ppid,
const int epoll_port,
const int epoll_domain);
static timer_t epoll_timerid;
#endif
static int max_servers = 1;
/*
* stress_set_epoll_port()
* set the default port base
*/
void stress_set_epoll_port(const char *opt)
{
int epoll_port;
stress_set_net_port("epoll-port", opt,
MIN_EPOLL_PORT,
MAX_EPOLL_PORT - (STRESS_PROCS_MAX * MAX_SERVERS),
&epoll_port);
set_setting("epoll-port", TYPE_ID_INT, &epoll_port);
}
/*
* stress_set_epoll_domain()
* set the socket domain option
*/
int stress_set_epoll_domain(const char *name)
{
int ret, epoll_domain;
ret = stress_set_net_domain(DOMAIN_ALL, "epoll-domain",
name, &epoll_domain);
set_setting("epoll-domain", TYPE_ID_INT, &epoll_domain);
switch (epoll_domain) {
case AF_INET:
case AF_INET6:
max_servers = 4;
break;
case AF_UNIX:
default:
max_servers = 1;
}
return ret;
}
#if defined(HAVE_LIB_RT) && defined(__linux__) && NEED_GLIBC(2,3,2)
/*
* epoll_timer_handler()
* catch timer signal and cancel if no more runs flagged
*/
static void MLOCKED epoll_timer_handler(int sig)
{
(void)sig;
/* Cancel timer if we detect no more runs */
if (!g_keep_stressing_flag) {
struct itimerspec timer;
timer.it_value.tv_sec = 0;
timer.it_value.tv_nsec = 0;
timer.it_interval.tv_sec = timer.it_value.tv_sec;
timer.it_interval.tv_nsec = timer.it_value.tv_nsec;
(void)timer_settime(epoll_timerid, 0, &timer, NULL);
}
}
/*
* epoll_spawn()
* spawn a process
*/
static pid_t epoll_spawn(
const args_t *args,
epoll_func_t func,
const int child,
const pid_t ppid,
const int epoll_port,
const int epoll_domain)
{
pid_t pid;
again:
pid = fork();
if (pid < 0) {
if (g_keep_stressing_flag && (errno == EAGAIN))
goto again;
return -1;
}
if (pid == 0) {
(void)setpgid(0, g_pgrp);
stress_parent_died_alarm();
func(args, child, ppid, epoll_port, epoll_domain);
exit(EXIT_SUCCESS);
}
(void)setpgid(pid, g_pgrp);
return pid;
}
/*
* epoll_set_fd_nonblock()
* set non-blocking mode on fd
*/
static int epoll_set_fd_nonblock(const int fd)
{
int flags;
if ((flags = fcntl(fd, F_GETFL, 0)) < 0)
return -1;
if (fcntl(fd, F_SETFL, flags | O_NONBLOCK) < 0)
return -1;
return 0;
}
/*
* epoll_recv_data()
* receive data on fd
*/
static void epoll_recv_data(const int fd)
{
while (g_keep_stressing_flag) {
char buf[8192];
ssize_t n;
n = recv(fd, buf, sizeof(buf), 0);
if (n == -1) {
if (errno != EAGAIN)
(void)close(fd);
break;
} else if (n == 0) {
(void)close(fd);
break;
}
}
}
/*
* epoll_ctl_add()
* add fd to epoll list
*/
static int epoll_ctl_add(const int efd, const int fd)
{
struct epoll_event event;
(void)memset(&event, 0, sizeof(event));
event.data.fd = fd;
event.events = EPOLLIN | EPOLLET;
if (epoll_ctl(efd, EPOLL_CTL_ADD, fd, &event) < 0)
return -1;
return 0;
}
/*
* epoll_notification()
* handle accept notification on sfd, add
* fd's to epoll event list
*/
static int epoll_notification(
const args_t *args,
const int efd,
const int sfd)
{
for (;;) {
struct sockaddr saddr;
socklen_t slen = sizeof(saddr);
int fd;
if ((fd = accept(sfd, &saddr, &slen)) < 0) {
if ((errno == EAGAIN) || (errno == EWOULDBLOCK)) {
/* all incoming connections handled so finish */
return 0;
}
if ((errno == EMFILE) || (errno == ENFILE)) {
/* out of file descriptors! */
return 0;
}
pr_fail_err("accept");
return -1;
}
/*
* Add non-blocking fd to epoll event list
*/
if (epoll_set_fd_nonblock(fd) < 0) {
pr_fail_err("setting socket to non-blocking");
(void)close(fd);
return -1;
}
if (epoll_ctl_add(efd, fd) < 0) {
pr_fail_err("epoll ctl add");
(void)close(fd);
return -1;
}
}
return 0;
}
/*
* epoll_client()
* rapidly try to connect to server(s) and
* send a relatively short message
*/
static int epoll_client(
const args_t *args,
const pid_t ppid,
const int epoll_port,
const int epoll_domain)
{
int port_counter = 0;
uint64_t connect_timeouts = 0;
struct sigevent sev;
struct itimerspec timer;
struct sockaddr *addr = NULL;
if (stress_sighandler(args->name, SIGRTMIN, epoll_timer_handler, NULL) < 0)
return -1;
do {
char buf[4096];
int fd, saved_errno;
int retries = 0;
int ret = -1;
int port = epoll_port + port_counter +
(max_servers * args->instance);
socklen_t addr_len = 0;
/* Cycle through the servers */
port_counter = (port_counter + 1) % max_servers;
retry:
if (!g_keep_stressing_flag)
break;
if ((fd = socket(epoll_domain, SOCK_STREAM, 0)) < 0) {
pr_fail_dbg("socket");
return -1;
}
sev.sigev_notify = SIGEV_SIGNAL;
sev.sigev_signo = SIGRTMIN;
sev.sigev_value.sival_ptr = &epoll_timerid;
if (timer_create(CLOCK_REALTIME, &sev, &epoll_timerid) < 0) {
pr_fail_err("timer_create");
(void)close(fd);
return -1;
}
/*
* Allow 1 second for connection to occur,
* connect can block if the connection table
* fills up because we're waiting for TIME-OUTs
* to occur on previously closed connections
*/
timer.it_value.tv_sec = 0;
timer.it_value.tv_nsec = 250000000;
timer.it_interval.tv_sec = timer.it_value.tv_sec;
timer.it_interval.tv_nsec = timer.it_value.tv_nsec;
if (timer_settime(epoll_timerid, 0, &timer, NULL) < 0) {
pr_fail_err("timer_settime");
(void)close(fd);
return -1;
}
stress_set_sockaddr(args->name, args->instance, ppid,
epoll_domain, port, &addr, &addr_len, NET_ADDR_ANY);
errno = 0;
ret = connect(fd, addr, addr_len);
saved_errno = errno;
/* No longer need timer */
if (timer_delete(epoll_timerid) < 0) {
pr_fail_err("timer_delete");
(void)close(fd);
return -1;
}
if (ret < 0) {
switch (saved_errno) {
case EINTR:
connect_timeouts++;
break;
case ECONNREFUSED: /* No servers yet running */
case ENOENT: /* unix domain not yet created */
break;
default:
pr_dbg("%s: connect failed: %d (%s)\n",
args->name, saved_errno, strerror(saved_errno));
break;
}
(void)close(fd);
(void)shim_usleep(100000); /* Twiddle fingers for a moment */
retries++;
if (retries > 1000) {
/* Sigh, give up.. */
errno = saved_errno;
pr_fail_dbg("too many connects");
return -1;
}
goto retry;
}
(void)memset(buf, 'A' + (*args->counter % 26), sizeof(buf));
if (send(fd, buf, sizeof(buf), 0) < 0) {
(void)close(fd);
pr_fail_dbg("send");
break;
}
(void)close(fd);
(void)shim_sched_yield();
inc_counter(args);
} while (keep_stressing());
#if defined(AF_UNIX)
if (addr && (epoll_domain == AF_UNIX)) {
struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;
(void)unlink(addr_un->sun_path);
}
#endif
if (connect_timeouts)
pr_dbg("%s: %" PRIu64 " x 0.25 second "
"connect timeouts, connection table full "
"(instance %" PRIu32 ")\n",
args->name, connect_timeouts, args->instance);
return EXIT_SUCCESS;
}
/*
* epoll_server()
* wait on connections and read data
*/
static void epoll_server(
const args_t *args,
const int child,
const pid_t ppid,
const int epoll_port,
const int epoll_domain)
{
int efd = -1, sfd = -1, rc = EXIT_SUCCESS;
int so_reuseaddr = 1;
int port = epoll_port + child + (max_servers * args->instance);
struct epoll_event *events = NULL;
struct sockaddr *addr = NULL;
socklen_t addr_len = 0;
if (stress_sig_stop_stressing(args->name, SIGALRM) < 0) {
rc = EXIT_FAILURE;
goto die;
}
if ((sfd = socket(epoll_domain, SOCK_STREAM, 0)) < 0) {
pr_fail_err("socket");
rc = EXIT_FAILURE;
goto die;
}
if (setsockopt(sfd, SOL_SOCKET, SO_REUSEADDR,
&so_reuseaddr, sizeof(so_reuseaddr)) < 0) {
pr_fail_err("setsockopt");
rc = EXIT_FAILURE;
goto die_close;
}
stress_set_sockaddr(args->name, args->instance, ppid,
epoll_domain, port, &addr, &addr_len, NET_ADDR_ANY);
if (bind(sfd, addr, addr_len) < 0) {
pr_fail_err("bind");
rc = EXIT_FAILURE;
goto die_close;
}
if (epoll_set_fd_nonblock(sfd) < 0) {
pr_fail_err("setting socket to non-blocking");
rc = EXIT_FAILURE;
goto die_close;
}
if (listen(sfd, SOMAXCONN) < 0) {
pr_fail_err("listen");
rc = EXIT_FAILURE;
goto die_close;
}
if ((efd = epoll_create1(0)) < 0) {
pr_fail_err("epoll_create1");
rc = EXIT_FAILURE;
goto die_close;
}
if (epoll_ctl_add(efd, sfd) < 0) {
pr_fail_err("epoll ctl add");
rc = EXIT_FAILURE;
goto die_close;
}
if ((events = calloc(MAX_EPOLL_EVENTS,
sizeof(struct epoll_event))) == NULL) {
pr_fail_err("epoll ctl add");
rc = EXIT_FAILURE;
goto die_close;
}
do {
int n, i;
(void)memset(events, 0, MAX_EPOLL_EVENTS * sizeof(struct epoll_event));
errno = 0;
/*
* Wait for 100ms for an event, allowing us to
* to break out if keep_stressing_flag has been changed
*/
n = epoll_wait(efd, events, MAX_EPOLL_EVENTS, 100);
if (n < 0) {
if (errno != EINTR) {
pr_fail_err("epoll_wait");
rc = EXIT_FAILURE;
goto die_close;
}
break;
}
for (i = 0; i < n; i++) {
if ((events[i].events & EPOLLERR) ||
(events[i].events & EPOLLHUP) ||
(!(events[i].events & EPOLLIN))) {
/*
* Error has occurred or fd is not
* for reading anymore.. so reap fd
*/
(void)close(events[i].data.fd);
} else if (sfd == events[i].data.fd) {
/*
* The listening socket has notification(s)
* pending, so handle incoming connections
*/
if (epoll_notification(args, efd, sfd) < 0)
break;
} else {
/*
* The fd has data available, so read it
*/
epoll_recv_data(events[i].data.fd);
}
}
} while (keep_stressing());
die_close:
if (efd != -1)
(void)close(efd);
if (sfd != -1)
(void)close(sfd);
die:
#if defined(AF_UNIX)
if (addr && (epoll_domain == AF_UNIX)) {
struct sockaddr_un *addr_un = (struct sockaddr_un *)addr;
(void)unlink(addr_un->sun_path);
}
#endif
free(events);
exit(rc);
}
/*
* stress_epoll
* stress by heavy socket I/O
*/
int stress_epoll(const args_t *args)
{
pid_t pids[MAX_SERVERS], ppid = getppid();
int i, rc = EXIT_SUCCESS;
int epoll_port = DEFAULT_EPOLL_PORT;
int epoll_domain = AF_UNIX;
(void)get_setting("epoll-port", &epoll_port);
(void)get_setting("epoll-domain", &epoll_domain);
if (max_servers == 1) {
pr_dbg("%s: process [%d] using socket port %d\n",
args->name, args->pid,
epoll_port + args->instance);
} else {
pr_dbg("%s: process [%d] using socket ports %d..%d\n",
args->name, args->pid,
epoll_port + (max_servers * args->instance),
epoll_port + (max_servers * (args->instance + 1)) - 1);
}
/*
* Spawn off servers to handle multi port connections.
* The (src address, src port, dst address, dst port) tuple
* is kept in the connection table for a default of 60 seconds
* which means for many fast short connections we can
* fill this table up and new connections get blocked until
* this table empties. One strategy is to reduce TIME_WAIT (not
* good) so the easiest way forward is to just increase the
* number of ports being listened to to increase the tuple
* range and hence allow more connections. See
* http://vincent.bernat.im/en/blog/2014-tcp-time-wait-state-linux.html
* Typically, we are limited to ~500 connections per second
* on a default Linux configuration.
*/
(void)memset(pids, 0, sizeof(pids));
for (i = 0; i < max_servers; i++) {
pids[i] = epoll_spawn(args, epoll_server, i, ppid, epoll_port, epoll_domain);
if (pids[i] < 0) {
pr_fail_dbg("fork");
goto reap;
}
}
epoll_client(args, ppid, epoll_port, epoll_domain);
reap:
for (i = 0; i < max_servers; i++) {
int status;
if (pids[i] > 0) {
(void)kill(pids[i], SIGKILL);
if (waitpid(pids[i], &status, 0) < 0) {
pr_fail_dbg("waitpid");
}
}
}
return rc;
}
#else
int stress_epoll(const args_t *args)
{
return stress_not_implemented(args);
}
#endif