summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Metrics/Metrics_Local_Cache_Test.cpp~
blob: 06b6e18ddb768da516f3358553bf0590fb6f21b6 (plain)
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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
// $Id$

#include "ace/OS.h"
#include "ace/ARGV.h"
#include "ace/Arg_Shifter.h"
#include "ace/Sched_Params.h"

#include "tao/corba.h"
#include "tao/RTCORBA/Multi_Priority_Mapping.h"
#include "tao/RTCORBA/RTCORBA.h"
#include "orbsvcs/Metrics/Metrics_Logger.h"
#include "orbsvcs/Rate_Group_Info.h"

#include "Metrics_Local_Cache_Test.h"

ACE_RCSID(Metrics_Local_Cache_Test, Metrics_Local_Cache_Test, "$Id$")

const int base_port = 10015;

const unsigned int TAO_Metrics_Local_Cache_Test::number_of_hrt_rates_ = 5;
const unsigned int TAO_Metrics_Local_Cache_Test::number_of_srt_rates_ = 1;

const unsigned long TAO_Metrics_Local_Cache_Test::the_hrt_Rates_in_microseconds_[] =
{
     25000,             // 25 msec
     50000,             // 50 msec
    100000,             // 100 msec
    200000,             // 200 msec
    1000000             // 1 sec
};

const int TAO_Metrics_Local_Cache_Test::highest_rate_group_priority_ =
// VxWorks targets
#ifdef VXWORKS
      // Priorities on VxWorks range from 0 to 255 with 255 being the lowest priority.
      // Recommended priority is below the lowest net task of VxWorks.  (60 is a safe number)
     150;
#else
// Windows targets
#ifdef ACE_WIN32

#if defined (ACE_THR_PRI_FIFO_DEF)
      ACE_Sched_Params::priority_max (ACE_SCHED_FIFO);
#else
      ACE_DEFAULT_THREAD_PRIORITY;
#endif /* ACE_THR_PRI_FIFO_DEF */

// Other targets including Linux
#else
     50;
#endif

#endif

char *orb_init_params =
#if defined INFRA_HAS_TOA_GIOP_LITE && defined INFRA_HAS_TAO_DEBUG
   "-ORBObjRefStyle URL -ORBDebugLevel 1 -ORBGIOPlite -ORBCollocationStrategy direct";
#elif defined INFRA_HAS_TOA_GIOP_LITE
   "-ORBObjRefStyle URL -ORBGIOPlite -ORBCollocationStrategy direct";
#elif defined INFRA_HAS_TAO_DEBUG
   "-ORBObjRefStyle URL -ORBDebugLevel 10 -ORBCollocationStrategy direct";
#else
   "-ORBObjRefStyle URL -ORBCollocationStrategy direct";
#endif  // ORB Init options

int enable_debug = 0;
// Used to enable LM_DEBUG messages

int enable_trace = 0;
  // Used to enable LM_TRACE messages

//////////////////////////////////////////
// class TAO_Metrics_Local_Cache_Test //
//////////////////////////////////////////

// Default Constructor.

TAO_Metrics_Local_Cache_Test::TAO_Metrics_Local_Cache_Test (int local_monitor,
                                                            int local_logger)
  : orb_(CORBA::ORB::_nil()),
    logger_impl_ (0),
    upcall_monitor_impl_(0),
    H40_push_consumer_adapter_ (0),
    H20_push_consumer_adapter_ (0),
    H10_push_consumer_adapter_ (0),
    H05_push_consumer_adapter_ (0),
    H01_push_consumer_adapter_ (0),
    S40_push_consumer_adapter_ (0),
    S20_push_consumer_adapter_ (0),
    S10_push_consumer_adapter_ (0),
    S05_push_consumer_adapter_ (0),
    S01_push_consumer_adapter_ (0),
    logger_ior_file_name_ (0),
    monitor_ior_file_name_ (0),
    log_file_name_ (0),
    log_output_ (0),
    visualize_ (0),
    local_monitor_(local_monitor),
    local_logger_(local_logger),
    rate_group_info_(0),
    master_host_name_(0),
    master_host_ip_(0)
{
}


