summaryrefslogtreecommitdiff
path: root/ACE/ace/Monitor_Base.cpp
blob: ce445ae9d7bd3c0cd5a8fc5a7977760df9d0467f (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
#include "ace/Monitor_Base.h"

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

#include "ace/ACE.h"
#include "ace/Monitor_Admin_Manager.h"
#include "ace/Monitor_Control_Action.h"
#include "ace/Monitor_Point_Registry.h"
#include "ace/Guard_T.h"
#include "ace/Dynamic_Service.h"
#include "ace/OS_NS_sys_time.h"

#if !defined (__ACE_INLINE__)
#include "ace/Monitor_Base.inl"
#endif /* __ACE_INLINE__ */

ACE_BEGIN_VERSIONED_NAMESPACE_DECL

namespace ACE
{
  namespace Monitor_Control
  {
    Monitor_Base::Monitor_Base (const char* name,
                                Monitor_Control_Types::Information_Type type)
      : ACE_Refcountable_T<ACE_SYNCH_MUTEX> (1)
      , data_ (type)
      , name_ (name)
    {
    }

    Monitor_Base::~Monitor_Base ()
    {
      ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);

      if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          for (size_t i = 0UL; i < this->data_.index_; ++i)
            {
#if defined (ACE_HAS_ALLOC_HOOKS)
              ACE_Allocator::instance()->free(this->data_.list_[i]);
#else
              delete [] this->data_.list_[i];
#endif /* ACE_HAS_ALLOC_HOOKS */
            }
        }
    }

    void
    Monitor_Base::update ()
    {
      /// Overridden in derived classes.
    }

    void
    Monitor_Base::receive (double data)
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR ((LM_ERROR,
                      ACE_TEXT ("receive: can't store numeric value - ")
                      ACE_TEXT ("%s is a string type monitor\n"),
                      this->name_.c_str ()));
          return;
        }

      ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
      this->data_.timestamp_ = ACE_OS::gettimeofday ();
      this->data_.value_ = data;

      if (this->data_.type_ != Monitor_Control_Types::MC_COUNTER)
        {
          this->data_.sum_ += data;
          this->data_.sum_of_squares_ += (data * data);
          ++this->data_.index_;
        }

      if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER)
        {
          ++this->data_.last_;
          this->data_.maximum_ = this->data_.last_;
        }
      else
        {
          this->data_.last_ = data;

          if (!this->data_.minimum_set_)
            {
              this->data_.minimum_set_ = true;
              this->data_.minimum_ = data;
            }
          else if (this->data_.minimum_ > data)
            {
              this->data_.minimum_ = data;
            }

          if (this->data_.maximum_ < data)
            {
              this->data_.maximum_ = data;
            }
        }
    }

    void
    Monitor_Base::receive (size_t data)
    {
      this->receive (static_cast<double> (data));
    }

    void
    Monitor_Base::receive (const Monitor_Control_Types::NameList& data)
    {
      if (this->data_.type_ != Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR ((LM_ERROR,
                      ACE_TEXT ("receive: can't store string values - ")
                      ACE_TEXT ("%s is a numeric type monitor\n"),
                      this->name_.c_str ()));
          return;
        }

      ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);

      for (size_t i = 0UL; i < this->data_.index_; ++i)
        {
          ACE::strdelete (this->data_.list_[i]);
        }

      this->data_.index_ = data.size ();
      this->data_.list_.max_size (this->data_.index_);

      for (size_t i = 0UL; i < this->data_.index_; ++i)
        {
          this->data_.list_[i] = ACE::strnew (data[i].c_str ());
        }
    }

    long
    Monitor_Base::add_constraint (const char* expression,
                                  Control_Action* action)
    {
      /// Thread-safe and guaranteed to be unique.
      long id = Monitor_Point_Registry::instance ()->constraint_id ();

      CONSTRAINTS::value_type entry;
      entry.first = id;
      entry.second.expr = expression;
      entry.second.control_action = action;

      /// This is thread-safe on its own so we don't have
      /// to guard it here.
      action->add_ref ();

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, -1);

        /// Since we know external key is unique,
        /// we don't check for failure.
        (void) this->constraints_.insert (entry);
      }

      return id;
    }

    Control_Action*
    Monitor_Base::remove_constraint (const long constraint_id)
    {
      Control_Action* retval = 0;

      {
        ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0);

        CONSTRAINT_ITERATOR i = this->constraints_.find (constraint_id);

        if (i != this->constraints_.end ())
          {
            retval = i->second.control_action;
            (void) this->constraints_.erase (constraint_id);
          }
      }

      return retval;
    }

    void
    Monitor_Base::retrieve (Monitor_Control_Types::Data& data) const
    {
      ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);

      data = this->data_;
    }

    void
    Monitor_Base::clear ()
    {
      ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);

      this->clear_i ();
    }

    void
    Monitor_Base::retrieve_and_clear (Monitor_Control_Types::Data& data)
    {
      ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);

      data = this->data_;
      this->clear_i ();
    }

    void
    Monitor_Base::add_to_registry (const ACE_Time_Value& time)
    {
      MC_ADMINMANAGER *mgr =
        ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");

      if (!mgr->admin ().monitor_point (this, time))
        {
          ACELIB_ERROR ((LM_ERROR,
                      "monitor point %s registration failed\n",
                      this->name ()));
        }
    }

    void
    Monitor_Base::remove_from_registry ()
    {
      if (!Monitor_Point_Registry::instance ()->remove (this->name ()))
        {
// (JP) There is a problem with this failing on a single ACE_Message_Queue
//      monitor per process. I think it is the message queue associated
//      with the default reactor, maybe because at that low level, ACE
//      is using malloc with placement, then free, which may bypass the
//      normal destructors. In any case, it happens only at shutdown
//      and there seems to be no memory leak.
//          ACELIB_ERROR ((LM_ERROR,
//                      "monitor point %s unregistration failed\n",
//                      this->name ()));
        }
    }

    double
    Monitor_Base::average () const
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
          || this->data_.type_ == Monitor_Control_Types::MC_GROUP
          || this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("average: %s is wrong monitor type\n"),
                             this->name_.c_str ()),
                            0);
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);

      return (this->data_.index_== 0UL
              ? 0.0
              : this->data_.sum_ / this->data_.index_);
    }

    double
    Monitor_Base::sum_of_squares () const
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
          || this->data_.type_ == Monitor_Control_Types::MC_GROUP
          || this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("sum_of_squares: %s ")
                             ACE_TEXT ("is wrong monitor type\n"),
                             this->name_.c_str ()),
                            0);
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);

      return this->data_.sum_of_squares_;
    }

    size_t
    Monitor_Base::count () const
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_GROUP)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("count: %s is a monitor group\n"),
                             this->name_.c_str ()),
                            0UL);
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0UL);

      return (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
              ? static_cast<size_t> (this->data_.last_)
              : this->data_.index_);
    }

    double
    Monitor_Base::minimum_sample () const
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
          || this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("minimum_sample: %s ")
                             ACE_TEXT ("is wrong monitor type\n"),
                             this->name_.c_str ()),
                            0);
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);

      return this->data_.minimum_;
    }

    double
    Monitor_Base::maximum_sample () const
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
          || this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("maximum_sample: %s ")
                             ACE_TEXT ("is wrong monitor type\n"),
                             this->name_.c_str ()),
                            0);
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);

      return this->data_.maximum_;
    }

    double
    Monitor_Base::last_sample () const
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
          || this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR_RETURN ((LM_ERROR,
                             ACE_TEXT ("last_sample: %s ")
                             ACE_TEXT ("is wrong monitor type\n"),
                             this->name_.c_str ()),
                            0);
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);

      return this->data_.last_;
    }

    Monitor_Control_Types::NameList
    Monitor_Base::get_list () const
    {
      Monitor_Control_Types::NameList retval;

      if (this->data_.type_ != Monitor_Control_Types::MC_LIST)
        {
          ACELIB_ERROR ((LM_ERROR,
                      ACE_TEXT ("get_list: %s is not a ")
                      ACE_TEXT ("list monitor type\n"),
                      this->name_.c_str ()));

          return retval;
        }

      ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, retval);

      for (size_t i = 0UL; i < this->data_.index_; ++i)
        {
          retval.push_back (this->data_.list_[i]);
        }

      return retval;
    }

    void
    Monitor_Base::clear_i ()
    {
      if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
        {
          for (size_t i = 0UL; i < this->data_.index_; ++i)
            {
              ACE::strdelete (this->data_.list_[i]);
            }

          this->data_.list_.max_size (0UL);
        }

      this->data_.value_ = 0.0;
      this->data_.timestamp_ = ACE_Time_Value::zero;
      this->data_.index_ = 0UL;
      this->data_.minimum_set_ = false;
      this->data_.minimum_ = 0.0;
      this->data_.maximum_ = 0.0;
      this->data_.sum_ = 0.0;
      this->data_.sum_of_squares_ = 0.0;
      this->data_.last_ = 0.0;
    }
  }
}

ACE_END_VERSIONED_NAMESPACE_DECL

#endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */