summaryrefslogtreecommitdiff
path: root/CIAO/DAnCE/NodeManager/MonitorController.cpp
blob: dea59b146091b0ff99fa7bdd252b0fb35d78fe90 (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
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// MonitorController.cpp,v 1.7 2006/02/02 16:32:26 wotte Exp
//----------------------------------------------------------------------------------
/**
 * @file MonitorController.cpp
 *
 * @brief The Monitor Controller implementation.
 *
 * This is the facade class for Monitor
 *
 * @author Nilabja Roy <nilabjar@dre.vanderbilt.edu>
 */
//----------------------------------------------------------------------------------

#include "orbsvcs/CosNamingC.h"
#include "MonitorController.h"
#include "BaseMonitor.h"
#include "CIAO_common.h"


#include "ace/Log_Msg.h"
#include "ace/DLL.h"
#include "ace/SString.h"

#include "NodeManager_Impl.h"

#include "MonitorCB.h"
//#include "Profile_Code.h"


namespace CIAO
{
  typedef MonitorBase* (*MonitorFactory) ();

  /// for the CIAO monitor
  const char* monitor_lib_name = "ciaomonlib";

  /// na monitor lib name
  const char* na_monitor_lib_name = "namonlib";


  // The interval after which update will be sent.
  // This value will sent by the EM in the later implementation
  const int interval = 10;

  static const char* factory_func = "createMonitor";
}

CIAO::MonitorController::MonitorController (CORBA::ORB_ptr orb,
                                            const ::Deployment::Domain& domain,
                                            ::CIAO::NodeManager_Impl_Base* node_mgr
                                            )
  : terminate_flag_ (0),
    orb_ (orb),
    initial_domain_ (domain),
    node_mgr_ (node_mgr),
    monitor_cpu_usage_ (0),
    monitor_NA_usage_ (0),
    add_component_pid_ (1)
{
}

int CIAO::MonitorController::init ()
{
  ACE_DEBUG ((LM_DEBUG , "Inside the init function [%s]\n",
	          initial_domain_.node[0].name.in ()));


  create_the_servant ();

  ACE_DEBUG ((LM_DEBUG , "After the create servant function [%s]\n",
	          initial_domain_.node[0].name.in ()));
  upload_obj_ref ();

  ACE_DEBUG ((LM_DEBUG , "After the upload obj ref function [%s]\n",
	          initial_domain_.node[0].name.in ()));
    // Parse the intial domain and setup the options
  parse_initial_domain ();
  ACE_DEBUG ((LM_DEBUG , "After the parse_initial_domain function [%s]\n",
	          initial_domain_.node[0].name.in ()));

    // Populat and startup the monitor list
  populate_monitor_list ();
  ACE_DEBUG ((LM_DEBUG , "After the populate monitor list function [%s]\n",
	          initial_domain_.node[0].name.in ()));

  return 1;
}

::Deployment::Domain* CIAO::MonitorController::update_data_for_TM ()
{
	ACE_DEBUG ((LM_DEBUG , "The list size is %d\n",
		    monitor_list_.size ()));

  ::Deployment::Domain* domain =
    new ::Deployment::Domain (this->initial_domain_);

  // Set the new domain resource to zero
  domain->node[0].resource.length (0);

  for (size_t i = 0;i < monitor_list_.size ();i++)
  {
    ::Deployment::Domain *new_domain =
      monitor_list_[i]->monitor_->get_current_data ();
    add_resource_to_domain (*domain , *new_domain);
  }

  return domain;
}

int CIAO::MonitorController::svc (void)
{
//   ACE_DEBUG ((LM_DEBUG , "Inside the Node for [%s]\n",
// 	      initial_domain_.node[0].name.in ()));


    // Parse the intial domain and setup the options
  parse_initial_domain ();

    // Populat and startup the monitor list
  populate_monitor_list ();

    // Wait for system to stabilize itself
  ACE_OS::sleep (interval);

    // The loop in which UpdateData is called
    while (!terminating ())
      {

	ACE_DEBUG ((LM_DEBUG , "The list size is %d\n",
		    monitor_list_.size ()));


        ::Deployment::Domain domain = this->initial_domain_;

        // Set the new domain resource to zero
        domain.node[0].resource.length (0);


        for (size_t i =0;i < monitor_list_.size ();i++)
          {

            ::Deployment::Domain *new_domain =
              monitor_list_[i]->monitor_->get_current_data ();

             add_resource_to_domain (domain , *new_domain);
          }


        // ****** add component data *******************

        NodeManager_Impl_Base::Component_Ids cids =
          node_mgr_->get_component_detail ();

//          ACE_DEBUG ((LM_DEBUG , "\nThe process id is [%d]\n",
//                      CORBA::Long (cids.process_id_)));


        // Here save the old resource length
        int counter = domain.node[0].resource.length ();

        // if pid is already added , dont add
        if (add_component_pid_)
          {
            // then add more resource element to the
            // domain structure
            // ACE_DEBUG ((LM_DEBUG , "Going to add CID/PID data\n"));
            int new_res_size = domain.node[0].resource.length () +
              cids.cid_seq_.size ();

            domain.node[0].resource.length (new_res_size);

            ACE_Unbounded_Set_Iterator<ACE_CString> iter (cids.cid_seq_);

//             for (iter = cids.cid_seq_.begin ();
//                  iter != cids.cid_seq_.end ();
//                  iter++,counter++)
//               {
//                 domain.node[0].resource[counter].name =
//                   CORBA::string_dup ("Component");
//                 domain.node[0].resource[counter].resourceType.length (0);

//                 // Have one property for now
//                 domain.node[0].resource[counter].property.length (1);
//                 domain.node[0].resource[counter].property[0].name =
//                   CORBA::string_dup ((*iter).c_str ());
//                 domain.node[0].resource[counter].property[0].kind =
//                   ::Deployment::Quantity;
//                 domain.node[0].resource[counter].property[0].dynamic =
//                   0;
//                 domain.node[0].resource[counter].property[0].value <<=
//                   CORBA::Long (cids.process_id_);

//                 //                ACE_DEBUG ((LM_DEBUG , "The process id is [%d]\n",
//                 //                            CORBA::Long (cids.process_id_)));
//               }
            // set the add_component_pid_ to 0
            add_component_pid_ = 0;
          }

        //******add compoennt data


        // data will be updated in intervals of 10 secs.
        // in the latest version of spec , this value will
        // come from Execution Manager
        ACE_OS::sleep (interval);

      }

  if (CIAO::debug_level () > 9)
    {
      ACE_DEBUG ((LM_DEBUG , "CIAO::Monitor::Terminating Monitor\n"));
    }
 return 0;
}

CIAO::MonitorController::~MonitorController ()
{
  ACE_DEBUG ((LM_DEBUG , "CIAO::MonitorController::Destructor\n"));
  terminate ();
  wait ();
}

void CIAO::MonitorController::terminate ()
{
  // make the terminate flag false
  ACE_GUARD (ACE_SYNCH_MUTEX,
             guard,
             lock_
             );
  //Ace_DEBUG ((LM_DEBUG , "WITHIN TERMINATE CALL  ......\n"));
  terminate_flag_=1;
}

bool CIAO::MonitorController::terminating ()
{
  ACE_GUARD_RETURN (ACE_SYNCH_MUTEX,
                    guard,
                    lock_,
                    0
                    );
  return terminate_flag_;
}


void CIAO::MonitorController::parse_initial_domain ()
{
  for (unsigned int i = 0;
       i < initial_domain_.node[0].resource.length ();
       i++)
    {
      // check if cpu usage to be monitored or not
      if (!strcmp (initial_domain_.node[0].resource[i].name, "Processor"))
        monitor_cpu_usage_ = 1;

      ACE_DEBUG ((LM_DEBUG, "Initial Domain,Resource Name %s\n",
                  initial_domain_.node[0].resource[i].name.in () ));

      // check if NA usage to be monitored or not
      if (!strcmp (initial_domain_.node[0].resource[i].name, "NA_Monitor"))
        monitor_NA_usage_ = 1;
    }
}

void CIAO::MonitorController::populate_monitor_list ()
{

  MonitorElement* monitor_elem;
  if (monitor_cpu_usage_)
    {
      // create a MonitorElement
      monitor_elem = new MonitorElement;

      // forming the library name
      monitor_elem->lib_name_ = ACE_DLL_PREFIX;
      monitor_elem->lib_name_  += monitor_lib_name;

      int retval
        = monitor_elem->dll_.open (monitor_elem->lib_name_.c_str ());

      ACE_DEBUG ((LM_DEBUG, "Monitor Lib name %s\n",
                  monitor_elem->lib_name_.c_str () ));

      if (retval != 0)
        {
          ACE_ERROR ((LM_ERROR,"%p","dll.open"));
          return;
        }

      MonitorFactory factory =
        (MonitorFactory) monitor_elem->dll_.symbol (factory_func);

      if (factory == 0)
        {
          ACE_ERROR ((LM_ERROR,"%p","dll.symbol"));
          return;
        }

      // here creating the monitor object
        monitor_elem->monitor_ = (MonitorBase*) factory ();

        ACE_DEBUG ((LM_DEBUG, "Inside the init call\n"));

        // get the resource specific data ..

        ::Deployment::Domain resource_domain_ =
            this->create_monitor_domain ("Processor");

        monitor_elem->monitor_->initialize_params (resource_domain_,
                                                   interval);

        monitor_elem->monitor_->set_context (this);

        monitor_list_.push_back (monitor_elem);

        ACE_DEBUG ((LM_DEBUG , "The list size is %d\n",
              monitor_list_.size ()));

    } // if monitor_cpu_usage
  if (monitor_NA_usage_)
    {


     // create a MonitorElement
      monitor_elem = new MonitorElement;

      // forming the library name
      monitor_elem->lib_name_ = ACE_DLL_PREFIX;
      monitor_elem->lib_name_  += na_monitor_lib_name;

      ACE_DEBUG ((LM_DEBUG, "Inside the NA_Usage\n"));

      ACE_DEBUG ((LM_DEBUG, "NA Monitor Lib name %s\n",
                  monitor_elem->lib_name_.c_str () ));
      int retval
        = monitor_elem->dll_.open (monitor_elem->lib_name_.c_str ());


      if (retval != 0)
        {
          ACE_ERROR ((LM_ERROR,"%p","dll.open"));
          return;
        }

      MonitorFactory factory =
        (MonitorFactory) monitor_elem->dll_.symbol (factory_func);

      if (factory == 0)
        {
          ACE_ERROR ((LM_ERROR,"%p","dll.symbol"));
          return;
        }


        // here creating the monitor object
        monitor_elem->monitor_ = (MonitorBase*) factory ();

        ACE_DEBUG ((LM_DEBUG, "Inside the NA init call\n"));

        // get the resource specific data ..

        ::Deployment::Domain resource_domain_ =
            this->create_monitor_domain ("NA_Monitor");


        monitor_elem->monitor_->initialize_params (resource_domain_,
                                                   interval);


        monitor_elem->monitor_->set_context (this);

        monitor_list_.push_back (monitor_elem);

    }
}

CIAO::NodeManager_Impl_Base::Component_Ids
CIAO::MonitorController::get_component_process ()
{
    return node_mgr_->get_component_detail ();
}

::Deployment::Domain CIAO::MonitorController::
create_monitor_domain (const char * resource)
{
  ::Deployment::Domain domain;

  domain.UUID = CORBA::string_dup (initial_domain_.UUID);

  domain.label = CORBA::string_dup (initial_domain_.label);

  domain.sharedResource = initial_domain_.sharedResource;
  domain.interconnect = initial_domain_.interconnect;
  domain.bridge = initial_domain_.bridge;
  domain.infoProperty = initial_domain_.infoProperty;

  // set the node ...
  domain.node.length (1);

  domain.node[0].name =
    CORBA::string_dup (initial_domain_.node[0].name);

  domain.node[0].resource.length (1);


  for (size_t i = 0;
       i < initial_domain_.node[0].resource.length ();
       i++)
    {
      if (!strcmp (initial_domain_.node[0].resource[i].name, resource))
        domain.node[0].resource[0] =
          initial_domain_.node[0].resource[i];
    }

  return domain;
}

void CIAO::MonitorController::
add_resource_to_domain (::Deployment::Domain& new_domain,
                        ::Deployment::Domain monitored_domain)
{
  size_t current_size = new_domain.node[0].resource.length ();

  new_domain.node[0].resource.length (current_size + 1);

  //  ACE_DEBUG ((LM_DEBUG , "Adding the resource \n"));

  new_domain.node[0].resource[current_size] =
    monitored_domain.node[0].resource[0];
}

void CIAO::MonitorController::upload_obj_ref ()
{
  CORBA::Object_var naming_context_object =
    orb_->resolve_initial_references ("NameService");
  CosNaming::NamingContext_var naming_context =
    CosNaming::NamingContext::_narrow (naming_context_object.in ());

 // check for context first
  CosNaming::Name name (1);
  name.length (1);
  name[0].id = CORBA::string_dup ("Node_Monitor");

  try
  {
    naming_context->resolve (name);
  }
  catch (CosNaming::NamingContext::NotFound& ex)
  {
    ex._tao_print_exception ("MonitorController::upload_obj_ref\t\n");

    try
    {
      naming_context->bind_new_context (name);
    }
    catch (CORBA::Exception & e)
    {
      ex._tao_print_exception ("MonitorController::upload_obj_ref:bind_new_context\t\n");
      return;
    }

  }

  name.length (2);

  name[0].id = CORBA::string_dup ("Node_Monitor");
  name[1].id = CORBA::string_dup (initial_domain_.node[0].name.in ());

  try
  {
    naming_context->rebind (name, monitorv_);
  }
  catch (CORBA::Exception& ex)
  {
    ex._tao_print_exception ("MonitorController::upload_obj_ref\t\n");
    return;
  }
}

void CIAO::MonitorController::create_the_servant ()
{
  CORBA::Object_var poa_object =
      orb_->resolve_initial_references ("RootPOA");

  poa_ = PortableServer::POA::_narrow (poa_object.in ());

  monitor_i_ = new Onl_Monitor_NM_Monitor_i (this, this->node_mgr_);

  monitorv_ = monitor_i_->_this ();
}

auto_ptr<Deployment::Domain>
CIAO::MonitorController::get_initial_domain ()
{
  auto_ptr<Deployment::Domain> domain (new ::Deployment::Domain (this->initial_domain_));
  return domain;
}