TAO_Metrics_Local_Cache_Test::TAO_Metrics_Local_Cache_Test (int argc,
                                                            char *argv[],
                                                            int local_monitor,
                                                            int local_logger)
  : orb_(CORBA::ORB::_nil()),
    logger_impl_ (0),
    H40_push_consumer_adapter_ (0),
    H20_push_consumer_adapter_ (0),
    H10_push_consumer_adapter_ (0),
    H05_push_consumer_adapter_ (0),
    H01_push_consumer_adapter_ (0),
    S40_push_consumer_adapter_ (0),
    S20_push_consumer_adapter_ (0),
    S10_push_consumer_adapter_ (0),
    S05_push_consumer_adapter_ (0),
    S01_push_consumer_adapter_ (0),
    logger_ior_file_name_ (0),
    monitor_ior_file_name_ (0),
    log_file_name_ (0),
    log_output_ (0),
    visualize_ (0),
    local_monitor_(local_monitor),
    local_logger_(local_logger),
    rate_group_info_(0)
{
  this->init (argc, argv);
}

// Destructor.

TAO_Metrics_Local_Cache_Test::~TAO_Metrics_Local_Cache_Test (void)
{
}

void
TAO_Metrics_Local_Cache_Test::generate_orb_args (char *& orb_init_str)
{
   int port = 0;
   char endpoint[255];   // Just use a large value to ensure the buffer is large enough for an endpoint
   RTCORBA::Priority the_corba_priority;

   const unsigned int number_of_rates = TAO_Metrics_Local_Cache_Test::number_of_hrt_rates_ +
                                        TAO_Metrics_Local_Cache_Test::number_of_srt_rates_;

   rate_group_info_ = new TAO_Rate_Group_Info* [number_of_rates];

   // Set the number of rate groups
   TAO_Rate_Group_Info::set_num_rate_groups(number_of_rates);

   // Set the base corba priority.  Priority of the highest endpoint
   TAO_Rate_Group_Info::set_corba_base_priority(50);

   // Set the spacing between consecutive endpoints.
   TAO_Rate_Group_Info::set_priority_spacing(1);

#ifdef ACE_WIN32 // WINDOWS
   // Priorities are not contiguous on Windows Platforms
   TAO_Rate_Group_Info::set_priorities_contiguous(0);
#else // !WINDOWS
   TAO_Rate_Group_Info::set_priorities_contiguous(1);
#endif

   unsigned int rate_ndx = 0;
   for (rate_ndx = 0; rate_ndx < number_of_rates; rate_ndx++)
   {
      rate_group_info_[rate_ndx] = new TAO_Rate_Group_Info;
   }

   int next_priority = 0;
   int prev_priority = 0;
   for (rate_ndx = 0;
        rate_ndx < TAO_Metrics_Local_Cache_Test::number_of_hrt_rates_;
        rate_ndx++)
   {
      // Rate for this HRT Rate Group
      rate_group_info_[rate_ndx]->set_rate( TAO_Metrics_Local_Cache_Test::the_hrt_Rates_in_microseconds_[rate_ndx] );

      // OS Priority of the highest rate group
      if (rate_ndx == 0)
      {
         next_priority = highest_rate_group_priority_;
      }
      else
      {
         next_priority = ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                                              prev_priority,
                                                              ACE_SCOPE_THREAD);
      }

      prev_priority = next_priority;
      rate_group_info_[rate_ndx]->set_rate_group_priority( next_priority );
   }

   if ( TAO_Metrics_Local_Cache_Test::number_of_srt_rates_ > 0 )
   {
      const unsigned int index_of_srt_rates = TAO_Metrics_Local_Cache_Test::number_of_hrt_rates_;

      // Temporary rate patch for Dynamic Queue
      rate_group_info_[index_of_srt_rates]->set_rate(1375000);
      if ( TAO_Metrics_Local_Cache_Test::number_of_hrt_rates_ == 0 )
      {
         next_priority = TAO_Metrics_Local_Cache_Test::highest_rate_group_priority_;
      }
      else
      {
         next_priority = ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                                              prev_priority,
                                                              ACE_SCOPE_THREAD);
      }

      rate_group_info_[index_of_srt_rates]->set_rate_group_priority(next_priority);  // Dynamic Rate Group Priority
   }

   TAO_Multi_Priority_Mapping priorityMapping (rate_group_info_[0]->get_rate_group_priority(),       // Priority for the highest rate group
                                              TAO_Rate_Group_Info::get_corba_base_priority(),    // The starting CORBA priority value
                                              TAO_Rate_Group_Info::get_priority_spacing(),       // Priority spacing between rate groups.
                                              TAO_Rate_Group_Info::get_priorities_contiguous(),  // Priority spacing between rate groups.
                                              ACE_SCHED_FIFO);

   u_int init_str_len = 0;
   if (master_host_ip_ != 0)
   {
      // Allocate an orb init string
      init_str_len = (number_of_rates) * (
                      21 + // " -ORBendpoint iiop://"
                      ACE_OS::strlen(this->master_host_ip_) +
                      6 +  // ":" + Port Number length
                      10 +  // "/priority="
                      5 ) +  // Port Number length
                      1 /* Null Terminator */;
   }
   else if (master_host_name_ != 0)
   {
      // Allocate an orb init string
      init_str_len = (number_of_rates) * (
                      21 + // " -ORBendpoint iiop://"
                      ACE_OS::strlen(this->master_host_name_) +
                      6 +  // ":" + Port Number length
                      10 +  // "/priority="
                      5 ) +  // Port Number length
                      1 /* Null Terminator */;
   }
   else
   {
      ACE_ERROR((LM_ERROR,"You must specify either a master host name or master host IP\n%a"));
   }


   // Include the application provided init string.
   if (init_str_len)
   {
      init_str_len += ACE_OS::strlen(orb_init_params) + 1;
   }

   orb_init_str = CORBA::string_alloc (init_str_len);
   *orb_init_str =0;

   // Build the endpoint string;
   for (rate_ndx = 0; rate_ndx < number_of_rates; rate_ndx++)
   {
      port = base_port + rate_ndx;
      priorityMapping.to_CORBA(rate_group_info_[rate_ndx]->get_rate_group_priority(), the_corba_priority);

      // Build the endpoint string
      ACE_OS::sprintf(endpoint,
                      " -ORBendpoint iiop://%s:%d/priority=%d",
                      this->master_host_name_,
                      port,
                      the_corba_priority );

      // Append to the string
           ACE_OS::strcat( orb_init_str, endpoint );
   }

   if (orb_init_str)
   {
      // Concatenate the application defined init string
      ACE_OS::strcat( orb_init_str, " " );
      ACE_OS::strcat( orb_init_str, orb_init_params );
   }

   ACE_DEBUG((LM_DEBUG, "ORB _init string is %s\n",orb_init_str ));
}

