00001
00002
00003 #include "ace/Monitor_Base.h"
00004
00005 #if defined (ACE_HAS_MONITOR_FRAMEWORK) && (ACE_HAS_MONITOR_FRAMEWORK == 1)
00006
00007 #include "ace/Monitor_Admin_Manager.h"
00008 #include "ace/Monitor_Control_Action.h"
00009 #include "ace/Monitor_Point_Registry.h"
00010 #include "ace/Guard_T.h"
00011 #include "ace/Dynamic_Service.h"
00012 #include "ace/OS_NS_sys_time.h"
00013
00014 #if !defined (__ACE_INLINE__)
00015 #include "ace/Monitor_Base.inl"
00016 #endif
00017
00018 ACE_BEGIN_VERSIONED_NAMESPACE_DECL
00019
00020 namespace ACE
00021 {
00022 namespace Monitor_Control
00023 {
00024 Monitor_Base::Monitor_Base (const char* name,
00025 Monitor_Control_Types::Information_Type type)
00026 : ACE_Refcountable_T<ACE_SYNCH_MUTEX> (1)
00027 , data_ (type)
00028 , name_ (name)
00029 {
00030 }
00031
00032 Monitor_Base::~Monitor_Base (void)
00033 {
00034 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00035
00036 if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
00037 {
00038 for (size_t i = 0UL; i < this->data_.index_; ++i)
00039 {
00040 delete [] this->data_.list_[i];
00041 }
00042 }
00043 }
00044
00045 void
00046 Monitor_Base::update (void)
00047 {
00048
00049 }
00050
00051 void
00052 Monitor_Base::receive (double data)
00053 {
00054 if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
00055 {
00056 ACE_ERROR ((LM_ERROR,
00057 ACE_TEXT ("receive: can't store numeric value - ")
00058 ACE_TEXT ("%s is a string type monitor\n"),
00059 this->name_.c_str ()));
00060 return;
00061 }
00062
00063 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00064 this->data_.timestamp_ = ACE_OS::gettimeofday ();
00065 this->data_.value_ = data;
00066
00067 if (this->data_.type_ != Monitor_Control_Types::MC_COUNTER)
00068 {
00069 this->data_.sum_ += data;
00070 this->data_.sum_of_squares_ += (data * data);
00071 ++this->data_.index_;
00072 }
00073
00074 if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER)
00075 {
00076 ++this->data_.last_;
00077 this->data_.maximum_ = this->data_.last_;
00078 }
00079 else
00080 {
00081 this->data_.last_ = data;
00082
00083 if (!this->data_.minimum_set_)
00084 {
00085 this->data_.minimum_set_ = true;
00086 this->data_.minimum_ = data;
00087 }
00088 else if (this->data_.minimum_ > data)
00089 {
00090 this->data_.minimum_ = data;
00091 }
00092
00093 if (this->data_.maximum_ < data)
00094 {
00095 this->data_.maximum_ = data;
00096 }
00097 }
00098 }
00099
00100 void
00101 Monitor_Base::receive (size_t data)
00102 {
00103 this->receive (static_cast<double> (data));
00104 }
00105
00106 void
00107 Monitor_Base::receive (const Monitor_Control_Types::NameList& data)
00108 {
00109 if (this->data_.type_ != Monitor_Control_Types::MC_LIST)
00110 {
00111 ACE_ERROR ((LM_ERROR,
00112 ACE_TEXT ("receive: can't store string values - ")
00113 ACE_TEXT ("%s is a numeric type monitor\n"),
00114 this->name_.c_str ()));
00115 return;
00116 }
00117
00118 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00119
00120 for (size_t i = 0UL; i < this->data_.index_; ++i)
00121 {
00122 ACE::strdelete (this->data_.list_[i]);
00123 }
00124
00125 this->data_.index_ = data.size ();
00126 this->data_.list_.max_size (this->data_.index_);
00127
00128 for (size_t i = 0UL; i < this->data_.index_; ++i)
00129 {
00130 this->data_.list_[i] = ACE::strnew (data[i].c_str ());
00131 }
00132 }
00133
00134 long
00135 Monitor_Base::add_constraint (const char* expression,
00136 Control_Action* action)
00137 {
00138
00139 long id = Monitor_Point_Registry::instance ()->constraint_id ();
00140
00141 CONSTRAINTS::value_type entry;
00142 entry.first = id;
00143 entry.second.expr = expression;
00144 entry.second.control_action = action;
00145
00146
00147
00148 action->add_ref ();
00149
00150 {
00151 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, -1);
00152
00153
00154
00155 (void) this->constraints_.insert (entry);
00156 }
00157
00158 return id;
00159 }
00160
00161 Control_Action*
00162 Monitor_Base::remove_constraint (const long constraint_id)
00163 {
00164 Control_Action* retval = 0;
00165
00166 {
00167 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0);
00168
00169 CONSTRAINT_ITERATOR i = this->constraints_.find (constraint_id);
00170
00171 if (i != this->constraints_.end ())
00172 {
00173 retval = i->second.control_action;
00174 (void) this->constraints_.erase (constraint_id);
00175 }
00176 }
00177
00178 return retval;
00179 }
00180
00181 void
00182 Monitor_Base::retrieve (Monitor_Control_Types::Data& data) const
00183 {
00184 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00185
00186 data = this->data_;
00187 }
00188
00189 void
00190 Monitor_Base::clear (void)
00191 {
00192 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00193
00194 this->clear_i ();
00195 }
00196
00197 void
00198 Monitor_Base::retrieve_and_clear (Monitor_Control_Types::Data& data)
00199 {
00200 ACE_GUARD (ACE_SYNCH_MUTEX, guard, this->mutex_);
00201
00202 data = this->data_;
00203 this->clear_i ();
00204 }
00205
00206 void
00207 Monitor_Base::add_to_registry (const ACE_Time_Value& time)
00208 {
00209 MC_ADMINMANAGER *mgr =
00210 ACE_Dynamic_Service<MC_ADMINMANAGER>::instance ("MC_ADMINMANAGER");
00211
00212 if (!mgr->admin ().monitor_point (this, time))
00213 {
00214 ACE_ERROR ((LM_ERROR,
00215 "monitor point %s registration failed\n",
00216 this->name ()));
00217 }
00218 }
00219
00220 void
00221 Monitor_Base::remove_from_registry (void)
00222 {
00223 if (!Monitor_Point_Registry::instance ()->remove (this->name ()))
00224 {
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234 }
00235 }
00236
00237 double
00238 Monitor_Base::average (void) const
00239 {
00240 if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
00241 || this->data_.type_ == Monitor_Control_Types::MC_GROUP
00242 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
00243 {
00244 ACE_ERROR_RETURN ((LM_ERROR,
00245 ACE_TEXT ("average: %s is wrong monitor type\n"),
00246 this->name_.c_str ()),
00247 0);
00248 }
00249
00250 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
00251
00252 return (this->data_.index_== 0UL
00253 ? 0.0
00254 : this->data_.sum_ / this->data_.index_);
00255 }
00256
00257 double
00258 Monitor_Base::sum_of_squares (void) const
00259 {
00260 if (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
00261 || this->data_.type_ == Monitor_Control_Types::MC_GROUP
00262 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
00263 {
00264 ACE_ERROR_RETURN ((LM_ERROR,
00265 ACE_TEXT ("sum_of_squares: %s ")
00266 ACE_TEXT ("is wrong monitor type\n"),
00267 this->name_.c_str ()),
00268 0);
00269 }
00270
00271 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
00272
00273 return this->data_.sum_of_squares_;
00274 }
00275
00276 size_t
00277 Monitor_Base::count (void) const
00278 {
00279 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP)
00280 {
00281 ACE_ERROR_RETURN ((LM_ERROR,
00282 ACE_TEXT ("count: %s is a monitor group\n"),
00283 this->name_.c_str ()),
00284 0UL);
00285 }
00286
00287 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0UL);
00288
00289 return (this->data_.type_ == Monitor_Control_Types::MC_COUNTER
00290 ? static_cast<size_t> (this->data_.last_)
00291 : this->data_.index_);
00292 }
00293
00294 double
00295 Monitor_Base::minimum_sample (void) const
00296 {
00297 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
00298 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
00299 {
00300 ACE_ERROR_RETURN ((LM_ERROR,
00301 ACE_TEXT ("minimum_sample: %s ")
00302 ACE_TEXT ("is wrong monitor type\n"),
00303 this->name_.c_str ()),
00304 0);
00305 }
00306
00307 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
00308
00309 return this->data_.minimum_;
00310 }
00311
00312 double
00313 Monitor_Base::maximum_sample (void) const
00314 {
00315 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
00316 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
00317 {
00318 ACE_ERROR_RETURN ((LM_ERROR,
00319 ACE_TEXT ("maximum_sample: %s ")
00320 ACE_TEXT ("is wrong monitor type\n"),
00321 this->name_.c_str ()),
00322 0);
00323 }
00324
00325 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
00326
00327 return this->data_.maximum_;
00328 }
00329
00330 double
00331 Monitor_Base::last_sample (void) const
00332 {
00333 if (this->data_.type_ == Monitor_Control_Types::MC_GROUP
00334 || this->data_.type_ == Monitor_Control_Types::MC_LIST)
00335 {
00336 ACE_ERROR_RETURN ((LM_ERROR,
00337 ACE_TEXT ("last_sample: %s ")
00338 ACE_TEXT ("is wrong monitor type\n"),
00339 this->name_.c_str ()),
00340 0);
00341 }
00342
00343 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, 0.0);
00344
00345 return this->data_.last_;
00346 }
00347
00348 Monitor_Control_Types::NameList
00349 Monitor_Base::get_list (void) const
00350 {
00351 Monitor_Control_Types::NameList retval;
00352
00353 if (this->data_.type_ != Monitor_Control_Types::MC_LIST)
00354 {
00355 ACE_ERROR ((LM_ERROR,
00356 ACE_TEXT ("get_list: %s is not a ")
00357 ACE_TEXT ("list monitor type\n"),
00358 this->name_.c_str ()));
00359
00360 return retval;
00361 }
00362
00363 ACE_GUARD_RETURN (ACE_SYNCH_MUTEX, guard, this->mutex_, retval);
00364
00365 for (size_t i = 0UL; i < this->data_.index_; ++i)
00366 {
00367 retval.push_back (this->data_.list_[i]);
00368 }
00369
00370 return retval;
00371 }
00372
00373 void
00374 Monitor_Base::clear_i (void)
00375 {
00376 if (this->data_.type_ == Monitor_Control_Types::MC_LIST)
00377 {
00378 for (size_t i = 0UL; i < this->data_.index_; ++i)
00379 {
00380 ACE::strdelete (this->data_.list_[i]);
00381 }
00382
00383 this->data_.list_.max_size (0UL);
00384 }
00385
00386 this->data_.value_ = 0.0;
00387 this->data_.timestamp_ = ACE_Time_Value::zero;
00388 this->data_.index_ = 0UL;
00389 this->data_.minimum_set_ = false;
00390 this->data_.minimum_ = 0.0;
00391 this->data_.maximum_ = 0.0;
00392 this->data_.sum_ = 0.0;
00393 this->data_.sum_of_squares_ = 0.0;
00394 this->data_.last_ = 0.0;
00395 }
00396 }
00397 }
00398
00399 ACE_END_VERSIONED_NAMESPACE_DECL
00400
00401 #endif
00402