summaryrefslogtreecommitdiff
path: root/TAO/performance-tests/Cubit/TAO/MT_Cubit/server.cpp
blob: e97ab4e5541545dfb2d78d47998ce4c7c98e0bad (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
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/tests
//
// = FILENAME
//    server.cpp
//
// = AUTHOR
//    Andy Gokhale,
//    Sumedh Mungee,
//    Sergio Flores-Gaitan, and 
//    Nagarajan Surendran
//
// ============================================================================

#include "server.h"
#include "ace/Sched_Params.h"

#if defined (NO_ACE_QUANTIFY)
#include "quantify.h"
#endif /* NO_ACE_QUANTIFY */

ACE_RCSID(MT_Cubit, server, "$Id$")

Globals::Globals (void)
  : ior_file (0),
    base_port (0),
    num_of_objs (2),
    use_name_service (1),
    thread_per_rate (0),
    use_multiple_priority (0),
    run_utilization_test (0),
    ready_ (0),
    ready_cnd_ (ready_mtx_),
    barrier_ (0)
{
  if (ACE_OS::hostname (hostname, BUFSIZ) != 0)
    ACE_OS::perror ("gethostname");
}

int
Globals::parse_args (int argc, char *argv[])
{
  ACE_Get_Opt opts (argc, argv, "sh:p:t:f:rmU");
  int c;

  ACE_DEBUG ((LM_DEBUG,
              "%s",
              hostname));

  while ((c = opts ()) != -1)
    {
      //      ACE_DEBUG ((LM_DEBUG,"parse_args:%c ,",c));
      switch (c)
      {
      case 'U':
        run_utilization_test = 1;
        break;
      case 'm':
        use_multiple_priority = 1;
        break;
      case 'r':
        thread_per_rate = 1;
        break;
      case 's':
        //        ACE_DEBUG ((LM_DEBUG,"Not using naming service\n"));
        use_name_service = 0;
        break;
      case 'f':
        //        ior_file = opts.optarg;
        ACE_NEW_RETURN (ior_file,
                        char[BUFSIZ],-1);
        ACE_OS::strcpy (ior_file,
                        opts.optarg);
        //        ACE_DEBUG ((LM_DEBUG,"Using file %s",ior_file));
        break;
      case 'h':
        ACE_OS::strcpy (hostname, opts.optarg);
        //        ACE_DEBUG ((LM_DEBUG, "h\n"));
        break;
      case 'p':
        base_port = ACE_OS::atoi (opts.optarg);
        ACE_DEBUG ((LM_DEBUG, "base_port:%d\n",base_port));
        break;
      case 't':
        num_of_objs = ACE_OS::atoi (opts.optarg);
        //        ACE_DEBUG ((LM_DEBUG,"num_of_objs:%d\n",num_of_objs));
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s"
                           " \t[-s Means NOT to use the name service] \n"
                           " \t[-p <port>]\n"
                           " \t[-h <my_hostname>]\n"
                           " \t[-t <num_objects>]\n"
                           " \t[-f <ior_file>]\n"
                           " \t[-r Use thread per rate]\n"
                           "\n", argv [0]),
                          -1);
      }
    }
  if (thread_per_rate == 1)
    num_of_objs = 4;

  if (run_utilization_test == 1)
    num_of_objs = 1;

  // Indicates successful parsing of command line
  return 0;
}

Cubit_Task::Cubit_Task (void)
{
  // No-op.
}

Cubit_Task::Cubit_Task (const char *args,
                        const char *orbname,
                        u_int num_of_objs,
                        Task_State *ts,
                        ACE_Thread_Manager *thr_mgr,
                        u_int task_id)
  : ACE_MT (ACE_Task<ACE_MT_SYNCH> (thr_mgr)),
    key_ ("Cubit"),
    orbname_ ((char *) orbname),
    orbargs_ ((char *) args),
    num_of_objs_ (num_of_objs),
    servants_ (0),
    servants_iors_ (0),
    task_id_ (task_id),
    ts_ (ts)
{
}

int
Cubit_Task::svc (void)
{
  ACE_hthread_t thr_handle;
  ACE_Thread::self (thr_handle);
  int prio;

  // thr_getprio () on the current thread should never fail.
  ACE_OS::thr_getprio (thr_handle, prio);

  ACE_DEBUG ((LM_DEBUG,
              "(%P|%t) Beginning Cubit task with args = '%s' and priority %d\n",
              orbargs_,
              prio));

  int rc = this->initialize_orb ();

  if (rc == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "ORB initialization failed.\n"),
                      -1);

  rc = this->create_servants ();
  if (rc == -1)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Create Servants failed.\n"),
                      -1);
  TAO_TRY
    {
      GLOBALS::instance ()->barrier_->wait ();

      // Handle requests for this object until we're killed, or one of
      // the methods asks us to exit.
      if (this->orb_manager_.run (TAO_TRY_ENV) == -1)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%p\n",
                           "run"),
                          -1);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("poa->destroy()");
    }
  TAO_ENDTRY;

  // Need to clean up and do a CORBA::release on everything we've
  // created!

  for (u_int i = 0; i < num_of_objs_; i++)
    delete this->servants_[i];

  return 0;
}

int
Cubit_Task::initialize_orb (void)
{
  TAO_TRY
    {
      ACE_ARGV args (this->orbargs_);

      int argc = args.argc ();
      char **argv = args.argv ();

      // /*DONE*/@@ Naga, can you please try to use the TAO_Object_Manager for
      // /*DONE*/all of this initialization, rather than doing it all by hand?

      if (this->orb_manager_.init_child_poa (argc,
                                             argv,
                                             "persistent_poa",
                                             TAO_TRY_ENV) == -1)
        return -1;


      this->orb_ = this->orb_manager_.orb ();
      // Do the argument parsing.
      if (this->task_id_ == 0)
        {
          //          ACE_DEBUG ((LM_DEBUG,"parsing the arguments\n"));
          if (GLOBALS::instance ()->parse_args (argc,
                                                argv) < 0)
            return -1;
          ACE_NEW_RETURN (GLOBALS::instance ()->barrier_,
                          ACE_Barrier (GLOBALS::instance ()->num_of_objs + 1),
                          -1);
          //          ACE_DEBUG ((LM_DEBUG,"(%t)Arguments parsed successfully\n"));
          ACE_MT (ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ready_mon, GLOBALS::instance ()->ready_mtx_, 1));
          GLOBALS::instance ()->ready_ = 1;
          GLOBALS::instance ()->ready_cnd_.broadcast ();
          ready_mon.release ();
          if (GLOBALS::instance ()->barrier_ == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               "(%t)Unable to create barrier\n"),
                              -1);
        }

      if (GLOBALS::instance ()->use_name_service == 0)
        return 0;

      CORBA::Object_var naming_obj =
        this->orb_->resolve_initial_references ("NameService");
      if (CORBA::is_nil (naming_obj.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to resolve the Name Service.\n"),
                          -1);

      this->naming_context_ =
        CosNaming::NamingContext::_narrow (naming_obj.in (),
                                           TAO_TRY_ENV);

      // Check the environment and return 1 if exception occurred or
      // nil pointer.
      if (TAO_TRY_ENV.exception () != 0 ||
          CORBA::is_nil (this->naming_context_.in ())==CORBA::B_TRUE )
        return -1;

      // Register the servant with the Naming Context....
      CosNaming::Name cubit_context_name (1);
      cubit_context_name.length (1);
      cubit_context_name[0].id =
        CORBA::string_dup ("MT_Cubit");

      TAO_TRY_ENV.clear ();
      CORBA::Object_var objref =
        this->naming_context_->bind_new_context (cubit_context_name,
                                                 TAO_TRY_ENV);

      if (TAO_TRY_ENV.exception() != 0)
        {
          CosNaming::NamingContext::AlreadyBound_ptr ex =
            CosNaming::NamingContext::AlreadyBound::_narrow (TAO_TRY_ENV.exception());
          if (ex != 0)
            {
              TAO_TRY_ENV.clear ();
              objref = this->naming_context_->resolve (cubit_context_name,
                                                       TAO_TRY_ENV);
              printf("NamingContext::AlreadyBound\n");
            }
          else
            TAO_TRY_ENV.print_exception ("bind() Cubit context object\n");
        }
      TAO_CHECK_ENV;

      this->mt_cubit_context_ =
        CosNaming::NamingContext::_narrow (objref.in (),
                                           TAO_TRY_ENV);
      TAO_CHECK_ENV;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("orb_init");
      return -1;
    }
  TAO_ENDTRY;

  return 0;
}

CORBA::String
Cubit_Task::get_servant_ior (u_int index)
{
  if (index >= num_of_objs_)
    return 0;

  return ACE_OS::strdup (this->servants_iors_[index]);
}

