Revert "Minor modernization of DynamicAny code"
[ACE_TAO.git] / TAO / performance-tests / RTCorba / Thread_Pool / server.cpp
blob25860a549bf81af70e412b383013b7c70c4a27e8
1 #include "ace/Get_Opt.h"
2 #include "ace/Stats.h"
3 #include "ace/Task.h"
4 #include "ace/Sample_History.h"
5 #include "tao/ORB_Core.h"
6 #include "tao/debug.h"
7 #include "tao/RTPortableServer/RTPortableServer.h"
8 #include "testS.h"
9 #include "tests/RTCORBA/common_args.cpp"
10 #include "tests/RTCORBA/check_supported_priorities.cpp"
12 class test_i :
13 public POA_test
15 public:
16 test_i (CORBA::ORB_ptr orb,
17 PortableServer::POA_ptr poa);
19 void method (CORBA::ULong work,
20 CORBA::ULong prime_number);
22 //FUZZ: disable check_for_lack_ACE_OS
23 void shutdown ();
24 //FUZZ: enable check_for_lack_ACE_OS
26 PortableServer::POA_ptr _default_POA ();
28 private:
29 CORBA::ORB_var orb_;
30 PortableServer::POA_var poa_;
33 test_i::test_i (CORBA::ORB_ptr orb,
34 PortableServer::POA_ptr poa)
35 : orb_ (CORBA::ORB::_duplicate (orb)),
36 poa_ (PortableServer::POA::_duplicate (poa))
40 void
41 test_i::method (CORBA::ULong work,
42 CORBA::ULong prime_number)
44 if (TAO_debug_level > 0)
45 ACE_DEBUG ((LM_DEBUG,
46 "test_i::method: %hd units of work\n",
47 work));
49 for (; work != 0; work--)
50 ACE::is_prime (prime_number,
52 prime_number / 2);
55 PortableServer::POA_ptr
56 test_i::_default_POA ()
58 return PortableServer::POA::_duplicate (this->poa_.in ());
61 void
62 test_i::shutdown ()
64 this->orb_->shutdown (false);
67 static const ACE_TCHAR *ior_output_file = ACE_TEXT("ior");
68 static CORBA::ULong static_threads = 1;
69 static CORBA::ULong dynamic_threads = 0;
70 static CORBA::ULong number_of_lanes = 0;
71 static RTCORBA::Priority default_thread_priority = 0;
72 static RTCORBA::Priority pool_priority = ACE_INT16_MIN;
74 static const ACE_TCHAR *bands_file = ACE_TEXT("empty-file");
75 static const ACE_TCHAR *lanes_file = ACE_TEXT("empty-file");
77 int
78 parse_args (int argc, ACE_TCHAR *argv[])
80 ACE_Get_Opt get_opts (argc, argv,
81 "b:f:hl:n:o:s:" // server options
82 "c:e:g:hi:j:k:m:p:q:r:t:u:v:w:x:y:z:" // client options
84 int c;
86 while ((c = get_opts ()) != -1)
87 switch (c)
89 case 'b':
90 bands_file = get_opts.opt_arg ();
91 break;
93 case 'f':
94 pool_priority = ACE_OS::atoi (get_opts.opt_arg ());
95 break;
97 case 'l':
98 lanes_file = get_opts.opt_arg ();
99 break;
101 case 'n':
102 number_of_lanes = ACE_OS::atoi (get_opts.opt_arg ());
103 break;
105 case 'o':
106 ior_output_file = get_opts.opt_arg ();
107 break;
109 case 's':
110 static_threads = ACE_OS::atoi (get_opts.opt_arg ());
111 break;
113 case 'c':
114 case 'e':
115 case 'g':
116 case 'i':
117 case 'j':
118 case 'k':
119 case 'm':
120 case 'p':
121 case 'q':
122 case 'r':
123 case 't':
124 case 'u':
125 case 'v':
126 case 'w':
127 case 'x':
128 case 'y':
129 case 'z':
130 // client options: ignored.
131 break;
133 case 'h':
134 case '?':
135 default:
136 ACE_ERROR_RETURN ((LM_ERROR,
137 "usage: %s\n"
138 "\t-b <bands file> (defaults to %s)\n"
139 "\t-f <pool priority> (defaults to %d)\n"
140 "\t-h <help: shows options menu>\n"
141 "\t-l <lanes file> (defaults to %s)\n"
142 "\t-n <number of lanes> (defaults to %d)\n"
143 "\t-o <ior file> (defaults to %s)\n"
144 "\t-s <static threads> (defaults to %d)\n"
145 "\n",
146 argv [0],
147 bands_file,
148 default_thread_priority,
149 lanes_file,
150 number_of_lanes,
151 ior_output_file,
152 static_threads),
153 -1);
156 return 0;
160 write_ior_to_file (const ACE_TCHAR *ior_file,
161 CORBA::ORB_ptr orb,
162 CORBA::Object_ptr object)
164 CORBA::String_var ior =
165 orb->object_to_string (object);
167 FILE *output_file =
168 ACE_OS::fopen (ior_file,
169 "w");
171 if (output_file == 0)
172 ACE_ERROR_RETURN ((LM_ERROR,
173 "Cannot open output file for writing IOR: %s",
174 ior_file),
175 -1);
177 ACE_OS::fprintf (output_file,
178 "%s",
179 ior.in ());
181 ACE_OS::fclose (output_file);
183 return 0;
186 class Task : public ACE_Task_Base
188 public:
189 Task (ACE_Thread_Manager &thread_manager,
190 CORBA::ORB_ptr orb);
192 int svc ();
194 CORBA::ORB_var orb_;
197 Task::Task (ACE_Thread_Manager &thread_manager,
198 CORBA::ORB_ptr orb)
199 : ACE_Task_Base (&thread_manager),
200 orb_ (CORBA::ORB::_duplicate (orb))
205 Task::svc ()
209 CORBA::Object_var object =
210 this->orb_->resolve_initial_references ("RootPOA");
212 PortableServer::POA_var root_poa =
213 PortableServer::POA::_narrow (object.in ());
215 PortableServer::POAManager_var poa_manager =
216 root_poa->the_POAManager ();
218 object =
219 this->orb_->resolve_initial_references ("RTORB");
221 RTCORBA::RTORB_var rt_orb =
222 RTCORBA::RTORB::_narrow (object.in ());
224 object =
225 this->orb_->resolve_initial_references ("RTCurrent");
227 RTCORBA::Current_var current =
228 RTCORBA::Current::_narrow (object.in ());
230 default_thread_priority =
231 current->the_priority ();
233 int result = 0;
234 CORBA::ULong stacksize = 0;
235 CORBA::Boolean allow_request_buffering = 0;
236 CORBA::ULong max_buffered_requests = 0;
237 CORBA::ULong max_request_buffer_size = 0;
239 CORBA::PolicyList policies;
241 CORBA::Boolean allow_borrowing = 0;
242 if (number_of_lanes != 0)
244 get_auto_priority_lanes_and_bands (number_of_lanes,
245 rt_orb.in (),
246 stacksize,
247 static_threads,
248 dynamic_threads,
249 allow_request_buffering,
250 max_buffered_requests,
251 max_request_buffer_size,
252 allow_borrowing,
253 policies,
256 else if (ACE_OS::strcmp (lanes_file, ACE_TEXT("empty-file")) != 0)
258 result =
259 get_priority_lanes ("server",
260 lanes_file,
261 rt_orb.in (),
262 stacksize,
263 static_threads,
264 dynamic_threads,
265 allow_request_buffering,
266 max_buffered_requests,
267 max_request_buffer_size,
268 allow_borrowing,
269 policies,
272 if (result != 0)
273 return result;
275 result =
276 get_priority_bands ("server",
277 bands_file,
278 rt_orb.in (),
279 policies,
282 if (result != 0)
283 return result;
285 else
287 if (pool_priority == ACE_INT16_MIN)
288 pool_priority =
289 default_thread_priority;
291 RTCORBA::ThreadpoolId threadpool_id =
292 rt_orb->create_threadpool (stacksize,
293 static_threads,
294 dynamic_threads,
295 pool_priority,
296 allow_request_buffering,
297 max_buffered_requests,
298 max_request_buffer_size);
300 policies.length (policies.length () + 1);
301 policies[policies.length () - 1] =
302 rt_orb->create_threadpool_policy (threadpool_id);
304 if (ACE_OS::strcmp (bands_file, ACE_TEXT("empty-file")) != 0)
306 result =
307 get_priority_bands ("server",
308 bands_file,
309 rt_orb.in (),
310 policies,
313 if (result != 0)
314 return result;
318 policies.length (policies.length () + 1);
319 policies[policies.length () - 1] =
320 root_poa->create_implicit_activation_policy
321 (PortableServer::IMPLICIT_ACTIVATION);
323 policies.length (policies.length () + 1);
324 policies[policies.length () - 1] =
325 rt_orb->create_priority_model_policy (RTCORBA::CLIENT_PROPAGATED,
326 default_thread_priority);
328 PortableServer::POA_var poa =
329 root_poa->create_POA ("RT POA",
330 poa_manager.in (),
331 policies);
333 test_i *servant =
334 new test_i (this->orb_.in (),
335 poa.in ());
337 PortableServer::ServantBase_var safe_servant (servant);
338 ACE_UNUSED_ARG (safe_servant);
340 test_var test =
341 servant->_this ();
343 result =
344 write_ior_to_file (ior_output_file,
345 this->orb_.in (),
346 test.in ());
348 if (result != 0)
349 return result;
351 poa_manager->activate ();
353 this->orb_->run ();
355 this->orb_->destroy ();
357 catch (const CORBA::Exception& ex)
359 ex._tao_print_exception ("Exception caught:");
360 return 1;
363 return 0;
367 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
371 CORBA::ORB_var orb =
372 CORBA::ORB_init (argc,
373 argv);
375 int result =
376 parse_args (argc, argv);
377 if (result != 0)
378 return result;
380 // Make sure we can support multiple priorities that are required
381 // for this test.
382 check_supported_priorities (orb.in ());
384 // Thread Manager for managing task.
385 ACE_Thread_Manager thread_manager;
387 // Create task.
388 Task task (thread_manager,
389 orb.in ());
391 // Task activation flags.
392 long flags =
393 THR_NEW_LWP |
394 THR_JOINABLE |
395 orb->orb_core ()->orb_params ()->thread_creation_flags ();
397 // Activate task.
398 result =
399 task.activate (flags);
400 ACE_ASSERT (result != -1);
401 ACE_UNUSED_ARG (result);
403 // Wait for task to exit.
404 result =
405 thread_manager.wait ();
406 ACE_ASSERT (result != -1);
408 catch (const CORBA::Exception& ex)
410 ex._tao_print_exception ("Exception caught:");
411 return -1;
414 return 0;