summaryrefslogtreecommitdiff
path: root/TAO/orbsvcs/tests/Event/Performance/Latency.cpp
blob: fe88fc14a2db7e982643663c4b1c84c215ad6350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
// $Id$

#include "Latency.h"
#include "orbsvcs/Event_Service_Constants.h"
#include "tao/PortableServer/PortableServer.h"
#include "ace/High_Res_Timer.h"
#include "ace/Get_Opt.h"
#include "ace/Sample_History.h"
#include "ace/Basic_Stats.h"
#include "ace/Sched_Params.h"

ACE_RCSID(EC_Tests_Performance, Latency, "$Id$")

int iterations = 1000;
int do_dump_history = 0;
const char *ec_ior = "file://ec.ior";

/// Parse the arguments.
static int parse_args (int argc, char *argv[]);

int
main (int argc, char *argv [])
{
  int priority =
    (ACE_Sched_Params::priority_min (ACE_SCHED_FIFO)
     + ACE_Sched_Params::priority_max (ACE_SCHED_FIFO)) / 2;
  priority = ACE_Sched_Params::next_priority (ACE_SCHED_FIFO,
                                                  priority);
  // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.

  if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
                                              priority,
                                              ACE_SCOPE_PROCESS)) != 0)
    {
      if (ACE_OS::last_error () == EPERM)
        {
          ACE_DEBUG ((LM_DEBUG,
                      "server (%P|%t): user is not superuser, "
                      "test runs in time-shared class\n"));
        }
      else
        ACE_ERROR ((LM_ERROR,
                    "server (%P|%t): sched_params failed\n"));
    }

  ACE_TRY_NEW_ENV
    {
      CORBA::ORB_var orb =
        CORBA::ORB_init (argc, argv, "", ACE_TRY_ENV);
      ACE_TRY_CHECK;

#if (TAO_HAS_MESSAGING == 1)
      CORBA::Object_var manager_object =
        orb->resolve_initial_references ("ORBPolicyManager",
                                         ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::PolicyManager_var policy_manager =
        CORBA::PolicyManager::_narrow (manager_object.in (),
                                       ACE_TRY_ENV);
      ACE_TRY_CHECK;

      CORBA::Any sync_scope;
      sync_scope <<= Messaging::SYNC_WITH_TARGET;

      CORBA::PolicyList policy_list (1);
      policy_list.length (1);
      policy_list[0] =
        orb->create_policy (Messaging::SYNC_SCOPE_POLICY_TYPE,
                            sync_scope,
                            ACE_TRY_ENV);
      ACE_TRY_CHECK;
      policy_manager->set_policy_overrides (policy_list,
                                            CORBA::SET_OVERRIDE,
                                            ACE_TRY_ENV);
      ACE_TRY_CHECK;
#else
      ACE_DEBUG ((LM_DEBUG,
                  "CORBA Messaging disabled in this configuration,"
                  " test may not be optimally configured\n"));
#endif /* TAO_HAS_MESSAGING */

      CORBA::Object_var poa_object =
        orb->resolve_initial_references("RootPOA");
      if (CORBA::is_nil (poa_object.in ()))
        ACE_ERROR_RETURN ((LM_ERROR,
                           " (%P|%t) Unable to initialize the POA.\n"),
                          1);

      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in (), ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::POAManager_var poa_manager =
        root_poa->the_POAManager (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      poa_manager->activate (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      if (parse_args (argc, argv) != 0)
        return 1;

      // Get the event channel object reference
      CORBA::Object_var object =
        orb->string_to_object (ec_ior, ACE_TRY_ENV);
      ACE_TRY_CHECK;

      RtecEventChannelAdmin::EventChannel_var ec =
        RtecEventChannelAdmin::EventChannel::_narrow (object.in (),
                                                      ACE_TRY_ENV);
      ACE_TRY_CHECK;
      if (CORBA::is_nil (ec.in ()))
        {
          ACE_ERROR ((LM_ERROR,
                      "(%P|%t) Invalid or nil event channel\n"));
          return 1;
        }

      ACE_DEBUG ((LM_DEBUG, "Resolved event service\n"));

      // Now create the history
      ACE_Sample_History history (iterations);
      ACE_SYNCH_MUTEX history_mutex;

      // The consumer
      EC_Latency_Consumer consumer (&history,
                                    &history_mutex,
                                    iterations);
      // Connect the consumer

      RtecEventChannelAdmin::ConsumerAdmin_var consumer_admin =
        ec->for_consumers (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      RtecEventChannelAdmin::ProxyPushSupplier_var proxy_supplier =
        consumer_admin->obtain_push_supplier (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      RtecEventComm::PushConsumer_var consumer_reference =
        consumer._this (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Simple subscription, but usually the helper classes in
      // $TAO_ROOT/orbsvcs/Event_Utils.h are a better way to do this.
      RtecEventChannelAdmin::ConsumerQOS consumer_qos;
      consumer_qos.dependencies.length (2);
      RtecEventComm::EventHeader& h0 =
        consumer_qos.dependencies[0].event.header;
      h0.type   = ACE_ES_DISJUNCTION_DESIGNATOR;
      h0.source = 1;

      RtecEventComm::EventHeader& h1 =
        consumer_qos.dependencies[1].event.header;
      h1.type   = ACE_ES_EVENT_UNDEFINED; // first free event type
      h1.source = ACE_ES_EVENT_SOURCE_ANY;

      proxy_supplier->connect_push_consumer (consumer_reference.in (),
                                             consumer_qos,
                                             ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG, "Connected consumer\n"));

      // The supplier
      EC_Latency_Supplier supplier;

      // The canonical protocol to connect to the EC
      RtecEventChannelAdmin::SupplierAdmin_var supplier_admin =
        ec->for_suppliers (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      RtecEventChannelAdmin::ProxyPushConsumer_var proxy_consumer =
        supplier_admin->obtain_push_consumer (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      RtecEventComm::PushSupplier_var supplier_reference =
        supplier._this (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      // Simple publication, but usually the helper classes in
      // $TAO_ROOT/orbsvcs/Event_Utils.h are a better way to do this.
      RtecEventChannelAdmin::SupplierQOS supplier_qos;
      supplier_qos.publications.length (1);
      RtecEventComm::EventHeader& sh0 =
        supplier_qos.publications[0].event.header;
      sh0.type   = ACE_ES_EVENT_UNDEFINED; // first free event type
      sh0.source = 1;                      // first free event source

      proxy_consumer->connect_push_supplier (supplier_reference.in (),
                                             supplier_qos,
                                             ACE_TRY_ENV);
      ACE_TRY_CHECK;

      ACE_DEBUG ((LM_DEBUG, "Connected supplier\n"));

      Task task (proxy_consumer.in (), iterations);

      task.activate ();

      while (!task.done () || !consumer.done ())
        {
          ACE_Time_Value tv (10, 0); //0, 10000);
          orb->run (tv, ACE_TRY_ENV);
          ACE_TRY_CHECK;
        }

      ACE_Thread_Manager::instance ()->wait ();

      // Calibrate the high resolution timer *before* starting the
      // test.
      ACE_High_Res_Timer::calibrate ();

      ACE_UINT32 gsf = ACE_High_Res_Timer::global_scale_factor ();
      if (do_dump_history)
        {
          history.dump_samples ("HISTORY", gsf);
        }

      ACE_Basic_Stats stats;
      history.collect_basic_stats (stats);
      stats.dump_results ("Latency", gsf);

      proxy_supplier->disconnect_push_supplier (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      proxy_consumer->disconnect_push_consumer (ACE_TRY_ENV);
      ACE_TRY_CHECK;

      PortableServer::ObjectId_var id;

      id = root_poa->servant_to_id (&consumer, ACE_TRY_ENV);
      ACE_TRY_CHECK;
      root_poa->deactivate_object (id.in (), ACE_TRY_ENV);
      ACE_TRY_CHECK;

      id = root_poa->servant_to_id (&supplier, ACE_TRY_ENV);
      root_poa->deactivate_object (id.in (), ACE_TRY_ENV);
      ACE_TRY_CHECK;

      orb->destroy (ACE_TRY_ENV);
      ACE_TRY_CHECK;
    }
  ACE_CATCH (CORBA::Exception, ex)
    {
      ACE_PRINT_EXCEPTION (ex, argv[0]);
    }
  ACE_ENDTRY;
  return 0;
}

// ****************************************************************

int
parse_args (int argc, char *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, "hi:k:");
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'h':
        do_dump_history = 1;
        break;

      case 'i':
	iterations = ACE_OS::atoi (get_opts.optarg);
	break;

      case 'k':
	ec_ior = get_opts.optarg;
	break;

      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s "
			   "-i <iterations>"
			   "-k <IOR>"
                           "\n",
                           argv [0]),
                          -1);
      }
  // Indicates sucessful parsing of the command line
  return 0;
}

// ****************************************************************

EC_Latency_Consumer::EC_Latency_Consumer (ACE_Sample_History *history,
                                          ACE_SYNCH_MUTEX *mutex,
                                          int message_count)
  : history_ (history)
  , mutex_ (mutex)
  , remaining_messages_ (message_count)
{
}

int
EC_Latency_Consumer::done (void)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, *this->mutex_, -1);
  return this->remaining_messages_ <= 0;
}

void
EC_Latency_Consumer::push (const RtecEventComm::EventSet& events,
                           CORBA::Environment &)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  ACE_hrtime_t creation;
  ORBSVCS_Time::TimeT_to_hrtime (creation,
                                 events[0].header.creation_time);
  ACE_hrtime_t now = ACE_OS::gethrtime ();

  ACE_GUARD (ACE_SYNCH_MUTEX, ace_mon, *this->mutex_);
  this->history_->sample (now - creation);
  this->remaining_messages_--;
}

void
EC_Latency_Consumer::disconnect_push_consumer (CORBA::Environment &)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
}

// ****************************************************************

EC_Latency_Supplier::EC_Latency_Supplier (void)
{
}

void
EC_Latency_Supplier::disconnect_push_supplier (CORBA::Environment &)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
}

// ****************************************************************

Task::Task (RtecEventChannelAdmin::ProxyPushConsumer_ptr consumer,
            int iterations)
  : consumer_ (RtecEventChannelAdmin::ProxyPushConsumer::_duplicate (consumer))
  , remaining_messages_ (iterations)
{
}

int
Task::done (void)
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->mutex_, 1);
  return this->remaining_messages_ == 0;
}

int
Task::svc (void)
{
  ACE_TRY_NEW_ENV
    {
      RtecEventComm::EventSet event (1);
      event.length (1);
      event[0].header.type   = ACE_ES_EVENT_UNDEFINED;
      event[0].header.source = 1;
      event[0].header.ttl    = 1;

      for (;;)
        {
          ACE_hrtime_t creation = ACE_OS::gethrtime ();
          ORBSVCS_Time::hrtime_to_TimeT (event[0].header.creation_time,
                                         creation);
          this->consumer_->push (event, ACE_TRY_ENV);
          ACE_TRY_CHECK;

          // ACE_Time_Value tv (0, 5000);
          // ACE_OS::sleep (tv);

          ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, ace_mon, this->mutex_, -1);
          if (this->remaining_messages_ % 1000 == 0)
            {
              ACE_DEBUG ((LM_DEBUG, "Only %d messages to go\n",
                          this->remaining_messages_));
            }

          this->remaining_messages_--;
          if (this->remaining_messages_ == 0)
            return 0;
        }
    }
  ACE_CATCH (CORBA::SystemException, ex)
    {
      ACE_PRINT_EXCEPTION (ex, "Task::svc");
    }
  ACE_ENDTRY;
  return 0;
}


#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)

#elif defined(ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)

#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */