blob: ef50d048c56ec8da9b6c1bca828a93fbdaa320f3 [file] [log] [blame]
Benny Prijono5accbd02006-03-30 16:32:18 +00001/* $Id$ */
2/*
Benny Prijonoa771a512007-02-19 01:13:53 +00003 * Copyright (C)2003-2007 Benny Prijono <benny@prijono.org>
Benny Prijono5accbd02006-03-30 16:32:18 +00004 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19#include "test.h"
20
21#if INCLUDE_IOQUEUE_UNREG_TEST
22/*
23 * This tests the thread safety of ioqueue unregistration operation.
24 */
25
26#include <pj/errno.h>
27#include <pj/ioqueue.h>
28#include <pj/log.h>
29#include <pj/os.h>
30#include <pj/pool.h>
31#include <pj/sock.h>
32#include <pj/compat/socket.h>
33#include <pj/string.h>
34
35
36#define THIS_FILE "ioq_unreg.c"
37
38
39enum test_method
40{
41 UNREGISTER_IN_APP,
42 UNREGISTER_IN_CALLBACK,
43};
44
45static int thread_quitting;
46static enum test_method test_method;
47static pj_time_val time_to_unregister;
48
49struct sock_data
50{
51 pj_sock_t sock;
52 pj_sock_t csock;
53 pj_pool_t *pool;
54 pj_ioqueue_key_t *key;
55 pj_mutex_t *mutex;
56 pj_ioqueue_op_key_t *op_key;
57 char *buffer;
58 pj_size_t bufsize;
59 pj_bool_t unregistered;
60 unsigned received;
61} sock_data;
62
63static void on_read_complete(pj_ioqueue_key_t *key,
64 pj_ioqueue_op_key_t *op_key,
65 pj_ssize_t bytes_read)
66{
67 pj_ssize_t size;
68 char *sendbuf = "Hello world";
69 pj_status_t status;
70
71 if (sock_data.unregistered)
72 return;
73
74 pj_mutex_lock(sock_data.mutex);
75
76 if (sock_data.unregistered) {
77 /* No need to unlock. Mutex may have been destroyed */
78 return;
79 }
80
81 if (bytes_read < 0) {
Benny Prijonoc5b6dbf2006-09-10 16:33:48 +000082 if (-bytes_read != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL))
Benny Prijono5accbd02006-03-30 16:32:18 +000083 app_perror("ioqueue reported recv error", -bytes_read);
84 } else {
85 sock_data.received += bytes_read;
86 }
87
88 if (test_method == UNREGISTER_IN_CALLBACK) {
89 pj_time_val now;
90
91 pj_gettimeofday(&now);
92 if (PJ_TIME_VAL_GTE(now, time_to_unregister)) {
93 sock_data.unregistered = 1;
94 pj_ioqueue_unregister(key);
95 pj_mutex_destroy(sock_data.mutex);
96 pj_pool_release(sock_data.pool);
97 sock_data.pool = NULL;
98 return;
99 }
100 }
101
102 do {
103 size = sock_data.bufsize;
104 status = pj_ioqueue_recv(key, op_key, sock_data.buffer, &size, 0);
105 if (status != PJ_EPENDING && status != PJ_SUCCESS)
106 app_perror("recv() error", status);
107
108 } while (status == PJ_SUCCESS);
109
110 pj_mutex_unlock(sock_data.mutex);
111
112 size = pj_ansi_strlen(sendbuf);
113 status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
114 if (status != PJ_SUCCESS)
115 app_perror("send() error", status);
116
117 size = pj_ansi_strlen(sendbuf);
118 status = pj_sock_send(sock_data.csock, sendbuf, &size, 0);
119 if (status != PJ_SUCCESS)
120 app_perror("send() error", status);
121
122}
123
124static int worker_thread(void *arg)
125{
Benny Prijonof260e462007-04-30 21:03:32 +0000126 pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg;
Benny Prijono5accbd02006-03-30 16:32:18 +0000127
128 while (!thread_quitting) {
129 pj_time_val timeout = { 0, 20 };
130 pj_ioqueue_poll(ioqueue, &timeout);
131 }
132
133 return 0;
134}
135
136/*
137 * Perform unregistration test.
138 *
139 * This will create ioqueue and register a server socket. Depending
140 * on the test method, either the callback or the main thread will
141 * unregister and destroy the server socket after some period of time.
142 */
143static int perform_unreg_test(pj_ioqueue_t *ioqueue,
144 pj_pool_t *test_pool,
145 const char *title,
146 pj_bool_t other_socket)
147{
148 enum { WORKER_CNT = 1, MSEC = 500, QUIT_MSEC = 500 };
149 int i;
150 pj_thread_t *thread[WORKER_CNT];
151 struct sock_data osd;
152 pj_ioqueue_callback callback;
153 pj_time_val end_time;
154 pj_status_t status;
155
156
157 /* Sometimes its important to have other sockets registered to
158 * the ioqueue, because when no sockets are registered, the ioqueue
159 * will return from the poll early.
160 */
161 if (other_socket) {
162 status = app_socket(PJ_AF_INET, PJ_SOCK_DGRAM, 0, 56127, &osd.sock);
163 if (status != PJ_SUCCESS) {
164 app_perror("Error creating other socket", status);
165 return -12;
166 }
167
Benny Prijonoac623b32006-07-03 15:19:31 +0000168 pj_bzero(&callback, sizeof(callback));
Benny Prijono5accbd02006-03-30 16:32:18 +0000169 status = pj_ioqueue_register_sock(test_pool, ioqueue, osd.sock,
170 NULL, &callback, &osd.key);
171 if (status != PJ_SUCCESS) {
172 app_perror("Error registering other socket", status);
173 return -13;
174 }
175
176 } else {
177 osd.key = NULL;
178 osd.sock = PJ_INVALID_SOCKET;
179 }
180
181 /* Init both time duration of testing */
182 thread_quitting = 0;
183 pj_gettimeofday(&time_to_unregister);
184 time_to_unregister.msec += MSEC;
185 pj_time_val_normalize(&time_to_unregister);
186
187 end_time = time_to_unregister;
188 end_time.msec += QUIT_MSEC;
189 pj_time_val_normalize(&end_time);
190
191
192 /* Create polling thread */
193 for (i=0; i<WORKER_CNT; ++i) {
194 status = pj_thread_create(test_pool, "unregtest", &worker_thread,
195 ioqueue, 0, 0, &thread[i]);
196 if (status != PJ_SUCCESS) {
197 app_perror("Error creating thread", status);
198 return -20;
199 }
200 }
201
202 /* Create pair of client/server sockets */
203 status = app_socketpair(PJ_AF_INET, PJ_SOCK_DGRAM, 0,
204 &sock_data.sock, &sock_data.csock);
205 if (status != PJ_SUCCESS) {
206 app_perror("app_socketpair error", status);
207 return -30;
208 }
209
210
211 /* Initialize test data */
212 sock_data.pool = pj_pool_create(mem, "sd", 1000, 1000, NULL);
Benny Prijonof260e462007-04-30 21:03:32 +0000213 sock_data.buffer = (char*) pj_pool_alloc(sock_data.pool, 128);
Benny Prijono5accbd02006-03-30 16:32:18 +0000214 sock_data.bufsize = 128;
Benny Prijonof260e462007-04-30 21:03:32 +0000215 sock_data.op_key = (pj_ioqueue_op_key_t*)
216 pj_pool_alloc(sock_data.pool,
Benny Prijono5accbd02006-03-30 16:32:18 +0000217 sizeof(*sock_data.op_key));
218 sock_data.received = 0;
219 sock_data.unregistered = 0;
220
221 pj_ioqueue_op_key_init(sock_data.op_key, sizeof(*sock_data.op_key));
222
223 status = pj_mutex_create_simple(sock_data.pool, "sd", &sock_data.mutex);
224 if (status != PJ_SUCCESS) {
225 app_perror("create_mutex() error", status);
226 return -35;
227 }
228
229 /* Register socket to ioqueue */
Benny Prijonoac623b32006-07-03 15:19:31 +0000230 pj_bzero(&callback, sizeof(callback));
Benny Prijono5accbd02006-03-30 16:32:18 +0000231 callback.on_read_complete = &on_read_complete;
232 status = pj_ioqueue_register_sock(sock_data.pool, ioqueue, sock_data.sock,
233 NULL, &callback, &sock_data.key);
234 if (status != PJ_SUCCESS) {
235 app_perror("pj_ioqueue_register error", status);
236 return -40;
237 }
238
239 /* Bootstrap the first send/receive */
240 on_read_complete(sock_data.key, sock_data.op_key, 0);
241
242 /* Loop until test time ends */
243 for (;;) {
244 pj_time_val now, timeout;
245
246 pj_gettimeofday(&now);
247
248 if (test_method == UNREGISTER_IN_APP &&
249 PJ_TIME_VAL_GTE(now, time_to_unregister) &&
250 sock_data.pool)
251 {
252 pj_mutex_lock(sock_data.mutex);
253
254 sock_data.unregistered = 1;
255 pj_ioqueue_unregister(sock_data.key);
256 pj_mutex_destroy(sock_data.mutex);
257 pj_pool_release(sock_data.pool);
258 sock_data.pool = NULL;
259 }
260
261 if (PJ_TIME_VAL_GT(now, end_time) && sock_data.unregistered)
262 break;
263
264 timeout.sec = 0; timeout.msec = 10;
265 pj_ioqueue_poll(ioqueue, &timeout);
266 //pj_thread_sleep(1);
267
268 }
269
270 thread_quitting = 1;
271
272 for (i=0; i<WORKER_CNT; ++i) {
273 pj_thread_join(thread[i]);
274 pj_thread_destroy(thread[i]);
275 }
276
277 if (other_socket) {
278 pj_ioqueue_unregister(osd.key);
279 }
280
281 pj_sock_close(sock_data.csock);
282
283 PJ_LOG(3,(THIS_FILE, "....%s: done (%d KB/s)",
284 title, sock_data.received * 1000 / MSEC / 1000));
285 return 0;
286}
287
288int udp_ioqueue_unreg_test(void)
289{
290 enum { LOOP = 10 };
291 int i, rc;
292 char title[30];
293 pj_ioqueue_t *ioqueue;
294 pj_pool_t *test_pool;
295
296 test_method = UNREGISTER_IN_APP;
297
298 test_pool = pj_pool_create(mem, "unregtest", 4000, 4000, NULL);
299
300 rc = pj_ioqueue_create(test_pool, 16, &ioqueue);
301 if (rc != PJ_SUCCESS) {
302 app_perror("Error creating ioqueue", rc);
303 return -10;
304 }
305
306
307 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 0/3 (%s)",
308 pj_ioqueue_name()));
309 for (i=0; i<LOOP; ++i) {
310 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
311 rc = perform_unreg_test(ioqueue, test_pool, title, 0);
312 if (rc != 0)
313 return rc;
314 }
315
316
317 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 1/3 (%s)",
318 pj_ioqueue_name()));
319 for (i=0; i<LOOP; ++i) {
320 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
321 rc = perform_unreg_test(ioqueue, test_pool, title, 1);
322 if (rc != 0)
323 return rc;
324 }
325
326 test_method = UNREGISTER_IN_CALLBACK;
327
328 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 2/3 (%s)",
329 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, 0);
333 if (rc != 0)
334 return rc;
335 }
336
337
338 PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 3/3 (%s)",
339 pj_ioqueue_name()));
340 for (i=0; i<LOOP; ++i) {
341 pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP);
342 rc = perform_unreg_test(ioqueue, test_pool, title, 1);
343 if (rc != 0)
344 return rc;
345 }
346
347 pj_ioqueue_destroy(ioqueue);
348 pj_pool_release(test_pool);
349
350 return 0;
351}
352
353
354
355#else
356/* To prevent warning about "translation unit is empty"
357 * when this test is disabled.
358 */
359int dummy_uiq_unreg;
360#endif /* INCLUDE_IOQUEUE_UNREG_TEST */
361
362