| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
|
| 21 |
#if INCLUDE_IOQUEUE_UNREG_TEST |
| 22 |
|
| 23 |
|
| 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 |
#define TRACE(expr) |
| 39 |
|
| 40 |
|
| 41 |
enum test_method |
| 42 |
{ |
| 43 |
UNREGISTER_IN_APP, |
| 44 |
UNREGISTER_IN_CALLBACK, |
| 45 |
}; |
| 46 |
|
| 47 |
static int thread_quitting; |
| 48 |
static enum test_method test_method; |
| 49 |
static pj_time_val time_to_unregister; |
| 50 |
|
| 51 |
struct sock_data |
| 52 |
{ |
| 53 |
pj_sock_t sock; |
| 54 |
pj_sock_t csock; |
| 55 |
pj_pool_t *pool; |
| 56 |
pj_ioqueue_key_t *key; |
| 57 |
pj_mutex_t *mutex; |
| 58 |
pj_ioqueue_op_key_t *op_key; |
| 59 |
char *buffer; |
| 60 |
pj_size_t bufsize; |
| 61 |
pj_bool_t unregistered; |
| 62 |
pj_ssize_t received; |
| 63 |
} sock_data; |
| 64 |
|
| 65 |
static void on_read_complete(pj_ioqueue_key_t *key, |
| 66 |
pj_ioqueue_op_key_t *op_key, |
| 67 |
pj_ssize_t bytes_read) |
| 68 |
{ |
| 69 |
pj_ssize_t size; |
| 70 |
char *sendbuf = "Hello world"; |
| 71 |
pj_status_t status; |
| 72 |
|
| 73 |
TRACE((THIS_FILE, "......on_read_complete(): unregistered=%d, bytes=%d", |
| 74 |
sock_data.unregistered, bytes_read)); |
| 75 |
|
| 76 |
if (sock_data.unregistered) { |
| 77 |
TRACE((THIS_FILE, "........bailing out")); |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
pj_mutex_lock(sock_data.mutex); |
| 82 |
|
| 83 |
if (sock_data.unregistered) { |
| 84 |
TRACE((THIS_FILE, "........bailing out")); |
| 85 |
pj_mutex_unlock(sock_data.mutex); |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if (bytes_read < 0) { |
| 90 |
if (-bytes_read != PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) |
| 91 |
app_perror("ioqueue reported recv error", (pj_status_t)-bytes_read); |
| 92 |
} else { |
| 93 |
sock_data.received += bytes_read; |
| 94 |
} |
| 95 |
|
| 96 |
if (test_method == UNREGISTER_IN_CALLBACK) { |
| 97 |
pj_time_val now; |
| 98 |
|
| 99 |
pj_gettimeofday(&now); |
| 100 |
if (PJ_TIME_VAL_GTE(now, time_to_unregister)) { |
| 101 |
sock_data.unregistered = 1; |
| 102 |
TRACE((THIS_FILE, "......on_read_complete(): unregistering")); |
| 103 |
pj_mutex_unlock(sock_data.mutex); |
| 104 |
pj_ioqueue_unregister(key); |
| 105 |
return; |
| 106 |
} |
| 107 |
} |
| 108 |
|
| 109 |
do { |
| 110 |
size = sock_data.bufsize; |
| 111 |
status = pj_ioqueue_recv(key, op_key, sock_data.buffer, &size, 0); |
| 112 |
TRACE((THIS_FILE, "........recv, status=%d", status)); |
| 113 |
if (status != PJ_EPENDING && status != PJ_SUCCESS) |
| 114 |
app_perror("recv() error", status); |
| 115 |
|
| 116 |
} while (status == PJ_SUCCESS); |
| 117 |
|
| 118 |
pj_mutex_unlock(sock_data.mutex); |
| 119 |
|
| 120 |
size = pj_ansi_strlen(sendbuf); |
| 121 |
status = pj_sock_send(sock_data.csock, sendbuf, &size, 0); |
| 122 |
if (status != PJ_SUCCESS) |
| 123 |
app_perror("send() error", status); |
| 124 |
|
| 125 |
size = pj_ansi_strlen(sendbuf); |
| 126 |
status = pj_sock_send(sock_data.csock, sendbuf, &size, 0); |
| 127 |
if (status != PJ_SUCCESS) |
| 128 |
app_perror("send() error", status); |
| 129 |
|
| 130 |
TRACE((THIS_FILE, "........done")); |
| 131 |
} |
| 132 |
|
| 133 |
static int worker_thread(void *arg) |
| 134 |
{ |
| 135 |
pj_ioqueue_t *ioqueue = (pj_ioqueue_t*) arg; |
| 136 |
|
| 137 |
while (!thread_quitting) { |
| 138 |
pj_time_val timeout = { 0, 200 }; |
| 139 |
pj_ioqueue_poll(ioqueue, &timeout); |
| 140 |
} |
| 141 |
|
| 142 |
return 0; |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
static int perform_unreg_test(pj_ioqueue_t *ioqueue, |
| 153 |
pj_pool_t *test_pool, |
| 154 |
const char *title, |
| 155 |
pj_bool_t other_socket) |
| 156 |
{ |
| 157 |
enum { WORKER_CNT = 1, MSEC = 500, QUIT_MSEC = 500 }; |
| 158 |
int i; |
| 159 |
pj_thread_t *thread[WORKER_CNT]; |
| 160 |
struct sock_data osd; |
| 161 |
pj_ioqueue_callback callback; |
| 162 |
pj_time_val end_time; |
| 163 |
pj_status_t status; |
| 164 |
#if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_IOCP |
| 165 |
pj_time_val max_time; |
| 166 |
#endif |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
if (other_socket) { |
| 174 |
status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, -1, &osd.sock); |
| 175 |
if (status != PJ_SUCCESS) { |
| 176 |
app_perror("Error creating other socket", status); |
| 177 |
return -12; |
| 178 |
} |
| 179 |
|
| 180 |
pj_bzero(&callback, sizeof(callback)); |
| 181 |
status = pj_ioqueue_register_sock(test_pool, ioqueue, osd.sock, |
| 182 |
NULL, &callback, &osd.key); |
| 183 |
if (status != PJ_SUCCESS) { |
| 184 |
app_perror("Error registering other socket", status); |
| 185 |
return -13; |
| 186 |
} |
| 187 |
|
| 188 |
} else { |
| 189 |
osd.key = NULL; |
| 190 |
osd.sock = PJ_INVALID_SOCKET; |
| 191 |
} |
| 192 |
|
| 193 |
|
| 194 |
thread_quitting = 0; |
| 195 |
pj_gettimeofday(&time_to_unregister); |
| 196 |
time_to_unregister.msec += MSEC; |
| 197 |
pj_time_val_normalize(&time_to_unregister); |
| 198 |
|
| 199 |
end_time = time_to_unregister; |
| 200 |
end_time.msec += QUIT_MSEC; |
| 201 |
pj_time_val_normalize(&end_time); |
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
for (i=0; i<WORKER_CNT; ++i) { |
| 206 |
status = pj_thread_create(test_pool, "unregtest", &worker_thread, |
| 207 |
ioqueue, 0, 0, &thread[i]); |
| 208 |
if (status != PJ_SUCCESS) { |
| 209 |
app_perror("Error creating thread", status); |
| 210 |
return -20; |
| 211 |
} |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
status = app_socketpair(pj_AF_INET(), pj_SOCK_DGRAM(), 0, |
| 216 |
&sock_data.sock, &sock_data.csock); |
| 217 |
if (status != PJ_SUCCESS) { |
| 218 |
app_perror("app_socketpair error", status); |
| 219 |
return -30; |
| 220 |
} |
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
sock_data.pool = pj_pool_create(mem, "sd", 1000, 1000, NULL); |
| 225 |
sock_data.buffer = (char*) pj_pool_alloc(sock_data.pool, 128); |
| 226 |
sock_data.bufsize = 128; |
| 227 |
sock_data.op_key = (pj_ioqueue_op_key_t*) |
| 228 |
pj_pool_alloc(sock_data.pool, |
| 229 |
sizeof(*sock_data.op_key)); |
| 230 |
sock_data.received = 0; |
| 231 |
sock_data.unregistered = 0; |
| 232 |
|
| 233 |
pj_ioqueue_op_key_init(sock_data.op_key, sizeof(*sock_data.op_key)); |
| 234 |
|
| 235 |
status = pj_mutex_create_simple(sock_data.pool, "sd", &sock_data.mutex); |
| 236 |
if (status != PJ_SUCCESS) { |
| 237 |
app_perror("create_mutex() error", status); |
| 238 |
return -35; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
pj_bzero(&callback, sizeof(callback)); |
| 243 |
callback.on_read_complete = &on_read_complete; |
| 244 |
status = pj_ioqueue_register_sock(sock_data.pool, ioqueue, sock_data.sock, |
| 245 |
NULL, &callback, &sock_data.key); |
| 246 |
if (status != PJ_SUCCESS) { |
| 247 |
app_perror("pj_ioqueue_register error", status); |
| 248 |
return -40; |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
on_read_complete(sock_data.key, sock_data.op_key, 0); |
| 253 |
|
| 254 |
#if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_IOCP |
| 255 |
pj_gettimeofday(&max_time); |
| 256 |
max_time.sec += 10; |
| 257 |
#endif |
| 258 |
|
| 259 |
|
| 260 |
for (;;) { |
| 261 |
pj_time_val now, timeout; |
| 262 |
int n; |
| 263 |
|
| 264 |
pj_gettimeofday(&now); |
| 265 |
|
| 266 |
if (test_method == UNREGISTER_IN_APP && |
| 267 |
PJ_TIME_VAL_GTE(now, time_to_unregister) && |
| 268 |
!sock_data.unregistered) |
| 269 |
{ |
| 270 |
sock_data.unregistered = 1; |
| 271 |
TRACE((THIS_FILE, "......main: unregistering")); |
| 272 |
|
| 273 |
pj_mutex_lock(sock_data.mutex); |
| 274 |
pj_mutex_unlock(sock_data.mutex); |
| 275 |
pj_ioqueue_unregister(sock_data.key); |
| 276 |
} |
| 277 |
else if (test_method == UNREGISTER_IN_CALLBACK && |
| 278 |
PJ_TIME_VAL_GT(now, time_to_unregister) && |
| 279 |
!sock_data.unregistered) |
| 280 |
{ |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
pj_ssize_t size; |
| 285 |
char *sendbuf = "Ping"; |
| 286 |
|
| 287 |
size = pj_ansi_strlen(sendbuf); |
| 288 |
status = pj_sock_send(sock_data.csock, sendbuf, &size, 0); |
| 289 |
if (status != PJ_SUCCESS) |
| 290 |
app_perror("send() error for callback trigger", status); |
| 291 |
|
| 292 |
pj_thread_sleep(200); |
| 293 |
} |
| 294 |
|
| 295 |
if (PJ_TIME_VAL_GT(now, end_time) && sock_data.unregistered) |
| 296 |
break; |
| 297 |
|
| 298 |
#if PJ_IOQUEUE_IMP == PJ_IOQUEUE_IMP_IOCP |
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
if (test_method == UNREGISTER_IN_CALLBACK && |
| 304 |
PJ_TIME_VAL_GT(now, max_time) && |
| 305 |
!sock_data.unregistered) |
| 306 |
{ |
| 307 |
PJ_LOG(2,(THIS_FILE, "....%s: timed out waiting for " |
| 308 |
"unregister callback", title)); |
| 309 |
pj_mutex_lock(sock_data.mutex); |
| 310 |
sock_data.unregistered = 1; |
| 311 |
pj_mutex_unlock(sock_data.mutex); |
| 312 |
pj_ioqueue_unregister(sock_data.key); |
| 313 |
break; |
| 314 |
} |
| 315 |
#endif |
| 316 |
|
| 317 |
timeout.sec = 0; timeout.msec = 200; |
| 318 |
n = pj_ioqueue_poll(ioqueue, &timeout); |
| 319 |
if (n < 0) { |
| 320 |
app_perror("pj_ioqueue_poll error", -n); |
| 321 |
pj_thread_sleep(1); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
thread_quitting = 1; |
| 326 |
|
| 327 |
for (i=0; i<WORKER_CNT; ++i) { |
| 328 |
pj_thread_join(thread[i]); |
| 329 |
pj_thread_destroy(thread[i]); |
| 330 |
} |
| 331 |
|
| 332 |
|
| 333 |
pj_mutex_destroy(sock_data.mutex); |
| 334 |
pj_pool_release(sock_data.pool); |
| 335 |
sock_data.pool = NULL; |
| 336 |
|
| 337 |
if (other_socket) { |
| 338 |
pj_ioqueue_unregister(osd.key); |
| 339 |
} |
| 340 |
|
| 341 |
pj_sock_close(sock_data.csock); |
| 342 |
|
| 343 |
PJ_LOG(3,(THIS_FILE, "....%s: done (%ld KB/s)", |
| 344 |
title, sock_data.received * 1000 / MSEC / 1000)); |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
static int udp_ioqueue_unreg_test_imp(pj_bool_t allow_concur) |
| 349 |
{ |
| 350 |
enum { LOOP = 10 }; |
| 351 |
int i, rc; |
| 352 |
char title[30]; |
| 353 |
pj_ioqueue_t *ioqueue; |
| 354 |
pj_pool_t *test_pool; |
| 355 |
|
| 356 |
PJ_LOG(3,(THIS_FILE, "..testing with concurency=%d", allow_concur)); |
| 357 |
|
| 358 |
test_method = UNREGISTER_IN_APP; |
| 359 |
|
| 360 |
test_pool = pj_pool_create(mem, "unregtest", 4000, 4000, NULL); |
| 361 |
|
| 362 |
rc = pj_ioqueue_create(test_pool, 16, &ioqueue); |
| 363 |
if (rc != PJ_SUCCESS) { |
| 364 |
app_perror("Error creating ioqueue", rc); |
| 365 |
return -10; |
| 366 |
} |
| 367 |
|
| 368 |
rc = pj_ioqueue_set_default_concurrency(ioqueue, allow_concur); |
| 369 |
if (rc != PJ_SUCCESS) { |
| 370 |
app_perror("Error in pj_ioqueue_set_default_concurrency()", rc); |
| 371 |
return -12; |
| 372 |
} |
| 373 |
|
| 374 |
PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 0/3, unregister in app (%s)", |
| 375 |
pj_ioqueue_name())); |
| 376 |
for (i=0; i<LOOP; ++i) { |
| 377 |
pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP); |
| 378 |
rc = perform_unreg_test(ioqueue, test_pool, title, 0); |
| 379 |
if (rc != 0) |
| 380 |
return rc; |
| 381 |
} |
| 382 |
|
| 383 |
|
| 384 |
PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 1/3, unregister in app (%s)", |
| 385 |
pj_ioqueue_name())); |
| 386 |
for (i=0; i<LOOP; ++i) { |
| 387 |
pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP); |
| 388 |
rc = perform_unreg_test(ioqueue, test_pool, title, 1); |
| 389 |
if (rc != 0) |
| 390 |
return rc; |
| 391 |
} |
| 392 |
|
| 393 |
test_method = UNREGISTER_IN_CALLBACK; |
| 394 |
|
| 395 |
PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 2/3, unregister in cb (%s)", |
| 396 |
pj_ioqueue_name())); |
| 397 |
for (i=0; i<LOOP; ++i) { |
| 398 |
pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP); |
| 399 |
rc = perform_unreg_test(ioqueue, test_pool, title, 0); |
| 400 |
if (rc != 0) |
| 401 |
return rc; |
| 402 |
} |
| 403 |
|
| 404 |
|
| 405 |
PJ_LOG(3, (THIS_FILE, "...ioqueue unregister stress test 3/3, unregister in cb (%s)", |
| 406 |
pj_ioqueue_name())); |
| 407 |
for (i=0; i<LOOP; ++i) { |
| 408 |
pj_ansi_snprintf(title, sizeof(title), "repeat %d/%d", i, LOOP); |
| 409 |
rc = perform_unreg_test(ioqueue, test_pool, title, 1); |
| 410 |
if (rc != 0) |
| 411 |
return rc; |
| 412 |
} |
| 413 |
|
| 414 |
pj_ioqueue_destroy(ioqueue); |
| 415 |
pj_pool_release(test_pool); |
| 416 |
|
| 417 |
return 0; |
| 418 |
} |
| 419 |
|
| 420 |
int udp_ioqueue_unreg_test(void) |
| 421 |
{ |
| 422 |
int rc; |
| 423 |
|
| 424 |
rc = udp_ioqueue_unreg_test_imp(PJ_TRUE); |
| 425 |
if (rc != 0) |
| 426 |
return rc; |
| 427 |
|
| 428 |
rc = udp_ioqueue_unreg_test_imp(PJ_FALSE); |
| 429 |
if (rc != 0) |
| 430 |
return rc; |
| 431 |
|
| 432 |
return 0; |
| 433 |
} |
| 434 |
|
| 435 |
#else |
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
int dummy_uiq_unreg; |
| 440 |
#endif |
| 441 |
|
| 442 |
|