1 #include "ECT_Consumer_Driver.h"
3 #include "orbsvcs/CosNamingC.h"
4 #include "orbsvcs/Event_Utilities.h"
5 #include "orbsvcs/Event_Service_Constants.h"
6 #include "orbsvcs/Time_Utilities.h"
8 #include "tao/Timeprobe.h"
11 #include "ace/Get_Opt.h"
12 #include "ace/Auto_Ptr.h"
13 #include "ace/Sched_Params.h"
14 #include "ace/OS_NS_errno.h"
15 #include "ace/OS_NS_unistd.h"
18 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
20 ECT_Consumer_Driver driver
;
21 return driver
.run (argc
, argv
);
24 // ****************************************************************
26 ECT_Consumer_Driver::ECT_Consumer_Driver (void)
29 type_start_ (ACE_ES_EVENT_UNDEFINED
),
32 shutdown_event_channel_ (1),
38 ECT_Consumer_Driver::~ECT_Consumer_Driver (void)
43 ECT_Consumer_Driver::run (int argc
, ACE_TCHAR
* argv
[])
48 CORBA::ORB_init (argc
, argv
);
50 CORBA::Object_var poa_object
=
51 this->orb_
->resolve_initial_references("RootPOA");
53 if (CORBA::is_nil (poa_object
.in ()))
54 ACE_ERROR_RETURN ((LM_ERROR
,
55 " (%P|%t) Unable to initialize the POA.\n"),
58 PortableServer::POA_var root_poa
=
59 PortableServer::POA::_narrow (poa_object
.in ());
61 PortableServer::POAManager_var poa_manager
=
62 root_poa
->the_POAManager ();
64 if (this->parse_args (argc
, argv
))
67 if (TAO_debug_level
> 0)
70 "Execution parameters:\n"
73 " type start = <%d>\n"
74 " type count = <%d>\n"
75 " pid file name = <%s>\n",
82 this->pid_file_name_
?this->pid_file_name_
:ACE_TEXT("nil")) );
85 if (this->pid_file_name_
!= 0)
87 FILE* pid
= ACE_OS::fopen (this->pid_file_name_
, "w");
90 ACE_OS::fprintf (pid
, "%ld\n",
91 static_cast<long> (ACE_OS::getpid ()));
97 ACE_Sched_Params::priority_min (ACE_SCHED_FIFO
);
98 // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.
100 if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO
,
102 ACE_SCOPE_PROCESS
)) != 0)
104 if (ACE_OS::last_error () == EPERM
)
105 ACE_DEBUG ((LM_DEBUG
,
106 "%s: user is not superuser, "
107 "so remain in time-sharing class\n", argv
[0]));
109 ACE_ERROR ((LM_ERROR
,
110 "%s: ACE_OS::sched_params failed\n", argv
[0]));
113 if (ACE_OS::thr_setprio (min_priority
) == -1)
115 ACE_ERROR ((LM_ERROR
, "(%P|%t) main thr_setprio failed,"
116 "no real-time features\n"));
119 CORBA::Object_var naming_obj
=
120 this->orb_
->resolve_initial_references ("NameService");
122 if (CORBA::is_nil (naming_obj
.in ()))
123 ACE_ERROR_RETURN ((LM_ERROR
,
124 " (%P|%t) Unable to get the Naming Service.\n"),
127 CosNaming::NamingContext_var naming_context
=
128 CosNaming::NamingContext::_narrow (naming_obj
.in ());
130 CosNaming::Name
schedule_name (1);
131 schedule_name
.length (1);
132 schedule_name
[0].id
= CORBA::string_dup ("ScheduleService");
134 CORBA::Object_var sched_obj
=
135 naming_context
->resolve (schedule_name
);
136 if (CORBA::is_nil (sched_obj
.in ()))
138 RtecScheduler::Scheduler_var scheduler
=
139 RtecScheduler::Scheduler::_narrow (sched_obj
.in ());
141 CosNaming::Name
name (1);
143 name
[0].id
= CORBA::string_dup ("EventService");
145 CORBA::Object_var ec_obj
=
146 naming_context
->resolve (name
);
148 RtecEventChannelAdmin::EventChannel_var channel
;
149 if (CORBA::is_nil (ec_obj
.in ()))
150 channel
= RtecEventChannelAdmin::EventChannel::_nil ();
152 channel
= RtecEventChannelAdmin::EventChannel::_narrow (ec_obj
.in ());
154 poa_manager
->activate ();
156 this->connect_consumers (scheduler
.in (), channel
.in ());
158 ACE_DEBUG ((LM_DEBUG
, "connected consumer(s)\n"));
160 ACE_DEBUG ((LM_DEBUG
, "running the test\n"));
163 ACE_Time_Value
tv (1, 0);
164 this->orb_
->perform_work (tv
);
165 ACE_GUARD_RETURN (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
, 1);
166 if (this->active_count_
<= 0)
169 ACE_DEBUG ((LM_DEBUG
, "event loop finished\n"));
171 this->dump_results ();
173 this->disconnect_consumers ();
175 if (this->shutdown_event_channel_
!= 0)
180 root_poa
->destroy (1, 1);
182 this->orb_
->destroy ();
184 catch (const CORBA::SystemException
& sys_ex
)
186 sys_ex
._tao_print_exception ("SYS_EX");
188 catch (const CORBA::Exception
& ex
)
190 ex
._tao_print_exception ("NON SYS EX");
196 ECT_Consumer_Driver::shutdown_consumer (void*)
199 // (reinterpret_cast<Test_Consumer**> (consumer_cookie)
200 // - this->consumers_);
202 // ACE_DEBUG ((LM_DEBUG, "(%t) events received by consumer %d\n", ID));
204 ACE_GUARD (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
);
205 this->active_count_
--;
209 ECT_Consumer_Driver::connect_consumers
210 (RtecScheduler::Scheduler_ptr scheduler
,
211 RtecEventChannelAdmin::EventChannel_ptr channel
)
214 ACE_GUARD (TAO_SYNCH_MUTEX
, ace_mon
, this->lock_
);
215 this->active_count_
= this->n_consumers_
;
217 for (int i
= 0; i
< this->n_consumers_
; ++i
)
220 ACE_OS::sprintf (buf
, "consumer_%02d", i
);
222 ACE_NEW (this->consumers_
[i
],
224 this->consumers_
+ i
,
226 this->stall_length_
));
228 this->consumers_
[i
]->connect (scheduler
,
237 ECT_Consumer_Driver::dump_results (void)
239 ACE_High_Res_Timer::global_scale_factor_type gsf
=
240 ACE_High_Res_Timer::global_scale_factor ();
242 ACE_Throughput_Stats throughput
;
243 for (int i
= 0; i
< this->n_consumers_
; ++i
)
245 ACE_TCHAR buf
[BUFSIZ
];
246 ACE_OS::sprintf (buf
, ACE_TEXT("consumer_%02d"), i
);
248 this->consumers_
[i
]->dump_results (buf
, gsf
);
249 this->consumers_
[i
]->accumulate (throughput
);
251 throughput
.dump_results (ACE_TEXT("ECT_Consumer/totals"), gsf
);
255 ECT_Consumer_Driver::disconnect_consumers (void)
257 for (int i
= 0; i
< this->n_consumers_
; ++i
)
259 this->consumers_
[i
]->disconnect ();
261 delete this->consumers_
[i
];
262 this->consumers_
[i
] = 0;
267 ECT_Consumer_Driver::parse_args (int argc
, ACE_TCHAR
*argv
[])
269 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("xdc:s:h:p:o:"));
272 while ((opt
= get_opt ()) != EOF
)
277 this->shutdown_event_channel_
= 0;
281 this->n_consumers_
= ACE_OS::atoi (get_opt
.opt_arg ());
285 this->n_suppliers_
= ACE_OS::atoi (get_opt
.opt_arg ());
291 char* arg
= ACE_OS::strtok_r (ACE_TEXT_ALWAYS_CHAR(get_opt
.opt_arg ()), ",", &aux
);
293 this->type_start_
= ACE_ES_EVENT_UNDEFINED
+ ACE_OS::atoi (arg
);
294 arg
= ACE_OS::strtok_r (0, ",", &aux
);
295 this->type_count_
= ACE_OS::atoi (arg
);
300 this->pid_file_name_
= get_opt
.opt_arg ();
304 this->stall_length_
= ACE_OS::atoi (get_opt
.opt_arg ());
309 ACE_DEBUG ((LM_DEBUG
,
315 "-h <type_start,type_count> "
316 "-p <pid file name> "
323 if (this->n_suppliers_
<= 0)
325 ACE_DEBUG ((LM_DEBUG
,
326 "%s: number of suppliers (%d) is out of range, "
327 "reset to default (%d)\n",
328 argv
[0], this->n_suppliers_
,
330 this->n_suppliers_
= 1;
333 if (this->n_consumers_
<= 0)
335 ACE_ERROR_RETURN ((LM_ERROR
,
336 "%s: number of consumers or "
337 "suppliers out of range\n", argv
[0]), -1);
340 if (this->type_count_
<= 0)
342 this->type_count_
= 1;
343 ACE_ERROR_RETURN ((LM_ERROR
,
344 "%s: number of event types "
345 "suppliers out of range, reset to default (1)\n",