blob: ab47c7949a77e533235c62b7effca8c26979e308 [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);
96 pj_mutex_destroy(sock_data.mutex);
97 pj_pool_release(sock_data.pool);
98 sock_data.pool = NULL;
99 return;
100 }
101 }
102
103 do {
104 size = sock_data.bufsize;
105 status = pj_ioqueue_recv(key, op_key, sock_data.buffer, &size, 0);
106 if (status != PJ_EPENDING && status != PJ_SUCCESS)
107 app_perror("recv() error", status);
108
109 } while (status == PJ_SUCCESS);
110
111 pj_mutex_unlock(sock_data.mutex);
112
113 size = pj_ansi_strlen(sendbuf);
114 status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
115 if (status != PJ_SUCCESS)
116 app_perror("send() error", status);
117
118 size = pj_ansi_strlen(sendbuf);
119 status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
120 if (status != PJ_SUCCESS)
121 app_perror("send() error", status);
122
123}
124
125static int worker_thread(void *arg)
126{
Benny Prijonof260e462007-04-30 21:03:32 +0000127 pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg;
Benny Prijono5accbd02006-03-30 16:32:18 +0000128
129 while (!thread_quitting) {
130 pj_time_val timeout = { 0, 20 };
131 pj_ioqueue_poll(ioqueue, &timeout);
132 }
133
134 return 0;
135}
136
137/*
138 * Perform unregistration test.
139 *
140 * This will create ioqueue and register a server socket. Depending
141 * on the test method, either the callback or the main thread will
142 * unregister and destroy the server socket after some period of time.
143 */
144static int perform_unreg_test(pj_ioqueue_t *ioqueue,
145 pj_pool_t *test_pool,
146 const char *title,
147 pj_bool_t other_socket)
148{
149 enum { WORKER_CNT = 1, MSEC = 500, QUIT_MSEC = 500 };
150 int i;
151 pj_thread_t *thread[WORKER_CNT];
152 struct sock_data osd;
153 pj_ioqueue_callback callback;
154 pj_time_val end_time;
155 pj_status_t status;
156
157
158 /* Sometimes its important to have other sockets registered to
159 * the ioqueue, because when no sockets are registered, the ioqueue
160 * will return from the poll early.
161 */
162 if (other_socket) {
Benny Prijono8ab968f2007-07-20 08:08:30 +0000163 status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 56127, &osd.sock);
Benny Prijono5accbd02006-03-30 16:32:18 +0000164 if (status != PJ_SUCCESS) {
165 app_perror("Error creating other socket", status);
166 return -12;
167 }
168
Benny Prijonoac623b32006-07-03 15:19:31 +0000169 pj_bzero(&callback, sizeof(callback));
Benny Prijono5accbd02006-03-30 16:32:18 +0000170 status = pj_ioqueue_register_sock(test_pool, ioqueue, osd.sock,
171 NULL, &callback, &osd.key);
172 if (status != PJ_SUCCESS) {
173 app_perror("Error registering other socket", status);
174 return -13;
175 }
176
177 } else {
178 osd.key = NULL;
179 osd.sock = PJ_INVALID_SOCKET;
180 }
181
182 /* Init both time duration of testing */
183 thread_quitting = 0;
184 pj_gettimeofday(&time_to_unregister);
185 time_to_unregister.msec += MSEC;
186 pj_time_val_normalize(&time_to_unregister);
187
188 end_time = time_to_unregister;
189 end_time.msec += QUIT_MSEC;
190 pj_time_val_normalize(&end_time);
191
192
193 /* Create polling thread */
194 for (i=0; i<WORKER_CNT; ++i) {
195 status = pj_thread_create(test_pool, "unregtest", &worker_thread,
196 ioqueue, 0, 0, &thread[i]);
197 if (status != PJ_SUCCESS) {
198 app_perror("Error creating thread", status);
199 return -20;
200 }
201 }
202
203 /* Create pair of client/server sockets */
Benny Prijono8ab968f2007-07-20 08:08:30 +0000204 status = app_socketpair(pj_AF_INET(), pj_SOCK_DGRAM(), 0,
Benny Prijono5accbd02006-03-30 16:32:18 +0000205 &sock_data.sock, &sock_data.csock);
206 if (status != PJ_SUCCESS) {
207 app_perror("app_socketpair error", status);
208 return -30;
209 }
210
211
212 /* Initialize test data */
213 sock_data.pool = pj_pool_create(mem, "sd", 1000, 1000, NULL);
Benny Prijonof260e462007-04-30 21:03:32 +0000214 sock_data.buffer = (char*) pj_pool_alloc(sock_data.pool, 128);
Benny Prijono5accbd02006-03-30 16:32:18 +0000215 sock_data.bufsize = 128;
Benny Prijonof260e462007-04-30 21:03:32 +0000216 sock_data.op_key = (pj_ioqueue_op_key_t*)
217 pj_pool_alloc(sock_data.pool,
Benny Prijono5accbd02006-03-30 16:32:18 +0000218 sizeof(*sock_data.op_key));
219 sock_data.received = 0;
220 sock_data.unregistered = 0;
221
222 pj_ioqueue_op_key_init(sock_data.op_key, sizeof(*sock_data.op_key));
223
224 status = pj_mutex_create_simple(sock_data.pool, "sd", &sock_data.mutex);
225 if (status != PJ_SUCCESS) {
226 app_perror("create_mutex() error", status);
227 return -35;
228 }
229
230 /* Register socket to ioqueue */
Benny Prijonoac623b32006-07-03 15:19:31 +0000231 pj_bzero(&callback, sizeof(callback));
Benny Prijono5accbd02006-03-30 16:32:18 +0000232 callback.on_read_complete = &on_read_complete;
233 status = pj_ioqueue_register_sock(sock_data.pool, ioqueue, sock_data.sock,
234 NULL, &callback, &sock_data.key);
235 if (status != PJ_SUCCESS) {
236 app_perror("pj_ioqueue_register error", status);
237 return -40;
238 }
239
240 /* Bootstrap the first send/receive */
241 on_read_complete(sock_data.key, sock_data.op_key, 0);
242
243 /* Loop until test time ends */
244 for (;;) {
245 pj_time_val now, timeout;
246
247 pj_gettimeofday(&now);
248
249 if (test_method == UNREGISTER_IN_APP &&
250 PJ_TIME_VAL_GTE(now, time_to_unregister) &&
251 sock_data.pool)
252 {
253 pj_mutex_lock(sock_data.mutex);
254
255 sock_data.unregistered = 1;
256 pj_ioqueue_unregister(sock_data.key);
Benny Prijonoa0886c92007-05-28 12:10:35 +0000257 pj_mutex_unlock(sock_data.mutex);
Benny Prijono5accbd02006-03-30 16:32:18 +0000258 pj_mutex_destroy(sock_data.mutex);
259 pj_pool_release(sock_data.pool);
260 sock_data.pool = NULL;
261 }
262
263 if (PJ_TIME_VAL_GT(now, end_time) && sock_data.unregistered)
264 break;
265
266 timeout.sec = 0; timeout.msec = 10;
267 pj_ioqueue_poll(ioqueue, &timeout);
268 //pj_thread_sleep(1);
269
270 }
271
272 thread_quitting = 1;
273
274 for (i=0; i<WORKER_CNT; ++i) {
275 pj_thread_join(thread[i]);
276 pj_thread_destroy(thread[i]);
277 }
278
279 if (other_socket) {
280 pj_ioqueue_unregister(osd.key);
281 }
282
283 pj_sock_close(sock_data.csock);
284
285 PJ_LOG(3,(THIS_FILE, "....%s: done (%d KB/s)",
286 title, sock_data.received * 1000 / MSEC / 1000));
287 return 0;
288}
289
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000290static int udp_ioqueue_unreg_test_imp(pj_bool_t allow_concur)
Benny Prijono5accbd02006-03-30 16:32:18 +0000291{
292 enum { LOOP = 10 };
293 int i, rc;
294 char title[30];
295 pj_ioqueue_t *ioqueue;
296 pj_pool_t *test_pool;
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000297
298 PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur));
299
Benny Prijono5accbd02006-03-30 16:32:18 +0000300 test_method = UNREGISTER_IN_APP;
301
302 test_pool = pj_pool_create(mem, "unregtest", 4000, 4000, NULL);
303
304 rc = pj_ioqueue_create(test_pool, 16, &ioqueue);
305 if (rc != PJ_SUCCESS) {
306 app_perror("Error creating ioqueue", rc);
307 return -10;
308 }
309
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000310 rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur);
311 if (rc != PJ_SUCCESS) {
312 app_perror("Error in pj_ioqueue_set_default_concurrency()", rc);
313 return -12;
314 }
Benny Prijono5accbd02006-03-30 16:32:18 +0000315
316 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 0/3 (%s)",
317 pj_ioqueue_name()));
318 for (i=0; i<LOOP; ++i) {
319 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
320 rc = perform_unreg_test(ioqueue, test_pool, title, 0);
321 if (rc != 0)
322 return rc;
323 }
324
325
326 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 1/3 (%s)",
327 pj_ioqueue_name()));
328 for (i=0; i<LOOP; ++i) {
329 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
330 rc = perform_unreg_test(ioqueue, test_pool, title, 1);
331 if (rc != 0)
332 return rc;
333 }
334
335 test_method = UNREGISTER_IN_CALLBACK;
336
337 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 2/3 (%s)",
338 pj_ioqueue_name()));
339 for (i=0; i<LOOP; ++i) {
340 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
341 rc = perform_unreg_test(ioqueue, test_pool, title, 0);
342 if (rc != 0)
343 return rc;
344 }
345
346
347 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 3/3 (%s)",
348 pj_ioqueue_name()));
349 for (i=0; i<LOOP; ++i) {
350 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
351 rc = perform_unreg_test(ioqueue, test_pool, title, 1);
352 if (rc != 0)
353 return rc;
354 }
355
356 pj_ioqueue_destroy(ioqueue);
357 pj_pool_release(test_pool);
358
359 return 0;
360}
361
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000362int udp_ioqueue_unreg_test(void)
363{
364 int rc;
Benny Prijono5accbd02006-03-30 16:32:18 +0000365
Benny Prijonoe3f79fd2008-02-13 15:17:28 +0000366 rc = udp_ioqueue_unreg_test_imp(PJ_TRUE);
367 if (rc != 0)
368 return rc;
369
370 rc = udp_ioqueue_unreg_test_imp(PJ_FALSE);
371 if (rc != 0)
372 return rc;
373
374 return 0;
375}
Benny Prijono5accbd02006-03-30 16:32:18 +0000376
377#else
378/* To prevent warning about "translation unit is empty"
379 * when this test is disabled.
380 */
381int dummy_uiq_unreg;
382#endif /* INCLUDE_IOQUEUE_UNREG_TEST */
383
384