int
Cubit_Task::create_servants (void)
{
  TAO_TRY
    {
      // Create the array of cubit implementations.
      ACE_NEW_RETURN (this->servants_,
                      Cubit_i *[this->num_of_objs_],
                      -1);

      // Create the array of strings.
      ACE_NEW_RETURN (this->servants_iors_,
                      CORBA::String [this->num_of_objs_],
                      -1);

      char *buffer;
      // @@ Naga, can you please document why the value "3" is here?
      int len = ACE_OS::strlen (this->key_) + 3;

      ACE_NEW_RETURN (buffer,
                      char[len],
                      -1);

      // This loop creates multiple servants, and prints out their
      // IORs.
      for (u_int i = 0;
           i < this->num_of_objs_;
           i++)
        {
          ACE_OS::sprintf (buffer,
                           "%s%02d",
                           (char *) this->key_,
                           this->task_id_);

          ACE_NEW_RETURN (this->servants_[i],
                          Cubit_i (ts_),
                          -1);

          if (this->servants_[i] == 0)
            ACE_ERROR_RETURN ((LM_ERROR,
                               " (%P|%t) Unable to create "
                               "implementation object #%d\n",
                               i),
                              2);

          this->orb_manager_.activate_under_child_poa (buffer,
                                                       this->servants_[i],
                                                       TAO_TRY_ENV);
          TAO_CHECK_ENV;

          // Stringify the objref we'll be implementing, and print it
          // to stdout.  Someone will take that string and give it to
          // some client.  Then release the object.

          Cubit_var cubit =
            this->servants_[i]->_this (TAO_TRY_ENV);
          TAO_CHECK_ENV;

          CORBA::String_var str =
            this->orb_->object_to_string (cubit.in (),
                                          TAO_TRY_ENV);
          TAO_CHECK_ENV;

          this->servants_iors_[i] = ACE_OS::strdup (str.in ());

          // Register the servant with the Naming Context....
          CosNaming::Name cubit_name (1);
          cubit_name.length (1);
          cubit_name[0].id =
            CORBA::string_dup (buffer);

          if (CORBA::is_nil (this->mt_cubit_context_.in ()) == CORBA::B_FALSE)
            {
              this->mt_cubit_context_->bind (cubit_name,
                                             cubit.in (),
                                             TAO_TRY_ENV);
              if (TAO_TRY_ENV.exception () != 0)
                TAO_TRY_ENV.print_exception ("Attempt to bind() a cubit object to the name service Failed!\n");
              else
                ACE_DEBUG ((LM_DEBUG,
                            " (%t) Cubit object bound to the name \"%s\".\n",
                            buffer));
            }
        }

      delete [] buffer;
    }
  TAO_CATCHANY
    {
      TAO_TRY_ENV.print_exception ("print IOR");
      return -1;
    }
  TAO_ENDTRY;
  return 0;
}



int
Server::initialize (int argc, char **argv)
{
  this->argc_ = argc;
  this->argv_ = argv;
#if defined (VXWORKS)
#if defined (VME_DRIVER)
   STATUS status = vmeDrv ();

   if (status != OK)
     printf ("ERROR on call to vmeDrv()\n");

   status = vmeDevCreate ("/vme");

   if (status != OK)
     printf ("ERROR on call to vmeDevCreate()\n");
#endif /* defined (VME_DRIVER) */

#if defined (FORCE_ARGS)
   int argc = 4;
   char *argv[] = 
   {
     "server",
     "-s",
     "-f",
     "ior.txt" 
   };
#endif /* defined (FORCE_ARGS) */
#endif /* defined (VXWORKS) */

   // Make sure we've got plenty of socket handles.  This call will
   // use the default maximum.
   ACE::set_handle_limit ();
   return 0;
}

