Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/ioq_stress_test.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2022 Teluu Inc. (http://www.teluu.com)
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 */
18 #include "test.h"
19 #include <pjlib.h>
20
21 #define THIS_FILE "ioq_stress_test.c"
22 #define MAX_THREADS 16
23 //#define TRACE(log) PJ_LOG(3,log)
24 #define TRACE(log)
25 #define MAX_ASYNC 16
26 #define MAX_UDP_MISMATCH_LOG 10
27
28 #define RETCODE_CONNECT_FAILED 650
29
30 /* index in socks[], keys[] array */
31 #define SERVER 0
32 #define CLIENT 1
33
34 /* epoll errors that can be ignored */
35 #define IGNORE_ERROR(e) (((e)==PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)))
36
37 typedef struct test_desc test_desc;
38
39 /* op_key user data */
40 typedef union op_key_user_data {
41 struct {
42 int side; /* CLIENT or SERVER */
43 pj_status_t status; /* PJ_SUCCESS: idle, PJ_EPENDING: pending, other: error */
44 unsigned count; /* how many times were used */
45 } common;
46
47 struct {
48 int side;
49 pj_status_t status;
50 unsigned count;
51
52 pj_sock_t new_sock;
53 pj_ioqueue_op_key_t recv_op;
54 unsigned *recv_buf;
55 } server;
56
57 struct {
58 int side;
59 pj_status_t status;
60 unsigned count;
61
62 pj_ioqueue_op_key_t connect_op;
63 pj_ioqueue_op_key_t send_op;
64 unsigned *send_buf;
65 } client;
66 } op_key_user_data;
67
68
69 /* Description for a single test. This description contains two parts:
70 * - configuration
71 * - run-time state
72 *
73 * A single test contains an ioqueue, and (mostly) of a pair of socket:
74 * server and client. (when TCP is used, a listening socket will be created
75 * too).
76 *
77 * Each socket (server or client) will initiate MAX_ASYNC simultaneous
78 * send() and recev() operations.
79 *
80 * One single pass of a test completes when the designated number of "packets"
81 * have been exchanged by the client/server (cfg.rx_cnt).
82 *
83 * Then the test may be repeated "cfg.repeat" times, using the same ioqueue but
84 * different set of sockets, to test ioqueue key recycling mechanism.
85 */
86 struct test_desc
87 {
88 struct {
89 const char *title;
90 int expected_ret_code;
91 int max_fd; /* ioqueue's max_fd */
92 pj_bool_t allow_concur; /* ioqueue's allow_concur setting */
93 unsigned epoll_flags; /* epoll_flags: EXCLUSIVE, ONESHOT */
94 int sock_type; /* tcp or udp? */
95 int n_threads; /* number of worker threads polling ioqueue */
96
97 pj_bool_t cancel_connect; /* TCP connect then cancel immediately */
98 pj_bool_t failed_connect; /* TCP connect to nowhere */
99 pj_bool_t reject_connect; /* close previously listen() without calling accept */
100 unsigned tx_so_buf_size; /* SO_SNDBUF in multples of sizeof(int) */
101 unsigned rx_so_buf_size; /* SO_RCVBUF in multples of sizeof(int) */
102 unsigned pkt_len; /* size of each send()/recv() in mult. sizeof(int) */
103 unsigned tx_cnt; /* total number of ints to send */
104 unsigned rx_cnt; /* total number of ints to read */
105 unsigned n_servers; /* number of servers (=parallel recv) to instantiate */
106 unsigned n_clients; /* number of clients (=parallel send) to instantiate */
107 pj_bool_t sequenced; /* incoming packets must be in sequence */
108 unsigned repeat;
109 } cfg;
110
111 struct {
112 pj_pool_t *pool;
113 pj_ioqueue_t *ioq;
114 pj_grp_lock_t *grp_lock;
115 pj_sock_t listen_sock; /* tcp listening socket */
116 pj_ioqueue_key_t *listen_key;/* tcp listening key */
117
118 pj_sock_t socks[2]; /* server/client sockets */
119 unsigned cnt[2]; /* number of packets sent/received */
120 pj_ioqueue_key_t *keys[2]; /* server/client keys */
121 pj_status_t connect_status;
122
123 int retcode; /* test retcode. non-zero will abort. */
124 unsigned udp_rx_mismatch_cnt;/* UDP RX out-of-order counter */
125
126 /* okud: op_key user data */
127 unsigned okuds_cnt[2];
128 op_key_user_data okuds[2][MAX_ASYNC];
129
130 } state;
131 };
132
133 static pj_ioqueue_callback test_cb;
134
135 static op_key_user_data *alloc_okud(test_desc *test, int side)
136 {
137 op_key_user_data *okud;
138
139 assert(test->state.okuds_cnt[side] < MAX_ASYNC);
140 okud = &test->state.okuds[side][test->state.okuds_cnt[side]++];
141 okud->common.side = side;
142 okud->common.status = PJ_EPENDING;
143 okud->common.count = 0;
144 return okud;
145 }
146
147 static void on_read_complete(pj_ioqueue_key_t *key,
148 pj_ioqueue_op_key_t *op_key,
149 pj_ssize_t bytes_read)
150 {
151 test_desc *test = (test_desc*)pj_ioqueue_get_user_data(key);
152 op_key_user_data *okud = (op_key_user_data*)op_key->user_data;
153
154 assert(okud->common.side == SERVER);
155
156 do {
157 unsigned counter = test->state.cnt[SERVER];
158
159 /* can only handle multiple of ints,*/
160 PJ_ASSERT_ON_FAIL(bytes_read<0 || bytes_read%sizeof(int)==0,
161 {
162 test->state.retcode = 410;
163 okud->server.status = PJ_EBUG;
164 return;
165 });
166
167 if (bytes_read != -12345 && bytes_read <= 0 && !IGNORE_ERROR(-bytes_read)) {
168 TRACE((THIS_FILE, "op_key:%p: stopping due to read=%ld",
169 op_key, bytes_read));
170 PJ_PERROR(1,(THIS_FILE, (pj_status_t)-bytes_read, "%ld is", -bytes_read));
171 okud->server.status = (bytes_read == 0)? PJ_RETURN_OS_ERROR(OSERR_ENOTCONN) :
172 (pj_status_t)-bytes_read;
173 break;
174 }
175
176 if (bytes_read > 0) {
177 pj_lock_acquire((pj_lock_t*)test->state.grp_lock);
178 if (test->cfg.sequenced) {
179 unsigned *p, *start, *end;
180 pj_bool_t has_error = PJ_FALSE;
181
182 start = okud->server.recv_buf;
183 end = start + (bytes_read / sizeof(int));
184
185 for (p=start; p!=end; ++p) {
186 counter = test->state.cnt[SERVER]++;
187 if (*p != counter && test->cfg.sock_type==pj_SOCK_STREAM()) {
188 PJ_LOG(3,(THIS_FILE, " Error: TCP RX sequence mismatch at idx=%ld. Expecting %d, got %d",
189 p-start, counter, *p));
190 test->state.retcode = 412;
191 okud->server.status = PJ_EBUG;
192 pj_lock_release((pj_lock_t*)test->state.grp_lock);
193 return;
194 } else if (test->cfg.sock_type==pj_SOCK_DGRAM()) {
195 if (*p < counter && !has_error) {
196 /* As it turns out, this could happen sometimes
197 * with UDP even when allow_concurrent is set to FALSE.
198 * Maybe the kernel allows the packet to be out of
199 * order? (tested on Linux epoll). Or could there be
200 * bug somewhere?
201 */
202 test->state.udp_rx_mismatch_cnt++;
203 if (test->state.udp_rx_mismatch_cnt <= MAX_UDP_MISMATCH_LOG) {
204 PJ_LOG(3,(THIS_FILE, " UDP RX sequence mismatch at idx=%ld. Expecting %d, got %d",
205 p-start, counter, *p));
206 }
207 //test->state.retcode = 413;
208 //okud->server.status = PJ_EBUG;
209 //pj_lock_release((pj_lock_t*)test->state.grp_lock);
210 //return;
211 has_error = PJ_TRUE;
212 } else if (*p > counter) {
213 test->state.cnt[SERVER] = *p;
214 }
215 }
216 }
217 } else {
218 test->state.cnt[SERVER] += (unsigned)(bytes_read / 4);
219 }
220 pj_lock_release((pj_lock_t*)test->state.grp_lock);
221 }
222
223 if (counter >= test->cfg.rx_cnt) {
224 //TRACE((THIS_FILE, "op_key:%p: RX counter reached (%d)",
225 // op_key, counter));
226 okud->server.status = PJ_SUCCESS;
227 break;
228 }
229
230 /* next read */
231 bytes_read = test->cfg.pkt_len * sizeof(int);
232 okud->server.status = pj_ioqueue_recv(key, op_key, okud->server.recv_buf,
233 &bytes_read, 0);
234 if (okud->server.status != PJ_SUCCESS && okud->server.status != PJ_EPENDING) {
235 PJ_PERROR(1,(THIS_FILE, okud->server.status, "pj_ioqueue_recv() error"));
236 test->state.retcode = 420;
237 return;
238 }
239
240 } while (okud->server.status==PJ_SUCCESS &&
241 test->state.retcode==0 // <= this can be set by other thread
242 );
243 }
244
245 static void on_write_complete(pj_ioqueue_key_t *key,
246 pj_ioqueue_op_key_t *op_key,
247 pj_ssize_t bytes_sent)
248 {
249 test_desc *test = (test_desc*)pj_ioqueue_get_user_data(key);
250 op_key_user_data *okud = (op_key_user_data*)op_key->user_data;
251 unsigned *p, *end;
252 unsigned counter;
253
254 pj_assert(okud != NULL);
255 assert(okud->common.side == CLIENT);
256
257 PJ_ASSERT_ON_FAIL(bytes_sent<0 || bytes_sent%sizeof(int)==0,
258 {
259 test->state.retcode = 501;
260 okud->client.status = PJ_EBUG;
261 return;
262 });
263
264 if (bytes_sent != -12345 && bytes_sent <= 0 && !IGNORE_ERROR(-bytes_sent)) {
265 TRACE((THIS_FILE, "op_key:%p: stopping due to sent=%ld",
266 op_key, bytes_sent));
267 okud->client.status = (bytes_sent == 0)? PJ_RETURN_OS_ERROR(OSERR_ENOTCONN) :
268 (pj_status_t)-bytes_sent;
269 return;
270 }
271
272 /* for TCP, client stop transmission when number of transmitted
273 * count is done. For UDP, continue transmitting indefinitely (until
274 * receiver says to stop) because packets may be dropped.
275 */
276 counter = test->state.cnt[CLIENT];
277 if (test->cfg.sock_type==pj_SOCK_STREAM() && counter >= test->cfg.tx_cnt) {
278 //TRACE((THIS_FILE, "op_key:%p: TX counter reached (%d)",
279 // op_key, counter));
280 okud->client.status = PJ_SUCCESS;
281 return;
282 }
283
284 /* construct buffer as int sequence.
285 * protect with mutex to make sure sending is in sequence.
286 */
287 pj_lock_acquire((pj_lock_t*)test->state.grp_lock);
288
289 p = okud->client.send_buf;
290 end = p + test->cfg.pkt_len;
291 while (p != end)
292 *p++ = test->state.cnt[CLIENT]++;
293
294 /* next write */
295 bytes_sent = test->cfg.pkt_len * sizeof(int);
296 okud->client.status = pj_ioqueue_send(key, op_key, okud->client.send_buf,
297 &bytes_sent, 0);
298 if (okud->client.status != PJ_SUCCESS && okud->client.status != PJ_EPENDING) {
299 PJ_PERROR(1,(THIS_FILE, okud->client.status, "pj_ioqueue_send() error"));
300 test->state.retcode = 520;
301 } else if (okud->client.status == PJ_SUCCESS) {
302 counter = test->state.cnt[CLIENT];
303 if (test->cfg.sock_type==pj_SOCK_STREAM() && counter >= test->cfg.tx_cnt) {
304 //TRACE((THIS_FILE, "op_key:%p: TX counter reached (%d)",
305 // op_key, counter));
306 }
307 }
308
309 pj_lock_release((pj_lock_t*)test->state.grp_lock);
310 }
311
312 static void on_accept_complete(pj_ioqueue_key_t *key,
313 pj_ioqueue_op_key_t *op_key,
314 pj_sock_t sock,
315 pj_status_t status)
316 {
317 test_desc *test = (test_desc*)pj_ioqueue_get_user_data(key);
318 /* For accept, reuse okud for server socket */
319 op_key_user_data *okud = (op_key_user_data*)op_key->user_data;
320
321 assert(okud->common.side == SERVER);
322 if (status != PJ_SUCCESS) {
323 okud->server.status = status;
324 return;
325 }
326
327 test->state.socks[SERVER] = sock;
328 pj_lock_acquire((pj_lock_t*)test->state.grp_lock);
329
330 if (test->cfg.rx_so_buf_size) {
331 int value = test->cfg.rx_so_buf_size * sizeof(int);
332 okud->server.status = pj_sock_setsockopt(sock,
333 pj_SOL_SOCKET(),
334 pj_SO_RCVBUF(),
335 &value, sizeof(value));
336 }
337
338 status = pj_ioqueue_register_sock2(test->state.pool,
339 test->state.ioq,
340 test->state.socks[SERVER],
341 NULL, //test->state.grp_lock,
342 test,
343 &test_cb,
344 &test->state.keys[SERVER]);
345 if (status != PJ_SUCCESS) {
346 pj_lock_release((pj_lock_t*)test->state.grp_lock);
347 PJ_PERROR(1,(THIS_FILE, status, "pj_ioqueue_register_sock2 error"));
348 test->state.retcode = 605;
349 return;
350 }
351
352 if (!okud->server.recv_buf) {
353 okud->server.recv_buf = pj_pool_calloc(test->state.pool, test->cfg.pkt_len,
354 sizeof(int));
355 }
356
357 pj_ioqueue_op_key_init(&okud->server.recv_op, sizeof(pj_ioqueue_op_key_t));
358 okud->server.recv_op.user_data = okud;
359 pj_lock_release((pj_lock_t*)test->state.grp_lock);
360
361 on_read_complete(test->state.keys[SERVER], &okud->server.recv_op, -12345);
362 }
363
364 static void on_connect_complete(pj_ioqueue_key_t *key,
365 pj_status_t status)
366 {
367 test_desc *test = (test_desc*)pj_ioqueue_get_user_data(key);
368 unsigned i;
369
370 #if PJ_WIN64 || PJ_WIN32
371 if (test->cfg.expected_ret_code==RETCODE_CONNECT_FAILED &&
372 status==PJ_SUCCESS)
373 {
374 /* On Windows, when the server socket is closed even without accept(),
375 * connect() returns success but subsequent write will return
376 * error
377 */
378 pj_ioqueue_op_key_t op_key;
379 int send_buf = 0;
380 pj_ssize_t bytes_sent = sizeof(send_buf);
381
382 status = pj_ioqueue_send(key, &op_key, &send_buf, &bytes_sent, 0);
383 }
384 #endif
385
386 test->state.connect_status = status;
387 if (status != PJ_SUCCESS) {
388 if (test->cfg.expected_ret_code != RETCODE_CONNECT_FAILED)
389 PJ_PERROR(1, (THIS_FILE, status, "async connect failed"));
390 test->state.retcode = RETCODE_CONNECT_FAILED;
391 return;
392 }
393
394 for (i=0; i<test->cfg.n_clients; ++i) {
395 op_key_user_data *okud;
396
397 pj_lock_acquire((pj_lock_t*)test->state.grp_lock);
398 okud = alloc_okud(test, CLIENT);
399 if (!okud->client.send_buf) {
400 okud->client.send_buf = pj_pool_calloc(test->state.pool,
401 test->cfg.pkt_len,
402 sizeof(int));
403 }
404
405 pj_ioqueue_op_key_init(&okud->client.send_op, sizeof(pj_ioqueue_op_key_t));
406 okud->client.send_op.user_data = okud;
407 pj_lock_release((pj_lock_t*)test->state.grp_lock);
408
409 on_write_complete(key, &okud->client.send_op, -12345);
410 }
411 };
412
413 /* worker thread */
414 static int worker_thread(void *p)
415 {
416 test_desc *test = (test_desc*)p;
417 unsigned n_events = 0;
418
419 /* log indent is not propagated to other threads,
420 * so we set it explicitly here
421 */
422 pj_log_set_indent(3);
423
424 while (test->state.retcode==0) {
425 unsigned i;
426 pj_time_val timeout = {0, 10};
427
428 int rc = pj_ioqueue_poll(test->state.ioq, &timeout);
429 if (rc < 0) {
430 PJ_PERROR(1,(THIS_FILE, -rc, "pj_ioqueue_poll() error"));
431 }
432
433 if (rc > 0)
434 n_events += rc;
435
436 if (test->state.cnt[CLIENT] >= test->cfg.tx_cnt &&
437 test->state.cnt[SERVER] >= test->cfg.rx_cnt)
438 {
439 /* Done. Success */
440 break;
441 }
442
443 /* Check if client needs to send. UDP client needs to send until
444 * server receives enough packet (because packets can be lost).
445 * TCP client stops after enough packets are transmitted.
446 */
447 for (i=0; i<test->state.okuds_cnt[CLIENT] &&
448 (test->cfg.sock_type == pj_SOCK_DGRAM() ||
449 test->state.cnt[CLIENT] < test->cfg.tx_cnt); ++i)
450 {
451 op_key_user_data *okud = &test->state.okuds[CLIENT][i];
452 pj_lock_acquire((pj_lock_t*)test->state.grp_lock);
453 if (!pj_ioqueue_is_pending(test->state.keys[CLIENT],
454 &okud->client.send_op))
455 {
456 on_write_complete(test->state.keys[CLIENT],
457 &okud->client.send_op, -12345);
458 }
459 pj_lock_release((pj_lock_t*)test->state.grp_lock);
460 }
461
462 if (test->state.connect_status != PJ_SUCCESS &&
463 test->state.connect_status != PJ_EPENDING)
464 {
465 break;
466 }
467 }
468
469 /* Flush events. Check if polling is blocked (for ioq select, it should,
470 * but for epoll and kqueue backends, the blocking duration is just minimal
471 * to avoid busy loop).
472 */
473 if (test->state.retcode==test->cfg.expected_ret_code) {
474 pj_timestamp t0, t1;
475 unsigned i, msec, duration;
476
477 pj_get_timestamp(&t0);
478 for (i=0; i<10; ++i) {
479 pj_time_val timeout = {0, 100};
480 n_events += pj_ioqueue_poll(test->state.ioq, &timeout);
481 pj_thread_sleep(10);
482 }
483 pj_get_timestamp(&t1);
484 msec = pj_elapsed_msec(&t0, &t1);
485 duration = (!pj_ansi_strcmp(pj_ioqueue_name(), "select"))? 500: 200;
486 if (msec <= duration) {
487 test->state.retcode = 5000;
488 PJ_LOG(1,(THIS_FILE, "Error: pj_ioqueue_poll is not blocking, "
489 "time elapsed: %d, no events: %d",
490 msec, n_events));
491 }
492 }
493
494 TRACE((THIS_FILE, "thread exiting, n_events=%d", n_events));
495 return 0;
496 }
497
498
499 /* Perform single pass of test loop, reusing the ioqueue/pool state */
500 static int perform_single_pass(test_desc *test)
501 {
502 unsigned i;
503 int retcode = 0;
504 pj_sockaddr addr;
505 int namelen = sizeof(addr);
506 pj_str_t localhost = pj_str("127.0.0.1");
507
508 /* Reset iteration states */
509 test->state.retcode = 0;
510 test->state.connect_status = PJ_EPENDING;
511 test->state.listen_key = NULL;
512 test->state.listen_sock = PJ_INVALID_SOCKET;
513 for (i=0; i<2; ++i) {
514 int j;
515
516 test->state.socks[i] = PJ_INVALID_SOCKET;
517 test->state.keys[i] = NULL;
518 test->state.cnt[i] = 0;
519 test->state.okuds_cnt[i] = 0;
520
521 for (j=0; j<MAX_ASYNC; ++j) {
522 //test->state.okuds[i][j].common.status = PJ_EPENDING;
523 }
524 }
525
526 /*
527 * Init server side
528 */
529 if (test->cfg.sock_type == pj_SOCK_STREAM()) {
530 CHECK(20, pj_sock_socket(pj_AF_INET(), test->cfg.sock_type, 0,
531 &test->state.listen_sock));
532 CHECK(21, pj_sock_bind_in(test->state.listen_sock, 0, 0));
533 CHECK(22, pj_sock_getsockname(test->state.listen_sock, &addr, &namelen));
534 CHECK(23, pj_sock_listen(test->state.listen_sock, 5));
535 CHECK(24, pj_ioqueue_register_sock2(test->state.pool,
536 test->state.ioq,
537 test->state.listen_sock,
538 NULL, //test->state.grp_lock,
539 test,
540 &test_cb,
541 &test->state.listen_key));
542 if (!test->cfg.reject_connect) {
543 /* kick off parallel accepts */
544 for (i=0; i<test->cfg.n_servers; ++i) {
545 op_key_user_data *okud;
546
547 okud = alloc_okud(test, SERVER);
548 pj_ioqueue_op_key_init(&okud->server.recv_op,
549 sizeof(okud->server.recv_op));
550 okud->server.recv_op.user_data = okud;
551 okud->server.status = pj_ioqueue_accept(test->state.listen_key,
552 &okud->server.recv_op,
553 &okud->server.new_sock,
554 NULL,
555 NULL,
556 NULL);
557 CHECK(26, okud->server.status);
558 }
559 }
560 } else {
561 CHECK(30, pj_sock_socket(pj_AF_INET(), test->cfg.sock_type, 0,
562 &test->state.socks[SERVER]));
563 CHECK(31, pj_sock_bind_in(test->state.socks[SERVER], 0, 0));
564 CHECK(32, pj_sock_getsockname(test->state.socks[SERVER], &addr, &namelen));
565 CHECK(33, pj_ioqueue_register_sock2(test->state.pool,
566 test->state.ioq,
567 test->state.socks[SERVER],
568 NULL, //test->state.grp_lock,
569 test,
570 &test_cb,
571 &test->state.keys[SERVER]));
572
573 if (test->cfg.rx_so_buf_size) {
574 int value = test->cfg.rx_so_buf_size * sizeof(int);
575 CHECK(34, pj_sock_setsockopt(test->state.socks[SERVER],
576 pj_SOL_SOCKET(),
577 pj_SO_RCVBUF(),
578 &value, sizeof(value)));
579 }
580
581 /* kick off parallel recvs */
582 for (i=0; i<test->cfg.n_servers; ++i) {
583 op_key_user_data *okud;
584
585 okud = alloc_okud(test, SERVER);
586 if (!okud->server.recv_buf) {
587 okud->server.recv_buf = pj_pool_calloc(test->state.pool,
588 test->cfg.pkt_len,
589 sizeof(int));
590 }
591 pj_ioqueue_op_key_init(&okud->server.recv_op, sizeof(pj_ioqueue_op_key_t));
592 okud->server.recv_op.user_data = okud;
593
594 on_read_complete(test->state.keys[SERVER], &okud->server.recv_op,
595 -12345);
596 }
597 }
598
599
600 /*
601 * Client side
602 */
603 CHECK(40, pj_sock_socket(pj_AF_INET(), test->cfg.sock_type, 0,
604 &test->state.socks[CLIENT]));
605 addr.ipv4.sin_addr = pj_inet_addr(&localhost);
606 if (test->cfg.tx_so_buf_size) {
607 int value = test->cfg.tx_so_buf_size * sizeof(int);
608 CHECK(41, pj_sock_setsockopt(test->state.socks[CLIENT],
609 pj_SOL_SOCKET(),
610 pj_SO_RCVBUF(),
611 &value, sizeof(value)));
612 }
613
614 /* We cannot use the global group lock for registering keys (here and
615 * all below) because currently IOCP key uses the group lock handler for
616 * releasing its resources including the key itself. If the key is not
617 * released (the global group lock destroy is done very late) and
618 * as the ioqueue capacity for the tests are quite limited (~4-6 keys),
619 * ioqueue will get full quickly and tests will fail.
620 */
621 CHECK(42, pj_ioqueue_register_sock2(test->state.pool,
622 test->state.ioq,
623 test->state.socks[CLIENT],
624 NULL, //test->state.grp_lock,
625 test,
626 &test_cb,
627 &test->state.keys[CLIENT]));
628 if (test->cfg.sock_type == pj_SOCK_STREAM()) {
629 if (test->cfg.failed_connect) {
630 pj_sockaddr_in_init((pj_sockaddr_in*)&addr, &localhost, 39275);
631 test->state.connect_status = pj_ioqueue_connect(test->state.keys[CLIENT],
632 &addr,
633 sizeof(pj_sockaddr_in));
634 if (test->state.connect_status && test->state.connect_status!=PJ_EPENDING)
635 PJ_PERROR(1,(THIS_FILE, test->state.connect_status,
636 "TCP connect() error"));
637 CHECK(44, test->state.connect_status);
638 } else {
639 test->state.connect_status = pj_ioqueue_connect(test->state.keys[CLIENT],
640 &addr, namelen);
641 CHECK(45, test->state.connect_status);
642 }
643 /* To be continued when socket is connected (or fails) */
644 } else {
645 /* UDP connect */
646 CHECK(46, pj_ioqueue_connect(test->state.keys[CLIENT],
647 &addr, namelen));
648 /* kick off parallel send */
649 for (i=0; i<test->cfg.n_clients; ++i) {
650 op_key_user_data *okud;
651
652 okud = alloc_okud(test, CLIENT);
653 if (!okud->client.send_buf) {
654 okud->client.send_buf = pj_pool_calloc(test->state.pool,
655 test->cfg.pkt_len,
656 sizeof(int));
657 }
658 pj_ioqueue_op_key_init(&okud->client.send_op, sizeof(pj_ioqueue_op_key_t));
659 okud->client.send_op.user_data = okud;
660
661 on_write_complete(test->state.keys[CLIENT], &okud->client.send_op,
662 -12345);
663 }
664 }
665
666 if (test->cfg.reject_connect) {
667 CHECK(47, pj_ioqueue_unregister(test->state.listen_key));
668 test->state.listen_key = NULL;
669 test->state.listen_sock = PJ_INVALID_SOCKET;
670 } else if (test->cfg.cancel_connect) {
671 CHECK(48, pj_ioqueue_unregister(test->state.keys[CLIENT]));
672 }
673
674 pj_log_push_indent();
675
676 if (test->cfg.n_threads==0)
677 worker_thread(test);
678 else {
679 unsigned n_threads = test->cfg.n_threads;
680 pj_thread_t *threads[MAX_THREADS];
681
682 for (i=0; i<n_threads; ++i) {
683 CHECK(49, pj_thread_create(test->state.pool, "ioq_stress_test",
684 &worker_thread, test,
685 0, PJ_THREAD_SUSPENDED,
686 &threads[i]));
687 }
688
689 for (i=0; i<n_threads; ++i) {
690 pj_status_t status = pj_thread_resume(threads[i]);
691 PJ_ASSERT_RETURN(status==PJ_SUCCESS, 49);
692 }
693
694 worker_thread(test);
695
696 for (i=0; i<n_threads; ++i) {
697 pj_thread_join(threads[i]);
698 pj_thread_destroy(threads[i]);
699 }
700 }
701
702 retcode = test->state.retcode;
703
704 on_return:
705 if (test->state.listen_key)
706 pj_ioqueue_unregister(test->state.listen_key);
707 for (i=0; i<2; ++i) {
708 if (test->state.keys[i])
709 pj_ioqueue_unregister(test->state.keys[i]);
710 }
711
712 pj_log_pop_indent();
713 return (retcode==test->cfg.expected_ret_code)? 0 : retcode;
714 }
715
716 /* Perform tests according to test_desc */
717 static int perform_test(test_desc *test)
718 {
719 pj_ioqueue_cfg ioqueue_cfg;
720 unsigned rep;
721 int retcode;
722
723 PJ_LOG(3,(THIS_FILE, "%s", test->cfg.title));
724 pj_log_push_indent();
725
726 PJ_ASSERT_RETURN(test->cfg.n_servers <= MAX_ASYNC, 4000);
727 PJ_ASSERT_RETURN(test->cfg.n_clients <= MAX_ASYNC, 4001);
728
729 pj_bzero(&test->state, sizeof(test->state));
730
731 PJ_ASSERT_RETURN(test->state.pool == NULL, -1);
732 test->state.pool = pj_pool_create(mem, NULL, 4096, 4096, NULL);
733
734 pj_ioqueue_cfg_default(&ioqueue_cfg);
735 ioqueue_cfg.epoll_flags = test->cfg.epoll_flags;
736
737 CHECK(10, pj_ioqueue_create2(test->state.pool, test->cfg.max_fd,
738 &ioqueue_cfg, &test->state.ioq));
739 CHECK(11, pj_ioqueue_set_default_concurrency(test->state.ioq,
740 test->cfg.allow_concur));
741 CHECK(20, pj_grp_lock_create(test->state.pool, NULL, &test->state.grp_lock));
742 CHECK(21, pj_grp_lock_add_ref(test->state.grp_lock));
743
744 for (rep=0, retcode=0; rep<test->cfg.repeat && !retcode; ++rep) {
745 TRACE((THIS_FILE, "repeat %d/%d", rep+1, test->cfg.repeat));
746 retcode = perform_single_pass(test);
747 }
748
749 on_return:
750 if (test->state.udp_rx_mismatch_cnt > MAX_UDP_MISMATCH_LOG) {
751 PJ_LOG(3,(THIS_FILE, " UDP RX sequence mismatch total: %u "
752 "(only first %d shown)",
753 test->state.udp_rx_mismatch_cnt, MAX_UDP_MISMATCH_LOG));
754 }
755
756 if (test->state.ioq)
757 pj_ioqueue_destroy(test->state.ioq);
758 if (test->state.grp_lock) {
759 pj_grp_lock_dec_ref(test->state.grp_lock);
760 test->state.grp_lock = NULL;
761 }
762 if (test->state.pool)
763 pj_pool_release(test->state.pool);
764
765 if (retcode) {
766 PJ_LOG(3,(THIS_FILE, "test failed (retcode=%d)", retcode));
767 }
768 pj_log_pop_indent();
769 return retcode;
770 }
771
772
773 static test_desc tests[128] = {
774 /* simplest test, no threads, no parallel send/recv, it's good for debugging.
775 * max_fd is limited to test management of closing keys in ioqueue safe
776 * unregistration.
777 */
778 {
779 .cfg.title = "basic udp (single thread)",
780 .cfg.max_fd = 4,
781 .cfg.allow_concur = 1,
782 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
783 .cfg.sock_type = SOCK_DGRAM, /* cannot use pj_SOCK_DGRAM (not constant) */
784 .cfg.n_threads = 0,
785 .cfg.pkt_len = 4,
786 .cfg.tx_cnt = 512,
787 .cfg.rx_cnt = 512,
788 .cfg.n_servers = 1,
789 .cfg.n_clients = 1,
790 .cfg.repeat = 4
791 },
792 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
793 {
794 .cfg.title = "basic udp (single thread, EPOLLEXCLUSIVE)",
795 .cfg.max_fd = 4,
796 .cfg.allow_concur = 1,
797 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_EXCLUSIVE,
798 .cfg.sock_type = SOCK_DGRAM,
799 .cfg.n_threads = 0,
800 .cfg.pkt_len = 4,
801 .cfg.tx_cnt = 512,
802 .cfg.rx_cnt = 512,
803 .cfg.n_servers = 1,
804 .cfg.n_clients = 1,
805 .cfg.repeat = 4
806 },
807 {
808 .cfg.title = "basic udp (single thread, EPOLLONESHOT)",
809 .cfg.max_fd = 4,
810 .cfg.allow_concur = 1,
811 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_ONESHOT,
812 .cfg.sock_type = SOCK_DGRAM,
813 .cfg.n_threads = 0,
814 .cfg.pkt_len = 4,
815 .cfg.tx_cnt = 512,
816 .cfg.rx_cnt = 512,
817 .cfg.n_servers = 1,
818 .cfg.n_clients = 1,
819 .cfg.repeat = 4
820 },
821 {
822 .cfg.title = "basic udp (single thread, epoll plain)",
823 .cfg.max_fd = 4,
824 .cfg.allow_concur = 1,
825 .cfg.epoll_flags = 0,
826 .cfg.sock_type = SOCK_DGRAM,
827 .cfg.n_threads = 0,
828 .cfg.pkt_len = 4,
829 .cfg.tx_cnt = 512,
830 .cfg.rx_cnt = 512,
831 .cfg.n_servers = 1,
832 .cfg.n_clients = 1,
833 .cfg.repeat = 4
834 },
835 #endif
836 /* simplest test (tcp), no threads, no parallel send/recv, it's good for debugging.
837 * max_fd is limited to test management of closing keys in ioqueue safe
838 * unregistration.
839 */
840 {
841 .cfg.title = "basic tcp (single thread)",
842 .cfg.max_fd = 6,
843 .cfg.allow_concur = 1,
844 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
845 .cfg.sock_type = SOCK_STREAM,
846 .cfg.n_threads = 0,
847 .cfg.pkt_len = 4,
848 .cfg.tx_cnt = 20,
849 .cfg.rx_cnt = 20,
850 .cfg.n_servers = 1,
851 .cfg.n_clients = 1,
852 .cfg.repeat = 4
853 },
854 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
855 {
856 .cfg.title = "basic tcp (single thread, EPOLLEXCLUSIVE)",
857 .cfg.max_fd = 6,
858 .cfg.allow_concur = 1,
859 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_EXCLUSIVE,
860 .cfg.sock_type = SOCK_STREAM,
861 .cfg.n_threads = 0,
862 .cfg.pkt_len = 4,
863 .cfg.tx_cnt = 20,
864 .cfg.rx_cnt = 20,
865 .cfg.n_servers = 1,
866 .cfg.n_clients = 1,
867 .cfg.repeat = 4
868 },
869 {
870 .cfg.title = "basic tcp (single thread, EPOLLONESHOT)",
871 .cfg.max_fd = 6,
872 .cfg.allow_concur = 1,
873 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_ONESHOT,
874 .cfg.sock_type = SOCK_STREAM,
875 .cfg.n_threads = 0,
876 .cfg.pkt_len = 4,
877 .cfg.tx_cnt = 20,
878 .cfg.rx_cnt = 20,
879 .cfg.n_servers = 1,
880 .cfg.n_clients = 1,
881 .cfg.repeat = 4
882 },
883 {
884 .cfg.title = "basic tcp (single thread, plain)",
885 .cfg.max_fd = 6,
886 .cfg.allow_concur = 1,
887 .cfg.epoll_flags = 0,
888 .cfg.sock_type = SOCK_STREAM,
889 .cfg.n_threads = 0,
890 .cfg.pkt_len = 4,
891 .cfg.tx_cnt = 20,
892 .cfg.rx_cnt = 20,
893 .cfg.n_servers = 1,
894 .cfg.n_clients = 1,
895 .cfg.repeat = 4
896 },
897 #endif
898 /* failed TCP connect().
899 */
900 {
901 .cfg.title = "failed tcp connect (pls wait)",
902 .cfg.expected_ret_code = RETCODE_CONNECT_FAILED,
903 .cfg.max_fd = 6,
904 .cfg.allow_concur = 1,
905 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
906 .cfg.failed_connect = 1,
907 .cfg.sock_type = SOCK_STREAM,
908 .cfg.n_threads = MAX_THREADS,
909 .cfg.pkt_len = 4,
910 .cfg.tx_cnt = 20,
911 .cfg.rx_cnt = 20,
912 .cfg.n_servers = 1,
913 .cfg.n_clients = 1,
914 .cfg.repeat = 2
915 },
916 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
917 {
918 .cfg.title = "failed tcp connect (EPOLLEXCLUSIVE)",
919 .cfg.expected_ret_code = RETCODE_CONNECT_FAILED,
920 .cfg.max_fd = 6,
921 .cfg.allow_concur = 1,
922 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_EXCLUSIVE,
923 .cfg.failed_connect = 1,
924 .cfg.sock_type = SOCK_STREAM,
925 .cfg.n_threads = MAX_THREADS,
926 .cfg.pkt_len = 4,
927 .cfg.tx_cnt = 20,
928 .cfg.rx_cnt = 20,
929 .cfg.n_servers = 1,
930 .cfg.n_clients = 1,
931 .cfg.repeat = 2
932 },
933 {
934 .cfg.title = "failed tcp connect (EPOLLONESHOT)",
935 .cfg.expected_ret_code = RETCODE_CONNECT_FAILED,
936 .cfg.max_fd = 6,
937 .cfg.allow_concur = 1,
938 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_ONESHOT,
939 .cfg.failed_connect = 1,
940 .cfg.sock_type = SOCK_STREAM,
941 .cfg.n_threads = MAX_THREADS,
942 .cfg.pkt_len = 4,
943 .cfg.tx_cnt = 20,
944 .cfg.rx_cnt = 20,
945 .cfg.n_servers = 1,
946 .cfg.n_clients = 1,
947 .cfg.repeat = 2
948 },
949 {
950 .cfg.title = "failed tcp connect (plain)",
951 .cfg.expected_ret_code = RETCODE_CONNECT_FAILED,
952 .cfg.max_fd = 6,
953 .cfg.allow_concur = 1,
954 .cfg.epoll_flags = 0,
955 .cfg.failed_connect = 1,
956 .cfg.sock_type = SOCK_STREAM,
957 .cfg.n_threads = MAX_THREADS,
958 .cfg.pkt_len = 4,
959 .cfg.tx_cnt = 20,
960 .cfg.rx_cnt = 20,
961 .cfg.n_servers = 1,
962 .cfg.n_clients = 1,
963 .cfg.repeat = 2
964 },
965 #endif
966 /* reject TCP connect().
967 */
968 {
969 .cfg.title = "tcp connect rejected",
970 .cfg.expected_ret_code = RETCODE_CONNECT_FAILED,
971 .cfg.max_fd = 6,
972 .cfg.allow_concur = 1,
973 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
974 .cfg.reject_connect = 1,
975 .cfg.sock_type = SOCK_STREAM,
976 .cfg.n_threads = MAX_THREADS,
977 .cfg.pkt_len = 4,
978 .cfg.tx_cnt = 20,
979 .cfg.rx_cnt = 20,
980 .cfg.n_servers = 1,
981 .cfg.n_clients = 1,
982 .cfg.repeat = 2
983 },
984 /* quite involved test (udp). Multithreads, parallel send/recv operations,
985 * limitation in recv buffer. max_fd is very limited to test management of
986 * closing keys in ioqueue safe unregistration.
987 */
988 {
989 .cfg.title = "udp (multithreads)",
990 .cfg.max_fd = 4,
991 .cfg.allow_concur = 1,
992 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
993 .cfg.sock_type = SOCK_DGRAM,
994 .cfg.n_threads = MAX_THREADS,
995 .cfg.rx_so_buf_size = MAX_THREADS,
996 .cfg.pkt_len = 4,
997 .cfg.tx_cnt = 4*8*512,
998 .cfg.rx_cnt = 4*8*512,
999 .cfg.n_servers = MAX_ASYNC,
1000 .cfg.n_clients = MAX_ASYNC,
1001 .cfg.repeat = 4
1002 },
1003 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
1004 /* EPOLLEXCLUSIVE (udp).
1005 */
1006 {
1007 .cfg.title = "udp (multithreads, EPOLLEXCLUSIVE)",
1008 .cfg.max_fd = 4,
1009 .cfg.allow_concur = 1,
1010 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_EXCLUSIVE,
1011 .cfg.sock_type = SOCK_DGRAM,
1012 .cfg.n_threads = MAX_THREADS,
1013 .cfg.rx_so_buf_size = MAX_THREADS,
1014 .cfg.pkt_len = 4,
1015 .cfg.tx_cnt = 4*8*512,
1016 .cfg.rx_cnt = 4*8*512,
1017 .cfg.n_servers = MAX_ASYNC,
1018 .cfg.n_clients = MAX_ASYNC,
1019 .cfg.repeat = 4
1020 },
1021 /* EPOLLONESHOT (udp).
1022 */
1023 {
1024 .cfg.title = "udp (multithreads, EPOLLONESHOT)",
1025 .cfg.max_fd = 4,
1026 .cfg.allow_concur = 1,
1027 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_ONESHOT,
1028 .cfg.sock_type = SOCK_DGRAM,
1029 .cfg.n_threads = 2,
1030 .cfg.rx_so_buf_size = 2,
1031 .cfg.pkt_len = 4,
1032 .cfg.tx_cnt = 4*8*512,
1033 .cfg.rx_cnt = 4*8*512,
1034 .cfg.n_servers = 2,
1035 .cfg.n_clients = 2,
1036 .cfg.repeat = 4
1037 },
1038 {
1039 .cfg.title = "udp (multithreads, plain epoll)",
1040 .cfg.max_fd = 4,
1041 .cfg.allow_concur = 1,
1042 .cfg.epoll_flags = 0,
1043 .cfg.sock_type = SOCK_DGRAM,
1044 .cfg.n_threads = 2,
1045 .cfg.rx_so_buf_size = 2,
1046 .cfg.pkt_len = 4,
1047 .cfg.tx_cnt = 4*8*512,
1048 .cfg.rx_cnt = 4*8*512,
1049 .cfg.n_servers = 2,
1050 .cfg.n_clients = 2,
1051 .cfg.repeat = 4
1052 },
1053 #endif
1054 /* quite involved test (tcp). Multithreads, parallel send/recv operations,
1055 * limitation in recv buffer. max_fd is small/limited to test management of
1056 * closing keys in ioqueue safe unregistration.
1057 */
1058 {
1059 .cfg.title = "tcp (multithreads)",
1060 .cfg.max_fd = 6,
1061 .cfg.allow_concur = 1,
1062 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
1063 .cfg.sock_type = SOCK_STREAM,
1064 .cfg.n_threads = MAX_THREADS,
1065 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1066 .cfg.tx_so_buf_size = MAX_ASYNC,
1067 .cfg.pkt_len = 4,
1068 .cfg.tx_cnt = 4*MAX_THREADS*512,
1069 .cfg.rx_cnt = 4*MAX_THREADS*512,
1070 .cfg.n_servers = MAX_ASYNC,
1071 .cfg.n_clients = MAX_ASYNC,
1072 .cfg.repeat = 4
1073 },
1074 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
1075 {
1076 .cfg.title = "tcp (multithreads, EPOLLEXCLUSIVE)",
1077 .cfg.max_fd = 6,
1078 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_EXCLUSIVE,
1079 .cfg.allow_concur = 1,
1080 .cfg.sock_type = SOCK_STREAM,
1081 .cfg.n_threads = MAX_THREADS,
1082 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1083 .cfg.tx_so_buf_size = MAX_ASYNC,
1084 .cfg.pkt_len = 4,
1085 .cfg.tx_cnt = 4*MAX_THREADS*512,
1086 .cfg.rx_cnt = 4*MAX_THREADS*512,
1087 .cfg.n_servers = MAX_ASYNC,
1088 .cfg.n_clients = MAX_ASYNC,
1089 .cfg.repeat = 4
1090 },
1091 {
1092 .cfg.title = "tcp (multithreads, EPOLLONESHOT)",
1093 .cfg.max_fd = 6,
1094 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_ONESHOT,
1095 .cfg.allow_concur = 1,
1096 .cfg.sock_type = SOCK_STREAM,
1097 .cfg.n_threads = MAX_THREADS,
1098 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1099 .cfg.tx_so_buf_size = MAX_ASYNC,
1100 .cfg.pkt_len = 4,
1101 .cfg.tx_cnt = 4*MAX_THREADS*512,
1102 .cfg.rx_cnt = 4*MAX_THREADS*512,
1103 .cfg.n_servers = MAX_ASYNC,
1104 .cfg.n_clients = MAX_ASYNC,
1105 .cfg.repeat = 4
1106 },
1107 {
1108 .cfg.title = "tcp (multithreads, plain epoll)",
1109 .cfg.max_fd = 6,
1110 .cfg.epoll_flags = 0,
1111 .cfg.allow_concur = 1,
1112 .cfg.sock_type = SOCK_STREAM,
1113 .cfg.n_threads = MAX_THREADS,
1114 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1115 .cfg.tx_so_buf_size = MAX_ASYNC,
1116 .cfg.pkt_len = 4,
1117 .cfg.tx_cnt = 4*MAX_THREADS*512,
1118 .cfg.rx_cnt = 4*MAX_THREADS*512,
1119 .cfg.n_servers = MAX_ASYNC,
1120 .cfg.n_clients = MAX_ASYNC,
1121 .cfg.repeat = 4
1122 },
1123 #endif
1124 /* when concurrency is disabled, TCP packets should be received in correct
1125 * order
1126 */
1127 {
1128 .cfg.title = "tcp (multithreads, sequenced, concur=0)",
1129 .cfg.max_fd = 6,
1130 .cfg.allow_concur = 0,
1131 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
1132 .cfg.sequenced = 1,
1133 .cfg.sock_type = SOCK_STREAM,
1134 .cfg.n_threads = MAX_THREADS,
1135 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1136 .cfg.tx_so_buf_size = MAX_ASYNC,
1137 .cfg.pkt_len = 4,
1138 .cfg.tx_cnt = 4*MAX_THREADS*512,
1139 .cfg.rx_cnt = 4*MAX_THREADS*512,
1140 .cfg.n_servers = MAX_ASYNC,
1141 .cfg.n_clients = MAX_ASYNC,
1142 .cfg.repeat = 4
1143 },
1144 #if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL
1145 {
1146 .cfg.title = "tcp (multithreads, sequenced, concur=0, EPOLLEXCLUSIVE)",
1147 .cfg.max_fd = 6,
1148 .cfg.allow_concur = 0,
1149 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_EXCLUSIVE,
1150 .cfg.sequenced = 1,
1151 .cfg.sock_type = SOCK_STREAM,
1152 .cfg.n_threads = MAX_THREADS,
1153 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1154 .cfg.tx_so_buf_size = MAX_ASYNC,
1155 .cfg.pkt_len = 4,
1156 .cfg.tx_cnt = 4*MAX_THREADS*512,
1157 .cfg.rx_cnt = 4*MAX_THREADS*512,
1158 .cfg.n_servers = MAX_ASYNC,
1159 .cfg.n_clients = MAX_ASYNC,
1160 .cfg.repeat = 4
1161 },
1162 {
1163 .cfg.title = "tcp (multithreads, sequenced, concur=0, EPOLLONESHOT)",
1164 .cfg.max_fd = 6,
1165 .cfg.allow_concur = 0,
1166 .cfg.epoll_flags = PJ_IOQUEUE_EPOLL_ONESHOT,
1167 .cfg.sequenced = 1,
1168 .cfg.sock_type = SOCK_STREAM,
1169 .cfg.n_threads = MAX_THREADS,
1170 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1171 .cfg.tx_so_buf_size = MAX_ASYNC,
1172 .cfg.pkt_len = 4,
1173 .cfg.tx_cnt = 4*MAX_THREADS*512,
1174 .cfg.rx_cnt = 4*MAX_THREADS*512,
1175 .cfg.n_servers = MAX_ASYNC,
1176 .cfg.n_clients = MAX_ASYNC,
1177 .cfg.repeat = 4
1178 },
1179 {
1180 .cfg.title = "tcp (multithreads, sequenced, concur=0, epoll plain)",
1181 .cfg.max_fd = 6,
1182 .cfg.allow_concur = 0,
1183 .cfg.epoll_flags = 0,
1184 .cfg.sequenced = 1,
1185 .cfg.sock_type = SOCK_STREAM,
1186 .cfg.n_threads = MAX_THREADS,
1187 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1188 .cfg.tx_so_buf_size = MAX_ASYNC,
1189 .cfg.pkt_len = 4,
1190 .cfg.tx_cnt = 4*MAX_THREADS*512,
1191 .cfg.rx_cnt = 4*MAX_THREADS*512,
1192 .cfg.n_servers = MAX_ASYNC,
1193 .cfg.n_clients = MAX_ASYNC,
1194 .cfg.repeat = 4
1195 },
1196 #endif
1197 /* when concurrency is *enabled*, TCP packets are received in correct
1198 * order (tested with epoll, not sure if this behavior is true for
1199 * all backends)
1200 */
1201 {
1202 .cfg.title = "tcp (multithreads, sequenced, concur=1)",
1203 .cfg.max_fd = 6,
1204 .cfg.allow_concur = 1, /* enabled */
1205 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
1206 .cfg.sequenced = 1,
1207 .cfg.sock_type = SOCK_STREAM,
1208 .cfg.n_threads = MAX_THREADS,
1209 .cfg.rx_so_buf_size = 2, /* Set to small to control flow of tcp */
1210 .cfg.tx_so_buf_size = MAX_ASYNC,
1211 .cfg.pkt_len = 4,
1212 .cfg.tx_cnt = 4*MAX_THREADS*512,
1213 .cfg.rx_cnt = 4*MAX_THREADS*512,
1214 .cfg.n_servers = MAX_ASYNC,
1215 .cfg.n_clients = MAX_ASYNC,
1216 .cfg.repeat = 4
1217 },
1218 /* when concurrency is disabled, UDP packets should be received in increasing
1219 * order (although some packets may be lost). Edit: it turns out packets
1220 * can be received out of order, hence sequence verification is disabled
1221 * in the callback (only warning will be printed).
1222 */
1223 {
1224 .cfg.title = "udp (multithreads, sequenced, concur=0)",
1225 .cfg.max_fd = 6,
1226 .cfg.allow_concur = 0,
1227 .cfg.epoll_flags = PJ_IOQUEUE_DEFAULT_EPOLL_FLAGS,
1228 .cfg.sequenced = 1,
1229 .cfg.sock_type = SOCK_DGRAM,
1230 .cfg.n_threads = MAX_THREADS,
1231 .cfg.rx_so_buf_size = MAX_ASYNC, /* Set to small so some packets will be dropped */
1232 .cfg.tx_so_buf_size = MAX_ASYNC,
1233 .cfg.pkt_len = 4,
1234 .cfg.tx_cnt = 4*MAX_THREADS*512,
1235 .cfg.rx_cnt = 4*MAX_THREADS*512,
1236 .cfg.n_servers = MAX_ASYNC,
1237 .cfg.n_clients = MAX_ASYNC,
1238 .cfg.repeat = 4
1239 },
1240 };
1241
1242 int ioqueue_stress_test(void)
1243 {
1244 int i;
1245 int retcode = 0;
1246
1247 pj_log_push_indent();
1248
1249 test_cb.on_read_complete = on_read_complete;
1250 test_cb.on_write_complete = on_write_complete;
1251 test_cb.on_accept_complete = on_accept_complete;
1252 test_cb.on_connect_complete = on_connect_complete;
1253
1254 for (i=0; i<(int)PJ_ARRAY_SIZE(tests); ++i) {
1255 int r;
1256
1257 test_desc *test = &tests[i];
1258 if (!test->cfg.title)
1259 break;
1260
1261 r = perform_test(&tests[i]);
1262 if (r && !retcode)
1263 retcode = r;
1264 }
1265
1266 pj_log_pop_indent();
1267 return retcode;
1268 }
1269