Merge pull request #1551 from DOCGroup/plm_jira_333
[ACE_TAO.git] / TAO / examples / RTScheduling / Fixed_Priority_Scheduler / test.cpp
blob92b8bcd42423723a77d8ead0877ad625bfb8bfae
1 #include "test.h"
2 #include "tao/RTScheduling/RTScheduler_Manager.h"
3 #include "tao/ORB_Core.h"
4 #include "ace/Arg_Shifter.h"
5 #include "../Task_Stats.h"
6 #include "../Synch_i.h"
7 #include "ace/OS_NS_errno.h"
9 #include "ace/Event_Handler.h"
10 #include "ace/Sig_Handler.h"
12 class TestShutdown : public ACE_Event_Handler
14 public:
15 TestShutdown (CORBA::ORB_ptr orb)
16 : orb_(CORBA::ORB::_duplicate (orb))
18 #if !defined(ACE_LACKS_UNIX_SIGNALS)
19 this->shutdown_.register_handler (SIGTERM, this);
20 this->shutdown_.register_handler (SIGINT, this);
21 #elif defined(ACE_WIN32)
22 this->shutdown_.register_handler (SIGINT, this);
23 #endif
26 ~TestShutdown (void)
28 #if !defined(ACE_LACKS_UNIX_SIGNALS)
29 this->shutdown_.remove_handler (SIGTERM);
30 this->shutdown_.remove_handler (SIGINT);
31 #elif defined(ACE_WIN32)
32 this->shutdown_.remove_handler (SIGINT);
33 #endif
36 virtual int handle_signal (int, siginfo_t*, ucontext_t*)
38 ACE_DEBUG ((LM_DEBUG, "Shutting down...\n"));
39 this->orb_->shutdown ();
40 return 0;
43 private:
44 CORBA::ORB_var orb_;
46 ACE_Sig_Handler shutdown_;
49 DT_Test::DT_Test (void)
51 base_t = ACE_OS::gethrtime ();
54 void
55 DT_Test::check_supported_priorities (void)
57 // Check that we have sufficient priority range to run this
58 // test, i.e., more than 1 priority level.
60 this->thr_sched_policy_ = orb_->orb_core ()->orb_params ()->sched_policy ();
61 this->thr_scope_policy_ = orb_->orb_core ()->orb_params ()->scope_policy ();
63 if (thr_sched_policy_ == THR_SCHED_RR)
65 //if (TAO_debug_level > 0)
66 ACE_DEBUG ((LM_DEBUG, "Sched policy = THR_SCHED_RR\n"));
68 sched_policy_ = ACE_SCHED_RR;
70 else
71 if (thr_sched_policy_ == THR_SCHED_FIFO)
73 // if (TAO_debug_level > 0)
74 ACE_DEBUG ((LM_DEBUG, "Sched policy = THR_SCHED_FIFO\n"));
76 sched_policy_ = ACE_SCHED_FIFO;
78 else
80 if (TAO_debug_level > 0)
81 ACE_DEBUG ((LM_DEBUG, "Sched policy = THR_SCHED_OTHER\n"));
83 sched_policy_ = ACE_SCHED_OTHER;
86 if (thr_sched_policy_ == THR_SCHED_RR || thr_sched_policy_ == THR_SCHED_FIFO)
88 max_priority_ = ACE_Sched_Params::priority_max (sched_policy_);
89 min_priority_ = ACE_Sched_Params::priority_min (sched_policy_);
91 if (max_priority_ == min_priority_)
93 ACE_DEBUG ((LM_DEBUG,
94 "Not enough priority levels on this platform"
95 " to run the test, aborting\n"));
96 ACE_OS::exit (2);
98 else ACE_DEBUG ((LM_DEBUG, "max_priority = %d, min_priority = %d\n",
99 max_priority_, min_priority_));
104 DT_Test::init (int argc, ACE_TCHAR *argv [])
106 orb_ = CORBA::ORB_init (argc, argv);
108 this->check_supported_priorities ();
110 dt_creator_->orb (orb_.in ());
112 TASK_STATS::instance ()->init (dt_creator_->total_load ());
114 CORBA::Object_var manager_obj = orb_->resolve_initial_references ("RTSchedulerManager");
116 TAO_RTScheduler_Manager_var manager = TAO_RTScheduler_Manager::_narrow (manager_obj.in ());
119 ACE_NEW_RETURN (scheduler_,
120 Fixed_Priority_Scheduler (orb_.in ()),
121 -1);
123 manager->rtscheduler (scheduler_.in ());
125 CORBA::Object_var object =
126 orb_->resolve_initial_references ("RTScheduler_Current");
128 current_ =
129 RTScheduling::Current::_narrow (object.in ());
132 if (sched_policy_ != ACE_SCHED_OTHER)
134 //Set the main thread to max priority...
135 if (ACE_OS::sched_params (ACE_Sched_Params (sched_policy_,
137 ACE_SCOPE_PROCESS)) != 0)
139 if (ACE_OS::last_error () == EPERM)
141 ACE_DEBUG ((LM_DEBUG,
142 "(%P|%t): user is not superuser, "
143 "test runs in time-shared class\n"));
145 else
146 ACE_ERROR ((LM_ERROR,
147 "(%P|%t): sched_params failed\n"));
151 return 0;
154 void
155 DT_Test::run (int argc, ACE_TCHAR* argv [])
157 init (argc,argv);
159 TestShutdown killer (this->orb_.in ());
161 if (this->dt_creator_->resolve_naming_service () == -1)
162 return;
165 //TASK_STATS::instance ()->init (this->dt_creator_->dt_count () * 100);
168 this->dt_creator_->activate_root_poa ();
170 this->dt_creator_->activate_poa_list ();
171 this->dt_creator_->activate_job_list ();
172 this->dt_creator_->activate_schedule ();
174 this->dt_creator_->register_synch_obj ();
176 ACE_DEBUG ((LM_DEBUG,
177 "Registered Synch Object\n"));
180 dt_creator_->create_distributable_threads (current_.in ());
183 this->activate_task ();
185 char msg [BUFSIZ];
186 ACE_OS::sprintf (msg, "ORB RUN\n");
187 this->dt_creator_->log_msg (msg);
189 orb_->run ();
191 ACE_Thread_Manager::instance ()->wait ();
193 orb_->destroy ();
196 void
197 DT_Test::dt_creator (FP_DT_Creator* dt_creator)
199 this->dt_creator_ = dt_creator;
202 FP_DT_Creator*
203 DT_Test::dt_creator (void)
205 return this->dt_creator_;
208 Fixed_Priority_Scheduler*
209 DT_Test::scheduler (void)
211 return this->scheduler_.in ();
215 DT_Test::activate_task (void)
218 ACE_DEBUG ((LM_DEBUG,
219 "Test Activate Task\n"));
221 long flags;
222 flags = THR_NEW_LWP | THR_JOINABLE;
223 flags |=
224 orb_->orb_core ()->orb_params ()->scope_policy () |
225 orb_->orb_core ()->orb_params ()->sched_policy ();
227 if (this->activate (flags,
230 50) == -1)
232 if (ACE_OS::last_error () == EPERM)
233 ACE_ERROR_RETURN ((LM_ERROR,
234 ACE_TEXT ("Insufficient privilege to run this test.\n")),
235 -1);
238 return 0;
242 DT_Test::svc (void)
246 ACE_DEBUG ((LM_DEBUG,
247 "In test::svc\n"));
249 dt_creator_->create_distributable_threads (current_.in ());
251 catch (const CORBA::BAD_INV_ORDER &)
253 // This exception can occur if ORB is already shutdown.
255 catch (const CORBA::Exception& ex)
257 ex._tao_print_exception ("Caught exception:");
258 return -1;
261 return 0;
264 CORBA::ORB_ptr
265 DT_Test::orb (void)
267 return this->orb_.in ();
271 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
273 int status = 0;
275 ACE_Service_Config::static_svcs ()->insert (&ace_svc_desc_FP_DT_Creator);
279 DT_TEST::instance ()->run (argc, argv);
281 catch (const CORBA::Exception& ex)
283 ex._tao_print_exception ("Caught exception:");
284 status = 1;
287 ACE_Service_Config::static_svcs ()->remove (ACE_TEXT ("FP_DT_Creator"));
289 return status;
292 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, DT_Test, TAO_SYNCH_MUTEX);