blob: 6b19ed1f3df6c8355f7c0b7e549709ccfba39557 [file] [log] [blame]
Benny Prijono5accbd02006-03-30 16:32:18 +00001/* $Id$ */
2/*
Benny Prijono844653c2008-12-23 17:27:53 +00003 * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com)
4 * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org>
Benny Prijono5accbd02006-03-30 16:32:18 +00005 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20#include "test.h"
21
22#if INCLUDE_IOQUEUE_UNREG_TEST
23/*
24 * This tests the thread safety of ioqueue unregistration operation.
25 */
26
27#include <pj/errno.h>
28#include <pj/ioqueue.h>
29#include <pj/log.h>
30#include <pj/os.h>
31#include <pj/pool.h>
32#include <pj/sock.h>
33#include <pj/compat/socket.h>
34#include <pj/string.h>
35
36
37#define THIS_FILE "ioq_unreg.c"
38
39
40enum test_method
41{
42 UNREGISTER_IN_APP,
43 UNREGISTER_IN_CALLBACK,
44};
45
46static int thread_quitting;
47static enum test_method test_method;
48static pj_time_val time_to_unregister;
49
50struct sock_data
51{
52 pj_sock_t sock;
53 pj_sock_t csock;
54 pj_pool_t *pool;
55 pj_ioqueue_key_t *key;
56 pj_mutex_t *mutex;
57 pj_ioqueue_op_key_t *op_key;
58 char *buffer;
59 pj_size_t bufsize;
60 pj_bool_t unregistered;
61 unsigned received;
62} sock_data;
63
64static void on_read_complete(pj_ioqueue_key_t *key,
65 pj_ioqueue_op_key_t *op_key,
66 pj_ssize_t bytes_read)
67{
68 pj_ssize_t size;
69 char *sendbuf = "Hello world";
70 pj_status_t status;
71
72 if (sock_data.unregistered)
73 return;
74
75 pj_mutex_lock(sock_data.mutex);
76
77 if (sock_data.unregistered) {
Benny Prijonoa0886c92007-05-28 12:10:35 +000078 pj_mutex_unlock(sock_data.mutex);
Benny Prijono5accbd02006-03-30 16:32:18 +000079 return;
80 }
81
82 if (bytes_read < 0) {
Benny Prijonoc5b6dbf2006-09-10 16:33:48 +000083 if (-bytes_read != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL))
Benny Prijono5accbd02006-03-30 16:32:18 +000084 app_perror("ioqueue reported recv error", -bytes_read);
85 } else {
86 sock_data.received += bytes_read;
87 }
88
89 if (test_method == UNREGISTER_IN_CALLBACK) {
90 pj_time_val now;
91
92 pj_gettimeofday(&now);
93 if (PJ_TIME_VAL_GTE(now, time_to_unregister)) {
94 sock_data.unregistered = 1;
95 pj_ioqueue_unregister(key);
Benny Prijono5accbd02006-03-30 16:32:18 +000096 return;
97 }
98 }
99
100 do {
101 size = sock_data.bufsize;
102 status = pj_ioqueue_recv(key, op_key, sock_data.buffer, &size, 0);
103 if (status != PJ_EPENDING && status != PJ_SUCCESS)
104 app_perror("recv() error", status);
105
106 } while (status == PJ_SUCCESS);
107
108 pj_mutex_unlock(sock_data.mutex);
109
110 size = pj_ansi_strlen(sendbuf);
111 status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
112 if (status != PJ_SUCCESS)
113 app_perror("send() error", status);
114
115 size = pj_ansi_strlen(sendbuf);
116 status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
117 if (status != PJ_SUCCESS)
118 app_perror("send() error", status);
119
120}
121
122static int worker_thread(void *arg)
123{
Benny Prijonof260e462007-04-30 21:03:32 +0000124 pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg;
Benny Prijono5accbd02006-03-30 16:32:18 +0000125
126 while (!thread_quitting) {
127 pj_time_val timeout = { 0, 20 };
128 pj_ioqueue_poll(ioqueue, &timeout);
129 }
130
131 return 0;
132}
133
134/*
135 * Perform unregistration test.
136 *
137 * This will create ioqueue and register a server socket. Depending
138 * on the test method, either the callback or the main thread will
139 * unregister and destroy the server socket after some period of time.
140 */
141static int perform_unreg_test(pj_ioqueue_t *ioqueue,
142 pj_pool_t *test_pool,
143 const char *title,
144 pj_bool_t other_socket)
145{
146 enum { WORKER_CNT = 1, MSEC = 500, QUIT_MSEC = 500 };
147 int i;
148 pj_thread_t *thread[WORKER_CNT];
149 struct sock_data osd;
150 pj_ioqueue_callback callback;
151 pj_time_val end_time;
152 pj_status_t status;
153
154
155 /* Sometimes its important to have other sockets registered to
156 * the ioqueue, because when no sockets are registered, the ioqueue
157 * will return from the poll early.
158 */
159 if (other_socket) {
Benny Prijono8ab968f2007-07-20 08:08:30 +0000160 status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 56127, &osd.sock);
Benny Prijono5accbd02006-03-30 16:32:18 +0000161 if (status != PJ_SUCCESS) {
162 app_perror("Error creating other socket", status);
163 return -12;
164 }
165
Benny Prijonoac623b32006-07-03 15:19:31 +0000166 pj_bzero(&callback, sizeof(callback));
Benny Prijono5accbd02006-03-30 16:32:18 +0000167 status = pj_ioqueue_register_sock(test_pool, ioqueue, osd.sock,
168 NULL, &callback, &osd.key);
169 if (status != PJ_SUCCESS) {
170 app_perror("Error registering other socket", status);
171 return -13;
172 }
173
174 } else {
175 osd.key = NULL;
176 osd.sock = PJ_INVALID_SOCKET;
177 }
178
179 /* Init both time duration of testing */
180 thread_quitting = 0;
181 pj_gettimeofday(&time_to_unregister);
182 time_to_unregister.msec += MSEC;
183 pj_time_val_normalize(&time_to_unregister);
184
185 end_time = time_to_unregister;
186 end_time.msec += QUIT_MSEC;
187 pj_time_val_normalize(&end_time);
188
189
190 /* Create polling thread */
191 for (i=0; i<WORKER_CNT; ++i) {
192 status = pj_thread_create(test_pool, "unregtest", &worker_thread,
193 ioqueue, 0, 0, &thread[i]);
194 if (status != PJ_SUCCESS) {
195 app_perror("Error creating thread", status);
196 return -20;
197 }
198 }
199
200 /* Create pair of client/server sockets */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000201 status = app_socketpair(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
Benny Prijono5accbd02006-03-30 16:32:18 +0000202 &sock_data.sock, &sock_data.csock);
203 if (status != PJ_SUCCESS) {
204 app_perror("app_socketpair error", status);
205 return -30;
206 }
207
208
209 /* Initialize test data */
210 sock_data.pool = pj_pool_create(mem, "sd", 1000, 1000, NULL);
Benny Prijonof260e462007-04-30 21:03:32 +0000211 sock_data.buffer = (char*) pj_pool_alloc(sock_data.pool, 128);
Benny Prijono5accbd02006-03-30 16:32:18 +0000212 sock_data.bufsize = 128;
Benny Prijonof260e462007-04-30 21:03:32 +0000213 sock_data.op_key = (pj_ioqueue_op_key_t*)
214 pj_pool_alloc(sock_data.pool,
Benny Prijono5accbd02006-03-30 16:32:18 +0000215 sizeof(*sock_data.op_key));
216 sock_data.received = 0;
217 sock_data.unregistered = 0;
218
219 pj_ioqueue_op_key_init(sock_data.op_key, sizeof(*sock_data.op_key));
220
221 status = pj_mutex_create_simple(sock_data.pool, "sd", &sock_data.mutex);
222 if (status != PJ_SUCCESS) {
223 app_perror("create_mutex() error", status);
224 return -35;
225 }
226
227 /* Register socket to ioqueue */
Benny Prijonoac623b32006-07-03 15:19:31 +0000228 pj_bzero(&callback, sizeof(callback));
Benny Prijono5accbd02006-03-30 16:32:18 +0000229 callback.on_read_complete = &on_read_complete;
230 status = pj_ioqueue_register_sock(sock_data.pool, ioqueue, sock_data.sock,
231 NULL, &callback, &sock_data.key);
232 if (status != PJ_SUCCESS) {
233 app_perror("pj_ioqueue_register error", status);
234 return -40;
235 }
236
237 /* Bootstrap the first send/receive */
238 on_read_complete(sock_data.key, sock_data.op_key, 0);
239
240 /* Loop until test time ends */
241 for (;;) {
242 pj_time_val now, timeout;
Benny Prijono25cb51d2009-07-02 08:24:22 +0000243 int n;
Benny Prijono5accbd02006-03-30 16:32:18 +0000244
245 pj_gettimeofday(&now);
246
247 if (test_method == UNREGISTER_IN_APP &&
248 PJ_TIME_VAL_GTE(now, time_to_unregister) &&
Benny Prijono25cb51d2009-07-02 08:24:22 +0000249 !sock_data.unregistered)
Benny Prijono5accbd02006-03-30 16:32:18 +0000250 {
Benny Prijono5accbd02006-03-30 16:32:18 +0000251 sock_data.unregistered = 1;
Benny Prijono25cb51d2009-07-02 08:24:22 +0000252 /* Wait (as much as possible) for callback to complete */
253 pj_mutex_lock(sock_data.mutex);
254 pj_mutex_unlock(sock_data.mutex);
Benny Prijono5accbd02006-03-30 16:32:18 +0000255 pj_ioqueue_unregister(sock_data.key);
Benny Prijono5accbd02006-03-30 16:32:18 +0000256 }
257
258 if (PJ_TIME_VAL_GT(now, end_time) && sock_data.unregistered)
259 break;
260
261 timeout.sec = 0; timeout.msec = 10;
Benny Prijono25cb51d2009-07-02 08:24:22 +0000262 n = pj_ioqueue_poll(ioqueue, &timeout);
263 if (n < 0) {
264 app_perror("pj_ioqueue_poll error", -n);
265 pj_thread_sleep(1);
266 }
Benny Prijono5accbd02006-03-30 16:32:18 +0000267 }
268
269 thread_quitting = 1;
270
271 for (i=0; i<WORKER_CNT; ++i) {
272 pj_thread_join(thread[i]);
273 pj_thread_destroy(thread[i]);
274 }
275
Benny Prijono25cb51d2009-07-02 08:24:22 +0000276 /* Destroy data */
277 pj_mutex_destroy(sock_data.mutex);
278 pj_pool_release(sock_data.pool);
279 sock_data.pool = NULL;
280
Benny Prijono5accbd02006-03-30 16:32:18 +0000281 if (other_socket) {
282 pj_ioqueue_unregister(osd.key);
283 }
284
285 pj_sock_close(sock_data.csock);
286
287 PJ_LOG(3,(THIS_FILE, "....%s: done (%d KB/s)",
288 title, sock_data.received * 1000 / MSEC / 1000));
289 return 0;
290}
291
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000292static int udp_ioqueue_unreg_test_imp(pj_bool_t allow_concur)
Benny Prijono5accbd02006-03-30 16:32:18 +0000293{
294 enum { LOOP = 10 };
295 int i, rc;
296 char title[30];
297 pj_ioqueue_t *ioqueue;
298 pj_pool_t *test_pool;
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000299
300 PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur));
301
Benny Prijono5accbd02006-03-30 16:32:18 +0000302 test_method = UNREGISTER_IN_APP;
303
304 test_pool = pj_pool_create(mem, "unregtest", 4000, 4000, NULL);
305
306 rc = pj_ioqueue_create(test_pool, 16, &ioqueue);
307 if (rc != PJ_SUCCESS) {
308 app_perror("Error creating ioqueue", rc);
309 return -10;
310 }
311
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000312 rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
313 if (rc != PJ_SUCCESS) {
314 app_perror("Error in pj_ioqueue_set_default_concurrency()", rc);
315 return -12;
316 }
Benny Prijono5accbd02006-03-30 16:32:18 +0000317
Benny Prijono25cb51d2009-07-02 08:24:22 +0000318 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 0/3, unregister in app (%s)",
Benny Prijono5accbd02006-03-30 16:32:18 +0000319 pj_ioqueue_name()));
320 for (i=0; i<LOOP; ++i) {
321 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
322 rc = perform_unreg_test(ioqueue, test_pool, title, 0);
323 if (rc != 0)
324 return rc;
325 }
326
327
Benny Prijono25cb51d2009-07-02 08:24:22 +0000328 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 1/3, unregister in app (%s)",
Benny Prijono5accbd02006-03-30 16:32:18 +0000329 pj_ioqueue_name()));
330 for (i=0; i<LOOP; ++i) {
331 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
332 rc = perform_unreg_test(ioqueue, test_pool, title, 1);
333 if (rc != 0)
334 return rc;
335 }
336
337 test_method = UNREGISTER_IN_CALLBACK;
338
Benny Prijono25cb51d2009-07-02 08:24:22 +0000339 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 2/3, unregister in cb (%s)",
Benny Prijono5accbd02006-03-30 16:32:18 +0000340 pj_ioqueue_name()));
341 for (i=0; i<LOOP; ++i) {
342 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
343 rc = perform_unreg_test(ioqueue, test_pool, title, 0);
344 if (rc != 0)
345 return rc;
346 }
347
348
Benny Prijono25cb51d2009-07-02 08:24:22 +0000349 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 3/3, unregister in cb (%s)",
Benny Prijono5accbd02006-03-30 16:32:18 +0000350 pj_ioqueue_name()));
351 for (i=0; i<LOOP; ++i) {
352 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
353 rc = perform_unreg_test(ioqueue, test_pool, title, 1);
354 if (rc != 0)
355 return rc;
356 }
357
358 pj_ioqueue_destroy(ioqueue);
359 pj_pool_release(test_pool);
360
361 return 0;
362}
363
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000364int udp_ioqueue_unreg_test(void)
365{
366 int rc;
Benny Prijono5accbd02006-03-30 16:32:18 +0000367
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000368 rc = udp_ioqueue_unreg_test_imp(PJ_TRUE);
369 if (rc != 0)
Benny Prijono25cb51d2009-07-02 08:24:22 +0000370 return rc;
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000371
372 rc = udp_ioqueue_unreg_test_imp(PJ_FALSE);
373 if (rc != 0)
374 return rc;
375
376 return 0;
377}
Benny Prijono5accbd02006-03-30 16:32:18 +0000378
379#else
380/* To prevent warning about "translation unit is empty"
381 * when this test is disabled.
382 */
383int dummy_uiq_unreg;
384#endif /* INCLUDE_IOQUEUE_UNREG_TEST */
385
386