| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
#include "test.h" |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
#if INCLUDE_UDP_IOQUEUE_TEST |
| 36 |
|
| 37 |
#include <pjlib.h> |
| 38 |
|
| 39 |
#include <pj/compat/socket.h> |
| 40 |
|
| 41 |
#define THIS_FILE "test_udp" |
| 42 |
#define LOOP 2 |
| 43 |
|
| 44 |
#define BUF_MIN_SIZE 32 |
| 45 |
#define BUF_MAX_SIZE 2048 |
| 46 |
#define SOCK_INACTIVE_MIN (1) |
| 47 |
#define SOCK_INACTIVE_MAX (PJ_IOQUEUE_MAX_HANDLES - 2) |
| 48 |
#define POOL_SIZE (2*BUF_MAX_SIZE + SOCK_INACTIVE_MAX*128 + 2048) |
| 49 |
|
| 50 |
#undef TRACE_ |
| 51 |
#define TRACE_(msg) PJ_LOG(3,(THIS_FILE,"....." msg)) |
| 52 |
|
| 53 |
#if 0 |
| 54 |
# define TRACE__(args) PJ_LOG(3,args) |
| 55 |
#else |
| 56 |
# define TRACE__(args) |
| 57 |
#endif |
| 58 |
|
| 59 |
|
| 60 |
static pj_ssize_t callback_read_size, |
| 61 |
callback_write_size, |
| 62 |
callback_accept_status, |
| 63 |
callback_connect_status; |
| 64 |
static pj_ioqueue_key_t *callback_read_key, |
| 65 |
*callback_write_key, |
| 66 |
*callback_accept_key, |
| 67 |
*callback_connect_key; |
| 68 |
static pj_ioqueue_op_key_t *callback_read_op, |
| 69 |
*callback_write_op, |
| 70 |
*callback_accept_op; |
| 71 |
|
| 72 |
static void on_ioqueue_read(pj_ioqueue_key_t *key, |
| 73 |
pj_ioqueue_op_key_t *op_key, |
| 74 |
pj_ssize_t bytes_read) |
| 75 |
{ |
| 76 |
callback_read_key = key; |
| 77 |
callback_read_op = op_key; |
| 78 |
callback_read_size = bytes_read; |
| 79 |
TRACE__((THIS_FILE, " callback_read_key = %p, bytes=%d", |
| 80 |
key, bytes_read)); |
| 81 |
} |
| 82 |
|
| 83 |
static void on_ioqueue_write(pj_ioqueue_key_t *key, |
| 84 |
pj_ioqueue_op_key_t *op_key, |
| 85 |
pj_ssize_t bytes_written) |
| 86 |
{ |
| 87 |
callback_write_key = key; |
| 88 |
callback_write_op = op_key; |
| 89 |
callback_write_size = bytes_written; |
| 90 |
} |
| 91 |
|
| 92 |
static void on_ioqueue_accept(pj_ioqueue_key_t *key, |
| 93 |
pj_ioqueue_op_key_t *op_key, |
| 94 |
pj_sock_t sock, int status) |
| 95 |
{ |
| 96 |
PJ_UNUSED_ARG(sock); |
| 97 |
callback_accept_key = key; |
| 98 |
callback_accept_op = op_key; |
| 99 |
callback_accept_status = status; |
| 100 |
} |
| 101 |
|
| 102 |
static void on_ioqueue_connect(pj_ioqueue_key_t *key, int status) |
| 103 |
{ |
| 104 |
callback_connect_key = key; |
| 105 |
callback_connect_status = status; |
| 106 |
} |
| 107 |
|
| 108 |
static pj_ioqueue_callback test_cb = |
| 109 |
{ |
| 110 |
&on_ioqueue_read, |
| 111 |
&on_ioqueue_write, |
| 112 |
&on_ioqueue_accept, |
| 113 |
&on_ioqueue_connect, |
| 114 |
}; |
| 115 |
|
| 116 |
#if defined(PJ_WIN32) || defined(PJ_WIN64) |
| 117 |
# define S_ADDR S_un.S_addr |
| 118 |
#else |
| 119 |
# define S_ADDR s_addr |
| 120 |
#endif |
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
static int compliance_test(const pj_ioqueue_cfg *cfg) |
| 128 |
{ |
| 129 |
pj_sock_t ssock=-1, csock=-1; |
| 130 |
pj_sockaddr_in addr, dst_addr; |
| 131 |
int addrlen; |
| 132 |
unsigned short port; |
| 133 |
pj_pool_t *pool = NULL; |
| 134 |
char *send_buf, *recv_buf; |
| 135 |
pj_ioqueue_t *ioque = NULL; |
| 136 |
pj_ioqueue_key_t *skey = NULL, *ckey = NULL; |
| 137 |
pj_ioqueue_op_key_t read_op, write_op; |
| 138 |
int bufsize = BUF_MIN_SIZE; |
| 139 |
pj_ssize_t bytes; |
| 140 |
int status = -1; |
| 141 |
pj_str_t temp; |
| 142 |
pj_bool_t send_pending, recv_pending; |
| 143 |
pj_status_t rc; |
| 144 |
pj_str_t ioque_name; |
| 145 |
pj_str_t ioqueue_type; |
| 146 |
|
| 147 |
pj_set_os_error(PJ_SUCCESS); |
| 148 |
|
| 149 |
|
| 150 |
pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL); |
| 151 |
|
| 152 |
|
| 153 |
send_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 154 |
recv_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 155 |
|
| 156 |
|
| 157 |
TRACE_("creating sockets..."); |
| 158 |
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock); |
| 159 |
if (rc==PJ_SUCCESS) |
| 160 |
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock); |
| 161 |
else |
| 162 |
csock = PJ_INVALID_SOCKET; |
| 163 |
if (rc != PJ_SUCCESS) { |
| 164 |
app_perror("...ERROR in pj_sock_socket()", rc); |
| 165 |
status=-1; goto on_error; |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
TRACE_("bind socket..."); |
| 170 |
pj_bzero(&addr, sizeof(addr)); |
| 171 |
addr.sin_family = pj_AF_INET(); |
| 172 |
if (pj_sock_bind(ssock, &addr, sizeof(addr))) { |
| 173 |
status=-10; goto on_error; |
| 174 |
} |
| 175 |
|
| 176 |
|
| 177 |
addrlen = sizeof(addr); |
| 178 |
PJ_TEST_SUCCESS(pj_sock_getsockname(ssock, &addr, &addrlen), NULL, |
| 179 |
{status=-15; goto on_error;}); |
| 180 |
port = pj_sockaddr_get_port(&addr); |
| 181 |
|
| 182 |
|
| 183 |
TRACE_("create ioqueue..."); |
| 184 |
rc = pj_ioqueue_create2(pool, PJ_IOQUEUE_MAX_HANDLES, cfg, &ioque); |
| 185 |
if (rc != PJ_SUCCESS) { |
| 186 |
status=-20; goto on_error; |
| 187 |
} |
| 188 |
|
| 189 |
ioque_name = pj_str((char*)pj_ioqueue_name()); |
| 190 |
if (pj_strncmp(&ioque_name, pj_cstr(&ioqueue_type, "epoll"), 5) == 0 || |
| 191 |
pj_strncmp(&ioque_name, pj_cstr(&ioqueue_type, "kqueue"), 6) == 0 || |
| 192 |
pj_strncmp(&ioque_name, pj_cstr(&ioqueue_type, "iocp"), 4) == 0) { |
| 193 |
if (pj_ioqueue_get_os_handle(ioque) == NULL) { |
| 194 |
PJ_LOG(1,( |
| 195 |
THIS_FILE, |
| 196 |
"...pj_ioqueue_os_handle() unexpectedly returned NULL" |
| 197 |
)); |
| 198 |
status=-21; goto on_error; |
| 199 |
} |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
TRACE_("registering first sockets..."); |
| 206 |
rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL, |
| 207 |
&test_cb, &skey); |
| 208 |
if (rc != PJ_SUCCESS) { |
| 209 |
app_perror("...error(10): ioqueue_register error", rc); |
| 210 |
status=-25; goto on_error; |
| 211 |
} |
| 212 |
TRACE_("registering second sockets..."); |
| 213 |
rc = pj_ioqueue_register_sock( pool, ioque, csock, NULL, |
| 214 |
&test_cb, &ckey); |
| 215 |
if (rc != PJ_SUCCESS) { |
| 216 |
app_perror("...error(11): ioqueue_register error", rc); |
| 217 |
status=-26; goto on_error; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
pj_create_random_string(send_buf, bufsize); |
| 222 |
|
| 223 |
|
| 224 |
pj_ioqueue_op_key_init(&read_op, sizeof(read_op)); |
| 225 |
pj_ioqueue_op_key_init(&write_op, sizeof(write_op)); |
| 226 |
|
| 227 |
|
| 228 |
TRACE_("start recvfrom..."); |
| 229 |
pj_bzero(&addr, sizeof(addr)); |
| 230 |
addrlen = sizeof(addr); |
| 231 |
bytes = bufsize; |
| 232 |
rc = pj_ioqueue_recvfrom(skey, &read_op, recv_buf, &bytes, 0, |
| 233 |
&addr, &addrlen); |
| 234 |
if (rc != PJ_SUCCESS && rc != PJ_EPENDING) { |
| 235 |
app_perror("...error: pj_ioqueue_recvfrom", rc); |
| 236 |
status=-28; goto on_error; |
| 237 |
} else if (rc == PJ_EPENDING) { |
| 238 |
recv_pending = 1; |
| 239 |
PJ_LOG(3, (THIS_FILE, |
| 240 |
"......ok: recvfrom returned pending")); |
| 241 |
} else { |
| 242 |
PJ_LOG(3, (THIS_FILE, |
| 243 |
"......error: recvfrom returned immediate ok!")); |
| 244 |
status=-29; goto on_error; |
| 245 |
} |
| 246 |
|
| 247 |
|
| 248 |
TRACE_("set destination address..."); |
| 249 |
temp = pj_str("127.0.0.1"); |
| 250 |
if ((rc=pj_sockaddr_in_init(&dst_addr, &temp, port)) != 0) { |
| 251 |
app_perror("...error: unable to resolve 127.0.0.1", rc); |
| 252 |
status=-290; goto on_error; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
TRACE_("start sendto..."); |
| 257 |
bytes = bufsize; |
| 258 |
rc = pj_ioqueue_sendto(ckey, &write_op, send_buf, &bytes, 0, &dst_addr, |
| 259 |
sizeof(dst_addr)); |
| 260 |
if (rc != PJ_SUCCESS && rc != PJ_EPENDING) { |
| 261 |
app_perror("...error: pj_ioqueue_sendto", rc); |
| 262 |
status=-30; goto on_error; |
| 263 |
} else if (rc == PJ_EPENDING) { |
| 264 |
send_pending = 1; |
| 265 |
PJ_LOG(3, (THIS_FILE, |
| 266 |
"......ok: sendto returned pending")); |
| 267 |
} else { |
| 268 |
send_pending = 0; |
| 269 |
PJ_LOG(3, (THIS_FILE, |
| 270 |
"......ok: sendto returned immediate success")); |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
callback_read_size = callback_write_size = 0; |
| 275 |
callback_accept_status = callback_connect_status = -2; |
| 276 |
callback_read_key = callback_write_key = |
| 277 |
callback_accept_key = callback_connect_key = NULL; |
| 278 |
callback_read_op = callback_write_op = NULL; |
| 279 |
|
| 280 |
|
| 281 |
while (send_pending || recv_pending) { |
| 282 |
int ret; |
| 283 |
pj_time_val timeout = { 5, 0 }; |
| 284 |
|
| 285 |
TRACE_("poll..."); |
| 286 |
#ifdef PJ_SYMBIAN |
| 287 |
ret = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout)); |
| 288 |
#else |
| 289 |
ret = pj_ioqueue_poll(ioque, &timeout); |
| 290 |
#endif |
| 291 |
|
| 292 |
if (ret == 0) { |
| 293 |
PJ_LOG(1,(THIS_FILE, "...ERROR: timed out...")); |
| 294 |
status=-45; goto on_error; |
| 295 |
} else if (ret < 0) { |
| 296 |
app_perror("...ERROR in ioqueue_poll()", -ret); |
| 297 |
status=-50; goto on_error; |
| 298 |
} |
| 299 |
|
| 300 |
if (callback_read_key != NULL) { |
| 301 |
if (callback_read_size != bufsize) { |
| 302 |
status=-61; goto on_error; |
| 303 |
} |
| 304 |
if (callback_read_key != skey) { |
| 305 |
status=-65; goto on_error; |
| 306 |
} |
| 307 |
if (callback_read_op != &read_op) { |
| 308 |
status=-66; goto on_error; |
| 309 |
} |
| 310 |
|
| 311 |
if (pj_memcmp(send_buf, recv_buf, bufsize) != 0) { |
| 312 |
status=-67; goto on_error; |
| 313 |
} |
| 314 |
if (addrlen != sizeof(pj_sockaddr_in)) { |
| 315 |
status=-68; goto on_error; |
| 316 |
} |
| 317 |
if (addr.sin_family != pj_AF_INET()) { |
| 318 |
status=-69; goto on_error; |
| 319 |
} |
| 320 |
|
| 321 |
|
| 322 |
recv_pending = 0; |
| 323 |
} |
| 324 |
|
| 325 |
if (callback_write_key != NULL) { |
| 326 |
if (callback_write_size != bufsize) { |
| 327 |
status=-73; goto on_error; |
| 328 |
} |
| 329 |
if (callback_write_key != ckey) { |
| 330 |
status=-75; goto on_error; |
| 331 |
} |
| 332 |
if (callback_write_op != &write_op) { |
| 333 |
status=-76; goto on_error; |
| 334 |
} |
| 335 |
|
| 336 |
send_pending = 0; |
| 337 |
} |
| 338 |
} |
| 339 |
|
| 340 |
|
| 341 |
status = 0; |
| 342 |
|
| 343 |
on_error: |
| 344 |
if (skey) |
| 345 |
pj_ioqueue_unregister(skey); |
| 346 |
else if (ssock != -1) |
| 347 |
pj_sock_close(ssock); |
| 348 |
|
| 349 |
if (ckey) |
| 350 |
pj_ioqueue_unregister(ckey); |
| 351 |
else if (csock != -1) |
| 352 |
pj_sock_close(csock); |
| 353 |
|
| 354 |
if (ioque != NULL) |
| 355 |
pj_ioqueue_destroy(ioque); |
| 356 |
pj_pool_release(pool); |
| 357 |
return status; |
| 358 |
|
| 359 |
} |
| 360 |
|
| 361 |
|
| 362 |
static void on_read_complete(pj_ioqueue_key_t *key, |
| 363 |
pj_ioqueue_op_key_t *op_key, |
| 364 |
pj_ssize_t bytes_read) |
| 365 |
{ |
| 366 |
unsigned *p_packet_cnt = (unsigned*) pj_ioqueue_get_user_data(key); |
| 367 |
|
| 368 |
PJ_UNUSED_ARG(op_key); |
| 369 |
PJ_UNUSED_ARG(bytes_read); |
| 370 |
|
| 371 |
(*p_packet_cnt)++; |
| 372 |
} |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
|
| 379 |
static int unregister_test(const pj_ioqueue_cfg *cfg) |
| 380 |
{ |
| 381 |
pj_pool_t *pool; |
| 382 |
pj_ioqueue_t *ioqueue; |
| 383 |
pj_sock_t ssock; |
| 384 |
pj_sock_t rsock, rsock2; |
| 385 |
int i, addrlen; |
| 386 |
pj_sockaddr_in addr; |
| 387 |
pj_ioqueue_key_t *key, *key2; |
| 388 |
pj_ioqueue_op_key_t opkey; |
| 389 |
pj_ioqueue_callback cb; |
| 390 |
unsigned packet_cnt; |
| 391 |
char sendbuf[10], recvbuf[10]; |
| 392 |
void *user_data2 = (void*)(long)2; |
| 393 |
pj_ssize_t bytes; |
| 394 |
pj_time_val timeout; |
| 395 |
pj_status_t status; |
| 396 |
|
| 397 |
pool = pj_pool_create(mem, "test", 4000, 4000, NULL); |
| 398 |
if (!pool) { |
| 399 |
app_perror("Unable to create pool", PJ_ENOMEM); |
| 400 |
return -100; |
| 401 |
} |
| 402 |
|
| 403 |
status = pj_ioqueue_create2(pool, 1, cfg, &ioqueue); |
| 404 |
if (status != PJ_SUCCESS) { |
| 405 |
app_perror("Error creating ioqueue", status); |
| 406 |
return -110; |
| 407 |
} |
| 408 |
|
| 409 |
|
| 410 |
status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, -1, &ssock); |
| 411 |
if (status != PJ_SUCCESS) { |
| 412 |
app_perror("Error initializing socket", status); |
| 413 |
return -120; |
| 414 |
} |
| 415 |
|
| 416 |
|
| 417 |
status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, -1, &rsock); |
| 418 |
if (status != PJ_SUCCESS) { |
| 419 |
app_perror("Error initializing socket", status); |
| 420 |
return -130; |
| 421 |
} |
| 422 |
|
| 423 |
|
| 424 |
pj_bzero(&cb, sizeof(cb)); |
| 425 |
cb.on_read_complete = &on_read_complete; |
| 426 |
packet_cnt = 0; |
| 427 |
status = pj_ioqueue_register_sock(pool, ioqueue, rsock, &packet_cnt, |
| 428 |
&cb, &key); |
| 429 |
if (status != PJ_SUCCESS) { |
| 430 |
app_perror("Error registering to ioqueue", status); |
| 431 |
return -140; |
| 432 |
} |
| 433 |
|
| 434 |
|
| 435 |
pj_ioqueue_op_key_init(&opkey, sizeof(opkey)); |
| 436 |
|
| 437 |
|
| 438 |
bytes = sizeof(recvbuf); |
| 439 |
status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0); |
| 440 |
if (status != PJ_EPENDING) { |
| 441 |
app_perror("Expecting PJ_EPENDING, but got this", status); |
| 442 |
return -150; |
| 443 |
} |
| 444 |
|
| 445 |
|
| 446 |
addrlen = sizeof(addr); |
| 447 |
status = pj_sock_getsockname(rsock, &addr, &addrlen); |
| 448 |
if (status != PJ_SUCCESS) { |
| 449 |
app_perror("getsockname error", status); |
| 450 |
return -160; |
| 451 |
} |
| 452 |
|
| 453 |
|
| 454 |
|
| 455 |
|
| 456 |
addr.sin_addr = pj_inet_addr2("127.0.0.1"); |
| 457 |
|
| 458 |
|
| 459 |
pj_ansi_strxcpy(sendbuf, "Hello0123", sizeof(sendbuf)); |
| 460 |
|
| 461 |
|
| 462 |
bytes = sizeof(sendbuf); |
| 463 |
status = pj_sock_sendto(ssock, sendbuf, &bytes, 0, |
| 464 |
&addr, sizeof(addr)); |
| 465 |
|
| 466 |
if (status != PJ_SUCCESS) { |
| 467 |
app_perror("sendto error", status); |
| 468 |
return -170; |
| 469 |
} |
| 470 |
|
| 471 |
|
| 472 |
timeout.sec = 1; timeout.msec = 0; |
| 473 |
#ifdef PJ_SYMBIAN |
| 474 |
pj_symbianos_poll(-1, 1000); |
| 475 |
#else |
| 476 |
pj_ioqueue_poll(ioqueue, &timeout); |
| 477 |
#endif |
| 478 |
|
| 479 |
if (packet_cnt != 1) { |
| 480 |
return -180; |
| 481 |
} |
| 482 |
|
| 483 |
|
| 484 |
pj_thread_sleep(100); |
| 485 |
|
| 486 |
|
| 487 |
bytes = sizeof(recvbuf); |
| 488 |
status = pj_ioqueue_recv( key, &opkey, recvbuf, &bytes, 0); |
| 489 |
if (status != PJ_EPENDING) { |
| 490 |
app_perror("Expecting PJ_EPENDING, but got this", status); |
| 491 |
return -190; |
| 492 |
} |
| 493 |
|
| 494 |
|
| 495 |
packet_cnt = 0; |
| 496 |
|
| 497 |
|
| 498 |
bytes = sizeof(sendbuf); |
| 499 |
status = pj_sock_sendto(ssock, sendbuf, &bytes, 0, |
| 500 |
&addr, sizeof(addr)); |
| 501 |
|
| 502 |
if (status != PJ_SUCCESS) { |
| 503 |
app_perror("sendto error", status); |
| 504 |
return -200; |
| 505 |
} |
| 506 |
|
| 507 |
|
| 508 |
status = pj_ioqueue_unregister(key); |
| 509 |
if (status != PJ_SUCCESS) { |
| 510 |
app_perror("pj_ioqueue_unregister error", status); |
| 511 |
return -201; |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
for (i=0; i<10; ++i) { |
| 516 |
#ifdef PJ_SYMBIAN |
| 517 |
pj_symbianos_poll(-1, 100); |
| 518 |
#else |
| 519 |
timeout.sec = 0; timeout.msec = 100; |
| 520 |
pj_ioqueue_poll(ioqueue, &timeout); |
| 521 |
#endif |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
if (packet_cnt > 0) { |
| 526 |
PJ_LOG(3,(THIS_FILE, "....errror: not expecting to receive packet " |
| 527 |
"after socket has been closed")); |
| 528 |
return -210; |
| 529 |
} |
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
for (i=0; i<2; ++i) { |
| 534 |
status = pj_ioqueue_unregister(key); |
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
pj_thread_sleep(PJ_IOQUEUE_KEY_FREE_DELAY + 100); |
| 544 |
} |
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
status = app_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, -1, &rsock2); |
| 551 |
if (status != PJ_SUCCESS) { |
| 552 |
app_perror("Error initializing socket (2)", status); |
| 553 |
return -330; |
| 554 |
} |
| 555 |
|
| 556 |
|
| 557 |
status = pj_ioqueue_register_sock(pool, ioqueue, rsock2, user_data2, |
| 558 |
&cb, &key2); |
| 559 |
if (status != PJ_SUCCESS) { |
| 560 |
app_perror("Error registering to ioqueue (2)", status); |
| 561 |
return -340; |
| 562 |
} |
| 563 |
|
| 564 |
|
| 565 |
|
| 566 |
|
| 567 |
|
| 568 |
|
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
status = pj_ioqueue_unregister(key); |
| 578 |
if (status == PJ_SUCCESS) { |
| 579 |
PJ_LOG(3,(THIS_FILE, "....info: unregistering dead key was successful")); |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
pj_sock_close(ssock); |
| 584 |
pj_ioqueue_destroy(ioqueue); |
| 585 |
|
| 586 |
pj_pool_release(pool); |
| 587 |
|
| 588 |
return 0; |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
static int many_handles_test(const pj_ioqueue_cfg *cfg) |
| 598 |
{ |
| 599 |
enum { MAX = PJ_IOQUEUE_MAX_HANDLES }; |
| 600 |
pj_pool_t *pool; |
| 601 |
pj_ioqueue_t *ioqueue; |
| 602 |
pj_sock_t *sock; |
| 603 |
pj_ioqueue_key_t **key; |
| 604 |
pj_status_t rc; |
| 605 |
int count, i; |
| 606 |
|
| 607 |
PJ_LOG(3,(THIS_FILE,"...testing with so many handles")); |
| 608 |
|
| 609 |
pool = pj_pool_create(mem, NULL, 4000, 4000, NULL); |
| 610 |
if (!pool) |
| 611 |
return PJ_ENOMEM; |
| 612 |
|
| 613 |
key = (pj_ioqueue_key_t**) |
| 614 |
pj_pool_alloc(pool, MAX*sizeof(pj_ioqueue_key_t*)); |
| 615 |
sock = (pj_sock_t*) pj_pool_alloc(pool, MAX*sizeof(pj_sock_t)); |
| 616 |
|
| 617 |
|
| 618 |
rc = pj_ioqueue_create2(pool, MAX, cfg, &ioqueue); |
| 619 |
if (rc != PJ_SUCCESS || ioqueue == NULL) { |
| 620 |
app_perror("...error in pj_ioqueue_create", rc); |
| 621 |
return -10; |
| 622 |
} |
| 623 |
|
| 624 |
|
| 625 |
for (count=0; count<MAX; ++count) { |
| 626 |
sock[count] = PJ_INVALID_SOCKET; |
| 627 |
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &sock[count]); |
| 628 |
if (rc != PJ_SUCCESS || sock[count] == PJ_INVALID_SOCKET) { |
| 629 |
PJ_LOG(3,(THIS_FILE, "....unable to create %d-th socket, rc=%d", |
| 630 |
count, rc)); |
| 631 |
break; |
| 632 |
} |
| 633 |
key[count] = NULL; |
| 634 |
rc = pj_ioqueue_register_sock(pool, ioqueue, sock[count], |
| 635 |
NULL, &test_cb, &key[count]); |
| 636 |
if (rc != PJ_SUCCESS || key[count] == NULL) { |
| 637 |
PJ_LOG(3,(THIS_FILE, "....unable to register %d-th socket, rc=%d", |
| 638 |
count, rc)); |
| 639 |
return -30; |
| 640 |
} |
| 641 |
} |
| 642 |
|
| 643 |
|
| 644 |
|
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
|
| 656 |
|
| 657 |
|
| 658 |
for (i=count-1; i>=0; --i) { |
| 659 |
|
| 660 |
rc = pj_ioqueue_unregister(key[i]); |
| 661 |
if (rc != PJ_SUCCESS) { |
| 662 |
app_perror("...error in pj_ioqueue_unregister", rc); |
| 663 |
} |
| 664 |
} |
| 665 |
|
| 666 |
rc = pj_ioqueue_destroy(ioqueue); |
| 667 |
if (rc != PJ_SUCCESS) { |
| 668 |
app_perror("...error in pj_ioqueue_destroy", rc); |
| 669 |
} |
| 670 |
|
| 671 |
pj_pool_release(pool); |
| 672 |
|
| 673 |
PJ_LOG(3,(THIS_FILE,"....many_handles_test() ok")); |
| 674 |
|
| 675 |
return 0; |
| 676 |
} |
| 677 |
|
| 678 |
#if PJ_HAS_THREADS |
| 679 |
typedef struct parallel_recv_data |
| 680 |
{ |
| 681 |
unsigned buffer; |
| 682 |
pj_ssize_t len; |
| 683 |
} parallel_recv_data; |
| 684 |
|
| 685 |
|
| 686 |
static void on_read_complete2(pj_ioqueue_key_t *key, |
| 687 |
pj_ioqueue_op_key_t *op_key, |
| 688 |
pj_ssize_t bytes_read) |
| 689 |
{ |
| 690 |
unsigned *p_packet_cnt = (unsigned*) pj_ioqueue_get_user_data(key); |
| 691 |
parallel_recv_data *ud = (parallel_recv_data*)op_key->user_data; |
| 692 |
|
| 693 |
if (bytes_read < 0) { |
| 694 |
pj_status_t status = (pj_status_t)-bytes_read; |
| 695 |
|
| 696 |
if (status==PJ_STATUS_FROM_OS(PJ_BLOCKING_ERROR_VAL)) { |
| 697 |
TRACE__((THIS_FILE, "......recv() fail with status=%d, retrying", |
| 698 |
status)); |
| 699 |
ud->len = bytes_read = sizeof(ud->buffer); |
| 700 |
status = pj_ioqueue_recv(key, op_key, &ud->buffer, &ud->len, 0); |
| 701 |
if (status == PJ_EPENDING) |
| 702 |
return; |
| 703 |
} |
| 704 |
|
| 705 |
if (status != PJ_SUCCESS) { |
| 706 |
PJ_PERROR(3,(THIS_FILE, status, "......status=%d", status)); |
| 707 |
return; |
| 708 |
} |
| 709 |
} |
| 710 |
assert (bytes_read==sizeof(unsigned)); |
| 711 |
if (ud->buffer != *p_packet_cnt) { |
| 712 |
PJ_LOG(1,(THIS_FILE, "......error: invalid packet sequence " |
| 713 |
"(expecting %d, got %d)", |
| 714 |
*p_packet_cnt, ud->buffer)); |
| 715 |
} else { |
| 716 |
TRACE__((THIS_FILE, "......recv() sequence %d", ud->buffer)); |
| 717 |
} |
| 718 |
|
| 719 |
(*p_packet_cnt)++; |
| 720 |
} |
| 721 |
|
| 722 |
typedef struct parallel_thread_data |
| 723 |
{ |
| 724 |
pj_ioqueue_t *ioqueue; |
| 725 |
pj_bool_t quit_flag; |
| 726 |
unsigned id, timeout, wakeup_cnt, event_cnt, err_cnt; |
| 727 |
} parallel_thread_data; |
| 728 |
|
| 729 |
static int parallel_worker_thread(void *p) |
| 730 |
{ |
| 731 |
parallel_thread_data *arg = (parallel_thread_data*)p; |
| 732 |
pj_time_val t_end; |
| 733 |
|
| 734 |
pj_gettickcount(&t_end); |
| 735 |
t_end.sec += arg->timeout; |
| 736 |
|
| 737 |
while (!arg->quit_flag) { |
| 738 |
pj_time_val timeout = {0, 0}; |
| 739 |
int rc; |
| 740 |
|
| 741 |
timeout.sec = arg->timeout; |
| 742 |
rc = pj_ioqueue_poll(arg->ioqueue, &timeout); |
| 743 |
if (rc >= 1) { |
| 744 |
assert(rc==1); |
| 745 |
TRACE__((THIS_FILE, "......thread %d got event (total=%d)", |
| 746 |
arg->id, arg->wakeup_cnt)); |
| 747 |
++arg->wakeup_cnt; |
| 748 |
arg->event_cnt += rc; |
| 749 |
} else if (rc == 0) { |
| 750 |
if (!arg->quit_flag) { |
| 751 |
TRACE__((THIS_FILE, "......thread %d wakeup, no event (total=%d)", |
| 752 |
arg->id, arg->wakeup_cnt)); |
| 753 |
++arg->wakeup_cnt; |
| 754 |
} |
| 755 |
} else if (rc < 0) { |
| 756 |
TRACE__((THIS_FILE, "......thread %d got error", arg->id)); |
| 757 |
++arg->wakeup_cnt; |
| 758 |
++arg->err_cnt; |
| 759 |
} |
| 760 |
} |
| 761 |
|
| 762 |
return 0; |
| 763 |
} |
| 764 |
|
| 765 |
|
| 766 |
|
| 767 |
|
| 768 |
|
| 769 |
|
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
static int parallel_recv_test(const pj_ioqueue_cfg *cfg) |
| 775 |
{ |
| 776 |
pj_pool_t *pool; |
| 777 |
pj_sock_t ssock = PJ_INVALID_SOCKET, csock = PJ_INVALID_SOCKET; |
| 778 |
pj_ioqueue_t *ioqueue = NULL; |
| 779 |
pj_ioqueue_key_t *skey = NULL; |
| 780 |
pj_ioqueue_callback cb; |
| 781 |
enum { |
| 782 |
ASYNC_CNT = 16, |
| 783 |
PKT_SIZE = 16, |
| 784 |
SEND_DELAY_MSECS = 250, |
| 785 |
TIMEOUT_SECS = (SEND_DELAY_MSECS*ASYNC_CNT/1000)+2, |
| 786 |
}; |
| 787 |
typedef int packet_t; |
| 788 |
pj_thread_t *threads[ASYNC_CNT]; |
| 789 |
parallel_thread_data thread_datas[ASYNC_CNT], threads_total; |
| 790 |
pj_ioqueue_op_key_t recv_ops[ASYNC_CNT]; |
| 791 |
parallel_recv_data recv_datas[ASYNC_CNT]; |
| 792 |
unsigned i, async_send = 0, recv_packet_count = 0; |
| 793 |
int retcode; |
| 794 |
|
| 795 |
pool = pj_pool_create(mem, "test", 4000, 4000, NULL); |
| 796 |
if (!pool) { |
| 797 |
app_perror("Unable to create pool", PJ_ENOMEM); |
| 798 |
return -1100; |
| 799 |
} |
| 800 |
|
| 801 |
CHECK(-1110, app_socketpair(pj_AF_INET(), pj_SOCK_STREAM(), 0, |
| 802 |
&ssock, &csock)); |
| 803 |
CHECK(-1120, pj_ioqueue_create2(pool, 2, cfg, &ioqueue)); |
| 804 |
|
| 805 |
pj_bzero(&cb, sizeof(cb)); |
| 806 |
cb.on_read_complete = &on_read_complete2; |
| 807 |
CHECK(-1130, pj_ioqueue_register_sock(pool, ioqueue, ssock, &recv_packet_count, |
| 808 |
&cb, &skey)); |
| 809 |
|
| 810 |
|
| 811 |
pj_bzero(recv_datas, sizeof(recv_datas)); |
| 812 |
for (i=0; i<ASYNC_CNT; ++i) { |
| 813 |
pj_ioqueue_op_key_init(&recv_ops[i], sizeof(pj_ioqueue_op_key_t)); |
| 814 |
recv_ops[i].user_data = &recv_datas[i]; |
| 815 |
recv_datas[i].len = sizeof(packet_t); |
| 816 |
CHECK(-1140, pj_ioqueue_recv(skey, &recv_ops[i], &recv_datas[i].buffer, |
| 817 |
&recv_datas[i].len, 0)); |
| 818 |
} |
| 819 |
|
| 820 |
|
| 821 |
pj_bzero(thread_datas, sizeof(thread_datas)); |
| 822 |
for (i=0; i<ASYNC_CNT; ++i) { |
| 823 |
parallel_thread_data *arg = &thread_datas[i]; |
| 824 |
arg->ioqueue = ioqueue; |
| 825 |
arg->id = i; |
| 826 |
arg->timeout = TIMEOUT_SECS; |
| 827 |
|
| 828 |
CHECK(-1150, pj_thread_create(pool, "parallel_thread", |
| 829 |
parallel_worker_thread, arg, |
| 830 |
0, 0,&threads[i])); |
| 831 |
} |
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
pj_thread_sleep(100); |
| 837 |
for (i=0; i<ASYNC_CNT; ++i) { |
| 838 |
packet_t send_buf = i; |
| 839 |
pj_ssize_t len = sizeof(send_buf); |
| 840 |
pj_status_t status; |
| 841 |
|
| 842 |
pj_thread_sleep((i>0)*SEND_DELAY_MSECS); |
| 843 |
TRACE__((THIS_FILE, "....sending")); |
| 844 |
status = pj_sock_send(csock, &send_buf, &len, 0); |
| 845 |
if (status==PJ_EPENDING) { |
| 846 |
++async_send; |
| 847 |
TRACE__((THIS_FILE, "......(was async sent)")); |
| 848 |
} else if (status != PJ_SUCCESS) { |
| 849 |
PJ_PERROR(1,(THIS_FILE, status, "......send error")); |
| 850 |
retcode = -1160; |
| 851 |
goto on_return; |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
|
| 856 |
for (i=0; i<ASYNC_CNT; ++i) { |
| 857 |
parallel_thread_data *arg = &thread_datas[i]; |
| 858 |
arg->quit_flag = 1; |
| 859 |
} |
| 860 |
|
| 861 |
|
| 862 |
for (i=0; i<ASYNC_CNT; ++i) { |
| 863 |
CHECK(-1170, pj_thread_join(threads[i])); |
| 864 |
CHECK(-1180, pj_thread_destroy(threads[i])); |
| 865 |
} |
| 866 |
|
| 867 |
|
| 868 |
PJ_LOG(3,(THIS_FILE, "....Threads statistics:")); |
| 869 |
PJ_LOG(3,(THIS_FILE, " =============================")); |
| 870 |
PJ_LOG(3,(THIS_FILE, " Thread Wakeups Events Errors")); |
| 871 |
PJ_LOG(3,(THIS_FILE, " =============================")); |
| 872 |
pj_bzero(&threads_total, sizeof(threads_total)); |
| 873 |
for (i=0; i<ASYNC_CNT; ++i) { |
| 874 |
parallel_thread_data *arg = &thread_datas[i]; |
| 875 |
|
| 876 |
threads_total.wakeup_cnt += arg->wakeup_cnt; |
| 877 |
threads_total.event_cnt += arg->event_cnt; |
| 878 |
threads_total.err_cnt += arg->err_cnt; |
| 879 |
|
| 880 |
PJ_LOG(3,(THIS_FILE, " %6d %6d %6d %6d", |
| 881 |
arg->id, arg->wakeup_cnt, arg->event_cnt, arg->err_cnt)); |
| 882 |
} |
| 883 |
|
| 884 |
retcode = 0; |
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
if (recv_packet_count != ASYNC_CNT) { |
| 889 |
PJ_LOG(1,(THIS_FILE, "....error: rx packet count is %d (expecting %d)", |
| 890 |
recv_packet_count, ASYNC_CNT)); |
| 891 |
retcode = -1190; |
| 892 |
} |
| 893 |
if (threads_total.wakeup_cnt > ASYNC_CNT+async_send) { |
| 894 |
PJ_LOG(3,(THIS_FILE, "....info: total wakeup count is %d " |
| 895 |
"(the perfect count is %d). This shows that " |
| 896 |
"threads are woken up without getting any events", |
| 897 |
threads_total.wakeup_cnt, ASYNC_CNT+async_send)); |
| 898 |
} |
| 899 |
if (threads_total.err_cnt > 0) { |
| 900 |
PJ_LOG(3,(THIS_FILE, "....info: total error count is %d " |
| 901 |
"(it should be 0)", |
| 902 |
threads_total.err_cnt)); |
| 903 |
} |
| 904 |
|
| 905 |
if (retcode==0) |
| 906 |
PJ_LOG(3,(THIS_FILE, "....success")); |
| 907 |
|
| 908 |
on_return: |
| 909 |
if (skey) |
| 910 |
pj_ioqueue_unregister(skey); |
| 911 |
if (csock != PJ_INVALID_SOCKET) |
| 912 |
pj_sock_close(csock); |
| 913 |
if (ioqueue) |
| 914 |
pj_ioqueue_destroy(ioqueue); |
| 915 |
pj_pool_release(pool); |
| 916 |
return retcode; |
| 917 |
} |
| 918 |
|
| 919 |
#endif |
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
static int bench_test(const pj_ioqueue_cfg *cfg, int bufsize, |
| 929 |
int inactive_sock_count) |
| 930 |
{ |
| 931 |
pj_sock_t ssock=-1, csock=-1; |
| 932 |
pj_sockaddr_in addr; |
| 933 |
unsigned short port; |
| 934 |
pj_pool_t *pool = NULL; |
| 935 |
pj_sock_t *inactive_sock=NULL; |
| 936 |
pj_ioqueue_op_key_t *inactive_read_op; |
| 937 |
char *send_buf, *recv_buf; |
| 938 |
pj_ioqueue_t *ioque = NULL; |
| 939 |
pj_ioqueue_key_t *skey, *ckey, *keys[SOCK_INACTIVE_MAX+2]; |
| 940 |
pj_timestamp t1, t2, t_elapsed; |
| 941 |
int rc=0, i; |
| 942 |
pj_str_t temp; |
| 943 |
|
| 944 |
TRACE__((THIS_FILE, " bench test %d", inactive_sock_count)); |
| 945 |
|
| 946 |
|
| 947 |
pool = pj_pool_create(mem, NULL, POOL_SIZE, 4000, NULL); |
| 948 |
|
| 949 |
|
| 950 |
send_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 951 |
recv_buf = (char*)pj_pool_alloc(pool, bufsize); |
| 952 |
|
| 953 |
|
| 954 |
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &ssock); |
| 955 |
if (rc == PJ_SUCCESS) { |
| 956 |
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &csock); |
| 957 |
} else |
| 958 |
csock = PJ_INVALID_SOCKET; |
| 959 |
if (rc != PJ_SUCCESS) { |
| 960 |
app_perror("...error: pj_sock_socket()", rc); |
| 961 |
goto on_error; |
| 962 |
} |
| 963 |
|
| 964 |
|
| 965 |
pj_bzero(&addr, sizeof(addr)); |
| 966 |
addr.sin_family = pj_AF_INET(); |
| 967 |
PJ_TEST_SUCCESS(pj_sock_bind(ssock, &addr, sizeof(addr)), NULL, |
| 968 |
goto on_error); |
| 969 |
|
| 970 |
|
| 971 |
i = sizeof(addr); |
| 972 |
PJ_TEST_SUCCESS(pj_sock_getsockname(ssock, &addr, &i), NULL, |
| 973 |
goto on_error); |
| 974 |
port = pj_sockaddr_get_port(&addr); |
| 975 |
|
| 976 |
pj_assert(inactive_sock_count+2 <= PJ_IOQUEUE_MAX_HANDLES); |
| 977 |
|
| 978 |
|
| 979 |
rc = pj_ioqueue_create2(pool, PJ_IOQUEUE_MAX_HANDLES, cfg, &ioque); |
| 980 |
if (rc != PJ_SUCCESS) { |
| 981 |
app_perror("...error: pj_ioqueue_create()", rc); |
| 982 |
goto on_error; |
| 983 |
} |
| 984 |
|
| 985 |
|
| 986 |
|
| 987 |
inactive_sock = (pj_sock_t*)pj_pool_alloc(pool, |
| 988 |
inactive_sock_count*sizeof(pj_sock_t)); |
| 989 |
inactive_read_op = (pj_ioqueue_op_key_t*)pj_pool_alloc(pool, |
| 990 |
inactive_sock_count*sizeof(pj_ioqueue_op_key_t)); |
| 991 |
pj_bzero(&addr, sizeof(addr)); |
| 992 |
addr.sin_family = pj_AF_INET(); |
| 993 |
for (i=0; i<inactive_sock_count; ++i) { |
| 994 |
pj_ssize_t bytes; |
| 995 |
|
| 996 |
rc = pj_sock_socket(pj_AF_INET(), pj_SOCK_DGRAM(), 0, &inactive_sock[i]); |
| 997 |
if (rc != PJ_SUCCESS || inactive_sock[i] < 0) { |
| 998 |
app_perror("...error: pj_sock_socket()", rc); |
| 999 |
goto on_error; |
| 1000 |
} |
| 1001 |
if ((rc=pj_sock_bind(inactive_sock[i], &addr, sizeof(addr))) != 0) { |
| 1002 |
pj_sock_close(inactive_sock[i]); |
| 1003 |
inactive_sock[i] = PJ_INVALID_SOCKET; |
| 1004 |
app_perror("...error: pj_sock_bind()", rc); |
| 1005 |
goto on_error; |
| 1006 |
} |
| 1007 |
rc = pj_ioqueue_register_sock(pool, ioque, inactive_sock[i], |
| 1008 |
NULL, &test_cb, &keys[i]); |
| 1009 |
if (rc != PJ_SUCCESS) { |
| 1010 |
pj_sock_close(inactive_sock[i]); |
| 1011 |
inactive_sock[i] = PJ_INVALID_SOCKET; |
| 1012 |
app_perror("...error(1): pj_ioqueue_register_sock()", rc); |
| 1013 |
PJ_LOG(3,(THIS_FILE, "....i=%d", i)); |
| 1014 |
goto on_error; |
| 1015 |
} |
| 1016 |
bytes = bufsize; |
| 1017 |
pj_ioqueue_op_key_init(&inactive_read_op[i], |
| 1018 |
sizeof(inactive_read_op[i])); |
| 1019 |
rc = pj_ioqueue_recv(keys[i], &inactive_read_op[i], recv_buf, &bytes, 0); |
| 1020 |
if (rc != PJ_EPENDING) { |
| 1021 |
pj_sock_close(inactive_sock[i]); |
| 1022 |
inactive_sock[i] = PJ_INVALID_SOCKET; |
| 1023 |
app_perror("...error: pj_ioqueue_read()", rc); |
| 1024 |
goto on_error; |
| 1025 |
} |
| 1026 |
} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
rc = pj_ioqueue_register_sock(pool, ioque, ssock, NULL, |
| 1032 |
&test_cb, &skey); |
| 1033 |
if (rc != PJ_SUCCESS) { |
| 1034 |
app_perror("...error(2): pj_ioqueue_register_sock()", rc); |
| 1035 |
goto on_error; |
| 1036 |
} |
| 1037 |
|
| 1038 |
rc = pj_ioqueue_register_sock(pool, ioque, csock, NULL, |
| 1039 |
&test_cb, &ckey); |
| 1040 |
if (rc != PJ_SUCCESS) { |
| 1041 |
app_perror("...error(3): pj_ioqueue_register_sock()", rc); |
| 1042 |
goto on_error; |
| 1043 |
} |
| 1044 |
|
| 1045 |
|
| 1046 |
pj_sockaddr_in_init(&addr, pj_cstr(&temp, "127.0.0.1"), port); |
| 1047 |
|
| 1048 |
|
| 1049 |
t_elapsed.u64 = 0; |
| 1050 |
for (i=0; i<LOOP; ++i) { |
| 1051 |
pj_ssize_t bytes; |
| 1052 |
pj_ioqueue_op_key_t read_op, write_op; |
| 1053 |
|
| 1054 |
|
| 1055 |
pj_create_random_string(send_buf, bufsize); |
| 1056 |
|
| 1057 |
|
| 1058 |
pj_ioqueue_op_key_init(&read_op, sizeof(read_op)); |
| 1059 |
pj_ioqueue_op_key_init(&write_op, sizeof(write_op)); |
| 1060 |
|
| 1061 |
|
| 1062 |
bytes = bufsize; |
| 1063 |
rc = pj_ioqueue_recv(skey, &read_op, recv_buf, &bytes, 0); |
| 1064 |
if (rc != PJ_EPENDING) { |
| 1065 |
app_perror("...error: pj_ioqueue_read()", rc); |
| 1066 |
break; |
| 1067 |
} |
| 1068 |
|
| 1069 |
|
| 1070 |
bytes = bufsize; |
| 1071 |
rc = pj_ioqueue_sendto(ckey, &write_op, send_buf, &bytes, 0, |
| 1072 |
&addr, sizeof(addr)); |
| 1073 |
if (rc != PJ_SUCCESS && rc != PJ_EPENDING) { |
| 1074 |
app_perror("...error: pj_ioqueue_write()", rc); |
| 1075 |
break; |
| 1076 |
} |
| 1077 |
if (rc == PJ_SUCCESS) { |
| 1078 |
if (bytes < 0) { |
| 1079 |
app_perror("...error: pj_ioqueue_sendto()",(pj_status_t)-bytes); |
| 1080 |
break; |
| 1081 |
} |
| 1082 |
} |
| 1083 |
|
| 1084 |
|
| 1085 |
pj_get_timestamp(&t1); |
| 1086 |
|
| 1087 |
|
| 1088 |
callback_read_key = NULL; |
| 1089 |
callback_read_size = 0; |
| 1090 |
TRACE__((THIS_FILE, " waiting for key = %p", skey)); |
| 1091 |
do { |
| 1092 |
pj_time_val timeout = { 1, 0 }; |
| 1093 |
#ifdef PJ_SYMBIAN |
| 1094 |
rc = pj_symbianos_poll(-1, PJ_TIME_VAL_MSEC(timeout)); |
| 1095 |
#else |
| 1096 |
rc = pj_ioqueue_poll(ioque, &timeout); |
| 1097 |
#endif |
| 1098 |
TRACE__((THIS_FILE, " poll rc=%d", rc)); |
| 1099 |
} while (rc >= 0 && callback_read_key != skey); |
| 1100 |
|
| 1101 |
|
| 1102 |
pj_get_timestamp(&t2); |
| 1103 |
t_elapsed.u64 += (t2.u64 - t1.u64); |
| 1104 |
|
| 1105 |
if (rc < 0) { |
| 1106 |
app_perror(" error: pj_ioqueue_poll", -rc); |
| 1107 |
break; |
| 1108 |
} |
| 1109 |
|
| 1110 |
|
| 1111 |
if (callback_read_size != bufsize || |
| 1112 |
pj_memcmp(send_buf, recv_buf, bufsize)) |
| 1113 |
{ |
| 1114 |
rc = -10; |
| 1115 |
PJ_LOG(3,(THIS_FILE, " error: size/buffer mismatch")); |
| 1116 |
break; |
| 1117 |
} |
| 1118 |
|
| 1119 |
|
| 1120 |
do { |
| 1121 |
pj_time_val timeout = { 0, 10 }; |
| 1122 |
#ifdef PJ_SYMBIAN |
| 1123 |
PJ_UNUSED_ARG(timeout); |
| 1124 |
rc = pj_symbianos_poll(-1, 100); |
| 1125 |
#else |
| 1126 |
rc = pj_ioqueue_poll(ioque, &timeout); |
| 1127 |
#endif |
| 1128 |
} while (rc>0); |
| 1129 |
|
| 1130 |
rc = 0; |
| 1131 |
} |
| 1132 |
|
| 1133 |
|
| 1134 |
if (rc == 0) { |
| 1135 |
pj_timestamp tzero; |
| 1136 |
pj_uint32_t usec_delay; |
| 1137 |
|
| 1138 |
tzero.u32.hi = tzero.u32.lo = 0; |
| 1139 |
usec_delay = pj_elapsed_usec( &tzero, &t_elapsed); |
| 1140 |
|
| 1141 |
PJ_LOG(3, (THIS_FILE, "...%10d %15d % 9d", |
| 1142 |
bufsize, inactive_sock_count, usec_delay)); |
| 1143 |
|
| 1144 |
} else { |
| 1145 |
PJ_LOG(2, (THIS_FILE, "...ERROR rc=%d (buf:%d, fds:%d)", |
| 1146 |
rc, bufsize, inactive_sock_count+2)); |
| 1147 |
} |
| 1148 |
|
| 1149 |
|
| 1150 |
for (i=inactive_sock_count-1; i>=0; --i) { |
| 1151 |
pj_ioqueue_unregister(keys[i]); |
| 1152 |
} |
| 1153 |
|
| 1154 |
pj_ioqueue_unregister(skey); |
| 1155 |
pj_ioqueue_unregister(ckey); |
| 1156 |
|
| 1157 |
|
| 1158 |
pj_ioqueue_destroy(ioque); |
| 1159 |
pj_pool_release( pool); |
| 1160 |
return rc; |
| 1161 |
|
| 1162 |
on_error: |
| 1163 |
PJ_PERROR(1,(THIS_FILE, pj_get_netos_error(), "...ERROR")); |
| 1164 |
if (ssock >= 0) |
| 1165 |
pj_sock_close(ssock); |
| 1166 |
if (csock >= 0) |
| 1167 |
pj_sock_close(csock); |
| 1168 |
for (i=0; i<inactive_sock_count && inactive_sock && |
| 1169 |
inactive_sock[i]!=PJ_INVALID_SOCKET; ++i) |
| 1170 |
{ |
| 1171 |
pj_sock_close(inactive_sock[i]); |
| 1172 |
} |
| 1173 |
if (ioque != NULL) |
| 1174 |
pj_ioqueue_destroy(ioque); |
| 1175 |
pj_pool_release( pool); |
| 1176 |
return -1; |
| 1177 |
} |
| 1178 |
|
| 1179 |
static int udp_ioqueue_test_imp(const pj_ioqueue_cfg *cfg) |
| 1180 |
{ |
| 1181 |
int status; |
| 1182 |
int bufsize, sock_count; |
| 1183 |
char title[64]; |
| 1184 |
|
| 1185 |
pj_ansi_snprintf(title, sizeof(title), "%s (concur:%d, epoll_flags:0x%x)", |
| 1186 |
pj_ioqueue_name(), cfg->default_concurrency, |
| 1187 |
cfg->epoll_flags); |
| 1188 |
|
| 1189 |
|
| 1190 |
PJ_LOG(3, (THIS_FILE, "...compliance test (%s)", title)); |
| 1191 |
if ((status=compliance_test(cfg)) != 0) { |
| 1192 |
return status; |
| 1193 |
} |
| 1194 |
PJ_LOG(3, (THIS_FILE, "....compliance test ok")); |
| 1195 |
|
| 1196 |
|
| 1197 |
PJ_LOG(3, (THIS_FILE, "...unregister test (%s)", title)); |
| 1198 |
if ((status=unregister_test(cfg)) != 0) { |
| 1199 |
return status; |
| 1200 |
} |
| 1201 |
PJ_LOG(3, (THIS_FILE, "....unregister test ok")); |
| 1202 |
|
| 1203 |
if ((status=many_handles_test(cfg)) != 0) { |
| 1204 |
return status; |
| 1205 |
} |
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
PJ_LOG(4, (THIS_FILE, "...benchmarking different buffer size:")); |
| 1210 |
PJ_LOG(4, (THIS_FILE, "... note: buf=bytes sent, fds=# of fds, " |
| 1211 |
"elapsed=in timer ticks")); |
| 1212 |
|
| 1213 |
|
| 1214 |
PJ_LOG(3, (THIS_FILE, "...Benchmarking poll times for %s:", title)); |
| 1215 |
PJ_LOG(3, (THIS_FILE, "...=====================================")); |
| 1216 |
PJ_LOG(3, (THIS_FILE, "...Buf.size #inactive-socks Time/poll")); |
| 1217 |
PJ_LOG(3, (THIS_FILE, "... (bytes) (nanosec)")); |
| 1218 |
PJ_LOG(3, (THIS_FILE, "...=====================================")); |
| 1219 |
|
| 1220 |
|
| 1221 |
|
| 1222 |
for (bufsize=BUF_MIN_SIZE; bufsize <= BUF_MAX_SIZE; bufsize *= 2) { |
| 1223 |
if ((status=bench_test(cfg, bufsize, SOCK_INACTIVE_MIN)) != 0) |
| 1224 |
return status; |
| 1225 |
} |
| 1226 |
|
| 1227 |
bufsize = 512; |
| 1228 |
for (sock_count=SOCK_INACTIVE_MIN+2; |
| 1229 |
sock_count<=SOCK_INACTIVE_MAX+2; |
| 1230 |
sock_count *= 2) |
| 1231 |
{ |
| 1232 |
|
| 1233 |
if ((status=bench_test(cfg, bufsize, sock_count-2)) != 0) |
| 1234 |
return status; |
| 1235 |
} |
| 1236 |
return 0; |
| 1237 |
} |
| 1238 |
|
| 1239 |
int udp_ioqueue_test() |
| 1240 |
{ |
| 1241 |
pj_ioqueue_epoll_flag epoll_flags[] = { |
| 1242 |
#if PJ_IOQUEUE_IMP==PJ_IOQUEUE_IMP_EPOLL |
| 1243 |
PJ_IOQUEUE_EPOLL_AUTO, |
| 1244 |
PJ_IOQUEUE_EPOLL_EXCLUSIVE, |
| 1245 |
PJ_IOQUEUE_EPOLL_ONESHOT, |
| 1246 |
0, |
| 1247 |
#else |
| 1248 |
PJ_IOQUEUE_EPOLL_AUTO, |
| 1249 |
#endif |
| 1250 |
}; |
| 1251 |
pj_bool_t concurs[] = { PJ_TRUE, PJ_FALSE }; |
| 1252 |
int i, rc; |
| 1253 |
|
| 1254 |
for (i=0; i<(int)PJ_ARRAY_SIZE(epoll_flags); ++i) { |
| 1255 |
pj_ioqueue_cfg cfg; |
| 1256 |
|
| 1257 |
pj_ioqueue_cfg_default(&cfg); |
| 1258 |
cfg.epoll_flags = epoll_flags[i]; |
| 1259 |
|
| 1260 |
PJ_LOG(3, (THIS_FILE, "..%s UDP compliance test, epoll_flags=0x%x", |
| 1261 |
pj_ioqueue_name(), cfg.epoll_flags)); |
| 1262 |
|
| 1263 |
rc = udp_ioqueue_test_imp(&cfg); |
| 1264 |
if (rc) return rc; |
| 1265 |
|
| 1266 |
} |
| 1267 |
|
| 1268 |
for (i=0; i<(int)PJ_ARRAY_SIZE(concurs); ++i) { |
| 1269 |
pj_ioqueue_cfg cfg; |
| 1270 |
|
| 1271 |
pj_ioqueue_cfg_default(&cfg); |
| 1272 |
cfg.default_concurrency = concurs[i]; |
| 1273 |
|
| 1274 |
PJ_LOG(3, (THIS_FILE, "..%s UDP compliance test, concurrency=%d", |
| 1275 |
pj_ioqueue_name(), cfg.default_concurrency)); |
| 1276 |
|
| 1277 |
rc = udp_ioqueue_test_imp(&cfg); |
| 1278 |
if (rc) return rc; |
| 1279 |
} |
| 1280 |
|
| 1281 |
#if PJ_HAS_THREADS |
| 1282 |
for (i=0; i<(int)PJ_ARRAY_SIZE(epoll_flags); ++i) { |
| 1283 |
pj_ioqueue_cfg cfg; |
| 1284 |
|
| 1285 |
pj_ioqueue_cfg_default(&cfg); |
| 1286 |
cfg.epoll_flags = epoll_flags[i]; |
| 1287 |
|
| 1288 |
PJ_LOG(3, (THIS_FILE, "..%s UDP parallel compliance test, epoll_flags=0x%x", |
| 1289 |
pj_ioqueue_name(), cfg.epoll_flags)); |
| 1290 |
|
| 1291 |
rc = parallel_recv_test(&cfg); |
| 1292 |
if (rc) return rc; |
| 1293 |
|
| 1294 |
} |
| 1295 |
#endif |
| 1296 |
|
| 1297 |
return 0; |
| 1298 |
} |
| 1299 |
|
| 1300 |
#else |
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
int dummy_uiq_udp; |
| 1305 |
#endif |
| 1306 |
|
| 1307 |
|