Changes to attempt to silence bcc64x
[ACE_TAO.git] / TAO / orbsvcs / performance-tests / LoadBalancing / LBPerf / CPU / server.cpp
blob22fe88193805fe0e19bd1c5387a2475300c35950
1 #include "Roundtrip.h"
2 #include "ORBInitializer.h"
4 #include "orbsvcs/LoadBalancing/LB_CPU_Utilization_Monitor.h"
6 #include "ace/Get_Opt.h"
7 #include "ace/Sched_Params.h"
8 #include "ace/OS_NS_strings.h"
9 #include "ace/OS_NS_stdlib.h"
11 #include "tao/Strategies/advanced_resource.h"
13 const ACE_TCHAR *ior_output_file = ACE_TEXT("test.ior");
15 CORBA::Float reject_threshold = 0;
16 CORBA::Float critical_threshold = 0;
17 CORBA::Float dampening = 0;
18 const char * strategy = "Random";
19 int num_threads = 1;
21 int
22 parse_args (int argc, ACE_TCHAR *argv[])
24 ACE_Get_Opt get_opts (argc, argv, ACE_TEXT("o:s:r:c:d:"));
25 int c;
27 while ((c = get_opts ()) != -1)
28 switch (c)
30 case 'o':
31 ior_output_file = get_opts.opt_arg ();
32 break;
34 case 's':
35 strategy = get_opts.opt_arg ();
36 break;
38 case 'r':
39 reject_threshold =
40 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
41 break;
43 case 'c':
44 critical_threshold =
45 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
46 break;
48 case 'd':
49 dampening =
50 static_cast<CORBA::Float> (ACE_OS::atof (get_opts.opt_arg ()));
51 break;
53 case '?':
54 default:
55 ACE_ERROR_RETURN ((LM_ERROR,
56 "usage: %s\n"
57 " -o <iorfile>\n"
58 " -s <RoundRobin | Random | LeastLoaded>\n"
59 " -r <reject threshold> (LeastLoaded only)\n"
60 " -c <critical threshold> (LeastLoaded only)\n"
61 " -d <damping value> (LeastLoaded only)\n"
62 " -n <# of CPU burning threads>\n"
63 "\n",
64 argv [0]),
65 -1);
67 // Indicates successful parsing of the command line
68 return 0;
71 CORBA::Object_ptr
72 join_object_group (CORBA::ORB_ptr orb,
73 CosLoadBalancing::LoadManager_ptr lm,
74 const PortableGroup::Location & location)
76 CORBA::Object_var ns_object =
77 orb->resolve_initial_references ("NameService");
79 CosNaming::NamingContext_var nc =
80 CosNaming::NamingContext::_narrow (ns_object.in ());
82 CosNaming::Name name (1);
83 name.length (1);
85 name[0].id = "RoundtripObjectGroup";
86 name[0].kind = "Object Group";
88 CORBA::Object_var group;
90 try
92 group = nc->resolve (name);
94 catch (const CosNaming::NamingContext::NotFound& )
96 // Object group not created. Create one.
97 const char repository_id[] = "IDL:Test/Roundtrip:1.0";
99 PortableGroup::Criteria criteria (1);
100 criteria.length (1);
102 PortableGroup::Property & property = criteria[0];
103 property.nam.length (1);
105 property.nam[0].id =
106 CORBA::string_dup ("org.omg.PortableGroup.MembershipStyle");
108 PortableGroup::MembershipStyleValue msv =
109 PortableGroup::MEMB_APP_CTRL;
110 property.val <<= msv;
112 PortableGroup::GenericFactory::FactoryCreationId_var fcid;
114 group = lm->create_object (repository_id,
115 criteria,
116 fcid.out ());
120 nc->bind (name,
121 group.in ());
123 PortableGroup::Properties props (1);
124 props.length (1);
125 props[0].nam.length (1);
126 props[0].nam[0].id =
127 CORBA::string_dup ("org.omg.CosLoadBalancing.StrategyInfo");
129 CosLoadBalancing::StrategyInfo strategy_info;
131 strategy_info.name = CORBA::string_dup (strategy);
133 if (ACE_OS::strcasecmp (strategy, "LeastLoaded") == 0
134 && (reject_threshold != 0
135 || critical_threshold != 0
136 || dampening != 0))
138 CORBA::ULong len = 1;
140 PortableGroup::Properties & props =
141 strategy_info.props;
143 if (reject_threshold != 0)
145 const CORBA::ULong i = len - 1;
147 props.length (len++);
149 props[i].nam.length (1);
150 props[i].nam[0].id =
151 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.RejectThreshold");
152 props[i].val <<= reject_threshold;
155 if (critical_threshold != 0)
157 const CORBA::ULong i = len - 1;
159 props.length (len++);
161 props[i].nam.length (1);
162 props[i].nam[0].id =
163 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.CriticalThreshold");
164 props[i].val <<= critical_threshold;
167 if (dampening != 0)
169 const CORBA::ULong i = len - 1;
171 props.length (len++);
173 props[i].nam.length (1);
174 props[i].nam[0].id =
175 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.Dampening");
176 props[i].val <<= dampening;
181 props[0].val <<= strategy_info;
183 lm->set_default_properties (props);
185 catch (const CosNaming::NamingContext::AlreadyBound& )
187 // Somebody beat us to creating the object group. Clean up
188 // the one we created.
189 lm->delete_object (fcid.in ());
191 group = nc->resolve (name);
195 Roundtrip * roundtrip_impl;
196 ACE_NEW_THROW_EX (roundtrip_impl,
197 Roundtrip (orb),
198 CORBA::NO_MEMORY ());
200 PortableServer::ServantBase_var owner_transfer (roundtrip_impl);
202 Test::Roundtrip_var roundtrip =
203 roundtrip_impl->_this ();
205 group = lm->add_member (group.in (),
206 location,
207 roundtrip.in ());
209 return group._retn ();
213 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
215 int priority =
216 (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
217 + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
218 priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
219 priority);
220 // Enable FIFO scheduling
221 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
222 priority,
223 ACE_SCOPE_PROCESS)) != 0)
225 if (ACE_OS::last_error () == EPERM)
227 ACE_DEBUG ((LM_DEBUG,
228 "server (%P|%t): user is not superuser, "
229 "test runs in time-shared class\n"));
231 else
232 ACE_ERROR ((LM_ERROR,
233 "server (%P|%t): sched_params failed\n"));
239 ORBInitializer *initializer = 0;
240 ACE_NEW_RETURN (initializer,
241 ORBInitializer,
242 -1); // No exceptions yet!
243 PortableInterceptor::ORBInitializer_var orb_initializer =
244 initializer;
246 PortableInterceptor::register_orb_initializer (orb_initializer.in ());
248 CORBA::ORB_var orb =
249 CORBA::ORB_init (argc, argv);
252 CORBA::Object_var poa_object =
253 orb->resolve_initial_references ("RootPOA");
255 if (CORBA::is_nil (poa_object.in ()))
256 ACE_ERROR_RETURN ((LM_ERROR,
257 " (%P|%t) Unable to initialize the POA.\n"),
260 PortableServer::POA_var root_poa =
261 PortableServer::POA::_narrow (poa_object.in ());
263 PortableServer::POAManager_var poa_manager =
264 root_poa->the_POAManager ();
266 if (parse_args (argc, argv) != 0)
267 return 1;
269 poa_manager->activate ();
271 CORBA::Object_var lm_object =
272 orb->resolve_initial_references ("LoadManager");
274 CosLoadBalancing::LoadManager_var load_manager =
275 CosLoadBalancing::LoadManager::_narrow (lm_object.in ());
277 TAO_LB_CPU_Utilization_Monitor * monitor_servant;
278 ACE_NEW_THROW_EX (monitor_servant,
279 TAO_LB_CPU_Utilization_Monitor,
280 CORBA::NO_MEMORY ());
282 PortableServer::ServantBase_var safe_monitor_servant (monitor_servant);
284 CosLoadBalancing::LoadMonitor_var load_monitor =
285 monitor_servant->_this ();
287 PortableGroup::Location_var location =
288 load_monitor->the_location ();
290 CORBA::Object_var roundtrip =
291 ::join_object_group (orb.in (),
292 load_manager.in (),
293 location.in ());
295 TAO_LB_LoadAlert & alert_servant = initializer->load_alert ();
297 CosLoadBalancing::LoadAlert_var load_alert =
298 alert_servant._this ();
300 CORBA::String_var ior =
301 orb->object_to_string (roundtrip.in ());
303 // If the ior_output_file exists, output the ior to it
304 FILE *output_file= ACE_OS::fopen (ior_output_file, "w");
305 if (output_file == 0)
306 ACE_ERROR_RETURN ((LM_ERROR,
307 "Cannot open output file for writing IOR: %s\n",
308 ior_output_file),
310 ACE_OS::fprintf (output_file, "%s", ior.in ());
311 ACE_OS::fclose (output_file);
313 load_manager->register_load_monitor (location.in (),
314 load_monitor.in ());
316 load_manager->register_load_alert (location.in (),
317 load_alert.in ());
319 orb->run ();
321 ACE_DEBUG ((LM_DEBUG, "(%P|%t) server - event loop finished\n"));
323 root_poa->destroy (true, true);
325 orb->destroy ();
327 catch (const CORBA::Exception& ex)
329 ex._tao_print_exception ("Exception caught:");
330 return 1;
333 return 0;