Debian Codegraph @

Xpjproject/pjlib/src/pjlib-test/timer.c

raw symbols SCIP
1 /*
2 * Copyright (C) 2008-2011 Teluu Inc. (http://www.teluu.com)
3 * Copyright (C) 2003-2008 Benny Prijono <[email protected]>
4 *
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 /**
22 * \page page_pjlib_timer_test Test: Timer
23 *
24 * This file provides implementation of \b timer_test(). It tests the
25 * functionality of the timer heap.
26 *
27 *
28 * This file is <b>pjlib-test/timer.c</b>
29 *
30 * \include pjlib-test/timer.c
31 */
32
33
34 #if INCLUDE_TIMER_TEST
35
36 #include <pjlib.h>
37
38 #define LOOP 16
39 #define MIN_COUNT 250
40 #define MAX_COUNT (LOOP * MIN_COUNT)
41 #define MIN_DELAY 2
42 #define D (MAX_COUNT / 32000)
43 #define DELAY (D < MIN_DELAY ? MIN_DELAY : D)
44 #define THIS_FILE "timer_test"
45
46
47 static void timer_callback(pj_timer_heap_t *ht, pj_timer_entry *e)
48 {
49 PJ_UNUSED_ARG(ht);
50 PJ_UNUSED_ARG(e);
51 }
52
53 static int test_timer_heap(void)
54 {
55 int i, j;
56 pj_timer_entry *entry;
57 pj_pool_t *pool;
58 pj_timer_heap_t *timer;
59 pj_time_val delay;
60 pj_status_t status;
61 int err=0;
62 pj_size_t size;
63 unsigned count;
64
65 PJ_LOG(3,("test", "...Basic test"));
66
67 size = pj_timer_heap_mem_size(MAX_COUNT)+MAX_COUNT*sizeof(pj_timer_entry);
68 pool = pj_pool_create( mem, NULL, size, 4000, NULL);
69 if (!pool) {
70 PJ_LOG(3,("test", "...error: unable to create pool of %lu bytes",
71 (unsigned long)size));
72 return -10;
73 }
74
75 entry = (pj_timer_entry*)pj_pool_calloc(pool, MAX_COUNT, sizeof(*entry));
76 if (!entry)
77 return -20;
78
79 for (i=0; i<MAX_COUNT; ++i) {
80 entry[i].cb = &timer_callback;
81 }
82 status = pj_timer_heap_create(pool, MAX_COUNT, &timer);
83 if (status != PJ_SUCCESS) {
84 app_perror("...error: unable to create timer heap", status);
85 return -30;
86 }
87
88 count = MIN_COUNT;
89 for (i=0; i<LOOP; ++i) {
90 int early = 0;
91 //int done=0;
92 int cancelled=0;
93 int rc;
94 pj_timestamp t1, t2, t_sched, t_cancel, t_poll;
95 pj_time_val now, expire;
96
97 t_sched.u32.lo = t_cancel.u32.lo = t_poll.u32.lo = 0;
98
99 // Register timers
100 for (j=0; j<(int)count; ++j) {
101 delay.sec = pj_rand() % DELAY;
102 delay.msec = pj_rand() % 1000;
103
104 // Schedule timer
105 pj_get_timestamp(&t1);
106 rc = pj_timer_heap_schedule(timer, &entry[j], &delay);
107 if (rc != 0)
108 return -40;
109 pj_get_timestamp(&t2);
110
111 t_sched.u32.lo += (t2.u32.lo - t1.u32.lo);
112
113 // Poll timers.
114 pj_get_timestamp(&t1);
115 rc = pj_timer_heap_poll(timer, NULL);
116 pj_get_timestamp(&t2);
117 if (rc > 0) {
118 t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
119 early += rc;
120 }
121 }
122
123 // Set the time where all timers should finish
124 pj_gettimeofday(&expire);
125 delay.sec = DELAY;
126 delay.msec = 0;
127 PJ_TIME_VAL_ADD(expire, delay);
128
129 // Wait unfil all timers finish, cancel some of them.
130 do {
131 int index = pj_rand() % count;
132 pj_get_timestamp(&t1);
133 rc = pj_timer_heap_cancel(timer, &entry[index]);
134 pj_get_timestamp(&t2);
135 if (rc > 0) {
136 cancelled += rc;
137 t_cancel.u32.lo += (t2.u32.lo - t1.u32.lo);
138 }
139
140 pj_gettimeofday(&now);
141
142 pj_get_timestamp(&t1);
143 #if defined(PJ_SYMBIAN) && PJ_SYMBIAN!=0
144 /* On Symbian, we must use OS poll (Active Scheduler poll) since
145 * timer is implemented using Active Object.
146 */
147 rc = 0;
148 while (pj_symbianos_poll(-1, 0))
149 ++rc;
150 #else
151 rc = pj_timer_heap_poll(timer, NULL);
152 #endif
153 pj_get_timestamp(&t2);
154 if (rc > 0) {
155 //done += rc;
156 t_poll.u32.lo += (t2.u32.lo - t1.u32.lo);
157 }
158
159 } while (PJ_TIME_VAL_LTE(now, expire)&&pj_timer_heap_count(timer) > 0);
160
161 if (pj_timer_heap_count(timer)) {
162 PJ_LOG(3, (THIS_FILE, "ERROR: %lu timers left",
163 (unsigned long)pj_timer_heap_count(timer)));
164 ++err;
165 }
166 t_sched.u32.lo /= count;
167 t_cancel.u32.lo /= count;
168 t_poll.u32.lo /= count;
169 PJ_LOG(4, (THIS_FILE,
170 "...ok (count:%d, early:%d, cancelled:%d, "
171 "sched:%d, cancel:%d poll:%d)",
172 count, early, cancelled, t_sched.u32.lo, t_cancel.u32.lo,
173 t_poll.u32.lo));
174
175 count = count * 2;
176 if (count > MAX_COUNT)
177 break;
178 }
179
180 pj_pool_release(pool);
181 return err;
182 }
183
184
185 /***************
186 * Stress test *
187 ***************
188 * Test scenario (if RANDOMIZED_TEST is 0):
189 * 1. Create and schedule a number of timer entries.
190 * 2. Start threads for polling (simulating normal worker thread).
191 * Each expired entry will try to cancel and re-schedule itself
192 * from within the callback.
193 * 3. Start threads for cancelling random entries. Each successfully
194 * cancelled entry will be re-scheduled after some random delay.
195 *
196 * Test scenario (if RANDOMIZED_TEST is 1):
197 * 1. Create and schedule a number of timer entries.
198 * 2. Start threads which will, based on a configurable probability
199 * setting, randomly perform timer scheduling, cancelling, or
200 * polling (simulating normal worker thread).
201 * This test is considered a failure if:
202 * - It triggers assertion/crash.
203 * - There's an error message in the log, which indicates a potential
204 * bug in the implementation (note that race message is ok).
205 */
206 #define RANDOMIZED_TEST 1
207
208 #ifndef __has_feature
209 #define __has_feature(x) 0
210 #endif
211 #if defined(__SANITIZE_ADDRESS__) || \
212 (defined(__has_feature) && __has_feature(address_sanitizer))
213 #define ASAN_ENABLED 1
214 #else
215 #define ASAN_ENABLED 0
216 #endif
217
218 /* We should only simulate timer crash when using duplicate and
219 * not using ASan.
220 */
221 #define SIMULATE_CRASH PJ_TIMER_USE_COPY && !ASAN_ENABLED
222
223 #if RANDOMIZED_TEST
224 #define ST_STRESS_THREAD_COUNT 20
225 #define ST_POLL_THREAD_COUNT 0
226 #define ST_CANCEL_THREAD_COUNT 0
227 #else
228 #define ST_STRESS_THREAD_COUNT 0
229 #define ST_POLL_THREAD_COUNT 10
230 #define ST_CANCEL_THREAD_COUNT 10
231 #endif
232
233 #define ST_ENTRY_COUNT 10000
234 #define ST_DURATION 30000
235 #define ST_ENTRY_MAX_TIMEOUT_MS ST_DURATION/10
236
237 /* Number of group lock, may be zero, shared by timer entries, group lock
238 * can be useful to evaluate poll vs cancel race condition scenario, i.e:
239 * each group lock must have ref count==1 at the end of the test, otherwise
240 * assertion will raise.
241 */
242 #define ST_ENTRY_GROUP_LOCK_COUNT 1
243
244 #define BT_ENTRY_COUNT 100000
245 #define BT_ENTRY_SHOW_START 100
246 #define BT_ENTRY_SHOW_MULT 10
247 #define BT_REPEAT_RANDOM_TEST 4
248 #define BT_REPEAT_INC_TEST 4
249
250 struct thread_param
251 {
252 pj_timer_heap_t *timer;
253 pj_bool_t stopping;
254 pj_timer_entry *entries;
255 pj_atomic_t **status;
256 pj_atomic_t *n_sched, *n_cancel, *n_poll;
257 pj_grp_lock_t **grp_locks;
258 int err;
259
260 pj_atomic_t *idx;
261 struct {
262 pj_bool_t is_poll;
263 unsigned cnt;
264 } stat[ST_POLL_THREAD_COUNT + ST_CANCEL_THREAD_COUNT + 1];
265 /* Plus one here to avoid compile warning of zero-sized array */
266 };
267
268 static pj_status_t st_schedule_entry(pj_timer_heap_t *ht, pj_timer_entry *e)
269 {
270 pj_time_val delay = {0};
271 pj_grp_lock_t *grp_lock = NULL;
272 pj_status_t status;
273 struct thread_param *tparam = (struct thread_param *)e->user_data;
274
275 if (ST_ENTRY_GROUP_LOCK_COUNT && pj_rand() % 10) {
276 /* About 90% of entries should have group lock */
277 grp_lock = tparam->grp_locks[pj_rand() % ST_ENTRY_GROUP_LOCK_COUNT];
278 }
279
280 delay.msec = pj_rand() % ST_ENTRY_MAX_TIMEOUT_MS;
281 pj_time_val_normalize(&delay);
282 status = pj_timer_heap_schedule_w_grp_lock(ht, e, &delay, 1, grp_lock);
283 return status;
284 }
285
286 static void dummy_callback(pj_timer_heap_t *ht, pj_timer_entry *e)
287 {
288 PJ_UNUSED_ARG(ht);
289 PJ_LOG(4,("test", "dummy callback called %p %p", e, e->user_data));
290 }
291
292 static void st_entry_callback(pj_timer_heap_t *ht, pj_timer_entry *e)
293 {
294 struct thread_param *tparam = (struct thread_param *)e->user_data;
295
296 #if RANDOMIZED_TEST
297 /* Make sure the flag has been set. */
298 while (pj_atomic_get(tparam->status[e - tparam->entries]) != 1)
299 pj_thread_sleep(10);
300 pj_atomic_set(tparam->status[e - tparam->entries], 0);
301 #endif
302
303 /* try to cancel this */
304 pj_timer_heap_cancel_if_active(ht, e, 10);
305
306 /* busy doing something */
307 pj_thread_sleep(pj_rand() % 50);
308
309 /* reschedule entry */
310 if (!ST_STRESS_THREAD_COUNT)
311 st_schedule_entry(ht, e);
312 }
313
314 /* Randomized stress worker thread function. */
315 static int stress_worker(void *arg)
316 {
317 /* Enumeration of possible task. */
318 enum {
319 SCHEDULING = 0,
320 CANCELLING = 1,
321 POLLING = 2,
322 NOTHING = 3
323 };
324 /* Probability of a certain task being chosen.
325 * The first number indicates the probability of the first task,
326 * the second number for the second task, and so on.
327 */
328 int prob[3] = {75, 15, 5};
329 struct thread_param *tparam = (struct thread_param*)arg;
330 int t_idx, i;
331
332 t_idx = pj_atomic_inc_and_get(tparam->idx);
333 PJ_LOG(4,("test", "...thread #%d (random) started", t_idx));
334 while (!tparam->stopping) {
335 int job, task;
336 int idx, count;
337 pj_status_t prev_status, status;
338
339 /* Randomly choose which task to do */
340 job = pj_rand() % 100;
341 if (job < prob[0]) task = SCHEDULING;
342 else if (job < (prob[0] + prob[1])) task = CANCELLING;
343 else if (job < (prob[0] + prob[1] + prob[2])) task = POLLING;
344 else task = NOTHING;
345
346 idx = pj_rand() % ST_ENTRY_COUNT;
347 prev_status = pj_atomic_get(tparam->status[idx]);
348 if (task == SCHEDULING) {
349 if (prev_status != 0) continue;
350 status = st_schedule_entry(tparam->timer, &tparam->entries[idx]);
351 if (prev_status == 0 && status != PJ_SUCCESS) {
352 /* To make sure the flag has been set. */
353 pj_thread_sleep(20);
354 if (pj_atomic_get(tparam->status[idx]) == 1) {
355 /* Race condition with another scheduling. */
356 PJ_LOG(3,("test", "race schedule-schedule %d: %p",
357 idx, &tparam->entries[idx]));
358 } else {
359 if (tparam->err != 0) tparam->err = -210;
360 PJ_LOG(3,("test", "error: failed to schedule entry %d: %p",
361 idx, &tparam->entries[idx]));
362 }
363 } else if (prev_status == 1 && status == PJ_SUCCESS) {
364 /* Race condition with another cancellation or
365 * timer poll.
366 */
367 pj_thread_sleep(20);
368 PJ_LOG(3,("test", "race schedule-cancel/poll %d: %p",
369 idx, &tparam->entries[idx]));
370 }
371 if (status == PJ_SUCCESS) {
372 pj_atomic_set(tparam->status[idx], 1);
373 pj_atomic_inc(tparam->n_sched);
374 }
375 } else if (task == CANCELLING) {
376 count = pj_timer_heap_cancel_if_active(tparam->timer,
377 &tparam->entries[idx], 10);
378 if (prev_status == 0 && count > 0) {
379 /* To make sure the flag has been set. */
380 pj_thread_sleep(20);
381 if (pj_atomic_get(tparam->status[idx]) == 1) {
382 /* Race condition with scheduling. */
383 PJ_LOG(3,("test", "race cancel-schedule %d: %p",
384 idx, &tparam->entries[idx]));
385 } else {
386 if (tparam->err != 0) tparam->err = -220;
387 PJ_LOG(3,("test", "error: cancelling invalid entry %d: %p",
388 idx, &tparam->entries[idx]));
389 }
390 } else if (prev_status == 1 && count == 0) {
391 /* To make sure the flag has been cleared. */
392 pj_thread_sleep(20);
393 if (pj_atomic_get(tparam->status[idx]) == 0) {
394 /* Race condition with polling. */
395 PJ_LOG(3,("test", "race cancel-poll %d: %p",
396 idx, &tparam->entries[idx]));
397 } else {
398 if (tparam->err != 0) tparam->err = -230;
399 PJ_LOG(3,("test", "error: failed to cancel entry %d: %p",
400 idx, &tparam->entries[idx]));
401 }
402 }
403 if (count > 0) {
404 /* Make sure the flag has been set. */
405 while (pj_atomic_get(tparam->status[idx]) != 1)
406 pj_thread_sleep(10);
407 pj_atomic_set(tparam->status[idx], 0);
408 pj_atomic_inc(tparam->n_cancel);
409 }
410 } else if (task == POLLING) {
411 count = pj_timer_heap_poll(tparam->timer, NULL);
412 for (i = 0; i < count; i++) {
413 pj_atomic_inc_and_get(tparam->n_poll);
414 }
415 } else {
416 pj_thread_sleep(10);
417 }
418 }
419 PJ_LOG(4,("test", "...thread #%d (poll) stopped", t_idx));
420
421 return 0;
422 }
423
424 /* Poll worker thread function. */
425 static int poll_worker(void *arg)
426 {
427 struct thread_param *tparam = (struct thread_param*)arg;
428 int idx;
429
430 idx = pj_atomic_inc_and_get(tparam->idx);
431 tparam->stat[idx].is_poll = PJ_TRUE;
432
433 PJ_LOG(4,("test", "...thread #%d (poll) started", idx));
434 while (!tparam->stopping) {
435 unsigned count;
436 count = pj_timer_heap_poll(tparam->timer, NULL);
437 if (count > 0) {
438 /* Count expired entries */
439 PJ_LOG(5,("test", "...thread #%d called %d entries",
440 idx, count));
441 tparam->stat[idx].cnt += count;
442 } else {
443 pj_thread_sleep(10);
444 }
445 }
446 PJ_LOG(4,("test", "...thread #%d (poll) stopped", idx));
447
448 return 0;
449 }
450
451 #if ST_CANCEL_THREAD_COUNT
452 /* Cancel worker thread function. */
453 static int cancel_worker(void *arg)
454 {
455 struct thread_param *tparam = (struct thread_param*)arg;
456 int idx;
457
458 idx = pj_atomic_inc_and_get(tparam->idx);
459 tparam->stat[idx].is_poll = PJ_FALSE;
460
461 PJ_LOG(4,("test", "...thread #%d (cancel) started", idx));
462 while (!tparam->stopping) {
463 int count;
464 pj_timer_entry *e = &tparam->entries[pj_rand() % ST_ENTRY_COUNT];
465
466 count = pj_timer_heap_cancel_if_active(tparam->timer, e, 2);
467 if (count > 0) {
468 /* Count cancelled entries */
469 PJ_LOG(5,("test", "...thread #%d cancelled %d entries",
470 idx, count));
471 tparam->stat[idx].cnt += count;
472
473 /* Reschedule entry after some delay */
474 pj_thread_sleep(pj_rand() % 100);
475 st_schedule_entry(tparam->timer, e);
476 }
477 }
478 PJ_LOG(4,("test", "...thread #%d (cancel) stopped", idx));
479
480 return 0;
481 }
482 #endif
483
484 static int timer_stress_test(void)
485 {
486 unsigned count = 0, n_sched = 0, n_cancel = 0, n_poll = 0;
487 int i;
488 pj_timer_entry *entries = NULL;
489 pj_atomic_t **entries_status = NULL;
490 pj_grp_lock_t **grp_locks = NULL;
491 pj_pool_t *pool;
492 pj_timer_heap_t *timer = NULL;
493 pj_lock_t *timer_lock;
494 pj_status_t status;
495 int err=0;
496 pj_thread_t **stress_threads = NULL;
497 pj_thread_t **poll_threads = NULL;
498 pj_thread_t **cancel_threads = NULL;
499 struct thread_param tparam = {0};
500 #if SIMULATE_CRASH
501 pj_timer_entry *entry;
502 pj_pool_t *tmp_pool;
503 pj_time_val delay = {0};
504 #endif
505
506 PJ_LOG(3,("test", "...Stress test"));
507
508 pool = pj_pool_create( mem, NULL, 128, 128, NULL);
509 if (!pool) {
510 PJ_LOG(3,("test", "...error: unable to create pool"));
511 err = -10;
512 goto on_return;
513 }
514
515 /* Create timer heap.
516 * Initially we only create a fraction of what's required,
517 * to test the timer heap growth algorithm.
518 */
519 status = pj_timer_heap_create(pool, ST_ENTRY_COUNT/64, &timer);
520 if (status != PJ_SUCCESS) {
521 app_perror("...error: unable to create timer heap", status);
522 err = -20;
523 goto on_return;
524 }
525
526 /* Set recursive lock for the timer heap. */
527 status = pj_lock_create_recursive_mutex( pool, "lock", &timer_lock);
528 if (status != PJ_SUCCESS) {
529 app_perror("...error: unable to create lock", status);
530 err = -30;
531 goto on_return;
532 }
533 pj_timer_heap_set_lock(timer, timer_lock, PJ_TRUE);
534
535 /* Create group locks for the timer entry. */
536 if (ST_ENTRY_GROUP_LOCK_COUNT) {
537 grp_locks = (pj_grp_lock_t**)
538 pj_pool_calloc(pool, ST_ENTRY_GROUP_LOCK_COUNT,
539 sizeof(pj_grp_lock_t*));
540 tparam.grp_locks = grp_locks;
541 }
542 for (i=0; i<ST_ENTRY_GROUP_LOCK_COUNT; ++i) {
543 status = pj_grp_lock_create(pool, NULL, &grp_locks[i]);
544 if (status != PJ_SUCCESS) {
545 app_perror("...error: unable to create group lock", status);
546 err = -40;
547 goto on_return;
548 }
549 pj_grp_lock_add_ref(grp_locks[i]);
550 }
551
552 /* Create and schedule timer entries */
553 entries = (pj_timer_entry*)pj_pool_calloc(pool, ST_ENTRY_COUNT,
554 sizeof(*entries));
555 if (!entries) {
556 err = -50;
557 goto on_return;
558 }
559 entries_status = (pj_atomic_t**)pj_pool_calloc(pool, ST_ENTRY_COUNT,
560 sizeof(*entries_status));
561 if (!entries_status) {
562 err = -55;
563 goto on_return;
564 }
565
566 for (i=0; i<ST_ENTRY_COUNT; ++i) {
567 pj_timer_entry_init(&entries[i], 0, &tparam, &st_entry_callback);
568
569 status = pj_atomic_create(pool, -1, &entries_status[i]);
570 if (status != PJ_SUCCESS) {
571 err = -60;
572 goto on_return;
573 }
574 pj_atomic_set(entries_status[i], 0);
575
576 /* For randomized test, we schedule the entry inside the thread */
577 if (!ST_STRESS_THREAD_COUNT) {
578 status = st_schedule_entry(timer, &entries[i]);
579 if (status != PJ_SUCCESS) {
580 app_perror("...error: unable to schedule entry", status);
581 err = -60;
582 goto on_return;
583 }
584 }
585 }
586
587 tparam.stopping = PJ_FALSE;
588 tparam.timer = timer;
589 tparam.entries = entries;
590 tparam.status = entries_status;
591 status = pj_atomic_create(pool, -1, &tparam.idx);
592 if (status != PJ_SUCCESS) {
593 app_perror("...error: unable to create atomic", status);
594 err = -70;
595 goto on_return;
596 }
597 status = pj_atomic_create(pool, -1, &tparam.n_sched);
598 pj_assert (status == PJ_SUCCESS);
599 pj_atomic_set(tparam.n_sched, 0);
600 status = pj_atomic_create(pool, -1, &tparam.n_cancel);
601 pj_assert (status == PJ_SUCCESS);
602 pj_atomic_set(tparam.n_cancel, 0);
603 status = pj_atomic_create(pool, -1, &tparam.n_poll);
604 pj_assert (status == PJ_SUCCESS);
605 pj_atomic_set(tparam.n_poll, 0);
606
607 /* Start stress worker threads */
608 if (ST_STRESS_THREAD_COUNT) {
609 stress_threads = (pj_thread_t**)
610 pj_pool_calloc(pool, ST_STRESS_THREAD_COUNT,
611 sizeof(pj_thread_t*));
612 }
613 for (i=0; i<ST_STRESS_THREAD_COUNT; ++i) {
614 status = pj_thread_create( pool, "poll", &stress_worker, &tparam,
615 0, 0, &stress_threads[i]);
616 if (status != PJ_SUCCESS) {
617 app_perror("...error: unable to create stress thread", status);
618 err = -75;
619 goto on_return;
620 }
621 }
622
623 /* Start poll worker threads */
624 if (ST_POLL_THREAD_COUNT) {
625 poll_threads = (pj_thread_t**)
626 pj_pool_calloc(pool, ST_POLL_THREAD_COUNT,
627 sizeof(pj_thread_t*));
628 }
629 for (i=0; i<ST_POLL_THREAD_COUNT; ++i) {
630 status = pj_thread_create( pool, "poll", &poll_worker, &tparam,
631 0, 0, &poll_threads[i]);
632 if (status != PJ_SUCCESS) {
633 app_perror("...error: unable to create poll thread", status);
634 err = -80;
635 goto on_return;
636 }
637 }
638
639 /* Start cancel worker threads */
640 #if ST_CANCEL_THREAD_COUNT
641 cancel_threads = (pj_thread_t**)
642 pj_pool_calloc(pool, ST_CANCEL_THREAD_COUNT,
643 sizeof(pj_thread_t*));
644
645 for (i=0; i<ST_CANCEL_THREAD_COUNT; ++i) {
646 status = pj_thread_create( pool, "cancel", &cancel_worker, &tparam,
647 0, 0, &cancel_threads[i]);
648 if (status != PJ_SUCCESS) {
649 app_perror("...error: unable to create cancel thread", status);
650 err = -90;
651 goto on_return;
652 }
653 }
654 #endif
655
656 #if SIMULATE_CRASH
657 tmp_pool = pj_pool_create( mem, NULL, 4096, 128, NULL);
658 pj_assert(tmp_pool);
659 entry = (pj_timer_entry*)pj_pool_calloc(tmp_pool, 1, sizeof(*entry));
660 pj_assert(entry);
661 pj_timer_entry_init(entry, 0, &tparam, &dummy_callback);
662 delay.sec = 6;
663 status = pj_timer_heap_schedule(timer, entry, &delay);
664 pj_assert(status == PJ_SUCCESS);
665 pj_thread_sleep(1000);
666 PJ_LOG(3,("test", "...Releasing timer entry %p without cancelling it",
667 entry));
668 pj_pool_secure_release(&tmp_pool);
669 //pj_pool_release(tmp_pool);
670 //pj_memset(tmp_pool, 128, 4096);
671 #endif
672
673 /* Wait */
674 pj_thread_sleep(ST_DURATION);
675
676 on_return:
677
678 PJ_LOG(3,("test", "...Cleaning up resources"));
679 tparam.stopping = PJ_TRUE;
680
681 for (i=0; i<ST_STRESS_THREAD_COUNT; ++i) {
682 if (!stress_threads || !stress_threads[i])
683 continue;
684 pj_thread_join(stress_threads[i]);
685 pj_thread_destroy(stress_threads[i]);
686 }
687
688 for (i=0; i<ST_POLL_THREAD_COUNT; ++i) {
689 if (!poll_threads[i])
690 continue;
691 pj_thread_join(poll_threads[i]);
692 pj_thread_destroy(poll_threads[i]);
693 }
694
695 for (i=0; i<ST_CANCEL_THREAD_COUNT; ++i) {
696 if (!cancel_threads[i])
697 continue;
698 pj_thread_join(cancel_threads[i]);
699 pj_thread_destroy(cancel_threads[i]);
700 }
701
702 for (i=0; i<ST_POLL_THREAD_COUNT+ST_CANCEL_THREAD_COUNT; ++i) {
703 PJ_LOG(3,("test", "...Thread #%d (%s) executed %d entries",
704 i, (tparam.stat[i].is_poll? "poll":"cancel"),
705 tparam.stat[i].cnt));
706 }
707
708 for (i=0; i<ST_ENTRY_COUNT; ++i) {
709 count += pj_timer_heap_cancel_if_active(timer, &entries[i], 10);
710 if (entries_status)
711 pj_atomic_destroy(entries_status[i]);
712 }
713
714 for (i=0; i<ST_ENTRY_GROUP_LOCK_COUNT; ++i) {
715 /* Ref count must be equal to 1 */
716 if (pj_grp_lock_get_ref(grp_locks[i]) != 1) {
717 pj_assert(!"Group lock ref count must be equal to 1");
718 if (!err) err = -100;
719 }
720 pj_grp_lock_dec_ref(grp_locks[i]);
721 }
722
723 if (timer)
724 pj_timer_heap_destroy(timer);
725
726 PJ_LOG(3,("test", "Total memory of timer heap: %lu",
727 (unsigned long)pj_timer_heap_mem_size(ST_ENTRY_COUNT)));
728
729 if (tparam.idx)
730 pj_atomic_destroy(tparam.idx);
731 if (tparam.n_sched) {
732 n_sched = pj_atomic_get(tparam.n_sched);
733 PJ_LOG(3,("test", "Total number of scheduled entries: %d", n_sched));
734 pj_atomic_destroy(tparam.n_sched);
735 }
736 if (tparam.n_cancel) {
737 n_cancel = pj_atomic_get(tparam.n_cancel);
738 PJ_LOG(3,("test", "Total number of cancelled entries: %d", n_cancel));
739 pj_atomic_destroy(tparam.n_cancel);
740 }
741 if (tparam.n_poll) {
742 n_poll = pj_atomic_get(tparam.n_poll);
743 PJ_LOG(3,("test", "Total number of polled entries: %d", n_poll));
744 pj_atomic_destroy(tparam.n_poll);
745 }
746 PJ_LOG(3,("test", "Number of remaining active entries: %d", count));
747 if (n_sched) {
748 pj_bool_t match = PJ_TRUE;
749
750 #if SIMULATE_CRASH
751 n_sched++;
752 #endif
753 if (n_sched != (n_cancel + n_poll + count)) {
754 if (tparam.err != 0) tparam.err = -250;
755 match = PJ_FALSE;
756 }
757 PJ_LOG(3,("test", "Scheduled = cancelled + polled + remaining?: %s",
758 (match? "yes": "no")));
759 }
760
761 pj_pool_safe_release(&pool);
762
763 return (err? err: tparam.err);
764 }
765
766 static int get_random_delay()
767 {
768 return pj_rand() % BT_ENTRY_COUNT;
769 }
770
771 static int get_next_delay(int delay)
772 {
773 return ++delay;
774 }
775
776 typedef enum BENCH_TEST_TYPE {
777 RANDOM_SCH = 0,
778 RANDOM_CAN = 1,
779 INCREMENT_SCH = 2,
780 INCREMENT_CAN = 3
781 } BENCH_TEST_TYPE;
782
783 static char *get_test_name(BENCH_TEST_TYPE test_type) {
784 switch (test_type) {
785 case RANDOM_SCH:
786 case INCREMENT_SCH:
787 return "schedule";
788 case RANDOM_CAN:
789 case INCREMENT_CAN:
790 return "cancel";
791 }
792 return "undefined";
793 }
794
795 static void *get_format_num(unsigned n, char *out)
796 {
797 int c;
798 char buf[64];
799 char *p;
800
801 pj_ansi_snprintf(buf, 64, "%d", n);
802 c = 2 - pj_ansi_strlen(buf) % 3;
803 for (p = buf; *p != 0; ++p) {
804 *out++ = *p;
805 if (c == 1) {
806 *out++ = ',';
807 }
808 c = (c + 1) % 3;
809 }
810 *--out = 0;
811 return out;
812 }
813
814 static void print_bench(BENCH_TEST_TYPE test_type, pj_timestamp time_freq,
815 pj_timestamp time_start, int start_idx, int end_idx)
816 {
817 char start_idx_str[64];
818 char end_idx_str[64];
819 char num_req_str[64];
820 unsigned num_req;
821 pj_timestamp t2;
822
823 pj_get_timestamp(&t2);
824 pj_sub_timestamp(&t2, &time_start);
825
826 num_req = (unsigned)(time_freq.u64 * (end_idx-start_idx) / t2.u64);
827 if (test_type == RANDOM_CAN || test_type == INCREMENT_CAN) {
828 start_idx = BT_ENTRY_COUNT - start_idx;
829 end_idx = BT_ENTRY_COUNT - end_idx;
830 }
831 get_format_num(start_idx, start_idx_str);
832 get_format_num(end_idx, end_idx_str);
833 get_format_num(num_req, num_req_str);
834
835 PJ_LOG(3, (THIS_FILE, " Entries %s-%s: %s %s ent/sec",
836 start_idx_str, end_idx_str, get_test_name(test_type),
837 num_req_str));
838 }
839
840 static int bench_test(pj_timer_heap_t *timer,
841 pj_timer_entry *entries,
842 pj_timestamp freq,
843 BENCH_TEST_TYPE test_type)
844 {
845 pj_timestamp t1;
846 unsigned mult = BT_ENTRY_SHOW_START;
847 int i, j;
848
849 pj_get_timestamp(&t1);
850 /*Schedule random entry.*/
851 for (i=0, j=0; j < BT_ENTRY_COUNT; ++j) {
852 pj_time_val delay = { 0 };
853 pj_status_t status;
854
855 if (test_type == RANDOM_SCH || test_type == INCREMENT_SCH) {
856 if (test_type == RANDOM_SCH)
857 delay.msec = get_random_delay();
858 else
859 delay.msec = get_next_delay(delay.msec);
860
861 pj_timer_entry_init(&entries[j], 0, NULL, &dummy_callback);
862
863 status = pj_timer_heap_schedule(timer, &entries[j], &delay);
864 if (status != PJ_SUCCESS) {
865 app_perror("...error: unable to schedule timer entry", status);
866 return -50;
867 }
868 } else if (test_type == RANDOM_CAN || test_type == INCREMENT_CAN) {
869 unsigned num_ent = pj_timer_heap_cancel(timer, &entries[j]);
870 if (num_ent == 0) {
871 PJ_LOG(3, ("test", "...error: unable to cancel timer entry"));
872 return -60;
873 }
874 } else {
875 return -70;
876 }
877
878 if (j && (j % mult) == 0) {
879 print_bench(test_type, freq, t1, i, j);
880
881 i = j+1;
882 pj_get_timestamp(&t1);
883 mult *= BT_ENTRY_SHOW_MULT;
884 }
885 }
886 if (j > 0 && ((j-1) % mult != 0)) {
887 print_bench(test_type, freq, t1, i, j);
888 }
889 return 0;
890 }
891
892 static int timer_bench_test(void)
893 {
894 pj_pool_t *pool = NULL;
895 pj_timer_heap_t *timer = NULL;
896 pj_status_t status;
897 int err=0;
898 pj_timer_entry *entries = NULL;
899 pj_timestamp freq;
900 int i;
901
902 PJ_LOG(3,("test", "...Benchmark test"));
903
904 status = pj_get_timestamp_freq(&freq);
905 if (status != PJ_SUCCESS) {
906 PJ_LOG(3,("test", "...error: unable to get timestamp freq"));
907 err = -10;
908 goto on_return;
909 }
910
911 pool = pj_pool_create( mem, NULL, 128, 128, NULL);
912 if (!pool) {
913 PJ_LOG(3,("test", "...error: unable to create pool"));
914 err = -20;
915 goto on_return;
916 }
917
918 /* Create timer heap.*/
919 status = pj_timer_heap_create(pool, BT_ENTRY_COUNT/64, &timer);
920 if (status != PJ_SUCCESS) {
921 app_perror("...error: unable to create timer heap", status);
922 err = -30;
923 goto on_return;
924 }
925
926 /* Create and schedule timer entries */
927 entries = (pj_timer_entry*)pj_pool_calloc(pool, BT_ENTRY_COUNT,
928 sizeof(*entries));
929 if (!entries) {
930 err = -40;
931 goto on_return;
932 }
933
934 PJ_LOG(3,("test", "....random scheduling/cancelling test.."));
935 for (i = 0; i < BT_REPEAT_RANDOM_TEST; ++i) {
936 PJ_LOG(3,("test", " test %d of %d..", i+1, BT_REPEAT_RANDOM_TEST));
937 err = bench_test(timer, entries, freq, RANDOM_SCH);
938 if (err < 0)
939 goto on_return;
940
941 err = bench_test(timer, entries, freq, RANDOM_CAN);
942 if (err < 0)
943 goto on_return;
944 }
945
946 PJ_LOG(3,("test", "....increment scheduling/cancelling test.."));
947 for (i = 0; i < BT_REPEAT_INC_TEST; ++i) {
948 PJ_LOG(3,("test", " test %d of %d..", i+1, BT_REPEAT_INC_TEST));
949 err = bench_test(timer, entries, freq, INCREMENT_SCH);
950 if (err < 0)
951 goto on_return;
952
953 err = bench_test(timer, entries, freq, INCREMENT_CAN);
954 if (err < 0)
955 goto on_return;
956 }
957 on_return:
958 PJ_LOG(3,("test", "...Cleaning up resources"));
959 if (pool)
960 pj_pool_safe_release(&pool);
961 return err;
962 }
963
964 int timer_test()
965 {
966 int rc;
967
968 rc = test_timer_heap();
969 if (rc != 0)
970 return rc;
971
972 rc = timer_stress_test();
973 if (rc != 0)
974 return rc;
975
976 #if WITH_BENCHMARK
977 rc = timer_bench_test();
978 if (rc != 0)
979 return rc;
980 #else
981 /* Avoid unused warning */
982 PJ_UNUSED_ARG(timer_bench_test);
983 #endif
984
985 return 0;
986 }
987
988 #else
989 /* To prevent warning about "translation unit is empty"
990 * when this test is disabled.
991 */
992 int dummy_timer_test;
993 #endif /* INCLUDE_TIMER_TEST */
994
995