Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
| 2 | /* |
Benny Prijono | 844653c | 2008-12-23 17:27:53 +0000 | [diff] [blame] | 3 | * Copyright (C) 2008-2009 Teluu Inc. (http://www.teluu.com) |
| 4 | * Copyright (C) 2003-2008 Benny Prijono <benny@prijono.org> |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 5 | * |
| 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 | |
| 40 | enum test_method |
| 41 | { |
| 42 | UNREGISTER_IN_APP, |
| 43 | UNREGISTER_IN_CALLBACK, |
| 44 | }; |
| 45 | |
| 46 | static int thread_quitting; |
| 47 | static enum test_method test_method; |
| 48 | static pj_time_val time_to_unregister; |
| 49 | |
| 50 | struct 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 | |
| 64 | static 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 Prijono | a0886c9 | 2007-05-28 12:10:35 +0000 | [diff] [blame] | 78 | pj_mutex_unlock(sock_data.mutex); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 79 | return; |
| 80 | } |
| 81 | |
| 82 | if (bytes_read < 0) { |
Benny Prijono | c5b6dbf | 2006-09-10 16:33:48 +0000 | [diff] [blame] | 83 | if (-bytes_read != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 84 | 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 | |
| 125 | static int worker_thread(void *arg) |
| 126 | { |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 127 | pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg; |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 128 | |
| 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 | */ |
| 144 | static 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 Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 163 | status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, 56127, &osd.sock); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 164 | if (status != PJ_SUCCESS) { |
| 165 | app_perror("Error creating other socket", status); |
| 166 | return -12; |
| 167 | } |
| 168 | |
Benny Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 169 | pj_bzero(&callback, sizeof(callback)); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 170 | 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 Prijono | 8ab968f | 2007-07-20 08:08:30 +0000 | [diff] [blame] | 204 | status = app_socketpair(pj_AF_INET(), pj_SOCK_DGRAM(), 0, |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 205 | &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 Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 214 | sock_data.buffer = (char*) pj_pool_alloc(sock_data.pool, 128); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 215 | sock_data.bufsize = 128; |
Benny Prijono | f260e46 | 2007-04-30 21:03:32 +0000 | [diff] [blame] | 216 | sock_data.op_key = (pj_ioqueue_op_key_t*) |
| 217 | pj_pool_alloc(sock_data.pool, |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 218 | 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 Prijono | ac623b3 | 2006-07-03 15:19:31 +0000 | [diff] [blame] | 231 | pj_bzero(&callback, sizeof(callback)); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 232 | 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 | { |
Benny Prijono | c174868 | 2009-06-22 19:18:55 +0000 | [diff] [blame] | 253 | //Can't do this otherwise it'll deadlock |
| 254 | //pj_mutex_lock(sock_data.mutex); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 255 | |
| 256 | sock_data.unregistered = 1; |
| 257 | pj_ioqueue_unregister(sock_data.key); |
Benny Prijono | c174868 | 2009-06-22 19:18:55 +0000 | [diff] [blame] | 258 | //pj_mutex_unlock(sock_data.mutex); |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 259 | pj_mutex_destroy(sock_data.mutex); |
| 260 | pj_pool_release(sock_data.pool); |
| 261 | sock_data.pool = NULL; |
| 262 | } |
| 263 | |
| 264 | if (PJ_TIME_VAL_GT(now, end_time) && sock_data.unregistered) |
| 265 | break; |
| 266 | |
| 267 | timeout.sec = 0; timeout.msec = 10; |
| 268 | pj_ioqueue_poll(ioqueue, &timeout); |
| 269 | //pj_thread_sleep(1); |
| 270 | |
| 271 | } |
| 272 | |
| 273 | thread_quitting = 1; |
| 274 | |
| 275 | for (i=0; i<WORKER_CNT; ++i) { |
| 276 | pj_thread_join(thread[i]); |
| 277 | pj_thread_destroy(thread[i]); |
| 278 | } |
| 279 | |
| 280 | if (other_socket) { |
| 281 | pj_ioqueue_unregister(osd.key); |
| 282 | } |
| 283 | |
| 284 | pj_sock_close(sock_data.csock); |
| 285 | |
| 286 | PJ_LOG(3,(THIS_FILE, "....%s: done (%d KB/s)", |
| 287 | title, sock_data.received * 1000 / MSEC / 1000)); |
| 288 | return 0; |
| 289 | } |
| 290 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 291 | static int udp_ioqueue_unreg_test_imp(pj_bool_t allow_concur) |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 292 | { |
| 293 | enum { LOOP = 10 }; |
| 294 | int i, rc; |
| 295 | char title[30]; |
| 296 | pj_ioqueue_t *ioqueue; |
| 297 | pj_pool_t *test_pool; |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 298 | |
| 299 | PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur)); |
| 300 | |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 301 | test_method = UNREGISTER_IN_APP; |
| 302 | |
| 303 | test_pool = pj_pool_create(mem, "unregtest", 4000, 4000, NULL); |
| 304 | |
| 305 | rc = pj_ioqueue_create(test_pool, 16, &ioqueue); |
| 306 | if (rc != PJ_SUCCESS) { |
| 307 | app_perror("Error creating ioqueue", rc); |
| 308 | return -10; |
| 309 | } |
| 310 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 311 | rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur); |
| 312 | if (rc != PJ_SUCCESS) { |
| 313 | app_perror("Error in pj_ioqueue_set_default_concurrency()", rc); |
| 314 | return -12; |
| 315 | } |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 316 | |
| 317 | PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 0/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, 0); |
| 322 | if (rc != 0) |
| 323 | return rc; |
| 324 | } |
| 325 | |
| 326 | |
| 327 | PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 1/3 (%s)", |
| 328 | pj_ioqueue_name())); |
| 329 | for (i=0; i<LOOP; ++i) { |
| 330 | pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP); |
| 331 | rc = perform_unreg_test(ioqueue, test_pool, title, 1); |
| 332 | if (rc != 0) |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | test_method = UNREGISTER_IN_CALLBACK; |
| 337 | |
| 338 | PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 2/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, 0); |
| 343 | if (rc != 0) |
| 344 | return rc; |
| 345 | } |
| 346 | |
| 347 | |
| 348 | PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 3/3 (%s)", |
| 349 | pj_ioqueue_name())); |
| 350 | for (i=0; i<LOOP; ++i) { |
| 351 | pj_ansi_sprintf(title, "repeat %d/%d", i, LOOP); |
| 352 | rc = perform_unreg_test(ioqueue, test_pool, title, 1); |
| 353 | if (rc != 0) |
| 354 | return rc; |
| 355 | } |
| 356 | |
| 357 | pj_ioqueue_destroy(ioqueue); |
| 358 | pj_pool_release(test_pool); |
| 359 | |
| 360 | return 0; |
| 361 | } |
| 362 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 363 | int udp_ioqueue_unreg_test(void) |
| 364 | { |
| 365 | int rc; |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 366 | |
Benny Prijono | e3f79fd | 2008-02-13 15:17:28 +0000 | [diff] [blame] | 367 | rc = udp_ioqueue_unreg_test_imp(PJ_TRUE); |
| 368 | if (rc != 0) |
| 369 | return rc; |
| 370 | |
| 371 | rc = udp_ioqueue_unreg_test_imp(PJ_FALSE); |
| 372 | if (rc != 0) |
| 373 | return rc; |
| 374 | |
| 375 | return 0; |
| 376 | } |
Benny Prijono | 5accbd0 | 2006-03-30 16:32:18 +0000 | [diff] [blame] | 377 | |
| 378 | #else |
| 379 | /* To prevent warning about "translation unit is empty" |
| 380 | * when this test is disabled. |
| 381 | */ |
| 382 | int dummy_uiq_unreg; |
| 383 | #endif /* INCLUDE_IOQUEUE_UNREG_TEST */ |
| 384 | |
| 385 | |