Fixed typos
[ACE_TAO.git] / ACE / ace / Monitor_Base.cpp
blobc152ece18c9697ac41c0b06cb34f1b2c6f302f22
1 #include "ace/Monitor_Base.h"
3 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
5 #include "ace/ACE.h"
6 #include "ace/Monitor_Admin_Manager.h"
7 #include "ace/Monitor_Control_Action.h"
8 #include "ace/Monitor_Point_Registry.h"
9 #include "ace/Guard_T.h"
10 #include "ace/Dynamic_Service.h"
11 #include "ace/OS_NS_sys_time.h"
13 #if !defined (__ACE_INLINE__)
14 #include "ace/Monitor_Base.inl"
15 #endif /* __ACE_INLINE__ */
17 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
19 namespace ACE
21 namespace Monitor_Control
23 Monitor_Base::Monitor_Base (const char* name,
24 Monitor_Control_Types::Information_Type type)
25 : ACE_Refcountable_T<ACE_SYNCH_MUTEX> (1)
26 , data_ (type)
27 , name_ (name)
31 Monitor_Base::~Monitor_Base (void)
33 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
35 if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
37 for (size_t i = 0UL; i < this->data_.index_; ++i)
39 #if defined (ACE_HAS_ALLOC_HOOKS)
40 ACE_Allocator::instance()->free(this->data_.list_[i]);
41 #else
42 delete [] this->data_.list_[i];
43 #endif /* ACE_HAS_ALLOC_HOOKS */
48 void
49 Monitor_Base::update (void)
51 /// Overridden in derived classes.
54 void
55 Monitor_Base::receive (double data)
57 if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
59 ACELIB_ERROR ((LM_ERROR,
60 ACE_TEXT ("receive: can't store numeric value - ")
61 ACE_TEXT ("%s is a string type monitor\n"),
62 this->name_.c_str ()));
63 return;
66 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
67 this->data_.timestamp_ = ACE_OS::gettimeofday ();
68 this->data_.value_ = data;
70 if (this->data_.type_ != Monitor_Control_Types::MC_COUNTER)
72 this->data_.sum_ += data;
73 this->data_.sum_of_squares_ += (data * data);
74 ++this->data_.index_;
77 if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER)
79 ++this->data_.last_;
80 this->data_.maximum_ = this->data_.last_;
82 else
84 this->data_.last_ = data;
86 if (!this->data_.minimum_set_)
88 this->data_.minimum_set_ = true;
89 this->data_.minimum_ = data;
91 else if (this->data_.minimum_ > data)
93 this->data_.minimum_ = data;
96 if (this->data_.maximum_ < data)
98 this->data_.maximum_ = data;
103 void
104 Monitor_Base::receive (size_t data)
106 this->receive (static_cast<double> (data));
109 void
110 Monitor_Base::receive (const Monitor_Control_Types::NameList& data)
112 if (this->data_.type_ != Monitor_Control_Types::MC_LIST)
114 ACELIB_ERROR ((LM_ERROR,
115 ACE_TEXT ("receive: can't store string values - ")
116 ACE_TEXT ("%s is a numeric type monitor\n"),
117 this->name_.c_str ()));
118 return;
121 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
123 for (size_t i = 0UL; i < this->data_.index_; ++i)
125 ACE::strdelete (this->data_.list_[i]);
128 this->data_.index_ = data.size ();
129 this->data_.list_.max_size (this->data_.index_);
131 for (size_t i = 0UL; i < this->data_.index_; ++i)
133 this->data_.list_[i] = ACE::strnew (data[i].c_str ());
137 long
138 Monitor_Base::add_constraint (const char* expression,
139 Control_Action* action)
141 /// Thread-safe and guaranteed to be unique.
142 long id = Monitor_Point_Registry::instance ()->constraint_id ();
144 CONSTRAINTS::value_type entry;
145 entry.first = id;
146 entry.second.expr = expression;
147 entry.second.control_action = action;
149 /// This is thread-safe on its own so we don't have
150 /// to guard it here.
151 action->add_ref ();
154 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, -1);
156 /// Since we know external key is unique,
157 /// we don't check for failure.
158 (void) this->constraints_.insert (entry);
161 return id;
164 Control_Action*
165 Monitor_Base::remove_constraint (const long constraint_id)
167 Control_Action* retval = 0;
170 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0);
172 CONSTRAINT_ITERATOR i = this->constraints_.find (constraint_id);
174 if (i != this->constraints_.end ())
176 retval = i->second.control_action;
177 (void) this->constraints_.erase (constraint_id);
181 return retval;
184 void
185 Monitor_Base::retrieve (Monitor_Control_Types::Data& data) const
187 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
189 data = this->data_;
192 void
193 Monitor_Base::clear (void)
195 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
197 this->clear_i ();
200 void
201 Monitor_Base::retrieve_and_clear (Monitor_Control_Types::Data& data)
203 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
205 data = this->data_;
206 this->clear_i ();
209 void
210 Monitor_Base::add_to_registry (const ACE_Time_Value& time)
212 MC_ADMINMANAGER *mgr =
213 ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
215 if (!mgr->admin ().monitor_point (this, time))
217 ACELIB_ERROR ((LM_ERROR,
218 "monitor point %s registration failed\n",
219 this->name ()));
223 void
224 Monitor_Base::remove_from_registry (void)
226 if (!Monitor_Point_Registry::instance ()->remove (this->name ()))
228 // (JP) There is a problem with this failing on a single ACE_Message_Queue
229 // monitor per process. I think it is the message queue associated
230 // with the default reactor, maybe because at that low level, ACE
231 // is using malloc with placement, then free, which may bypass the
232 // normal destructors. In any case, it happens only at shutdown
233 // and there seems to be no memory leak.
234 // ACELIB_ERROR ((LM_ERROR,
235 // "monitor point %s unregistration failed\n",
236 // this->name ()));
240 double
241 Monitor_Base::average (void) const
243 if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
244 || this->data_.type_ == Monitor_Control_Types::MC_GROUP
245 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
247 ACELIB_ERROR_RETURN ((LM_ERROR,
248 ACE_TEXT ("average: %s is wrong monitor type\n"),
249 this->name_.c_str ()),
253 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
255 return (this->data_.index_== 0UL
256 ? 0.0
257 : this->data_.sum_ / this->data_.index_);
260 double
261 Monitor_Base::sum_of_squares (void) const
263 if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
264 || this->data_.type_ == Monitor_Control_Types::MC_GROUP
265 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
267 ACELIB_ERROR_RETURN ((LM_ERROR,
268 ACE_TEXT ("sum_of_squares: %s ")
269 ACE_TEXT ("is wrong monitor type\n"),
270 this->name_.c_str ()),
274 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
276 return this->data_.sum_of_squares_;
279 size_t
280 Monitor_Base::count (void) const
282 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP)
284 ACELIB_ERROR_RETURN ((LM_ERROR,
285 ACE_TEXT ("count: %s is a monitor group\n"),
286 this->name_.c_str ()),
287 0UL);
290 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0UL);
292 return (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
293 ? static_cast<size_t> (this->data_.last_)
294 : this->data_.index_);
297 double
298 Monitor_Base::minimum_sample (void) const
300 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
301 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
303 ACELIB_ERROR_RETURN ((LM_ERROR,
304 ACE_TEXT ("minimum_sample: %s ")
305 ACE_TEXT ("is wrong monitor type\n"),
306 this->name_.c_str ()),
310 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
312 return this->data_.minimum_;
315 double
316 Monitor_Base::maximum_sample (void) const
318 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
319 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
321 ACELIB_ERROR_RETURN ((LM_ERROR,
322 ACE_TEXT ("maximum_sample: %s ")
323 ACE_TEXT ("is wrong monitor type\n"),
324 this->name_.c_str ()),
328 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
330 return this->data_.maximum_;
333 double
334 Monitor_Base::last_sample (void) const
336 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
337 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
339 ACELIB_ERROR_RETURN ((LM_ERROR,
340 ACE_TEXT ("last_sample: %s ")
341 ACE_TEXT ("is wrong monitor type\n"),
342 this->name_.c_str ()),
346 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
348 return this->data_.last_;
351 Monitor_Control_Types::NameList
352 Monitor_Base::get_list (void) const
354 Monitor_Control_Types::NameList retval;
356 if (this->data_.type_ != Monitor_Control_Types::MC_LIST)
358 ACELIB_ERROR ((LM_ERROR,
359 ACE_TEXT ("get_list: %s is not a ")
360 ACE_TEXT ("list monitor type\n"),
361 this->name_.c_str ()));
363 return retval;
366 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, retval);
368 for (size_t i = 0UL; i < this->data_.index_; ++i)
370 retval.push_back (this->data_.list_[i]);
373 return retval;
376 void
377 Monitor_Base::clear_i (void)
379 if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
381 for (size_t i = 0UL; i < this->data_.index_; ++i)
383 ACE::strdelete (this->data_.list_[i]);
386 this->data_.list_.max_size (0UL);
389 this->data_.value_ = 0.0;
390 this->data_.timestamp_ = ACE_Time_Value::zero;
391 this->data_.index_ = 0UL;
392 this->data_.minimum_set_ = false;
393 this->data_.minimum_ = 0.0;
394 this->data_.maximum_ = 0.0;
395 this->data_.sum_ = 0.0;
396 this->data_.sum_of_squares_ = 0.0;
397 this->data_.last_ = 0.0;
402 ACE_END_VERSIONED_NAMESPACE_DECL
404 #endif /* ACE_HAS_MONITOR_FRAMEWORK==1 */