1 #include "ace/Get_Opt.h"
3 #include "ace/High_Res_Timer.h"
4 #include "ace/OS_NS_unistd.h"
5 #include "tao/RTScheduling/RTScheduler_Manager.h"
7 #include "MUF_Scheduler.h"
8 #include "orbsvcs/Time_Utilities.h"
10 const ACE_TCHAR
*ior
= ACE_TEXT("file://test.ior");
13 int enable_dynamic_scheduling
= 0;
19 * Use the ACE_Task_Base class to run server threads
21 class Worker
: public ACE_Task_Base
24 Worker (CORBA::ORB_ptr orb
,
25 Simple_Server_ptr server_ptr
,
26 RTScheduling::Current_ptr current
,
27 MUF_Scheduler
* scheduler
,
28 TimeBase::TimeT deadline
,
29 TimeBase::TimeT estimated_initial_execution_time
,
31 CORBA::Long server_load
);
35 virtual int svc (void);
36 // The thread entry point.
42 Simple_Server_var server_
;
43 RTScheduling::Current_var scheduler_current_
;
44 MUF_Scheduler
* scheduler_
;
45 TimeBase::TimeT deadline_
;
46 TimeBase::TimeT estimated_initial_execution_time_
;
48 CORBA::Long server_load_
;
53 parse_args (int argc
, ACE_TCHAR
*argv
[])
55 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("xk:i:ds"));
58 while ((c
= get_opts ()) != -1)
66 ior
= get_opts
.opt_arg ();
70 niterations
= ACE_OS::atoi (get_opts
.opt_arg ());
74 enable_dynamic_scheduling
= 1;
83 ACE_ERROR_RETURN ((LM_ERROR
,
87 "-d (enable dynamic scheduling)"
93 // Indicates successful parsing of the command line
98 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
100 MUF_Scheduler
* scheduler
=0;
101 RTScheduling::Current_var current
;
104 ACE_Sched_Params::Policy sched_policy
= ACE_SCHED_RR
;
105 int sched_scope
= ACE_SCOPE_THREAD
;
108 if (sched_policy
== ACE_SCHED_RR
)
109 flags
= THR_NEW_LWP
| THR_BOUND
| THR_JOINABLE
| THR_SCHED_RR
;
111 flags
= THR_NEW_LWP
| THR_BOUND
| THR_JOINABLE
| THR_SCHED_FIFO
;
113 ACE_hthread_t main_thr_handle
;
114 ACE_Thread::self (main_thr_handle
);
116 max_prio
= ACE_Sched_Params::priority_max (sched_policy
,
119 //FUZZ: disable check_for_lack_ACE_OS
120 ACE_Sched_Params
sched_params (sched_policy
, max_prio
);
121 //FUZZ: enable check_for_lack_ACE_OS
123 ACE_OS::sched_params (sched_params
);
125 if (ACE_Thread::getprio (main_thr_handle
, prio
) == -1)
127 if (errno
== ENOTSUP
)
130 ACE_TEXT ("getprio not supported\n")));
134 ACE_ERROR ((LM_ERROR
,
136 ACE_TEXT ("thr_getprio failed")));
140 ACE_DEBUG ((LM_DEBUG
, "(%t): main thread prio is %d\n", prio
));
144 RTScheduling::Scheduler_var sched_owner
;
147 CORBA::ORB_init (argc
, argv
);
149 if (parse_args (argc
, argv
) != 0)
152 CORBA::Object_var object
=
153 orb
->string_to_object (ior
);
155 Simple_Server_var server
=
156 Simple_Server::_narrow (object
.in ());
158 if (CORBA::is_nil (server
.in ()))
160 ACE_ERROR_RETURN ((LM_ERROR
,
161 "Object reference <%s> is nil.\n",
166 if (enable_dynamic_scheduling
)
168 ACE_DEBUG ((LM_DEBUG
, "Dyn Sched enabled\n"));
169 CORBA::Object_var manager_obj
=
170 orb
->resolve_initial_references ("RTSchedulerManager");
172 TAO_RTScheduler_Manager_var manager
=
173 TAO_RTScheduler_Manager::_narrow (manager_obj
.in ());
175 Kokyu::DSRT_Dispatcher_Impl_t disp_impl_type
;
178 disp_impl_type
= Kokyu::DSRT_CV_BASED
;
182 disp_impl_type
= Kokyu::DSRT_OS_BASED
;
185 ACE_NEW_RETURN (scheduler
,
186 MUF_Scheduler (orb
.in (),
190 sched_owner
= scheduler
;
192 manager
->rtscheduler (scheduler
);
194 CORBA::Object_var object
=
195 orb
->resolve_initial_references ("RTScheduler_Current");
198 RTScheduling::Current::_narrow (object
.in ());
202 TimeBase::TimeT deadline
;
203 TimeBase::TimeT exec_time
;
206 ORBSVCS_Time::Time_Value_to_TimeT (deadline
,
207 ACE_OS::gettimeofday () +
208 ACE_Time_Value (50,0) );
210 ORBSVCS_Time::Time_Value_to_TimeT (exec_time
,
211 ACE_OS::gettimeofday () +
212 ACE_Time_Value (10,0) );
214 Worker
worker1 (orb
.in (),
223 if (worker1
.activate (flags
, 1, 0, max_prio
) != 0)
225 ACE_ERROR ((LM_ERROR
,
226 "(%t|%T) cannot activate worker thread.\n"));
231 ORBSVCS_Time::Time_Value_to_TimeT (deadline
,
232 ACE_OS::gettimeofday () +
233 ACE_Time_Value (30,0) );
235 ORBSVCS_Time::Time_Value_to_TimeT (exec_time
,
236 ACE_OS::gettimeofday () +
237 ACE_Time_Value (10,0) );
240 Worker
worker2 (orb
.in (),
249 if (worker2
.activate (flags
, 1, 0, max_prio
) != 0)
251 ACE_ERROR ((LM_ERROR
,
252 "(%t|%T) cannot activate scheduler thread in RT mode.\n"));
255 ORBSVCS_Time::Time_Value_to_TimeT (deadline
,
256 ACE_OS::gettimeofday () +
257 ACE_Time_Value (100,0) );
259 ORBSVCS_Time::Time_Value_to_TimeT (exec_time
,
260 ACE_OS::gettimeofday () +
261 ACE_Time_Value (10,0) );
264 Worker
worker3 (orb
.in (),
272 if (worker3
.activate (flags
, 1, 0, max_prio
) != 0)
274 ACE_ERROR ((LM_ERROR
,
275 "(%t|%T) cannot activate scheduler thread in RT mode.\n"));
282 ACE_DEBUG ((LM_DEBUG
,
283 "(%t): wait for worker threads done in main thread\n"));
287 if (enable_dynamic_scheduling
)
289 MUF_Scheduling::SchedulingParameter sched_param
;
290 sched_param
.criticality
= 0;
291 sched_param
.deadline
= 0;
292 sched_param
.estimated_initial_execution_time
= 0;
293 CORBA::Policy_var sched_param_policy
=
294 scheduler
->create_scheduling_parameter (sched_param
);
295 CORBA::Policy_ptr implicit_sched_param
= 0;
296 current
->begin_scheduling_segment (0,
297 sched_param_policy
.in (),
298 implicit_sched_param
);
301 ACE_DEBUG ((LM_DEBUG
, "(%t): about to call server shutdown\n"));
304 ACE_DEBUG ((LM_DEBUG
, "after shutdown call in main thread\n"));
307 if (enable_dynamic_scheduling
)
309 current
->end_scheduling_segment (0);
313 scheduler
->shutdown ();
314 ACE_DEBUG ((LM_DEBUG
, "scheduler shutdown done\n"));
318 catch (const CORBA::Exception
& ex
)
320 ex
._tao_print_exception ("Exception caught:");
327 // ****************************************************************
329 Worker::Worker (CORBA::ORB_ptr orb
,
330 Simple_Server_ptr server_ptr
,
331 RTScheduling::Current_ptr current
,
332 MUF_Scheduler
* scheduler
,
333 TimeBase::TimeT deadline
,
334 TimeBase::TimeT estimated_initial_execution_time
,
336 CORBA::Long server_load
)
338 : orb_ (CORBA::ORB::_duplicate (orb
)),
339 server_ (Simple_Server::_duplicate (server_ptr
)),
340 scheduler_current_ (RTScheduling::Current::_duplicate (current
)),
341 scheduler_ (scheduler
),
342 deadline_ (deadline
),
343 estimated_initial_execution_time_ ( estimated_initial_execution_time
),
344 criticality_ (criticality
),
345 server_load_ (server_load
)
346 // sleep_time_ (sleep_time)
353 const char * name
= 0;
355 ACE_DEBUG ((LM_DEBUG, "(%t|%T):about to sleep for %d sec\n", sleep_time_));
356 ACE_OS::sleep (sleep_time_);
357 ACE_DEBUG ((LM_DEBUG, "(%t|%T):woke up from sleep for %d sec\n", sleep_time_));
359 ACE_hthread_t thr_handle
;
360 ACE_Thread::self (thr_handle
);
363 if (ACE_Thread::getprio (thr_handle
, prio
) == -1)
365 if (errno
== ENOTSUP
)
368 ACE_TEXT ("getprio not supported\n")));
372 ACE_ERROR ((LM_ERROR
,
374 ACE_TEXT ("thr_getprio failed")));
378 ACE_DEBUG ((LM_DEBUG
, "(%t|%T) worker activated with prio %d\n", prio
));
380 if (enable_dynamic_scheduling
)
382 MUF_Scheduling::SchedulingParameter sched_param
;
383 CORBA::Policy_var sched_param_policy
;
384 sched_param
.criticality
= criticality_
;
385 sched_param
.deadline
= deadline_
;
386 sched_param
.estimated_initial_execution_time
= 0;
387 sched_param_policy
= scheduler_
->create_scheduling_parameter (sched_param
);
388 CORBA::Policy_ptr implicit_sched_param
= 0;
389 ACE_DEBUG ((LM_DEBUG
, "(%t|%T):before begin_sched_segment\n"));
390 scheduler_current_
->begin_scheduling_segment (name
,
391 sched_param_policy
.in (),
392 implicit_sched_param
);
393 ACE_DEBUG ((LM_DEBUG
, "(%t|%T):after begin_sched_segment\n"));
396 ACE_DEBUG ((LM_DEBUG
, "(%t|%T):about to make two way call\n"));
397 server_
->test_method (server_load_
);
398 ACE_DEBUG ((LM_DEBUG
, "(%t|%T):two way call done\n"));
400 if (enable_dynamic_scheduling
)
402 scheduler_current_
->end_scheduling_segment (name
);
405 ACE_DEBUG ((LM_DEBUG
, "client worker thread (%t) done\n"));