Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / examples / RTScheduling / Job_i.cpp
blobb2a518cb81ed16413d041ef4b0de5751385e75f6
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);
22 Job_i::~Job_i (void)
24 delete task_stats_;
27 const ACE_CString&
28 Job_i::name (void)
30 return job_name_;
33 const ACE_CString&
34 Job_i::poa (void)
36 return POA_name_;
39 int
40 Job_i::init (ACE_Arg_Shifter& arg_shifter)
42 // Read the name of the Job
43 job_name_ = ACE_TEXT_ALWAYS_CHAR(arg_shifter.get_current ());
44 arg_shifter.consume_arg ();
46 // Read the name of the POA
47 POA_name_ = ACE_TEXT_ALWAYS_CHAR(arg_shifter.get_current ());
48 arg_shifter.consume_arg ();
50 return 0;
53 void
54 Job_i::work (CORBA::ULong work,
55 CORBA::Short importance)
57 static CORBA::ULong prime_number = 9619;
59 if (TAO_debug_level > 0)
60 ACE_DEBUG ((LM_DEBUG,
61 "Job_i::work: %d units of work\n",
62 work));
64 CORBA::Object_var object =
65 this->dt_creator_->orb ()->resolve_initial_references (
66 "RTScheduler_Current");
68 RTScheduling::Current_var current =
69 RTScheduling::Current::_narrow (object.in ());
70 RTScheduling::Current::IdType_var guid = current->id ();
72 if (guid_ == 0)
73 ACE_OS::memcpy (&guid_,
74 guid->get_buffer (),
75 sizeof (guid->length ()));
77 if (TAO_debug_level > 0)
78 ACE_DEBUG ((LM_DEBUG,
79 "%t Guid is %d, Importance is %d\n",
80 guid_,
81 importance));
83 ACE_TCHAR msg [BUFSIZ];
84 ACE_OS::sprintf (msg,
85 ACE_TEXT("Guid is ")
86 ACE_SIZE_T_FORMAT_SPECIFIER
87 ACE_TEXT("\n"), guid_);
89 dt_creator_->log_msg (ACE_TEXT_ALWAYS_CHAR(msg));
91 for (; work != 0; work--)
93 // ACE_hrtime_t now = ACE_OS::gethrtime ();
95 ACE_Time_Value *base_time = dt_creator_->base_time ();
96 if (base_time == 0)
97 return;
99 ACE_Time_Value run_time = ACE_OS::gettimeofday () - *(base_time);
100 TASK_STATS::instance ()->sample (run_time.sec (), guid_);
102 ACE_Time_Value count_down_time (1);
103 ACE_Countdown_Time count_down (&count_down_time);
105 while (count_down_time > ACE_Time_Value::zero)
107 ACE::is_prime (prime_number,
109 prime_number / 2);
110 count_down.update ();
113 run_time = ACE_OS::gettimeofday () - *(dt_creator_->base_time ());
114 TASK_STATS::instance ()->sample (run_time.sec (), guid_);
116 CORBA::Policy_var sched_param;
117 sched_param = dt_creator_->sched_param (importance);
118 const char * name = 0;
119 current->update_scheduling_segment (name,
120 sched_param.in (),
121 sched_param.in ());
125 void
126 Job_i::post_work (int /*guid*/,
127 int /*importance*/)
132 Job_i::guid (void)
134 return this->guid_;
137 void
138 Job_i::shutdown (void)
140 dt_creator_->job_ended ();
143 void
144 Job_i::dump_stats (void)
146 ACE_TCHAR fname [BUFSIZ];
147 ACE_OS::sprintf (fname,
148 ACE_TEXT("Job_")
149 ACE_SIZE_T_FORMAT_SPECIFIER
150 ACE_TEXT(".dat"),
151 guid_);
153 if (TAO_debug_level > 0)
154 ACE_DEBUG ((LM_DEBUG,
155 "File name %s\n",
156 fname));
158 ACE_TCHAR msg [BUFSIZ];
159 ACE_OS::sprintf (msg,
160 ACE_TEXT("#Schedule Output for DT ")
161 ACE_SIZE_T_FORMAT_SPECIFIER,
162 guid_);
164 task_stats_->dump_samples (fname, msg);