int
TAO_Metrics_Local_Cache_Test::parse_args (int argc,
                                          char *argv[])
{
   int num_orb_args = 0;

   ACE_Arg_Shifter arg_shifter (argc, argv);

   // Ignore the first argument.  Program Name
   arg_shifter.ignore_arg ();

   while (arg_shifter.is_anything_left ())
   {
      const ACE_TCHAR *current_arg = 0;

      if ( (current_arg = arg_shifter.get_the_parameter("-masterHostName")) )
      {
         this->master_host_name_ = CORBA::string_dup(current_arg);
         arg_shifter.consume_arg();
      }
      else if ( (current_arg = arg_shifter.get_the_parameter("-masterHostIP")) )
      {
         this->master_host_ip_ = CORBA::string_dup(current_arg);
         arg_shifter.consume_arg();
      }
      else if ( (current_arg = arg_shifter.get_the_parameter("-logger_ior")) )
      {
         this->logger_ior_file_name_ = CORBA::string_dup(current_arg);;
         arg_shifter.consume_arg();
      }
      else if (  arg_shifter.cur_arg_strncasecmp("-debug") >= 0 )
      {
         enable_debug = 1;
         arg_shifter.consume_arg();
      }
      else if (  arg_shifter.cur_arg_strncasecmp("-trace") >= 0 )
      {
         enable_trace = 1;
         arg_shifter.consume_arg();
      }
      else if (  arg_shifter.cur_arg_strncasecmp("?") >= 0 )
      {
         ACE_DEBUG ((LM_DEBUG,
                     "Usage: %s "
                     "[-masterHostName <master host name>] "
                     "[-masterHostIP <master host IP address>] "
                     "[-logger_ior logger_ior_file_name] "

                     "[-debug] (Enable LM_DEBUG log priority)"
                     "[-trace] (Enable LM_TRACE log priority)"
                     "\n",
                     argv[0]));
      }
      else
      {
         ACE_DEBUG((LM_DEBUG, "Unrecognized Command Line Arg: %s.  Assuming user-defined parameter\n", arg_shifter.get_current()));
         arg_shifter.ignore_arg ();
            num_orb_args++;
      }
   }
   return num_orb_args;
}

// Initialize the logger driver with the command line arguments.
int
TAO_Metrics_Local_Cache_Test::init (int argc, char *argv[])
{
   char *orb_init_string = 0;

   // Parse User supplied arguments leaving any ORB init arguments
   int orb_argc = this->parse_args (argc, argv);
   if (enable_debug)
   {
      ACE_Log_Msg::enable_debug_messages();
   }
   else
   {
      ACE_Log_Msg::disable_debug_messages();
   }

   if (enable_trace)
   {
      ACE_Log_Msg::enable_debug_messages(LM_TRACE);
   }
   else
   {
      ACE_Log_Msg::disable_debug_messages(LM_TRACE);
   }

   char** orb_argv = 0;
   ACE_ARGV *the_orb_args = 0;

   if (orb_argc == 0)
   {
      this->generate_orb_args(orb_init_string);
      the_orb_args = new ACE_ARGV (orb_init_string);
      orb_argc = the_orb_args->argc ();
      orb_argv = the_orb_args->argv ();
   }
   else
   {
      // If the user specified ORB args, then assume that the user is handling
      // proper setup of the ORB.
      orb_argv = argv;
   }

   ACE_TRY_NEW_ENV
   {
      // Initialize ORB.
      this->orb_ = CORBA::ORB_init (orb_argc, orb_argv);

      // Connect to the RootPOA.
      CORBA::Object_var poa_object =
         this->orb_->resolve_initial_references("RootPOA");

      if (CORBA::is_nil (poa_object.in ()))
         ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                           1);

      this->root_poa_ = PortableServer::POA::_narrow (poa_object.in (), ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (poa_manager_.in ()))
      {
         this->poa_manager_ = root_poa_->the_POAManager (ACE_TRY_ENV);
         ACE_TRY_CHECK;

         // Activate the POA manager.
         poa_manager_->activate (ACE_TRY_ENV);
         ACE_TRY_CHECK;
      }

      // Create a child poa with USER_ID and PERSISTENT policies.
      CORBA::PolicyList policies;
      policies.length (2);

      policies[0] = this->root_poa_->create_id_assignment_policy (PortableServer::USER_ID,
                                                                  ACE_TRY_ENV);
      ACE_TRY_CHECK;

      policies[1] = this->root_poa_->create_lifespan_policy(PortableServer::PERSISTENT,
                                                                ACE_TRY_ENV);
      ACE_TRY_CHECK;


      this->persistent_poa_ = this->root_poa_->create_POA("TheChildPOA",
                                                           this->poa_manager_.in (),
                                                           policies,
                                                           ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Cleanup the POA Policies
      policies[0]->destroy (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      policies[1]->destroy (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (local_logger_)
      {
         // Now construct the logger implementation, using the settings.
         ACE_NEW_THROW_EX (this->logger_impl_,
                           TAO_Metrics_Logger (this->visualize_,
                           this->log_output_,
                           1,  // 1 == generate_export_file
                           this->log_file_name_),
                           CORBA::NO_MEMORY ());
         ACE_TRY_CHECK;

         // Implicitly activate and store a reference to the logger.
         this->logger_ref_  = this->logger_impl_->_this (ACE_TRY_ENV);
         ACE_TRY_CHECK;

         // Output and log the logger IOR.
         ACE_DEBUG ((LM_DEBUG,
                     "\n\nThe TAO_Metrics logger IOR is:\n\n%s\n\n",
                     this->orb_->object_to_string (this->logger_ref_)));
      }
      else
      {
         if (this->logger_ior_file_name_ == 0)
         {
            ACE_ERROR((LM_ERROR, "You must specify a logger ior filename using the -logger_ior command line argument\n%a"));
         }

         u_int ior_str_len =
            ACE_OS::strlen("file:") +
            ACE_OS::strlen(this->logger_ior_file_name_);

         char *ior_str = CORBA::string_alloc (ior_str_len);
         *ior_str = 0;
         ACE_OS::strcat(ior_str, "file://");
         ACE_OS::strcat(ior_str, this->logger_ior_file_name_);

         CORBA::Object_var logger_obj = this->orb_->string_to_object ( ior_str , ACE_TRY_ENV);
         ACE_TRY_CHECK;

         // Turn Off priority selection for this reference.
         TAO::PrioritySpecification ps;
         ps.mode = TAO::USE_NO_PRIORITY;
         CORBA::Any value;
         value <<= ps;

         CORBA::PolicyList policy_list (1);
         policy_list.length (1);
         policy_list[0] = this->orb_->create_policy (TAO::CLIENT_PRIORITY_POLICY_TYPE,
                                                     value,
                                                     ACE_TRY_ENV);
         ACE_TRY_CHECK;


         // Obtain an object reference without endpoint profiles
         CORBA::Object_var logger_obj_no_priority_ref = logger_obj->_set_policy_overrides (policy_list,
                                                                                           CORBA::SET_OVERRIDE,
                                                                                           ACE_TRY_ENV);
         ACE_TRY_CHECK;

         this->logger_ref_ = Metrics::QoSLogger::_narrow (logger_obj_no_priority_ref.in(), ACE_TRY_ENV);
         ACE_TRY_CHECK;

      }

      if (local_monitor_)
      {
         // Now construct the logger implementation, using the settings.
         ACE_NEW_THROW_EX (this->upcall_monitor_impl_,
                           TAO_Metrics_ReportingUpcallMonitor (),
                           CORBA::NO_MEMORY ());
         ACE_TRY_CHECK;

         if (this->monitor_ior_file_name_ != 0)
         {
            FILE *miorf = fopen (this->monitor_ior_file_name_, "w");
            if (miorf != 0)
            {
               ACE_OS::fprintf (miorf,
                  "%s\n",
                  this->orb_->object_to_string (this->upcall_monitor_ref_));
               ACE_OS::fclose (miorf);
            }
         }

         // Implicitly activate and store a reference to the upcall monitor.
         this->upcall_monitor_ref_  = this->upcall_monitor_impl_->_this (ACE_TRY_ENV);
         ACE_TRY_CHECK;
      }

      // Output and log the upcall monitor IOR.
      ACE_DEBUG ((LM_DEBUG,
                  "\n\nThe TAO_Metrics upcall monitor IOR is:\n\n%s\n\n",
                  this->orb_->object_to_string (this->upcall_monitor_ref_)));


      // Create all the push consumer upcall adapters.
      TAO_String_Manager string_manager;
      string_manager = (const char *) "H40"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->H40_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, H40_push_consumer_,
                           string_manager, 1, 250000, 125000, RtecScheduler::VERY_HIGH_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "H20"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->H20_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, H20_push_consumer_,
                           string_manager, 2, 500000, 250000, RtecScheduler::VERY_HIGH_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "H10"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->H10_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, H10_push_consumer_,
                           string_manager, 3, 1000000, 500000, RtecScheduler::VERY_HIGH_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "H05"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->H05_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, H05_push_consumer_,
                           string_manager, 4, 2000000, 1000000, RtecScheduler::VERY_HIGH_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "H01"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->H01_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, H01_push_consumer_,
                           string_manager, 5, 10000000, 5000000, RtecScheduler::VERY_HIGH_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "S40"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->S40_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, S40_push_consumer_,
                           string_manager, 6, 250000, 125000, RtecScheduler::VERY_LOW_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "S20"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->S20_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, S20_push_consumer_,
                           string_manager, 7, 500000, 250000, RtecScheduler::VERY_LOW_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "S10"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->S10_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, S10_push_consumer_,
                           string_manager, 8, 1000000, 500000, RtecScheduler::VERY_LOW_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "S05"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->S05_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, S05_push_consumer_,
                           string_manager, 9, 2000000, 1000000, RtecScheduler::VERY_LOW_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      string_manager = (const char *) "S01"; // forces a copy (for lame platforms w/ non-const "")
      ACE_NEW_THROW_EX (this->S01_push_consumer_adapter_,
                        METRICS_UPCALL_ADAPTER_TYPE
                          (*upcall_monitor_impl_, cache_, frame_manager_, S01_push_consumer_,
                           string_manager, 10, 10000000, 5000000, RtecScheduler::VERY_LOW_CRITICALITY, 0, logger_ref_),
                        CORBA::NO_MEMORY ());
      ACE_TRY_CHECK;

      frame_manager_.register_period (10000000);  //  1 Hz
      frame_manager_.register_period ( 2000000);  //  5 Hz
      frame_manager_.register_period ( 1000000);  // 10 Hz
      frame_manager_.register_period (  500000);  // 20 Hz
      frame_manager_.register_period (  250000);  // 40 Hz
      frame_manager_.reset_all_frames ();

      // Register base metrics segments.
      cache_.register_base_metrics ("QUO",
                                    TAO_Metrics_Utils::Base_Metrics_Type::QUO,
                                    logger_ref_);
      cache_.register_base_metrics ("Sched",
                                    TAO_Metrics_Utils::Base_Metrics_Type::SCHED,
                                    logger_ref_);
      cache_.register_base_metrics ("RTARM",
                                    TAO_Metrics_Utils::Base_Metrics_Type::RTARM,
                                    logger_ref_);

      // Register all the probes with the logger.
      cache_.register_probes (logger_ref_);

    }

    ACE_CATCHANY
    {
       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "TAO_Metrics_Local_Cache_Test::init");
       return -1;
    }
    ACE_ENDTRY;

  delete the_orb_args;
  return 0;
}


// Runs the TAO_Metrics_Local_Cache_Test.

int
TAO_Metrics_Local_Cache_Test::run (CORBA_Environment& ACE_TRY_ENV)
{
  int result = 0;
  ACE_Thread_Manager orb_thread_manager;

  ACE_TRY
    {
      /////////////////////////////
      // Initialization sequence //
      /////////////////////////////

      // Spawn a thread that runs the orb event loop
      orb_thread_manager.spawn (TAO_Metrics_Local_Cache_Test::run_orb, this);

      ////////////////////////
      // Operation sequence //
      ////////////////////////

      ACE_Time_Value start_tv, end_tv;
      Metrics::Time interval;
      for (int i = 0; i < 5; ++i)
      {
         char buf[32];
         ACE_OS::sprintf (buf, "mission state %d\n", i);
         logger_ref_->send_banner (buf);
         ACE_TRY_CHECK;

         ACE_DEBUG ((LM_DEBUG, "(%P | %t) %s", buf));

         // Advance to current next frame for each period.
         ACE_OS::gettimeofday (start_tv);
         frame_manager_.update_all_frames (start_tv);

         ACE_Time_Value start_tv;
         u_int j;
         // Push a bunch of events to the consumer adapters.
         for (j = 0; j < 5; ++j)
         {
            // TBD - update these to do something interesting.
            cache_.report_base_metrics_start (0);
            cache_.report_base_metrics_stop (0);
            cache_.report_enqueue_start (0);
            cache_.report_enqueue_stop (0);
            cache_.report_enqueue_suspend (0);
            cache_.report_enqueue_resume (0);
            cache_.report_dequeue_start (0);
            cache_.report_dequeue_stop (0);
            cache_.report_dequeue_suspend (0);
            cache_.report_dequeue_resume (0);

            H01_push_consumer_adapter_->push (event_set_);
            H05_push_consumer_adapter_->push (event_set_);
            H10_push_consumer_adapter_->push (event_set_);
            H20_push_consumer_adapter_->push (event_set_);
            H40_push_consumer_adapter_->push (event_set_);

            S01_push_consumer_adapter_->push (event_set_);
            S05_push_consumer_adapter_->push (event_set_);
            S10_push_consumer_adapter_->push (event_set_);
            S20_push_consumer_adapter_->push (event_set_);
            S40_push_consumer_adapter_->push (event_set_);
         }

         ACE_OS::gettimeofday (end_tv);
         end_tv -= start_tv;
         ORBSVCS_Time::Time_Value_to_TimeT (interval, end_tv);

         upcall_monitor_ref_->report_aggregate_QoS (logger_ref_, interval);
         ACE_TRY_CHECK;

         Metrics::QoSParameter_t_ptr qos_ptr;
         Metrics::QoSParameter_Set_ptr qos_set_ptr;
         Metrics::Handle_Set_t handle_set (10);
         handle_set.length (10);

         ACE_DEBUG ((LM_DEBUG, "Querying individual handle QoS\n"));

         for (j = 1; j <= 10; ++j)
         {
            handle_set [j-1] = j;
            upcall_monitor_ref_->get_aggregate_QoS (j, qos_ptr);
            ACE_TRY_CHECK;
            ACE_DEBUG ((LM_DEBUG,
                        "[handle = %u] [entry_point = %s] [made = %u] "
                        "[missed = %u] [cancelled = %u] [is_hrt = %d]\n",
                        qos_ptr->handle, qos_ptr->entry_point,
                        qos_ptr->deadlines_made, qos_ptr->deadlines_missed,
                        qos_ptr->operations_cancelled, qos_ptr->is_hrt));
         }

         ACE_DEBUG ((LM_DEBUG, "Querying handle set QoS\n"));

         upcall_monitor_ref_->get_aggregate_QoS_set (handle_set, qos_set_ptr);
         ACE_TRY_CHECK;
         for (j = 0; j < qos_set_ptr->length (); ++j)
         {
            ACE_DEBUG ((LM_DEBUG,
                        "[handle = %u] [entry_point = %s] [made = %u] "
                        "[missed = %u] [cancelled = %u] [is_hrt = %d]\n",
                        (*qos_set_ptr)[j].handle, (*qos_set_ptr)[j].entry_point,
                        (*qos_set_ptr)[j].deadlines_made, (*qos_set_ptr)[j].deadlines_missed,
                        (*qos_set_ptr)[j].operations_cancelled, (*qos_set_ptr)[j].is_hrt));
         }

         cache_.flush_ACE_Metrics_Cache ();
         cache_.flush_TAO_Metrics_LocalCache ();
         cache_.output_statistics (logger_ref_);
         cache_.flip_supplier_and_consumer ();
         cache_.flush_ACE_Metrics_Cache ();
         cache_.flush_TAO_Metrics_LocalCache ();

         CORBA::ULong frame_id;
         frame_manager_.get_frame_data (10000000, start_tv, end_tv, frame_id);  // get  1 Hz data
         ACE_DEBUG ((LM_DEBUG,
                     "ending 1 Hz data frame: [id = %u] "
                     "[start = %u sec %u usec] [end = %u sec %u usec]\n",
                     frame_id, start_tv.sec (), start_tv.usec (), end_tv.sec (), end_tv.usec ()));

         frame_manager_.get_frame_data ( 2000000, start_tv, end_tv, frame_id);  // get  5 Hz data
         ACE_DEBUG ((LM_DEBUG,
                     "ending 5 Hz data frame: [id = %u] "
                     "[start = %u sec %u usec] [end = %u sec %u usec]\n",
                     frame_id, start_tv.sec (), start_tv.usec (), end_tv.sec (), end_tv.usec ()));

         frame_manager_.get_frame_data ( 1000000, start_tv, end_tv, frame_id);  // get 10 Hz data
         ACE_DEBUG ((LM_DEBUG,
                     "ending 10 Hz data frame: [id = %u] "
                     "[start = %u sec %u usec] [end = %u sec %u usec]\n",
                     frame_id, start_tv.sec (), start_tv.usec (), end_tv.sec (), end_tv.usec ()));

         frame_manager_.get_frame_data (  500000, start_tv, end_tv, frame_id);  // get 20 Hz data
         ACE_DEBUG ((LM_DEBUG,
                     "ending 20 Hz data frame: [id = %u] "
                     "[start = %u sec %u usec] [end = %u sec %u usec]\n",
                     frame_id, start_tv.sec (), start_tv.usec (), end_tv.sec (), end_tv.usec ()));

         frame_manager_.get_frame_data (  250000, start_tv, end_tv, frame_id);  // get 40 Hz data
         ACE_DEBUG ((LM_DEBUG,
                    "ending 40 Hz data frame: [id = %u] "
                    "[start = %u sec %u usec] [end = %u sec %u usec]\n",
                    frame_id, start_tv.sec (), start_tv.usec (), end_tv.sec (), end_tv.usec ()));
      }

      //////////////////////////
      // Termination sequence //
      //////////////////////////

      ACE_DEBUG ((LM_DEBUG, "about to clean up push consumers\n"));

      H40_push_consumer_adapter_->disconnect_push_consumer ();
      H20_push_consumer_adapter_->disconnect_push_consumer ();
      H10_push_consumer_adapter_->disconnect_push_consumer ();
      H05_push_consumer_adapter_->disconnect_push_consumer ();
      H01_push_consumer_adapter_->disconnect_push_consumer ();
      S40_push_consumer_adapter_->disconnect_push_consumer ();
      S20_push_consumer_adapter_->disconnect_push_consumer ();
      S10_push_consumer_adapter_->disconnect_push_consumer ();
      S05_push_consumer_adapter_->disconnect_push_consumer ();
      S01_push_consumer_adapter_->disconnect_push_consumer ();
    }

    ACE_CATCHANY
    {
       ACE_DEBUG ((LM_DEBUG, "caught an exception\n"));
       ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "TAO_Metrics_Local_Cache_Test::run");
       result = -1;
    }
    ACE_ENDTRY;

    ACE_DEBUG ((LM_DEBUG, "about to shut down orb\n"));

    // Shut down orb, then wait for the ORB thread to exit.
    this->orb_->shutdown ();

    ACE_DEBUG ((LM_DEBUG, "about to wait on orb thread\n"));

    orb_thread_manager.wait ();

    ACE_DEBUG ((LM_DEBUG, "all done with run\n"));

    return result;
}


