Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / examples / RTScheduling / Job_i.cpp
blobf4723e88252c0ce857c199d863b7ea746ba54e2a
1 #include "Job_i.h"
2 #include "DT_Creator.h"
3 #include "Task_Stats.h"
5 #include "tao/debug.h"
7 #include "ace/Arg_Shifter.h"
8 #include "ace/High_Res_Timer.h"
9 #include "ace/OS_NS_sys_time.h"
10 #include "ace/Countdown_Time.h"
12 Job_i::Job_i (DT_Creator* dt_creator)
13 : dt_creator_ (dt_creator),
14 guid_ (0)
16 // create the stat object.
17 ACE_NEW (task_stats_, Task_Stats);
18 task_stats_->init (100);
21 Job_i::~Job_i ()
23 delete task_stats_;
26 const ACE_CString&
27 Job_i::name ()
29 return job_name_;
32 const ACE_CString&
33 Job_i::poa ()
35 return POA_name_;
38 int
39 Job_i::init (ACE_Arg_Shifter& arg_shifter)
41 // Read the name of the Job
42 job_name_ = ACE_TEXT_ALWAYS_CHAR(arg_shifter.get_current ());
43 arg_shifter.consume_arg ();
45 // Read the name of the POA
46 POA_name_ = ACE_TEXT_ALWAYS_CHAR(arg_shifter.get_current ());
47 arg_shifter.consume_arg ();
49 return 0;
52 void
53 Job_i::work (CORBA::ULong work,
54 CORBA::Short importance)
56 static CORBA::ULong prime_number = 9619;
58 if (TAO_debug_level > 0)
59 ACE_DEBUG ((LM_DEBUG,
60 "Job_i::work: %d units of work\n",
61 work));
63 CORBA::Object_var object =
64 this->dt_creator_->orb ()->resolve_initial_references (
65 "RTScheduler_Current");
67 RTScheduling::Current_var current =
68 RTScheduling::Current::_narrow (object.in ());
69 RTScheduling::Current::IdType_var guid = current->id ();
71 if (guid_ == 0)
72 ACE_OS::memcpy (&guid_,
73 guid->get_buffer (),
74 sizeof (guid->length ()));
76 if (TAO_debug_level > 0)
77 ACE_DEBUG ((LM_DEBUG,
78 "%t Guid is %d, Importance is %d\n",
79 guid_,
80 importance));
82 ACE_TCHAR msg [BUFSIZ];
83 ACE_OS::sprintf (msg,
84 ACE_TEXT("Guid is ")
85 ACE_SIZE_T_FORMAT_SPECIFIER
86 ACE_TEXT("\n"), guid_);
88 dt_creator_->log_msg (ACE_TEXT_ALWAYS_CHAR(msg));
90 for (; work != 0; work--)
92 // ACE_hrtime_t now = ACE_OS::gethrtime ();
94 ACE_Time_Value *base_time = dt_creator_->base_time ();
95 if (base_time == 0)
96 return;
98 ACE_Time_Value run_time = ACE_OS::gettimeofday () - *(base_time);
99 TASK_STATS::instance ()->sample (run_time.sec (), guid_);
101 ACE_Time_Value count_down_time (1);
102 ACE_Countdown_Time count_down (&count_down_time);
104 while (count_down_time > ACE_Time_Value::zero)
106 ACE::is_prime (prime_number,
108 prime_number / 2);
109 count_down.update ();
112 run_time = ACE_OS::gettimeofday () - *(dt_creator_->base_time ());
113 TASK_STATS::instance ()->sample (run_time.sec (), guid_);
115 CORBA::Policy_var sched_param;
116 sched_param = dt_creator_->sched_param (importance);
117 const char * name = 0;
118 current->update_scheduling_segment (name,
119 sched_param.in (),
120 sched_param.in ());
124 void
125 Job_i::post_work (int /*guid*/,
126 int /*importance*/)
131 Job_i::guid ()
133 return this->guid_;
136 void
137 Job_i::shutdown ()
139 dt_creator_->job_ended ();
142 void
143 Job_i::dump_stats ()
145 ACE_TCHAR fname [BUFSIZ];
146 ACE_OS::sprintf (fname,
147 ACE_TEXT("Job_")
148 ACE_SIZE_T_FORMAT_SPECIFIER
149 ACE_TEXT(".dat"),
150 guid_);
152 if (TAO_debug_level > 0)
153 ACE_DEBUG ((LM_DEBUG,
154 "File name %s\n",
155 fname));
157 ACE_TCHAR msg [BUFSIZ];
158 ACE_OS::sprintf (msg,
159 ACE_TEXT("#Schedule Output for DT ")
160 ACE_SIZE_T_FORMAT_SPECIFIER,
161 guid_);
163 task_stats_->dump_samples (fname, msg);