1 // $Id: Monitor_Base.cpp 82328 2008-07-15 17:20:17Z parsons $
3 #include "ace/Monitor_Base.h"
5 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
7 #include "ace/Monitor_Admin_Manager.h"
8 #include "ace/Monitor_Control_Action.h"
9 #include "ace/Monitor_Point_Registry.h"
10 #include "ace/Guard_T.h"
11 #include "ace/Dynamic_Service.h"
12 #include "ace/OS_NS_sys_time.h"
14 #if !defined (__ACE_INLINE__)
15 #include "ace/Monitor_Base.inl"
16 #endif /* __ACE_INLINE__ */
18 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
22 namespace Monitor_Control
24 Monitor_Base::Monitor_Base (const char* name
,
25 Monitor_Control_Types::Information_Type type
)
26 : ACE_Refcountable_T
<ACE_SYNCH_MUTEX
> (1)
32 Monitor_Base::~Monitor_Base (void)
34 ACE_GUARD (ACE_SYNCH_MUTEX
, guard
, this->mutex_
);
36 if (this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
38 for (size_t i
= 0UL; i
< this->data_
.index_
; ++i
)
40 delete [] this->data_
.list_
[i
];
46 Monitor_Base::update (void)
48 /// Overridden in derived classes.
52 Monitor_Base::receive (double data
)
54 if (this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
57 ACE_TEXT ("receive: can't store numeric value - ")
58 ACE_TEXT ("%s is a string type monitor\n"),
59 this->name_
.c_str ()));
63 ACE_GUARD (ACE_SYNCH_MUTEX
, guard
, this->mutex_
);
64 this->data_
.timestamp_
= ACE_OS::gettimeofday ();
65 this->data_
.value_
= data
;
67 if (this->data_
.type_
!= Monitor_Control_Types::MC_COUNTER
)
69 this->data_
.sum_
+= data
;
70 this->data_
.sum_of_squares_
+= (data
* data
);
74 if (this->data_
.type_
== Monitor_Control_Types::MC_COUNTER
)
77 this->data_
.maximum_
= this->data_
.last_
;
81 this->data_
.last_
= data
;
83 if (!this->data_
.minimum_set_
)
85 this->data_
.minimum_set_
= true;
86 this->data_
.minimum_
= data
;
88 else if (this->data_
.minimum_
> data
)
90 this->data_
.minimum_
= data
;
93 if (this->data_
.maximum_
< data
)
95 this->data_
.maximum_
= data
;
101 Monitor_Base::receive (size_t data
)
103 this->receive (static_cast<double> (data
));
107 Monitor_Base::receive (const Monitor_Control_Types::NameList
& data
)
109 if (this->data_
.type_
!= Monitor_Control_Types::MC_LIST
)
111 ACE_ERROR ((LM_ERROR
,
112 ACE_TEXT ("receive: can't store string values - ")
113 ACE_TEXT ("%s is a numeric type monitor\n"),
114 this->name_
.c_str ()));
118 ACE_GUARD (ACE_SYNCH_MUTEX
, guard
, this->mutex_
);
120 for (size_t i
= 0UL; i
< this->data_
.index_
; ++i
)
122 ACE::strdelete (this->data_
.list_
[i
]);
125 this->data_
.index_
= data
.size ();
126 this->data_
.list_
.max_size (this->data_
.index_
);
128 for (size_t i
= 0UL; i
< this->data_
.index_
; ++i
)
130 this->data_
.list_
[i
] = ACE::strnew (data
[i
].c_str ());
135 Monitor_Base::add_constraint (const char* expression
,
136 Control_Action
* action
)
138 /// Thread-safe and guaranteed to be unique.
139 long id
= Monitor_Point_Registry::instance ()->constraint_id ();
141 CONSTRAINTS::value_type entry
;
143 entry
.second
.expr
= expression
;
144 entry
.second
.control_action
= action
;
146 /// This is thread-safe on its own so we don't have
147 /// to guard it here.
151 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, -1);
153 /// Since we know external key is unique,
154 /// we don't check for failure.
155 (void) this->constraints_
.insert (entry
);
162 Monitor_Base::remove_constraint (const long constraint_id
)
164 Control_Action
* retval
= 0;
167 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0);
169 CONSTRAINT_ITERATOR i
= this->constraints_
.find (constraint_id
);
171 if (i
!= this->constraints_
.end ())
173 retval
= i
->second
.control_action
;
174 (void) this->constraints_
.erase (constraint_id
);
182 Monitor_Base::retrieve (Monitor_Control_Types::Data
& data
) const
184 ACE_GUARD (ACE_SYNCH_MUTEX
, guard
, this->mutex_
);
190 Monitor_Base::clear (void)
192 ACE_GUARD (ACE_SYNCH_MUTEX
, guard
, this->mutex_
);
198 Monitor_Base::retrieve_and_clear (Monitor_Control_Types::Data
& data
)
200 ACE_GUARD (ACE_SYNCH_MUTEX
, guard
, this->mutex_
);
207 Monitor_Base::add_to_registry (const ACE_Time_Value
& time
)
209 MC_ADMINMANAGER
*mgr
=
210 ACE_Dynamic_Service
<MC_ADMINMANAGER
>::instance ("MC_ADMINMANAGER");
212 if (!mgr
->admin ().monitor_point (this, time
))
214 ACE_ERROR ((LM_ERROR
,
215 "monitor point %s registration failed\n",
221 Monitor_Base::remove_from_registry (void)
223 if (!Monitor_Point_Registry::instance ()->remove (this->name ()))
225 // (JP) There is a problem with this failing on a single ACE_Message_Queue
226 // monitor per process. I think it is the message queue associated
227 // with the default reactor, maybe because at that low level, ACE
228 // is using malloc with placement, then free, which may bypass the
229 // normal destructors. In any case, it happens only at shutdown
230 // and there seems to be no memory leak.
231 // ACE_ERROR ((LM_ERROR,
232 // "monitor point %s unregistration failed\n",
238 Monitor_Base::average (void) const
240 if (this->data_
.type_
== Monitor_Control_Types::MC_COUNTER
241 || this->data_
.type_
== Monitor_Control_Types::MC_GROUP
242 || this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
244 ACE_ERROR_RETURN ((LM_ERROR
,
245 ACE_TEXT ("average: %s is wrong monitor type\n"),
246 this->name_
.c_str ()),
250 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0.0);
252 return (this->data_
.index_
== 0UL
254 : this->data_
.sum_
/ this->data_
.index_
);
258 Monitor_Base::sum_of_squares (void) const
260 if (this->data_
.type_
== Monitor_Control_Types::MC_COUNTER
261 || this->data_
.type_
== Monitor_Control_Types::MC_GROUP
262 || this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
264 ACE_ERROR_RETURN ((LM_ERROR
,
265 ACE_TEXT ("sum_of_squares: %s ")
266 ACE_TEXT ("is wrong monitor type\n"),
267 this->name_
.c_str ()),
271 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0.0);
273 return this->data_
.sum_of_squares_
;
277 Monitor_Base::count (void) const
279 if (this->data_
.type_
== Monitor_Control_Types::MC_GROUP
)
281 ACE_ERROR_RETURN ((LM_ERROR
,
282 ACE_TEXT ("count: %s is a monitor group\n"),
283 this->name_
.c_str ()),
287 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0UL);
289 return (this->data_
.type_
== Monitor_Control_Types::MC_COUNTER
290 ? static_cast<size_t> (this->data_
.last_
)
291 : this->data_
.index_
);
295 Monitor_Base::minimum_sample (void) const
297 if (this->data_
.type_
== Monitor_Control_Types::MC_GROUP
298 || this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
300 ACE_ERROR_RETURN ((LM_ERROR
,
301 ACE_TEXT ("minimum_sample: %s ")
302 ACE_TEXT ("is wrong monitor type\n"),
303 this->name_
.c_str ()),
307 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0.0);
309 return this->data_
.minimum_
;
313 Monitor_Base::maximum_sample (void) const
315 if (this->data_
.type_
== Monitor_Control_Types::MC_GROUP
316 || this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
318 ACE_ERROR_RETURN ((LM_ERROR
,
319 ACE_TEXT ("maximum_sample: %s ")
320 ACE_TEXT ("is wrong monitor type\n"),
321 this->name_
.c_str ()),
325 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0.0);
327 return this->data_
.maximum_
;
331 Monitor_Base::last_sample (void) const
333 if (this->data_
.type_
== Monitor_Control_Types::MC_GROUP
334 || this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
336 ACE_ERROR_RETURN ((LM_ERROR
,
337 ACE_TEXT ("last_sample: %s ")
338 ACE_TEXT ("is wrong monitor type\n"),
339 this->name_
.c_str ()),
343 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, 0.0);
345 return this->data_
.last_
;
348 Monitor_Control_Types::NameList
349 Monitor_Base::get_list (void) const
351 Monitor_Control_Types::NameList retval
;
353 if (this->data_
.type_
!= Monitor_Control_Types::MC_LIST
)
355 ACE_ERROR ((LM_ERROR
,
356 ACE_TEXT ("get_list: %s is not a ")
357 ACE_TEXT ("list monitor type\n"),
358 this->name_
.c_str ()));
363 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX
, guard
, this->mutex_
, retval
);
365 for (size_t i
= 0UL; i
< this->data_
.index_
; ++i
)
367 retval
.push_back (this->data_
.list_
[i
]);
374 Monitor_Base::clear_i (void)
376 if (this->data_
.type_
== Monitor_Control_Types::MC_LIST
)
378 for (size_t i
= 0UL; i
< this->data_
.index_
; ++i
)
380 ACE::strdelete (this->data_
.list_
[i
]);
383 this->data_
.list_
.max_size (0UL);
386 this->data_
.value_
= 0.0;
387 this->data_
.timestamp_
= ACE_Time_Value::zero
;
388 this->data_
.index_
= 0UL;
389 this->data_
.minimum_set_
= false;
390 this->data_
.minimum_
= 0.0;
391 this->data_
.maximum_
= 0.0;
392 this->data_
.sum_
= 0.0;
393 this->data_
.sum_of_squares_
= 0.0;
394 this->data_
.last_
= 0.0;
399 ACE_END_VERSIONED_NAMESPACE_DECL
401 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */