00001 // $Id: MonitoredQuantity.h,v 1.10 2011/03/07 15:31:32 mommsen Exp $ 00003 00004 #ifndef EventFilter_StorageManager_MonitoredQuantity_h 00005 #define EventFilter_StorageManager_MonitoredQuantity_h 00006 00007 #include "boost/shared_ptr.hpp" 00008 #include "boost/thread/mutex.hpp" 00009 00010 #include <math.h> 00011 #include <stdint.h> 00012 #include <vector> 00013 00014 #include "EventFilter/StorageManager/interface/Utils.h" 00015 00016 00017 namespace stor 00018 { 00019 00029 class MonitoredQuantity 00030 { 00031 00032 public: 00033 class Stats; 00034 00035 enum DataSetType { FULL = 0, // the full data set (all samples) 00036 RECENT = 1 }; // recent data only 00037 00038 explicit MonitoredQuantity 00039 ( 00040 utils::Duration_t expectedCalculationInterval, 00041 utils::Duration_t timeWindowForRecentResults 00042 ); 00043 00047 void addSample(const double& value = 1); 00048 00052 void addSample(const int& value = 1); 00053 00057 void addSample(const unsigned int& value = 1); 00058 00062 void addSample(const long& value = 1); 00063 00067 void addSample(const unsigned long& value = 1); 00068 00072 void addSample(const long long& value = 1); 00073 00077 void addSample(const unsigned long long& value = 1); 00078 00083 void addSampleIfLarger(const double& value); 00084 00092 void calculateStatistics(const utils::TimePoint_t& currentTime = 00093 utils::getCurrentTime()); 00094 00099 void reset(); 00100 00105 void enable(); 00106 00110 void disable(); 00111 00115 bool isEnabled() const {return enabled_;} 00116 00121 void setNewTimeWindowForRecentResults(const utils::Duration_t& interval); 00122 00131 utils::Duration_t getTimeWindowForRecentResults() const 00132 { 00133 return intervalForRecentStats_; 00134 } 00135 00136 utils::Duration_t ExpectedCalculationInterval() const 00137 { 00138 return expectedCalculationInterval_; 00139 } 00140 00144 void getStats(Stats& stats) const; 00145 00146 private: 00147 00148 // Prevent copying of the MonitoredQuantity 00149 MonitoredQuantity(MonitoredQuantity const&); 00150 MonitoredQuantity& operator=(MonitoredQuantity const&); 00151 00152 // Helper functions. 00153 void resetAccumulators(); 00154 void resetResults(); 00155 00156 utils::TimePoint_t lastCalculationTime_; 00157 uint64_t workingSampleCount_; 00158 double workingValueSum_; 00159 double workingValueSumOfSquares_; 00160 double workingValueMin_; 00161 double workingValueMax_; 00162 double workingLastSampleValue_; 00163 00164 mutable boost::mutex accumulationMutex_; 00165 00166 uint32_t binCount_; 00167 uint32_t workingBinId_; 00168 std::vector<uint64_t> binSampleCount_; 00169 std::vector<double> binValueSum_; 00170 std::vector<double> binValueSumOfSquares_; 00171 std::vector<double> binValueMin_; 00172 std::vector<double> binValueMax_; 00173 std::vector<utils::Duration_t> binDuration_; 00174 std::vector<utils::TimePoint_t> binSnapshotTime_; 00175 00176 uint64_t fullSampleCount_; 00177 double fullSampleRate_; 00178 double fullValueSum_; 00179 double fullValueSumOfSquares_; 00180 double fullValueAverage_; 00181 double fullValueRMS_; 00182 double fullValueMin_; 00183 double fullValueMax_; 00184 double fullValueRate_; 00185 utils::Duration_t fullDuration_; 00186 00187 uint64_t recentSampleCount_; 00188 double recentSampleRate_; 00189 double recentValueSum_; 00190 double recentValueSumOfSquares_; 00191 double recentValueAverage_; 00192 double recentValueRMS_; 00193 double recentValueMin_; 00194 double recentValueMax_; 00195 double recentValueRate_; 00196 utils::Duration_t recentDuration_; 00197 double lastLatchedSampleValue_; 00198 double lastLatchedValueRate_; 00199 00200 mutable boost::mutex resultsMutex_; 00201 00202 bool enabled_; 00203 utils::Duration_t intervalForRecentStats_; // seconds 00204 const utils::Duration_t expectedCalculationInterval_; // seconds 00205 }; 00206 00207 struct MonitoredQuantity::Stats 00208 { 00209 uint64_t fullSampleCount; 00210 double fullSampleRate; 00211 double fullValueSum; 00212 double fullValueSumOfSquares; 00213 double fullValueAverage; 00214 double fullValueRMS; 00215 double fullValueMin; 00216 double fullValueMax; 00217 double fullValueRate; 00218 double fullSampleLatency; 00219 utils::Duration_t fullDuration; 00220 00221 uint64_t recentSampleCount; 00222 double recentSampleRate; 00223 double recentValueSum; 00224 double recentValueSumOfSquares; 00225 double recentValueAverage; 00226 double recentValueRMS; 00227 double recentValueMin; 00228 double recentValueMax; 00229 double recentValueRate; 00230 double recentSampleLatency; 00231 utils::Duration_t recentDuration; 00232 std::vector<uint64_t> recentBinnedSampleCounts; 00233 std::vector<double> recentBinnedValueSums; 00234 std::vector<utils::Duration_t> recentBinnedDurations; 00235 std::vector<utils::TimePoint_t> recentBinnedSnapshotTimes; 00236 00237 double lastSampleValue; 00238 double lastValueRate; 00239 bool enabled; 00240 00241 uint64_t getSampleCount(DataSetType t = FULL) const { return t == RECENT ? recentSampleCount : fullSampleCount; } 00242 double getValueSum(DataSetType t = FULL) const { return t == RECENT ? recentValueSum : fullValueSum; } 00243 double getValueAverage(DataSetType t = FULL) const { return t == RECENT ? recentValueAverage : fullValueAverage; } 00244 double getValueRate(DataSetType t = FULL) const { return t== RECENT ? recentValueRate : fullValueRate; } 00245 double getValueRMS(DataSetType t = FULL) const { return t == RECENT ? recentValueRMS : fullValueRMS; } 00246 double getValueMin(DataSetType t = FULL) const { return t == RECENT ? recentValueMin : fullValueMin; } 00247 double getValueMax(DataSetType t = FULL) const { return t == RECENT ? recentValueMax : fullValueMax; } 00248 utils::Duration_t getDuration(DataSetType t = FULL) const { return t == RECENT ? recentDuration : fullDuration; } 00249 double getSampleRate(DataSetType t = FULL) const { return t == RECENT ? recentSampleRate : fullSampleRate; } 00250 double getSampleLatency(DataSetType t = FULL) const { double v=getSampleRate(t); return v ? 1e6/v : INFINITY;} 00251 double getLastSampleValue() const { return lastSampleValue; } 00252 double getLastValueRate() const { return lastValueRate; } 00253 bool isEnabled() const { return enabled; } 00254 }; 00255 00256 typedef boost::shared_ptr<MonitoredQuantity> MonitoredQuantityPtr; 00257 00258 } // namespace stor 00259 00260 #endif // EventFilter_StorageManager_MonitoredQuantity_h 00261 00262 00263