1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
|
// $Id$
// ============================================================================
//
// = LIBRARY
// tests
//
// = FILENAME
// Timer_Queue_Test.cpp
//
// = DESCRIPTION
// This is a simple test of <ACE_Timer_Queue> and four of its
// subclasses (<ACE_Timer_List>, <ACE_Timer_Heap>,
// <ACE_Timer_Wheel>, and <ACE_Timer_Hash>). The test sets up a
// bunch of timers and then adds them to a timer queue. The
// functionality of the timer queue is then tested. No command
// line arguments are needed to run the test.
//
// = AUTHORS
// Douglas C. Schmidt <schmidt@cs.wustl.edu>,
// Prashant Jain <pjain@cs.wustl.edu>, and
// Darrell Brunsch <brunsch@cs.wustl.edu>
//
// ============================================================================
#include "test_config.h"
#include "randomize.h"
#include "ace/Profile_Timer.h"
#include "ace/Timer_Queue.h"
#include "ace/Timer_List.h"
#include "ace/Timer_Heap.h"
#include "ace/Timer_Wheel.h"
#include "ace/Timer_Hash.h"
#include "ace/Timer_Queue.h"
#include "ace/Recursive_Thread_Mutex.h"
#include "ace/Null_Mutex.h"
#include "ace/OS_NS_unistd.h"
#include "ace/Containers_T.h"
ACE_RCSID(tests, Timer_Queue_Test, "$Id$")
// Number of iterations for the performance tests. Some platforms
// have a very high ACE_DEFAULT_TIMERS (HP-UX is 400), so limit this
// to a reasonable run time.
#if (ACE_DEFAULT_TIMERS > 20)
static int max_iterations = 2000;
#else
static int max_iterations = ACE_DEFAULT_TIMERS * 100;
#endif
// Amount of time between each timer.
// (0 schedules all the timers to expire at exactly the same time.)
// in milliseconds
static int TIMER_DISTANCE = 50;
// Array of timer ids assigned to us that we need to keep track of.
static long *timer_ids = 0;
class Example_Handler : public ACE_Event_Handler
{
public:
Example_Handler (void): close_count_ (0) {}
virtual int handle_close (ACE_HANDLE, ACE_Reactor_Mask mask)
{
ACE_ASSERT (mask == ACE_Event_Handler::TIMER_MASK);
this->close_count_++;
return 0;
}
virtual int handle_timeout (const ACE_Time_Value &,
const void *arg)
{
int *act = (int *) arg;
ACE_ASSERT (*act == 42 || *act == 007);
int result = 0;
if (*act == 007)
result = -1; // This is the special value to trigger a handle_close
delete act;
return result;
}
int close_count_;
// Keeps track of the number of times that <handle_close> is called.
};
static void
test_functionality (ACE_Timer_Queue *tq)
{
Example_Handler eh;
ACE_ASSERT (tq->is_empty () != 0);
ACE_ASSERT (ACE_Time_Value::zero == ACE_Time_Value (0));
long timer_id;
long timer_id2;
// Do a test on earliest_time.
ACE_Time_Value earliest_time = tq->gettimeofday ();
const void *timer_act = 0;
ACE_NEW (timer_act, int (1));
timer_id = tq->schedule (&eh, timer_act, earliest_time);
ACE_OS::sleep (ACE_Time_Value (0, 10));
ACE_NEW (timer_act, int (1));
timer_id2 = tq->schedule (&eh, timer_act, tq->gettimeofday ());
long result = tq->earliest_time () == earliest_time;
ACE_ASSERT (result != 0);
tq->cancel (timer_id, &timer_act);
delete (int *) timer_act;
tq->cancel (timer_id2, &timer_act);
delete (int *) timer_act;
ACE_ASSERT (tq->is_empty () == 1);
ACE_ASSERT (eh.close_count_ == 0);
ACE_NEW (timer_act, int (1));
timer_id = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (timer_id != -1);
ACE_ASSERT (tq->is_empty () == 0); //==
ACE_NEW (timer_act, int (42));
result = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (result != -1);
ACE_ASSERT (tq->is_empty () == 0); //==
ACE_NEW (timer_act, int (42));
result = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (result != -1);
ACE_ASSERT (tq->is_empty () == 0); //==
// The following method will trigger a call to <handle_close>.
ACE_ASSERT (eh.close_count_ == 0);
result = tq->cancel (timer_id, &timer_act, 0);
ACE_ASSERT (result == 1);
delete (int *) timer_act;
ACE_ASSERT (tq->is_empty () == 0);
ACE_ASSERT (eh.close_count_ == 1);
result = tq->expire ();
ACE_ASSERT (result == 2);
ACE_NEW (timer_act, int (007));
result = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (result != -1);
const void *timer_act1 = 0;
ACE_NEW (timer_act1, int (42));
result = tq->schedule (&eh,
timer_act1,
tq->gettimeofday () + ACE_Time_Value (100));
ACE_ASSERT (result != -1);
const void *timer_act2 = 0;
ACE_NEW (timer_act2, int (42));
result = tq->schedule (&eh,
timer_act2,
tq->gettimeofday () + ACE_Time_Value (100));
ACE_ASSERT (result != -1);
// The following will trigger a call to <handle_close> when it
// cancels the second timer. This happens because the first timer
// has an <act> of 007, which causes eh.handle_timeout () to return
// -1. Since -1 is returned, all timers that use <eh> will be
// cancelled (and <handle_close> will only be called on the first
// timer that is cancelled).
ACE_ASSERT (eh.close_count_ == 1);
result = tq->expire ();
ACE_ASSERT (result == 1);
ACE_ASSERT (eh.close_count_ == 2);
ACE_ASSERT (tq->is_empty () != 0);
delete (int *) timer_act2;
delete (int *) timer_act1;
ACE_NEW (timer_act, int (4));
timer_id = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (timer_id != -1);
ACE_NEW (timer_act, int (4));
timer_id2 = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (timer_id2 != -1);
// The following method will trigger a call to <handle_close>.
ACE_ASSERT (eh.close_count_ == 2);
result = tq->cancel (timer_id, &timer_act);
ACE_ASSERT (result != -1);
delete (int *) timer_act;
result = tq->cancel (timer_id2, &timer_act);
ACE_ASSERT (result != -1);
delete (int *) timer_act;
ACE_ASSERT (eh.close_count_ == 2); // Only one call to handle_close() even though two timers
ACE_ASSERT (tq->is_empty () != 0);
result = tq->expire ();
ACE_ASSERT (result == 0);
// This tests to make sure that <handle_close> is called when there
// is only one timer of the type in the queue
ACE_ASSERT (eh.close_count_ == 2);
ACE_NEW (timer_act, int (007));
result = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (result != -1);
result = tq->expire ();
ACE_ASSERT (result == 1);
ACE_ASSERT (eh.close_count_ == 3);
ACE_NEW (timer_act, int (6));
timer_id = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (timer_id != -1);
ACE_NEW (timer_act, int (7));
timer_id2 = tq->schedule (&eh,
timer_act,
tq->gettimeofday ());
ACE_ASSERT (timer_id2 != -1);
ACE_ASSERT (eh.close_count_ == 3);
result = tq->cancel (timer_id, &timer_act);
ACE_ASSERT (result == 1);
ACE_ASSERT (eh.close_count_ == 3);
delete (int *) timer_act;
result = tq->cancel (timer_id2, &timer_act);
ACE_ASSERT (result == 1);
ACE_ASSERT (eh.close_count_ == 3);
delete (int *) timer_act;
result = tq->expire ();
ACE_ASSERT (result == 0);
ACE_ASSERT (eh.close_count_ == 3);
}
static void
test_performance (ACE_Timer_Queue *tq,
const ACE_TCHAR *test_name)
{
Example_Handler eh;
ACE_Profile_Timer timer;
int i;
const void *timer_act = 0;
ACE_ASSERT (tq->is_empty () != 0);
ACE_ASSERT (ACE_Time_Value::zero == ACE_Time_Value (0));
// Test the amount of time required to schedule all the timers.
ACE_Time_Value *times = 0;
ACE_NEW (times, ACE_Time_Value[max_iterations]);
// Set up a bunch of times TIMER_DISTANCE ms apart.
for (i = 0; i < max_iterations; ++i)
times[i] = tq->gettimeofday() + ACE_Time_Value(0, i * TIMER_DISTANCE * 1000);
ACE_Time_Value last_time = times[max_iterations-1];
timer.start ();
for (i = 0; i < max_iterations; ++i)
{
ACE_NEW (timer_act, int (42));
timer_ids[i] = tq->schedule (&eh,
timer_act,
times[i]);
ACE_ASSERT (timer_ids[i] != -1);
}
ACE_ASSERT (tq->is_empty () == 0);
timer.stop ();
ACE_Profile_Timer::ACE_Elapsed_Time et;
timer.elapsed_time (et);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time to schedule %d timers for %s\n"),
max_iterations, test_name));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
et.real_time, et.user_time, et.system_time));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time per call = %f usecs\n"),
(et.user_time / ACE_timer_t (max_iterations)) * 1000000));
// Test the amount of time required to cancel all the timers.
timer.start ();
for (i = max_iterations; i-- != 0; )
{
tq->cancel (timer_ids[i], &timer_act);
delete (int *) timer_act;
}
timer.stop ();
ACE_ASSERT (tq->is_empty () != 0);
timer.elapsed_time (et);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time to cancel %d timers for %s\n"),
max_iterations, test_name));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
et.real_time, et.user_time, et.system_time));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time per call = %f usecs\n"),
(et.user_time / ACE_timer_t (max_iterations)) * 1000000));
// Test the amount of time required to schedule and expire all the
// timers.
timer.start ();
for (i = 0; i < max_iterations; ++i)
{
ACE_NEW (timer_act, int (42));
long result = tq->schedule (&eh, timer_act, times[i]);
ACE_ASSERT (result != -1);
}
ACE_ASSERT (tq->is_empty () == 0);
// Expire all the timers.
tq->expire (last_time + ACE_Time_Value(1));
timer.stop ();
ACE_ASSERT (tq->is_empty () != 0);
timer.elapsed_time (et);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time to schedule and expire %d timers for %s\n"),
max_iterations, test_name));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
et.real_time, et.user_time, et.system_time));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time per call = %f usecs\n"),
(et.user_time / ACE_timer_t (max_iterations)) * 1000000));
randomize (times,
max_iterations,
static_cast<ACE_RANDR_TYPE> (ACE_OS::time (0L)));
// Test the amount of time required to randomly cancel all the
// timers.
for (i = 0; i < max_iterations; ++i)
{
ACE_NEW (timer_act, int (42));
timer_ids[i] = tq->schedule (&eh,
timer_act,
times[i]);
ACE_ASSERT (timer_ids[i] != -1);
}
ACE_ASSERT (tq->is_empty () == 0);
timer.start ();
for (i = max_iterations - 1; i >= 0; i--)
{
tq->cancel (timer_ids[i], &timer_act);
delete (int *) timer_act;
}
ACE_ASSERT (tq->is_empty () != 0);
timer.stop ();
timer.elapsed_time (et);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time to randomly cancel %d timers for %s\n"),
max_iterations,
test_name));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
et.real_time,
et.user_time,
et.system_time));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time per call = %f usecs\n"),
(et.user_time / ACE_timer_t (max_iterations)) * 1000000));
// Test the amount of time required to randomly schedule all the timers.
timer.start ();
for (i = 0; i < max_iterations; ++i)
{
ACE_NEW (timer_act, int (42));
timer_ids[i] = tq->schedule (&eh,
timer_act,
times[i]);
ACE_ASSERT (timer_ids[i] != -1);
}
timer.stop ();
ACE_ASSERT (tq->is_empty () == 0);
timer.elapsed_time (et);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time to randomly schedule %d timers for %s\n"),
max_iterations, test_name));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
et.real_time,
et.user_time,
et.system_time));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time per call = %f usecs\n"),
(et.user_time / ACE_timer_t (max_iterations)) * 1000000));
// Test the amount of time required to expire all the timers.
timer.start ();
tq->expire (last_time + ACE_Time_Value(1));
ACE_ASSERT (tq->is_empty ());
timer.stop ();
timer.elapsed_time (et);
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time to expire %d randomly scheduled timers for %s\n"),
max_iterations, test_name));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("real time = %f secs, user time = %f secs, system time = %f secs\n"),
et.real_time, et.user_time, et.system_time));
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("time per call = %f usecs\n"),
(et.user_time / ACE_timer_t (max_iterations)) * 1000000));
delete [] times;
}
// This test function was contributed with Bugzilla #2447 to test validity
// of ACE_Timer_Heap timer IDs around the boundary of having to enlarge
// the heap.
static void
test_unique_timer_heap_ids (void)
{
Example_Handler eh;
ACE_Timer_Heap timer_heap (44);
ACE_Time_Value anytime(1);
ACE_Bounded_Set<long> timer_ids (max_iterations);
long timer_id = -1;
bool all_unique = true;
for (int i = 0; i < 100; ++i)
{
timer_id = timer_heap.schedule (&eh, 0, anytime);
if (timer_id == -1)
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Schedule timer %d %p\n"),
i,
ACE_TEXT ("test_unique_timer_heap_ids")));
continue;
}
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Schedule timer %d. Timer id = %d\n"),
i,
timer_id));
if (1 == timer_ids.insert (timer_id))
{
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("Pass %d, id %d is not unique\n"),
i,
timer_id));
all_unique = false;
}
if (i == 0 || i == 1 || i == 47 || i == 48)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("Free Timer %d. Timer Id = %d\n"),
i,
timer_id));
timer_heap.cancel (timer_id);
if (timer_id == -1)
ACE_ERROR ((LM_ERROR,
ACE_TEXT ("%p\n"),
ACE_TEXT ("Failed to cancel timer")));
timer_ids.remove (timer_id);
}
}
if (all_unique)
ACE_DEBUG ((LM_INFO, ACE_TEXT ("All timer ids were unique.\n")));
return;
}
class Timer_Queue_Stack
{
// = TITLE
// Keeps track of the <Timer_Queue>s that we're going to test.
//
// = DESCRIPTION
// This data structure is organized as a stack to make it easy to implement.
public:
// = Initialization method
Timer_Queue_Stack (ACE_Timer_Queue *queue,
const ACE_TCHAR *name,
Timer_Queue_Stack *next = 0)
: queue_ (queue),
name_ (name),
next_ (next)
{}
// "Push" a new <queue> on the stack of <queue>s.
ACE_Timer_Queue *queue_;
// Pointer to the subclass of <ACE_Timer_Queue> that we're testing.
const ACE_TCHAR *name_;
// Name of the Queue that we're testing.
Timer_Queue_Stack *next_;
// Pointer to the next <Timer_Queue>.
};
int
run_main (int argc, ACE_TCHAR *argv[])
{
ACE_START_TEST (ACE_TEXT ("Timer_Queue_Test"));
if (argc > 1)
max_iterations = ACE_OS::atoi (argv[1]);
// = Perform initializations.
Timer_Queue_Stack *tq_stack = 0;
// Add new Timer_Queue implementations here. Note that these will
// be executed in "reverse order".
// Timer_Hash (Heap)
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_Hash_Heap,
ACE_TEXT ("ACE_Timer_Hash (Heap)"),
tq_stack),
-1);
// Timer_Hash
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_Hash,
ACE_TEXT ("ACE_Timer_Hash"),
tq_stack),
-1);
// Timer_stack
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_List,
ACE_TEXT ("ACE_Timer_List"),
tq_stack),
-1);
// Timer_Wheel without preallocated memory
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_Wheel,
ACE_TEXT ("ACE_Timer_Wheel (non-preallocated)"),
tq_stack),
-1);
// Timer_Wheel with preallocated memory.
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_Wheel (ACE_DEFAULT_TIMER_WHEEL_SIZE,
ACE_DEFAULT_TIMER_WHEEL_RESOLUTION,
max_iterations),
ACE_TEXT ("ACE_Timer_Wheel (preallocated)"),
tq_stack),
-1);
// Timer_Heap without preallocated memory.
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_Heap,
ACE_TEXT ("ACE_Timer_Heap (non-preallocated)"),
tq_stack),
-1);
// Timer_Heap with preallocate memory.
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (new ACE_Timer_Heap (max_iterations, 1),
ACE_TEXT ("ACE_Timer_Heap (preallocated)"),
tq_stack),
-1);
// Timer_Heap without preallocated memory, using high-res time.
(void) ACE_High_Res_Timer::global_scale_factor ();
ACE_Timer_Heap *tq_heap = new ACE_Timer_Heap;
tq_heap->gettimeofday (&ACE_High_Res_Timer::gettimeofday_hr);
ACE_NEW_RETURN (tq_stack,
Timer_Queue_Stack (tq_heap,
ACE_TEXT ("ACE_Timer_Heap (high-res timer)"),
tq_stack),
-1);
// Create the Timer ID array
ACE_NEW_RETURN (timer_ids,
long[max_iterations],
-1);
Timer_Queue_Stack *tq_ptr = tq_stack;
while (tq_ptr != 0)
{
ACE_DEBUG ((LM_DEBUG,
ACE_TEXT ("**** starting test of %s\n"),
tq_ptr->name_));
test_functionality (tq_ptr->queue_);
test_performance (tq_ptr->queue_,
tq_ptr->name_);
delete tq_ptr->queue_;
Timer_Queue_Stack *temp = tq_ptr;
tq_ptr = tq_ptr->next_;
delete temp;
}
delete [] timer_ids;
ACE_DEBUG
((LM_DEBUG,
ACE_TEXT ("**** starting unique IDs test for ACE_Timer_Heap\n")));
test_unique_timer_heap_ids ();
ACE_END_TEST;
return 0;
}
|