Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / RTScheduling / Fixed_Priority_Scheduler / test.cpp
blob942ade718a1cd27590bed4dd0b5cdb3a60135f40
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 ()
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 ()
51 base_t = ACE_OS::gethrtime ();
54 void
55 DT_Test::check_supported_priorities ()
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 ()
205 return this->dt_creator_;
208 Fixed_Priority_Scheduler*
209 DT_Test::scheduler ()
211 return this->scheduler_.in ();
215 DT_Test::activate_task ()
217 ACE_DEBUG ((LM_DEBUG,
218 "Test Activate Task\n"));
220 long flags;
221 flags = THR_NEW_LWP | THR_JOINABLE;
222 flags |=
223 orb_->orb_core ()->orb_params ()->scope_policy () |
224 orb_->orb_core ()->orb_params ()->sched_policy ();
226 if (this->activate (flags,
229 50) == -1)
231 if (ACE_OS::last_error () == EPERM)
232 ACE_ERROR_RETURN ((LM_ERROR,
233 ACE_TEXT ("Insufficient privilege to run this test.\n")),
234 -1);
237 return 0;
241 DT_Test::svc ()
245 ACE_DEBUG ((LM_DEBUG,
246 "In test::svc\n"));
248 dt_creator_->create_distributable_threads (current_.in ());
250 catch (const CORBA::BAD_INV_ORDER &)
252 // This exception can occur if ORB is already shutdown.
254 catch (const CORBA::Exception& ex)
256 ex._tao_print_exception ("Caught exception:");
257 return -1;
260 return 0;
263 CORBA::ORB_ptr
264 DT_Test::orb ()
266 return this->orb_.in ();
270 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
272 int status = 0;
274 ACE_Service_Config::static_svcs ()->insert (&ace_svc_desc_FP_DT_Creator);
278 DT_TEST::instance ()->run (argc, argv);
280 catch (const CORBA::Exception& ex)
282 ex._tao_print_exception ("Caught exception:");
283 status = 1;
286 ACE_Service_Config::static_svcs ()->remove (ACE_TEXT ("FP_DT_Creator"));
288 return status;
291 ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, DT_Test, TAO_SYNCH_MUTEX);