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";
22 parse_args (int argc
, ACE_TCHAR
*argv
[])
24 ACE_Get_Opt
get_opts (argc
, argv
, ACE_TEXT("o:s:r:c:d:"));
27 while ((c
= get_opts ()) != -1)
31 ior_output_file
= get_opts
.opt_arg ();
35 strategy
= get_opts
.opt_arg ();
40 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
45 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
50 static_cast<CORBA::Float
> (ACE_OS::atof (get_opts
.opt_arg ()));
55 ACE_ERROR_RETURN ((LM_ERROR
,
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"
67 // Indicates successful parsing of the command line
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);
85 name
[0].id
= "RoundtripObjectGroup";
86 name
[0].kind
= "Object Group";
88 CORBA::Object_var group
;
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);
102 PortableGroup::Property
& property
= criteria
[0];
103 property
.nam
.length (1);
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
,
123 PortableGroup::Properties
props (1);
125 props
[0].nam
.length (1);
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
138 CORBA::ULong len
= 1;
140 PortableGroup::Properties
& props
=
143 if (reject_threshold
!= 0)
145 const CORBA::ULong i
= len
- 1;
147 props
.length (len
++);
149 props
[i
].nam
.length (1);
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);
163 CORBA::string_dup ("org.omg.CosLoadBalancing.Strategy.LeastLoaded.CriticalThreshold");
164 props
[i
].val
<<= critical_threshold
;
169 const CORBA::ULong i
= len
- 1;
171 props
.length (len
++);
173 props
[i
].nam
.length (1);
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
,
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 (),
209 return group
._retn ();
213 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
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
,
220 // Enable FIFO scheduling
221 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO
,
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"));
232 ACE_ERROR ((LM_ERROR
,
233 "server (%P|%t): sched_params failed\n"));
239 ORBInitializer
*initializer
= 0;
240 ACE_NEW_RETURN (initializer
,
242 -1); // No exceptions yet!
243 PortableInterceptor::ORBInitializer_var orb_initializer
=
246 PortableInterceptor::register_orb_initializer (orb_initializer
.in ());
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)
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 (),
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",
310 ACE_OS::fprintf (output_file
, "%s", ior
.in ());
311 ACE_OS::fclose (output_file
);
313 load_manager
->register_load_monitor (location
.in (),
316 load_manager
->register_load_alert (location
.in (),
321 ACE_DEBUG ((LM_DEBUG
, "(%P|%t) server - event loop finished\n"));
323 root_poa
->destroy (true, true);
327 catch (const CORBA::Exception
& ex
)
329 ex
._tao_print_exception ("Exception caught:");