summaryrefslogtreecommitdiff
path: root/TAO/examples/Kokyu_dsrt_schedulers/edf_example/dt_two_prio_client.cpp
blob: d4a61e4d652989182603e8d9584857a485c44171 (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
//$Id$

#include "ace/Get_Opt.h"
#include "ace/Task.h"
#include "ace/High_Res_Timer.h"
#include "tao/RTScheduling/RTScheduler_Manager.h"
#include "test1C.h"
#include "EDF_Scheduler.h"
#include "orbsvcs/orbsvcs/Time_Utilities.h"
#include "Task_Stats.h"
#include "cpuload.h"

#include "dt_oneway_config.h"
#include "dt_oneway_dsui_families.h"
#include <dsui.h>
#include <cstdlib>

ACE_RCSID(MT_Server, client, "client.cpp,v 1.2 2003/10/08 13:26:32 venkita Exp")

const char *ior = "file://test1.ior";
int do_shutdown = 1;
int enable_dynamic_scheduling = 1;
int enable_yield = 1;
int enable_rand = 0;
int niteration1 = 5;
int niteration2 = 4;
int workload1 = 2;
int period1 = 4;
int workload2 = 2;
int period2 = 6;

int ID_BEGIN = 0;
class Worker : public ACE_Task_Base
{
  // = TITLE
  //   Run a server thread
  //
  // = DESCRIPTION
  //   Use the ACE_Task_Base class to run server threads
  //
public:
  Worker (CORBA::ORB_ptr orb,
          Simple_Server1_ptr server_ptr,
          RTScheduling::Current_ptr current,
          EDF_Scheduler* scheduler,
          long importance,
          CORBA::Long server_load,
          CORBA::Long period,
          CORBA::Long niteration,
	  int worker_id);
  // ctor

  virtual int svc (void);
  // The thread entry point.

private:
  CORBA::ORB_var orb_;
  // The orb

  Simple_Server1_var server_;
  RTScheduling::Current_var scheduler_current_;
  EDF_Scheduler* scheduler_;
  long importance_;
  CORBA::Long server_load_;
  CORBA::Long period_;
  CORBA::Long niteration_;
  int sleep_time_;
  unsigned int m_id;
};

class Worker_c : public ACE_Task_Base
{
  // = TITLE
  //   Run a server thread
  //
  // = DESCRIPTION
  //   Use the ACE_Task_Base class to run server threads
  //
public:
  Worker_c (CORBA::ORB_ptr orb,
          RTScheduling::Current_ptr current,
          EDF_Scheduler* scheduler,
          long importance,
          CORBA::Long server_load,
          CORBA::Long period,
          CORBA::Long niteration,
          int worker_id);
  // ctor

  virtual int svc (void);
  // The thread entry point.

private:
  CORBA::ORB_var orb_;
  // The orb

  RTScheduling::Current_var scheduler_current_;
  EDF_Scheduler* scheduler_;
  long importance_;
  CORBA::Long server_load_;
  CORBA::Long period_;
  CORBA::Long niteration_;
  int sleep_time_;
  unsigned int m_id;
};

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "k:sN:n:W:P:w:p:r");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        ior = get_opts.opt_arg ();
        break;

      case 's':
        enable_yield = 0;
        break;

      case 'N':
        niteration1 = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 'n':
        niteration2 = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 'W':
        workload1 = ACE_OS::atoi (get_opts.opt_arg ());
        break;
  
      case 'P':
        period1 = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 'w':
        workload2 = ACE_OS::atoi (get_opts.opt_arg ());
        break;

      case 'p':
        period2 = ACE_OS::atoi (get_opts.opt_arg ());
        break;
  
      case 'r':
        enable_rand = 1;
        break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
                           "-k <ior> "
                           "-s (disable yield)"
			   "-n <niterations>"
                           "-w <workload>"
                           "-p <period>"
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}