int
Server::start_servants (ACE_Thread_Manager *serv_thr_mgr,
                        Task_State *ts)
{

  ACE_ARGV tmp_args (this->argv_);
  const char *arg_buf = tmp_args.buf ();

  char *low_thread_args;

  int arg_len = ACE_OS::strlen (arg_buf);

  ACE_NEW_RETURN (low_thread_args,
                  char[arg_len + 1],
                  -1);

  ACE_OS::strcpy (low_thread_args,
                  arg_buf);
  char *args1;

  // @@ Naga, can you please explain why you need to do all of this?
  // i.e, we need some comments here!  In particular, what is args1
  // being used for and how will we know that ACE_DEFAULT_ARGV_BUFSIZ
  // is an appropriate size?  It seems to me that we should either (1)
  // add an accessor on ACE_ARGV to determine what this size ought to
  // be or (2) we should try to use/add a method on ACE_ARGV that
  // converts the argv back into a char * buffer or something!  At any
  // rate, this code should be cleaned up and abstracted better.
  ACE_NEW_RETURN (args1,
                  char[ACE_DEFAULT_ARGV_BUFSIZ],
                  -1);
  int i;

  for (i = 0; i < this->argc_ ; i++)
    {
      if (ACE_OS::strcmp (this->argv_[i], "-p") == 0
          && i - 1 < this->argc_)
        {
          GLOBALS::instance ()->base_port =
            ACE_OS::atoi (this->argv_[i + 1]);
          ACE_DEBUG ((LM_DEBUG,
                      "base_port:%d",
                      GLOBALS::instance()->base_port));
        }
      else if (ACE_OS::strcmp (this->argv_[i], "-h") == 0
               && i - 1 < this->argc_)
        ACE_OS::strcpy (GLOBALS::instance ()->hostname,
                        this->argv_[i+1]);
      else if (ACE_OS::strcmp (this->argv_[i], "-t") == 0
               && i - 1 < this->argc_)
        GLOBALS::instance ()->num_of_objs =
          ACE_OS::atoi (this->argv_ [i + 1]);
    }
  // Create an array to hold pointers to the Cubit objects.
  CORBA::String *cubits;

  ACE_NEW_RETURN (cubits,
                  CORBA::String [GLOBALS::instance ()->num_of_objs],
                  -1);
  ACE_OS::sprintf (args1,
                   "-ORBport %d "
                   "-ORBhost %s "
                   "-ORBobjrefstyle URL "
                   "-ORBsndsock 32768 "
                   "-ORBrcvsock 32768 ",
                   GLOBALS::instance ()->base_port,
                   GLOBALS::instance ()->hostname);

  char *high_thread_args;
  int args1_len = ACE_OS::strlen (args1);

  ACE_NEW_RETURN (high_thread_args,
                  char [arg_len + args1_len +1],
                  -1);

  ACE_OS::strcpy (high_thread_args, arg_buf);
  ACE_OS::strcat (high_thread_args, args1);
  Cubit_Task *high_priority_task;

  ACE_NEW_RETURN (high_priority_task,
                  Cubit_Task (high_thread_args,
                              "internet",
                              1,
                              ts,
                              serv_thr_mgr,
                              0), //task id 0.
                  -1);

#if defined (VXWORKS)
  ACE_Sched_Priority priority = ACE_THR_PRI_FIFO_DEF;
#elif defined (ACE_WIN32)
  ACE_Sched_Priority priority =
    ACE_Sched_Params::priority_max (ACE_SCHED_FIFO,
                                    ACE_SCOPE_THREAD);
#else
  // @@ Naga/Sergio, why is there a "25" here?  This seems like to
  // much of a "magic" number.  Can you make this more "abstract?"
  ACE_Sched_Priority priority = ACE_THR_PRI_FIFO_DEF + 25;
#endif /* VXWORKS */

  if (GLOBALS::instance ()->run_utilization_test == 1)
    priority =
      ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
                                       ACE_Sched_Params::priority_min (ACE_SCHED_FIFO,
                                                                       ACE_SCOPE_THREAD),
                                       ACE_SCOPE_THREAD);

  ACE_DEBUG ((LM_DEBUG,
              "Creating servant 0 with high priority %d\n",
              priority));

  // Make the high priority task an active object.
  if (high_priority_task->activate (THR_BOUND | ACE_SCHED_FIFO,
                                    1,
                                    0,
                                    priority) == -1)
    ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n"
                "\thigh_priority_task->activate failed"));

  //  ACE_DEBUG ((LM_DEBUG,"(%t) Waiting for argument parsing\n"));
  ACE_MT (ACE_GUARD_RETURN (ACE_Thread_Mutex, ready_mon, GLOBALS::instance ()->ready_mtx_,-1));

  while (!GLOBALS::instance ()->ready_)
    GLOBALS::instance ()->ready_cnd_.wait ();

  //  ACE_DEBUG ((LM_DEBUG,"(%t) Argument parsing waiting done\n"));

  if (GLOBALS::instance ()->run_utilization_test == 1)
    priority = 
      ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
                                       ACE_Sched_Params::priority_min (ACE_SCHED_FIFO,
                                                                       ACE_SCOPE_THREAD),
                                       ACE_SCOPE_THREAD);

  // Create an array to hold pointers to the low priority tasks.
  Cubit_Task **low_priority_task;

  ACE_NEW_RETURN (low_priority_task,
                  Cubit_Task *[GLOBALS::instance ()->num_of_objs],
                  -1);

  u_int number_of_low_priority_servants = 0;
  u_int number_of_priorities = 0;
  u_int grain = 0;
  u_int counter = 0;
  u_int j;

  number_of_low_priority_servants =
    GLOBALS::instance ()->num_of_objs - 1;

  // Drop the priority
  if (GLOBALS::instance ()->thread_per_rate == 1 
      || GLOBALS::instance ()->use_multiple_priority == 1)
    {
      number_of_priorities = 0;

      for (ACE_Sched_Priority_Iterator priority_iterator (ACE_SCHED_FIFO,
                                                          ACE_SCOPE_THREAD);
           priority_iterator.more ();
          priority_iterator.next ())
        number_of_priorities ++;

      // 1 priority is exclusive for the high priority client.
      number_of_priorities --;

      // Drop the priority, so that the priority of clients will
      // increase with increasing client number.
      for (j = 0;
           j < number_of_low_priority_servants;
           j++)
        priority =
          ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                               priority,
                                               ACE_SCOPE_THREAD);

      // Granularity of the assignment of the priorities.  Some OSs
      // have fewer levels of priorities than we have threads in our
      // test, so with this mechanism we assign priorities to groups
      // of threads when there are more threads than priorities.
      grain = number_of_low_priority_servants / number_of_priorities;
      counter = 0;

      if (grain <= 0)
        grain = 1;
    }
  else
    priority =
      ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                           priority,
                                           ACE_SCOPE_THREAD);

  ACE_DEBUG ((LM_DEBUG,
              "Creating %d servants starting at priority %d\n",
              number_of_low_priority_servants,
              priority));

  // Create the low priority servants.

  for (i = number_of_low_priority_servants; i > 0; i--)
    {
      char *args;

      ACE_NEW_RETURN (args,
                      char [ACE_DEFAULT_ARGV_BUFSIZ],
                      -1);
      ACE_OS::sprintf (args,
                       "-ORBport %d "
                       "-ORBhost %s "
                       "-ORBobjrefstyle URL "
                       "-ORBsndsock 32768 "
                       "-ORBrcvsock 32768 ",
                       (GLOBALS::instance ()->base_port == 0) ? (int) 0 :GLOBALS::instance ()->base_port+i,
                       GLOBALS::instance ()->hostname);

      int args_len = ACE_OS::strlen (args);
      char *new_args;
      
      ACE_NEW_RETURN  (new_args,
                       char [arg_len + args_len +1],
                       -1);

      ACE_OS::strcat (new_args,args);

      ACE_NEW_RETURN (low_priority_task [i - 1],
                      Cubit_Task (new_args,
				  "internet",
				  1,
				  ts,
				  serv_thr_mgr,
				  i),
                      -1);

      // Make the low priority task an active object.
      if (low_priority_task [i - 1]->activate (THR_BOUND | ACE_SCHED_FIFO,
                                           1,
                                           0,
                                           priority) == -1)
        ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n"
                    "\tlow_priority_task[i]->activate"));
      ACE_DEBUG ((LM_DEBUG,
                  "Created servant %d with priority %d\n",
                  i,
                  priority));

      // Use different priorities on thread per rate or multiple
      // priority.
      if (GLOBALS::instance ()->use_multiple_priority == 1 
          || GLOBALS::instance ()->thread_per_rate == 1)
        {
          counter = (counter + 1) % grain;
          if (counter == 0
              &&
               //Just so when we distribute the priorities among the
               //threads, we make sure we don't go overboard.
              (number_of_priorities * grain > number_of_low_priority_servants - (i - 1)))
            // Get the next higher priority.
            priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
                                                        priority,
                                                        ACE_SCOPE_THREAD);

        }
    } /* end of for() */

  GLOBALS::instance ()->barrier_->wait ();

  // Write the ior's to a file so the client can read them.
  {
    cubits[0] = high_priority_task->get_servant_ior (0);

    for (j = 0;
         j < GLOBALS::instance ()->num_of_objs-1;
         ++j)
      cubits[j + 1] = low_priority_task[j]->get_servant_ior (0);

    FILE *ior_f = 0;

    if (GLOBALS::instance ()->ior_file != 0)
      {
        //        ACE_DEBUG ((LM_DEBUG,"(%P|%t) Opening file:%s\n",GLOBALS::instance ()->ior_file));
        ior_f = ACE_OS::fopen (GLOBALS::instance ()->ior_file, "w");
      }

    for (j = 0;
         j < GLOBALS::instance ()->num_of_objs;
         ++j)
      {
        if (ior_f != 0)
          {
            //            ACE_DEBUG ((LM_DEBUG,"(%P|%t) ior_file is open :%s",GLOBALS::instance ()->ior_file));
            ACE_OS::fprintf (ior_f, "%s\n", cubits[j]);
            ACE_OS::printf ("cubits[%d] ior = %s\n",
                            j,
                            cubits[j]);
          }
      }

    if (ior_f != 0)
      {
        //        ACE_DEBUG ((LM_DEBUG,"(%P|%t) Closing ior file\n"));
        ACE_OS::fclose (ior_f);
      }
  }
  return 0;

}

Util_Thread *
Server::start_utilization (ACE_Thread_Manager *util_thr_mgr,
                           Task_State *ts)
{
  Util_Thread *util_task;

  ACE_NEW_RETURN (util_task,
                  Util_Thread (ts,
                               util_thr_mgr),
                  0);

  ACE_Sched_Priority priority =
    ACE_Sched_Params::priority_min (ACE_SCHED_FIFO,
                                    ACE_SCOPE_THREAD);
  
  ACE_DEBUG ((LM_DEBUG,
              "Creating Utilization Task with priority %d\n",
              priority));

  // Make the high priority task an active object.
  if (util_task->activate (THR_BOUND | ACE_SCHED_FIFO,
                           1,
                           0,
                           priority) == -1)
    {
      ACE_ERROR ((LM_ERROR, "(%P|%t) %p\n"
                  "\tutil_task->activate failed"));
    }

  return util_task;
}


// main routine.

#if defined (VXWORKS)
  // Rename main to server to make it easier to run both client and
  // server on one target.
extern "C"
int
server (int argc, char *argv[])
{
  ACE_Object_Manager ace_object_manager;
#else
int
main (int argc, char *argv[])
{
#endif
  // Dummy code to create the GLOBALS object in the global memory
  // instead of TSS.
  GLOBALS::instance ();

  Server server;
#if defined (ACE_HAS_THREADS)
  // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.
  if (ACE_OS::sched_params (
        ACE_Sched_Params (
          ACE_SCHED_FIFO,
#if defined (__Lynx__)
          30,
#elif defined (VXWORKS) /* ! __Lynx__ */
          6,
#elif defined (ACE_WIN32)
  ACE_Sched_Params::priority_max (ACE_SCHED_FIFO,
                                  ACE_SCOPE_THREAD),
#else
          ACE_THR_PRI_FIFO_DEF + 25,
#endif /* ! __Lynx__ */
          ACE_SCOPE_PROCESS)) != 0)
    {
      if (ACE_OS::last_error () == EPERM)
        ACE_DEBUG ((LM_MAX, "preempt: user is not superuser, "
                    "so remain in time-sharing class\n"));
      else
        ACE_ERROR_RETURN ((LM_ERROR, "%n: ACE_OS::sched_params failed\n%a"),
                          -1);
    }

  if (server.initialize (argc, argv) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error in Initialization\n"),
                      1);
  int _argc = 3;
  char *_argv[] = {"server",
                  "-t",
                  "1"};

  Task_State ts ( _argc, _argv);

  // parse the arguments to set the global task state.
  ts.parse_args (_argc,_argv);

  // preliminary argument processing
  
  for (int i=0;i<argc;i++)
    if (ACE_OS::strcmp (argv[i],"-U")==0)
      GLOBALS::instance ()->run_utilization_test =1;

  if (GLOBALS::instance ()->run_utilization_test == 1)
    {
      ts.run_server_utilization_test_ = 1;
      ts.loop_count_ = 0;
     }

  Util_Thread *util_task = 0;

  // Create the daemon thread in its own <ACE_Thread_Manager>.
  ACE_Thread_Manager servant_thread_manager;
  ACE_Thread_Manager util_thr_mgr;
  ACE_Time_Value total_elapsed;
  double util_task_duration = 0.0;
  double total_latency = 0.0;
  double total_latency_servants = 0.0;
  double total_util_task_duration = 0.0;

  if (GLOBALS::instance ()->run_utilization_test == 1)
    {
      if ((util_task = server.start_utilization (&util_thr_mgr, &ts)) == 0)
        ACE_ERROR_RETURN ((LM_ERROR,
                           "Error creating the utilization thread!\n"),
                          1);

      //
      // Time the utilization thread' "computation" to get %IdleCPU at the end of the test.
      //

#if defined (CHORUS)
      int pstartTime = 0;
      int pstopTime = 0;
      // Elapsed time will be in microseconds.
      pstartTime = pccTime1Get();
      // execute one computation.
      util_task->computation ();
      pstopTime = pccTime1Get();
      // Store the time in micro-seconds.
      util_task_duration = pstopTime - pstartTime;
#else /* CHORUS */
      ACE_High_Res_Timer timer_;
      // Elapsed time will be in microseconds.
      ACE_Time_Value delta_t;
      timer_.start ();
      // execute computation.
      for (int k=0; k < 10000; k++)
        util_task->computation ();
      timer_.stop ();
      timer_.elapsed_time (delta_t);
      // Store the time in milli-seconds.
      util_task_duration = ((double)delta_t.sec () *
                            ACE_ONE_SECOND_IN_USECS +
                            (double)delta_t.usec ())  / 10000;
#endif /* !CHORUS */
    }

#if defined (NO_ACE_QUANTIFY)
  quantify_stop_recording_data();
  quantify_clear_data ();
  quantify_start_recording_data();
#endif /* NO_ACE_QUANTIFY */

  if (server.start_servants (&servant_thread_manager,&ts) != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error creating the servants\n"),
                      1);

  ACE_DEBUG ((LM_DEBUG,
              "Wait for all the threads to exit\n"));

  // Wait for all the threads to exit.
  servant_thread_manager.wait ();
  //  ACE_Thread_Manager::instance ()->wait ();

  if (GLOBALS::instance ()->run_utilization_test == 1)
    {
      util_task->done_ = 1;

      // This will wait for the utilization thread to finish.
      util_thr_mgr.wait ();

      ts.timer_.elapsed_time (total_elapsed);

      total_util_task_duration =
        util_task_duration * util_task->get_number_of_computations ();

      total_latency = (total_elapsed.sec () *
                       ACE_ONE_SECOND_IN_USECS +
                       (double)total_elapsed.usec ());

      total_latency_servants = total_latency - total_util_task_duration;

      ACE_DEBUG ((LM_DEBUG,
                  "-------------------------- Stats -------------------------------\n"));

      ACE_DEBUG ((LM_DEBUG,
                  "(%t) UTILIZATION task performed \t%u computations\n"
                  "(%t) SERVANT task serviced \t\t%u CORBA calls\n"
                  "\t Ratio of computations to CORBA calls is %u.%u:1\n\n",
                  util_task->get_number_of_computations (),
                  ts.loop_count_,
                  util_task->get_number_of_computations () / ts.loop_count_,
                  (util_task->get_number_of_computations () % ts.loop_count_) * 100 / ts.loop_count_
                  ));

      ACE_DEBUG ((LM_DEBUG,
                  "(%t) Each computation had a duration of %f msecs\n"
                  "(%t) Total elapsed time of test is %f msecs\n",
                  util_task_duration / 1000,
                  total_latency / 1000));

      // Calc and print the CPU percentage. I add 0.5 to round to the
      // nearest integer before casting it to int.
      ACE_DEBUG ((LM_DEBUG,
                  "\t%% ORB Servant CPU utilization: %d %%\n"
                  "\t%% Idle time: %d %%\n",
                  (int) (total_latency_servants * 100 / total_latency + 0.5),
                  (int) (total_util_task_duration * 100 / total_latency + 0.5) ));
    }

#if defined (NO_ACE_QUANTIFY)
  quantify_stop_recording_data();
#endif /* NO_ACE_QUANTIFY */

#else
  ACE_DEBUG ((LM_DEBUG,
              "Test not run.  This platform doesn't seem to have threads.\n"));
#endif /* ACE_HAS_THREADS */
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Singleton<Globals,ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Singleton<Globals,ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */