summaryrefslogtreecommitdiff
path: root/ACE/TAO/orbsvcs/tests/Notify/MC/test_monitor.cpp
blob: 787ea85518c0cb7c5677dba8b6eabb8e0ece706e (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
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
// $Id$

#include "ace/Get_Opt.h"
#include "ace/Monitor_Point_Registry.h"

#include "orbsvcs/Notify/MonitorControl/NotificationServiceMCC.h"
#include "orbsvcs/Notify/MonitorControlExt/NotifyMonitoringExtC.h"

#if defined (TAO_HAS_MONITOR_FRAMEWORK) && (TAO_HAS_MONITOR_FRAMEWORK == 1)

using namespace ACE_VERSIONED_NAMESPACE_NAME::ACE::Monitor_Control;

#include "MonitorTestInterfaceS.h"
extern "C" int
sorter (const void* a, const void* b)
{
  const char* left = *(reinterpret_cast<const char* const*> (a));
  const char* right = *(reinterpret_cast<const char* const*> (b));
  return ACE_OS::strcmp (left, right);
}



// It's reasonable to assume that there is only one event channel
// factory and that we know its name.
class MonitorTestInterface_i: public virtual POA_MonitorTestInterface
{
public:
  MonitorTestInterface_i (
    CosNotification::NotificationServiceMonitorControl_ptr nsm)
    : nsm_ (CosNotification::NotificationServiceMonitorControl::_duplicate (nsm))
  {
  }

  virtual void
  running (MonitorTestInterface::Which proc);

  virtual void
  finished (MonitorTestInterface::Which proc);

private:
  void brain_dump (const char * context);
  void stat_dump (const char * statName);
  void consumer_stats_check (void);
private:
  ACE_CString base_;
  CosNotification::NotificationServiceMonitorControl_var nsm_;
};

void
MonitorTestInterface_i::running (MonitorTestInterface::Which proc)
{
  ACE_CString str;
  Monitor::Data_var data;
  Monitor::NameList list;
  Monitor::Numeric num;

  switch (proc)
    {
    case MonitorTestInterface::NotifyService:
      {
      data =
        nsm_->get_statistic (NotifyMonitoringExt::EventChannelFactoryNames);
      list = data->data_union.list ();

      if (list.length () != 1)
        {
          ACE_ERROR ((LM_ERROR,
                      "ERROR: There should be only one Event "
                      "Channel Factory\n"));
        }

      this->base_ = list[0];
      this->base_ += "/";
      str = this->base_ + NotifyMonitoringExt::ActiveEventChannelCount;
      data = this->nsm_->get_statistic (str.c_str ());
      num = data->data_union.num ();

      if (num.dlist[0].value != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      "ERROR: There should be no active Event "
                      "Channels\n"));
        }

      str = this->base_ + NotifyMonitoringExt::InactiveEventChannelCount;
      data = this->nsm_->get_statistic (str.c_str ());
      num = data->data_union.num ();

      if (num.dlist[0].value != 0)
        {
          ACE_ERROR ((LM_ERROR,
                      "ERROR: There should be no inactive Event "
                      "Channels\n"));
        }

      break;
      }
    case MonitorTestInterface::Consumer:
      {
      str = this->base_ + NotifyMonitoringExt::ActiveEventChannelNames;
      data = nsm_->get_statistic (str.c_str ());
      list = data->data_union.list ();

      if (list.length () != 1)
        {
          ACE_ERROR ((LM_ERROR,
                      "ERROR: There should be only one active "
                      "Event Channel\n"));
        }

      // Base will now be the factory plus the event channel.
      this->base_ = list[0];
      this->base_ += "/";

      str = this->base_ + NotifyMonitoringExt::EventChannelConsumerCount;
      data = this->nsm_->get_statistic (str.c_str ());
      num = data->data_union.num ();
      if (num.dlist[0].value != 1)
        ACE_ERROR ((LM_ERROR, "Monitor: ERROR: There should be only one Consumer\n"));

      str = this->base_ + NotifyMonitoringExt::EventChannelConsumerAdminCount;
      data = nsm_->get_statistic(str.c_str ());
      num = data->data_union.num ();
      if (num.dlist[0].value != 1)
        ACE_ERROR ((LM_ERROR,
                        "Monitor: ERROR: There should be only one ConsumerAdmin\n"));

      str = this->base_ + NotifyMonitoringExt::EventChannelQueueElementCount;
      data = nsm_->get_statistic(str.c_str ());
      num = data->data_union.num ();
      if (num.dlist[0].value != 0)
        ACE_ERROR ((LM_ERROR, "Monitor: ERROR: There should be no events queued\n"));

        brain_dump ("Running Consumer");

      break;
      }
    case MonitorTestInterface::Supplier:
      {
      str = this->base_ + NotifyMonitoringExt::EventChannelSupplierCount;
      data = nsm_->get_statistic(str.c_str ());
      num = data->data_union.num ();

      if (num.dlist[0].value != 1)
        ACE_ERROR ((LM_ERROR, "Monitor: ERROR: There should be only one Supplier\n"));

      str = this->base_ + NotifyMonitoringExt::EventChannelSupplierAdminCount;
      data = nsm_->get_statistic(str.c_str ());
      num = data->data_union.num ();
      if (num.dlist[0].value != 1)
        ACE_ERROR ((LM_ERROR,
              "Monitor: ERROR: There should be only one SupplierAdmin\n"));
        brain_dump ("Running Supplier");
      break;
      }
    default:
      ACE_ERROR ((LM_ERROR, "Monitor: ERROR: Impossible enum value %d\n", proc));
      break;
    }
}

void
MonitorTestInterface_i::consumer_stats_check()
{
  bool foundConsumerStats = false;
  Monitor::NameList_var names = nsm_->get_statistic_names ();
  CORBA::ULong length = names->length ();
  for(CORBA::ULong i = 0; i < length; i++)
    {
      const char * name = names[i].in ();
      size_t slashcount = 0;
      bool isConsumerQueueSize = false;
      for (size_t nCh = 0; name[nCh] != 0 && slashcount < 3; ++nCh)
        {
          if (name[nCh] == '/')
            {
              slashcount += 1;
              if(slashcount == 3)
              {
                isConsumerQueueSize = 0 == ACE_OS::strcmp(
                  &name[nCh + 1],
                  NotifyMonitoringExt::EventChannelQueueSize);
              }
            }
        }
      if (isConsumerQueueSize)
      {
        foundConsumerStats = true;
        // We have a consumer queue
        try
          {
            Monitor::Data_var queueSizeData =
              nsm_->get_statistic(name);

            Monitor::Numeric queueSizeNum = queueSizeData->data_union.num ();
            ACE_DEBUG ((LM_DEBUG, "Monitor: %s: Average: %f, Maximum: %f, Most recent: %f\n",
                name,
                queueSizeNum.average, queueSizeNum.maximum, queueSizeNum.last));
            if (queueSizeNum.average <= 0.0 || queueSizeNum.average > 2000.0)
              ACE_ERROR ((LM_ERROR, "Monitor: ERROR: %s average queue size [%f] should be greater than zero and less than 2000.\n",
                name,
                queueSizeNum.average));
            if (queueSizeNum.last > 2000.0)
              ACE_ERROR ((LM_ERROR, "Monitor: ERROR: %s most recent queue size [%f] should not be greater than 2000.\n",
                name,
                queueSizeNum.last));
          }
        catch (const CORBA::Exception& ex)
          {
            ex._tao_print_exception (name);
          }
      }

    }
  if(! foundConsumerStats)
    {
      ACE_ERROR ((LM_ERROR, "Monitor: ERROR: No consumer queue size statistics found.\n"
                ));
    }
}

void
MonitorTestInterface_i::brain_dump(const char * /*context*/)
{
#if 0 // verbose output should be controlled via a command line option
  ACE_DEBUG ((LM_DEBUG, "\nStatistics as of: %s\n", context));
  // Temporary::Dale: Dump known names
  CosNotification::NotificationServiceMonitorControl::NameList_var names =
    nsm_->get_statistic_names ();
  CORBA::ULong length = names->length ();
  ACE_DEBUG ((LM_DEBUG, "Statistic names [%d]\n", (int)length));

  // It's much easier to read once it's sorted
  const char** narray = 0;
  ACE_NEW_THROW_EX (narray,
                    const char* [length],
                    CORBA::NO_MEMORY ());
  for(CORBA::ULong i = 0; i < length; i++)
    narray[i] = names[i].in ();
  ACE_OS::qsort (narray, length,
                 sizeof (const char*), sorter);

  for(CORBA::ULong i = 0; i < length; i++)
    {
      stat_dump(narray[i]);
    }
  delete [] narray;
#endif // verbose option
}

void
MonitorTestInterface_i::stat_dump (const char * statName)
{
  try
    {
      Monitor::Data_var data = nsm_->get_statistic(statName);
      switch (data->data_union._d())
      {
      case Monitor::DATA_NUMERIC:
        {
        ACE_DEBUG ((LM_DEBUG, "Numeric: %s\n", statName));
        Monitor::Numeric num = data->data_union.num();
        ACE_DEBUG ((LM_DEBUG, "   count: %d, average: %f; sumsq: %f, min: %f, max: %f: last %f\n",
          (unsigned int)num.count, num.average, num.sum_of_squares, num.minimum, num.maximum, num.last));
        break;
        }
      default:
        {
        Monitor::NameList list = data->data_union.list ();
        size_t len = list.length ();
        ACE_DEBUG ((LM_DEBUG, "Text[%d]: %s\n", (int)len, statName));

        for (size_t i = 0; i < len; i++)
        {
          ACE_CString str = list[i].in ();
          ACE_DEBUG ((LM_DEBUG, "   %d: %s\n", (int)i, str.c_str()));
        }
        break;
        }
      }
    }
  catch (const CORBA::Exception& ex)
    {

      ex._tao_print_exception (statName);
    }
}


void
MonitorTestInterface_i::finished (MonitorTestInterface::Which proc)
{
//  ACE_CString str;
//  Monitor::Data_var data;
//  Monitor::NameList list;
//  Monitor::Numeric num;

  switch (proc)
    {
    case MonitorTestInterface::NotifyService:
      ACE_ERROR ((LM_ERROR, "This should not be called\n"));
      break;
    case MonitorTestInterface::Consumer:
      // By the time the consumer finishes, it should have destroyed
      // the event channel.
      this->running (MonitorTestInterface::NotifyService);
      break;
    case MonitorTestInterface::Supplier:
      {
      ACE_CString consumerCountName = this->base_ + NotifyMonitoringExt::EventChannelConsumerCount;
      Monitor::Data_var consumerCountData =
        nsm_->get_statistic(consumerCountName.c_str ());
      Monitor::Numeric consumerCountNum =
        consumerCountData->data_union.num ();
      if (consumerCountNum.last != 1)
      ACE_ERROR ((LM_ERROR, "Monitor: ERROR: There should still be one Consumer\n"));
  
      ACE_CString queueElementName = this->base_ + NotifyMonitoringExt::EventChannelQueueElementCount;
      Monitor::Data_var queueElementData =
        nsm_->get_statistic(queueElementName.c_str ());
      Monitor::Numeric queueElementNum =
        queueElementData->data_union.num ();
      if (queueElementNum.last == 0)
      ACE_ERROR ((LM_ERROR, "Monitor: ERROR: There should be at least one "
                            "event queued\n"));
  
      ACE_CString adminNamesName  = this->base_ + NotifyMonitoringExt::EventChannelConsumerAdminNames;
      Monitor::Data_var adminNamesData =
        nsm_->get_statistic(adminNamesName.c_str ());
      Monitor::NameList nameList =
        adminNamesData->data_union.list ();
      for (CORBA::ULong i = 0; i < nameList.length (); i++)
        {
          ACE_CString queueSizeName = nameList[i].in ();
          queueSizeName += "/";
          queueSizeName += NotifyMonitoringExt::EventChannelQueueSize;
          Monitor::Data_var queueSizeData =
            nsm_->get_statistic(queueSizeName.c_str ());
          Monitor::Numeric queueSizeNum =
            queueSizeData->data_union.num ();
        ACE_DEBUG ((LM_DEBUG, "Monitor: Queue Size: Average: %f, Maximum: %f, Most recent: %f\n",
            queueSizeNum.average, queueSizeNum.maximum, queueSizeNum.last));
          if (queueSizeNum.average <= 0.0 || queueSizeNum.average > 2000.0)
            ACE_ERROR ((LM_ERROR, "Monitor: ERROR: The average queue size [%f] should be greater than zero and less than 2000.\n",
              queueSizeNum.average));
          if (queueSizeNum.last > 2000.0)
            ACE_ERROR ((LM_ERROR, "Monitor: ERROR: The most recent queue size [%f] should not be greater than 2000.\n",
              queueSizeNum.last));
      }
  
      consumer_stats_check();
  
      brain_dump ("Finished Supplier");
      break;
      }
    default:
      ACE_ERROR ((LM_ERROR, "Impossible enum value %d\n", proc));
      break;
    }
}

static const ACE_TCHAR* ior_output_file = ACE_TEXT ("test_monitor.ior");
static const ACE_TCHAR* monitor_ior = 0;

static int
parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt get_opts (argc, argv, ACE_TEXT ("k:o:"));
  int c;

  while ((c = get_opts ()) != -1)
    switch (c)
      {
      case 'k':
        monitor_ior = get_opts.opt_arg ();
        break;
      case 'o':
        ior_output_file = get_opts.opt_arg ();
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
          "Monitor: usage: %s "
                           "-k <ior> "
                           "-o <file> "
                           "\n",
                           argv [0]),
                          -1);
      }
  return 0;
}

#endif /* TAO_HAS_MONITOR_FRAMEWORK==1 */

int
ACE_TMAIN (int argc, ACE_TCHAR* argv[])
{
  int status = 0;

#if defined (TAO_HAS_MONITOR_FRAMEWORK) && (TAO_HAS_MONITOR_FRAMEWORK == 1)

  try
    {
      CORBA::ORB_var orb = CORBA::ORB_init (argc, argv);

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

      CORBA::Object_var obj =
        orb->string_to_object (monitor_ior);
      CosNotification::NotificationServiceMonitorControl_var nsm =
        CosNotification::NotificationServiceMonitorControl::_narrow (obj.in ());

      if (CORBA::is_nil (nsm.in ()))
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Monitor: Unable to locate the "
                             "Notification Service Monitor\n"),
                            1);
        }

      MonitorTestInterface_i* mti = 0;
      ACE_NEW_RETURN (mti, MonitorTestInterface_i (nsm.in ()), 1);
      PortableServer::ServantBase_var owner_transfer (mti);

      CORBA::Object_var poa_object =
        orb->resolve_initial_references("RootPOA");
      PortableServer::POA_var root_poa =
        PortableServer::POA::_narrow (poa_object.in ());

      PortableServer::ObjectId_var id = root_poa->activate_object (mti);
      CORBA::Object_var object = root_poa->id_to_reference (id.in ());

      MonitorTestInterface_var test =
        MonitorTestInterface::_narrow (object.in ());
      CORBA::String_var ior = orb->object_to_string (test.in ());

      // Test the case where there are no consumers or suppliers first
      // before we write out our IOR
      mti->running (MonitorTestInterface::NotifyService);

      FILE *output_file= ACE_OS::fopen (ACE_TEXT_ALWAYS_CHAR(ior_output_file), ACE_TEXT ("w"));

      if (output_file == 0)
        {
          ACE_ERROR_RETURN ((LM_ERROR,
                             "Cannot open output file for writing IOR: %s\n",
                             ior_output_file),
                             1);
        }

      ACE_OS::fprintf (output_file, "%s", ior.in ());
      ACE_OS::fclose (output_file);

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

      orb->run ();
      orb->destroy ();
    }
  catch (const CORBA::Exception& ex)
    {
      ex._tao_print_exception ("test_monitor: ");
      status++;
    }

#else
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);
#endif /* TAO_HAS_MONITOR_FRAMEWORK==1 */

  return status;
}