int
main (int argc, char *argv[])
{
  ds_control ds_cntrl ("DT_Oneway", "dt_oneway_enable.dsui");
  ACE_High_Res_Timer non_dsui_timer;
  non_dsui_timer.calibrate ();
  non_dsui_timer.start();

  /* MEASURE: Program start time */
  DSUI_EVENT_LOG(MAIN_GROUP_FAM, START,1,0,NULL);

  EDF_Scheduler* scheduler=0;
  RTScheduling::Current_var current;
  int prio;
  int max_prio;
  ACE_Sched_Params::Policy  sched_policy = ACE_SCHED_RR;
  int sched_scope = ACE_SCOPE_THREAD;
  long flags;

  if (sched_policy == ACE_SCHED_RR) {
    /* MEASURE (DP): Schedule policy */
    char* policy = "ACE_SCHED_RR";
    DSUI_EVENT_LOG (MAIN_GROUP_FAM, SCHEDULE_SETUP, 1, strlen (policy), policy);
    flags = THR_NEW_LWP | THR_BOUND | THR_JOINABLE | THR_SCHED_RR;
  }
  else {
    /* MEASURE (DP): Schedule policy */
    char* policy = "ACE_SCHED_FIFO";
    DSUI_EVENT_LOG (MAIN_GROUP_FAM, SCHEDULE_SETUP, 1, strlen (policy), policy);
    flags = THR_NEW_LWP | THR_BOUND | THR_JOINABLE | THR_SCHED_FIFO;
  }

  ACE_hthread_t main_thr_handle;
  ACE_Thread::self (main_thr_handle);

  max_prio = ACE_Sched_Params::priority_max (sched_policy,
                                             sched_scope);

  ACE_Sched_Params sched_params (sched_policy, max_prio);

  ACE_OS::sched_params (sched_params);

  if (ACE_Thread::getprio (main_thr_handle, prio) == -1)
    {
      if (errno == ENOTSUP)
        {
          ACE_ERROR((LM_ERROR,
                     ACE_TEXT ("getprio not supported\n")
                     ));
        }
      else
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n")
                      ACE_TEXT ("thr_getprio failed")));
        }
    }

  ACE_DEBUG ((LM_DEBUG, "(%t|%T): main thread prio is %d\n", prio));

  CPULoad::calibrate(10);
  ACE_TRY_NEW_ENV
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv, "" ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (parse_args (argc, argv) != 0)
        return 1;

      CORBA::Object_var object =
        orb->string_to_object (ior ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      Simple_Server1_var server =
        Simple_Server1::_narrow (object.in () ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      if (CORBA::is_nil (server.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Object reference <%s> is nil\n",
                             ior),
                            1);
        }

      if (enable_dynamic_scheduling)
        {
          ACE_DEBUG ((LM_DEBUG, "Dyn Sched enabled\n"));
          CORBA::Object_ptr manager_obj =
            orb->resolve_initial_references ("RTSchedulerManager"
                                             ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          TAO_RTScheduler_Manager_var manager =
            TAO_RTScheduler_Manager::_narrow (manager_obj
                                              ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          Kokyu::DSRT_Dispatcher_Impl_t disp_impl_type;
          if (enable_yield)
            {
              disp_impl_type = Kokyu::DSRT_CV_BASED;
            }
          else
            {
              disp_impl_type = Kokyu::DSRT_OS_BASED;
            }

          ACE_NEW_RETURN (scheduler,
                          EDF_Scheduler (orb.in (),
                                         disp_impl_type,
                                         sched_policy,
                                         sched_scope), -1);

	  /* MEASURE: Scheduler start time */
	  DSUI_EVENT_LOG (MAIN_GROUP_FAM, SCHEDULER_STARTED, 1, 0, NULL);

          manager->rtscheduler (scheduler);

          CORBA::Object_var object =
            orb->resolve_initial_references ("RTScheduler_Current"
                                              ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

          current  =
            RTScheduling::Current::_narrow (object.in () ACE_ENV_ARG_PARAMETER);
          ACE_TRY_CHECK;

        }

      int importance=0;

      Worker worker2 (orb.in (),
                      server.in (),
                      current.in (),
                      scheduler,
                      importance,
                      workload2,
                      period2,
                      niteration2,
                      2);

      if (worker2.activate (flags, 1, 0, max_prio) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      "(%t|%T) cannot activate worker thread.\n"));
        }

//	usleep(5000);
      Worker_c worker1 (orb.in (),
                      current.in (),
                      scheduler,
                      importance,
                      workload1,
                      period1,
                      niteration1,
                      1);

      if (worker1.activate (flags, 1, 0, max_prio) != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      "(%t|%T) cannot activate worker thread.\n"));
        }


      worker2.wait ();
      worker1.wait ();

      /* MEASURE: Wait for worker thread done in main thread */
      // char* msg = "(%t): wait for worker threads done in main thread\n";
      // Get thread id
      // DSUI_EVENT_LOG (MAIN_GROUP_FAM, WORKER_WAIT_DONE, 1, strlen(msg), msg);

      ACE_DEBUG ((LM_DEBUG,
                  "(%t): wait for worker threads done in main thread\n"));

      if (do_shutdown)
        {
          if (enable_dynamic_scheduling)
            {
              EDF_Scheduling::SchedulingParameter sched_param;
              sched_param.importance = 0;
              ACE_Time_Value deadline_tv = ACE_OS::gettimeofday () + ACE_Time_Value (10,0);
              sched_param.deadline = deadline_tv.sec ()*10000000 + deadline_tv.usec ()*10;
              sched_param.task_id = ID_BEGIN++;
              sched_param.period = 0;
              CORBA::Policy_var sched_param_policy =
                scheduler->create_scheduling_parameter (sched_param);
              CORBA::Policy_ptr implicit_sched_param = 0;
              current->begin_scheduling_segment (0,
                                                 sched_param_policy.in (),
                                                 implicit_sched_param
                                                 ACE_ENV_ARG_PARAMETER);
              ACE_TRY_CHECK;
            }

            ACE_DEBUG ((LM_DEBUG, "(%t): about to call server shutdown\n"));

	    /* MEASURE: Call to shutdown server */
	    // char* msg = "(%t): wait for worker threads done in main thread\n";
	    // Get thread id
	    DSUI_EVENT_LOG (MAIN_GROUP_FAM, CALL_SERVER_SHUTDOWN, 1, 0, NULL);

            server->shutdown (ACE_ENV_SINGLE_ARG_PARAMETER);
            ACE_TRY_CHECK;

	    /* MEASURE: After call to server shutdown */
	    DSUI_EVENT_LOG (MAIN_GROUP_FAM, AFTER_SERVER_SHUTDOWN, 1, 0, NULL);
            ACE_DEBUG ((LM_DEBUG, "after shutdown call in main thread\n"));


            if (enable_dynamic_scheduling)
            {
              current->end_scheduling_segment (0 ACE_ENV_ARG_PARAMETER);
              ACE_TRY_CHECK;
            }
        }

      scheduler->shutdown ();

      /* MEASURE: Scheduler stop time */
      DSUI_EVENT_LOG (MAIN_GROUP_FAM, SCHEDULER_SHUTDOWN, 1, 0, NULL);
      ACE_DEBUG ((LM_DEBUG, "scheduler shutdown done\n"));
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Exception caught:");
      return 1;
    }
  ACE_ENDTRY;

  /* MEASURE: Program stop time */
  DSUI_EVENT_LOG(MAIN_GROUP_FAM, STOP, 1, 0, NULL);

  non_dsui_timer.stop();
  ACE_hrtime_t dsui_ovhd_time;
  non_dsui_timer.elapsed_time (dsui_ovhd_time);

  ACE_OS::printf ( ACE_TEXT ("Elapsed time: %lu (nsec)\n"), dsui_ovhd_time);
  return 0;
}

// ****************************************************************

Worker::Worker (CORBA::ORB_ptr orb,
                Simple_Server1_ptr server_ptr,
                RTScheduling::Current_ptr current,
                EDF_Scheduler* scheduler,
                long importance,
                CORBA::Long server_load,
                CORBA::Long period,
                CORBA::Long niteration,
		int worker_id)
  :  orb_ (CORBA::ORB::_duplicate (orb)),
     server_ (Simple_Server1::_duplicate (server_ptr)),
     scheduler_current_ (RTScheduling::Current::_duplicate (current)),
     scheduler_ (scheduler),
     importance_ (importance),
     server_load_ (server_load),
     period_(period),
     niteration_(niteration),
     m_id (worker_id)
     //     sleep_time_ (sleep_time)
{
}

int
Worker::svc (void)
{
  /* MEASURE: Worker start time */
  DSUI_EVENT_LOG (WORKER_GROUP_FAM, WORKER_STARTED, m_id, 0, NULL);

  ACE_DECLARE_NEW_CORBA_ENV;
  const char * name = 0;
  /*
  ACE_DEBUG ((LM_DEBUG, "(%t|%T):about to sleep for %d sec\n", sleep_time_));
  ACE_OS::sleep (sleep_time_);
  ACE_DEBUG ((LM_DEBUG, "(%t|%T):woke up from sleep for %d sec\n", sleep_time_));
  */
  ACE_hthread_t thr_handle;
  ACE_Thread::self (thr_handle);
  int prio;

  if (ACE_Thread::getprio (thr_handle, prio) == -1)
    {
      if (errno == ENOTSUP)
        {
          ACE_ERROR((LM_ERROR,
                     ACE_TEXT ("getprio not supported\n")
                     ));
        }
      else
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n")
                      ACE_TEXT ("thr_getprio failed")));
        }
    }

//  ACE_DEBUG ((LM_DEBUG, "(%t|%T) worker activated with prio %d AND iteration is %d\n", prio,niteration_));

     EDF_Scheduling::SchedulingParameter sched_param;
     CORBA::Policy_var sched_param_policy;
      CORBA::Policy_var implicit_sched_param;
  double rand2=0.0;

  if (enable_dynamic_scheduling)
    {
      sched_param.importance = importance_;
      ORBSVCS_Time::Time_Value_to_TimeT (sched_param.deadline,
                                         ACE_OS::gettimeofday () +
                                         ACE_Time_Value (period_,0) );
      sched_param.period = period_*10000000;
      sched_param.task_id = ID_BEGIN++;
      sched_param_policy = scheduler_->create_scheduling_parameter (sched_param);

      //If we do not define implicit_sched_param, the new spawned DT will have the default lowest prio.
      implicit_sched_param = sched_param_policy;

      /* MEASURE: Start of scheduling segment */
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, BEGIN_SCHED_SEGMENT, m_id, 0, NULL);
//      ACE_DEBUG ((LM_DEBUG, "(%t|%T):before begin_sched_segment\n"));

      scheduler_current_->begin_scheduling_segment (name,
                                                    sched_param_policy.in (),
                                                    implicit_sched_param.in ()
                                                    ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      /* MEASURE: End of scheduling segment */
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, END_SCHED_SEGMENT, m_id, 0, NULL);
//      ACE_DEBUG ((LM_DEBUG, "(%t|%T):after begin_sched_segment\n"));
    }

ACE_Time_Value start_t, repair_t;
   repair_t=ACE_Time_Value(0,0);

for(int i=0;i<niteration_;i++)
{
  if(i>0 && enable_dynamic_scheduling) 
  {
/*      if(enable_rand)
        ORBSVCS_Time::Time_Value_to_TimeT (sched_param.deadline,
                                         sched_param.deadline +
                                         ACE_Time_Value (period_,0) -
                                         ACE_Time_Value (0,rand) );
      else

        ORBSVCS_Time::Time_Value_to_TimeT (sched_param.deadline,
                                         sched_param.deadline +
                                         ACE_Time_Value (period_,0) );
*/
       sched_param.deadline = sched_param.deadline+period_*10000000;
      sched_param_policy = scheduler_->create_scheduling_parameter (sched_param);

      //If we do not define implicit_sched_param, the new spawned DT will have the default lowest prio.
      implicit_sched_param = sched_param_policy;
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, UPDATE_SCHED_SEGMENT_BEGIN, m_id, 0, NULL);
      scheduler_current_->update_scheduling_segment(name,
                                                    sched_param_policy.in (),
                                                    implicit_sched_param.in ()
                                                    ACE_ENV_ARG_PARAMETER);
     ACE_CHECK_RETURN (-1);
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, UPDATE_SCHED_SEGMENT_END, m_id, 0, NULL);
  }

  /*DTTIME:
    oneway call start on the client side.
  */
  /* MEASURE: One way call start */
//  ACE_DEBUG ((LM_DEBUG, "(%t|%T):Task %d's %d iter about to make one way call and deadline is %Q\n",sched_param.task_id, i, sched_param.deadline));
  //  TAO_debug_level = 1;
   if (i==0)
	start_t =  ACE_OS::gettimeofday ();
   else {
    repair_t = start_t+ACE_Time_Value(period_*i,0)-ACE_OS::gettimeofday ();
//    ACE_DEBUG((LM_DEBUG,"start %d,%d, repair %d, %d, period %d, i %d\n",
//		start_t.sec(), start_t.usec(), repair_t.sec(), repair_t.usec(),
//		period_, i));
    }
//  ACE_Time_Value actual = ACE_OS::gettimeofday ()+ACE_Time_Value (period_,0);
//  TimeBase::TimeT deadline = actual.sec ()*10000000 + actual.usec ()*10;

  timeval tv;

tv.tv_sec = server_load_;
tv.tv_usec = 0;

CPULoad::run(tv);

  DSUI_EVENT_LOG (WORKER_GROUP_FAM, ONE_WAY_CALL_START, m_id, 0, NULL);
  server_->test_method (server_load_, sched_param.task_id, i, sched_param.deadline ACE_ENV_ARG_PARAMETER);

  ACE_CHECK_RETURN (-1);

  /*DTTIME:
    oneway call done on the client side.
  */
  /* MEASURE: One way call done */
  DSUI_EVENT_LOG (WORKER_GROUP_FAM, ONE_WAY_CALL_DONE, m_id, 0, NULL);
//  ACE_DEBUG ((LM_DEBUG, "(%t|%T):one way call done\n"));

  scheduler_->kokyu_dispatcher_->update_schedule (*(scheduler_current_->id ()),
                                      Kokyu::BLOCK);
//  rand2 = 0.03+0.1*rand()/RAND_MAX;
  rand2 = 0.1*rand()/RAND_MAX;
  if(enable_rand)
  {
    int sleep_t = period_*1000000-period_*rand2*1000000+repair_t.sec()*1000000+repair_t.usec();
    if(sleep_t > 0) 
    { 
    ACE_DEBUG((LM_DEBUG,"NOW I AM GOING TO SLEEP FOR %d.\n", 
		(int)(period_*1000000-period_*rand2*1000000)));
    usleep(sleep_t);
    }
    else
    {
    ACE_DEBUG((LM_DEBUG,"NOW I AM GOING TO SLEEP FOR %d\n", 0));
    }
  }
  else
  {
    ACE_Time_Value current = ACE_OS::gettimeofday ();
    int sleep_t = sched_param.deadline/10-current.sec()*1000000-current.usec();
    ACE_DEBUG((LM_DEBUG,"(%t|%T)NOW I AM GOING TO SLEEP FOR %d\n", sleep_t));
    
    usleep(sleep_t);
  }
}
  if (enable_dynamic_scheduling)
    {
      scheduler_current_->end_scheduling_segment (name);
      ACE_CHECK_RETURN (-1);
    }

  ACE_DEBUG ((LM_DEBUG, "client worker thread (%t) done\n"));

  return 0;
}

//--------------------------------------------------------------
Worker_c::Worker_c (CORBA::ORB_ptr orb,
                RTScheduling::Current_ptr current,
                EDF_Scheduler* scheduler,
                long importance,
                CORBA::Long server_load,
                CORBA::Long period,
                CORBA::Long niteration,
                int worker_id)
  :  orb_ (CORBA::ORB::_duplicate (orb)),
     scheduler_current_ (RTScheduling::Current::_duplicate (current)),
     scheduler_ (scheduler),
     importance_ (importance),
     server_load_ (server_load),
     period_(period),
     niteration_(niteration),
     m_id (worker_id)
     //     sleep_time_ (sleep_time)
{
}

int
Worker_c::svc (void)
{
  /* MEASURE: Worker start time */
  DSUI_EVENT_LOG (WORKER_GROUP_FAM, WORKER_STARTED, m_id, 0, NULL);

  ACE_DECLARE_NEW_CORBA_ENV;
  const char * name = 0;
  /*
  ACE_DEBUG ((LM_DEBUG, "(%t|%T):about to sleep for %d sec\n", sleep_time_));
  ACE_OS::sleep (sleep_time_);
  ACE_DEBUG ((LM_DEBUG, "(%t|%T):woke up from sleep for %d sec\n", sleep_time_));
  */
  ACE_hthread_t thr_handle;
  ACE_Thread::self (thr_handle);
  int prio;

  if (ACE_Thread::getprio (thr_handle, prio) == -1)
    {
      if (errno == ENOTSUP)
        {
          ACE_ERROR((LM_ERROR,
                     ACE_TEXT ("getprio not supported\n")
                     ));
        }
      else
        {
          ACE_ERROR ((LM_ERROR,
                      ACE_TEXT ("%p\n")
                      ACE_TEXT ("thr_getprio failed")));
        }
    }

//  ACE_DEBUG ((LM_DEBUG, "(%t|%T) worker activated with prio %d AND iteration is %d\n", prio,niteration_));

     EDF_Scheduling::SchedulingParameter sched_param;
     CORBA::Policy_var sched_param_policy;
      CORBA::Policy_var implicit_sched_param;
  double rand2=0.0;

  if (enable_dynamic_scheduling)
    {
      sched_param.importance = importance_;
      ORBSVCS_Time::Time_Value_to_TimeT (sched_param.deadline,
                                         ACE_OS::gettimeofday () +
                                         ACE_Time_Value (period_,0) );
      sched_param.period = period_*10000000;
      sched_param.task_id = ID_BEGIN++;
      sched_param_policy = scheduler_->create_scheduling_parameter (sched_param);

      //If we do not define implicit_sched_param, the new spawned DT will have the default lowest prio.
      implicit_sched_param = sched_param_policy;

      /* MEASURE: Start of scheduling segment */
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, BEGIN_SCHED_SEGMENT, m_id, 0, NULL);
//      ACE_DEBUG ((LM_DEBUG, "(%t|%T):before begin_sched_segment\n"));

      scheduler_current_->begin_scheduling_segment (name,
                                                    sched_param_policy.in (),
                                                    implicit_sched_param.in ()
                                                    ACE_ENV_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      /* MEASURE: End of scheduling segment */
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, END_SCHED_SEGMENT, m_id, 0, NULL);
//      ACE_DEBUG ((LM_DEBUG, "(%t|%T):after begin_sched_segment\n"));
    }

ACE_Time_Value start_t, repair_t;
   repair_t=ACE_Time_Value(0,0);

for(int i=0;i<niteration_;i++)
{
  if(i>0 && enable_dynamic_scheduling)
  {
/*      if(enable_rand)
        ORBSVCS_Time::Time_Value_to_TimeT (sched_param.deadline,
                                         sched_param.deadline +
                                         ACE_Time_Value (period_,0) -
                                         ACE_Time_Value (0,rand) );
      else

        ORBSVCS_Time::Time_Value_to_TimeT (sched_param.deadline,
                                         sched_param.deadline +
                                         ACE_Time_Value (period_,0) );
*/
       sched_param.deadline = sched_param.deadline+period_*10000000;
      sched_param_policy = scheduler_->create_scheduling_parameter (sched_param);

      //If we do not define implicit_sched_param, the new spawned DT will have the default lowest prio.
      implicit_sched_param = sched_param_policy;
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, UPDATE_SCHED_SEGMENT_BEGIN, m_id, 0, NULL);
      scheduler_current_->update_scheduling_segment(name,
                                                    sched_param_policy.in (),
                                                    implicit_sched_param.in ()
                                                    ACE_ENV_ARG_PARAMETER);
     ACE_CHECK_RETURN (-1);
      DSUI_EVENT_LOG (WORKER_GROUP_FAM, UPDATE_SCHED_SEGMENT_END, m_id, 0, NULL);
  }

   if (i==0)
        start_t =  ACE_OS::gettimeofday ();
   else {
    repair_t = start_t+ACE_Time_Value(period_*i,0)-ACE_OS::gettimeofday ();
    }

  timeval tv;

tv.tv_sec = server_load_;
tv.tv_usec = 0;

CPULoad::run(tv);

  scheduler_->kokyu_dispatcher_->update_schedule (*(scheduler_current_->id ()),
                                      Kokyu::BLOCK);
  rand2 = 0.1*rand()/RAND_MAX;
  if(enable_rand)
  {
    int sleep_t = period_*1000000-period_*rand2*1000000+repair_t.sec()*1000000+repair_t.usec();
    if(sleep_t > 0)
    {
    ACE_DEBUG((LM_DEBUG,"NOW I AM GOING TO SLEEP FOR %d.\n",
                (int)(period_*1000000-period_*rand2*1000000)));
    usleep(sleep_t);
    }
    else
    {
    ACE_DEBUG((LM_DEBUG,"NOW I AM GOING TO SLEEP FOR %d\n", 0));
    }
  }
  else
  {
    ACE_Time_Value current = ACE_OS::gettimeofday ();
    int sleep_t = sched_param.deadline/10-current.sec()*1000000-current.usec();
    ACE_DEBUG((LM_DEBUG,"(%t|%T)NOW I AM GOING TO SLEEP FOR %d\n", sleep_t));

    usleep(sleep_t);
  }
}
  if (enable_dynamic_scheduling)
    {
      scheduler_current_->end_scheduling_segment (name);
      ACE_CHECK_RETURN (-1);
    }

  ACE_DEBUG ((LM_DEBUG, "client worker thread (%t) done\n"));

  return 0;
}