// Parses the command line arguments.

// Run the ORB event loop.
void *
TAO_Metrics_Local_Cache_Test::run_orb (void *that)
{
   ACE_TRY_NEW_ENV
   {
      ACE_static_cast (TAO_Metrics_Local_Cache_Test *, that)->orb_->run ();
      ACE_TRY_CHECK;
   }
   ACE_CATCHANY
   {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "TAO_Metrics_Local_Cache_Test::run_orb");
   }
   ACE_ENDTRY;

   return 0;
}


void TAO_Metrics_Local_Cache_Test_PushConsumer::
push (const RtecEventComm::EventSet &, CORBA_Environment &ACE_TRY_ENV)
{
   ACE_Time_Value pause_tv;

   switch (state_variable_)
   {
   case 0:
      pause_tv.set (0,0);  // instantaneous -  all should succeed
      break;
   case 1:
      pause_tv.set (0, 25000); // 40 Hz may miss
      break;
   case 2:
      pause_tv.set (0, 50000); // 20 Hz may miss
      break;
   case 3:
      pause_tv.set (0, 100000); // 10 Hz may miss
      break;
   case 4:
      pause_tv.set (0, 200000); // 5 Hz may miss
      break;
   case 5:
      pause_tv.set (1, 0); // 1 Hz may miss
      break;
   case 6:
   default:
      pause_tv.set (2, 0); // 1/2 Hz - none should succeed
      break;
   }

   state_variable_ = (state_variable_ + 1) % 7;
   ACE_OS::sleep (pause_tv);
}

void TAO_Metrics_Local_Cache_Test_PushConsumer::
disconnect_push_consumer (CORBA_Environment &ACE_TRY_ENV)
{
   ACE_DEBUG ((LM_DEBUG,
               "TAO_Metrics_Local_Cache_Test_PushConsumer::"
               "disconnect_push_consumer\n"));
}

int
main (int argc, char *argv[])
{
   ACE_TRY_NEW_ENV
   {
      TAO_Metrics_Local_Cache_Test logger_driver;

      ACE_DEBUG ((LM_DEBUG,
                  "%s; initializing TAO_Metrics logger driver\n", __FILE__));

      if (logger_driver.init (argc, argv) < 0)
      {
         ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "TAO_Metrics logger driver init failed."), 1);
      }

      ACE_DEBUG ((LM_DEBUG,
                  "%s; running TAO_Metrics logger driver\n", __FILE__));

      logger_driver.run (ACE_TRY_ENV);
      ACE_TRY_CHECK;
   }

   ACE_CATCHANY
   {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION, "TAO_Metrics logger test driver exception in main.");
      return 1;
   }
   ACE_ENDTRY;

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */