#include <EventFilter/StorageManager/interface/RollingSampleCounter.h>
Public Types | |
enum | AccumulationStyle { INCLUDE_SAMPLES_AFTER_BINNING = 0, INCLUDE_SAMPLES_IMMEDIATELY = 1 } |
Public Member Functions | |
void | addSample (double value=1.0, double currentTime=getCurrentTime()) |
Adds the specified sample value to the counter instance. | |
void | dumpData (std::ostream &outStream) |
Dumps the contents of the counter to the specified output stream. | |
double | getDuration (double currentTime=0.0) |
Returns the amount of time (in seconds) that has elapsed between the time that the earliest sample in the counter was stored and the time that the latest was stored. | |
int | getSampleCount () |
Returns the number of samples stored in the counter. | |
double | getSampleRate (double currentTime=0.0) |
Returns the rate of samples stored in the counter (number of samples divided by duration) or 0.0 if no samples have been stored. | |
double | getValueAverage () |
Returns the average value of the samples that have been stored in the counter or 0.0 if no samples have been added. | |
double | getValueRate () |
Returns the sample value rate (the sum of all sample values stored in the counter divided by the duration) or 0.0 if no samples have been stored. | |
double | getValueSum () |
Returns the sum of all sample values stored in the counter. | |
bool | hasValidResult () |
Tests if the counter has a valid result (enough samples have been stored). | |
RollingSampleCounter (int windowSize=-1, int binSize=-1, int validSubWindowSize=-1, AccumulationStyle style=INCLUDE_SAMPLES_AFTER_BINNING) | |
Constructor. | |
Private Member Functions | |
long long | getBinId (long long sampleCount) |
Calculates the bin ID for the given sample count. | |
void | shuffleBins (long long sampleCount) |
Modifies the internal list of bins so that it correctly reflects the current window. | |
Private Attributes | |
AccumulationStyle | accumStyle_ |
boost::shared_ptr< std::vector < double > > | binContents_ |
int | binCount_ |
int | binSize_ |
boost::shared_ptr< std::vector < double > > | binStartTimes_ |
boost::shared_ptr< std::vector < double > > | binStopTimes_ |
double | currentTotal_ |
boost::recursive_mutex | dataMutex_ |
long long | processedBinCount_ |
long long | sampleCount_ |
int | validBinCount_ |
int | windowSize_ |
long long | workingBinId_ |
double | workingBinStartTime_ |
double | workingBinSum_ |
Definition at line 19 of file RollingSampleCounter.h.
Definition at line 24 of file RollingSampleCounter.h.
00024 { INCLUDE_SAMPLES_AFTER_BINNING = 0, 00025 INCLUDE_SAMPLES_IMMEDIATELY = 1 };
RollingSampleCounter::RollingSampleCounter | ( | int | windowSize = -1 , |
|
int | binSize = -1 , |
|||
int | validSubWindowSize = -1 , |
|||
AccumulationStyle | style = INCLUDE_SAMPLES_AFTER_BINNING | |||
) |
Constructor.
Definition at line 13 of file RollingSampleCounter.cc.
References binContents_, binCount_, binSize_, binStartTimes_, binStopTimes_, getBinId(), INCLUDE_SAMPLES_IMMEDIATELY, int, processedBinCount_, validBinCount_, windowSize_, and workingBinId_.
00014 : 00015 accumStyle_(style), 00016 sampleCount_(0), 00017 processedBinCount_(0), 00018 workingBinSum_(0.0), 00019 workingBinStartTime_(0.0), 00020 currentTotal_(0.0) 00021 { 00022 // provide a reasonable default for the window size, if needed 00023 if (windowSize <= 0) { 00024 this->windowSize_ = 100; // 100 samples 00025 } 00026 else { 00027 this->windowSize_ = windowSize; 00028 } 00029 00030 // provide a reasonable default for the bin size, if needed 00031 if (binSize <= 0) { 00032 this->binSize_ = this->windowSize_; 00033 } 00034 else { 00035 this->binSize_ = binSize; 00036 } 00037 00038 // determine the bin count from the window and bin sizes 00039 if (this->windowSize_ > 0 && this->binSize_ > 0) { 00040 this->binCount_ = 00041 (int) (0.5 + ((double) this->windowSize_ / (double) this->binSize_)); 00042 if (this->binCount_ < 1) { 00043 this->binCount_ = 1; 00044 } 00045 00046 // recalculate the window size to handle rounding 00047 this->windowSize_ = this->binCount_ * this->binSize_; 00048 } 00049 else { 00050 this->binCount_ = 1; 00051 } 00052 00053 // determine the number of bins needed for a valid result 00054 this->validBinCount_ = 00055 (int) (0.5 + ((double) validSubWindowSize / (double) this->binSize_)); 00056 if (this->validBinCount_ <= 0) { 00057 this->validBinCount_ = 1; 00058 } 00059 00060 // if working with data immediately, assume the first bin is good 00061 if (this->accumStyle_ == INCLUDE_SAMPLES_IMMEDIATELY) { 00062 processedBinCount_ = 1; 00063 } 00064 00065 // initialize times and bins 00066 this->workingBinId_ = getBinId(this->sampleCount_); 00067 this->binStartTimes_.reset(new std::vector<double>); 00068 this->binStopTimes_.reset(new std::vector<double>); 00069 this->binContents_.reset(new std::vector<double>); 00070 for (int idx = 0; idx < this->binCount_; ++idx) { 00071 this->binStartTimes_->push_back(0.0); 00072 this->binStopTimes_->push_back(0.0); 00073 this->binContents_->push_back(0.0); 00074 } 00075 }
void RollingSampleCounter::addSample | ( | double | value = 1.0 , |
|
double | currentTime = getCurrentTime() | |||
) |
Adds the specified sample value to the counter instance.
Definition at line 80 of file RollingSampleCounter.cc.
References accumStyle_, binCount_, currentTotal_, dataMutex_, INCLUDE_SAMPLES_AFTER_BINNING, int, sampleCount_, shuffleBins(), sl, workingBinId_, workingBinStartTime_, and workingBinSum_.
00081 { 00082 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00083 00084 // shuffle the bins so that they correctly reflect the sample window 00085 shuffleBins(sampleCount_); 00086 ++sampleCount_; 00087 00088 // add the new sample to either the working sum or the current 00089 // sum depending on the accumulation style 00090 if (accumStyle_ == INCLUDE_SAMPLES_AFTER_BINNING) { 00091 workingBinSum_ += value; 00092 if (sampleCount_ == 1) { 00093 workingBinStartTime_ = currentTime; 00094 } 00095 } 00096 else { 00097 int binIndex = (int) (workingBinId_ % binCount_); 00098 (*binContents_)[binIndex] += value; 00099 currentTotal_ += value; 00100 if (sampleCount_ == 1) { 00101 (*binStartTimes_)[binIndex] = currentTime; 00102 } 00103 (*binStopTimes_)[binIndex] = currentTime; 00104 } 00105 }
void RollingSampleCounter::dumpData | ( | std::ostream & | outStream | ) |
Dumps the contents of the counter to the specified output stream.
Definition at line 253 of file RollingSampleCounter.cc.
References binContents_, binCount_, binSize_, binStartTimes_, binStopTimes_, currentTotal_, lat::endl(), stor::BaseCounter::getCurrentTime(), int, processedBinCount_, sampleCount_, windowSize_, workingBinId_, and workingBinSum_.
00254 { 00255 outStream << "RollingSampleCounter 0x" << std::hex 00256 << ((int) this) << std::dec << std::endl; 00257 char nowString[32]; 00258 sprintf(nowString, "%16.4f", getCurrentTime()); 00259 outStream << " Now = " << nowString << std::endl; 00260 outStream << " Window size = " << windowSize_ << std::endl; 00261 outStream << " Bin size = " << binSize_ << std::endl; 00262 outStream << " Sample count = " << sampleCount_ << std::endl; 00263 outStream << " Processed bin count = " << processedBinCount_ << std::endl; 00264 outStream << " Working index = " 00265 << ((int) (workingBinId_ % binCount_)) << std::endl; 00266 outStream << " Working value = " << workingBinSum_ << std::endl; 00267 outStream << " Current total = " << currentTotal_ << std::endl; 00268 00269 char binString[200]; 00270 for (int idx = 0; idx < binCount_; idx++) { 00271 sprintf(binString, 00272 " bin %2d, value %10.2f, startTime %13.2f, stopTime %13.2f", 00273 idx, (*binContents_)[idx], (*binStartTimes_)[idx], 00274 (*binStopTimes_)[idx]); 00275 outStream << binString << std::endl; 00276 } 00277 }
long long RollingSampleCounter::getBinId | ( | long long | sampleCount | ) | [private] |
Calculates the bin ID for the given sample count.
Definition at line 329 of file RollingSampleCounter.cc.
References binSize_.
Referenced by RollingSampleCounter(), and shuffleBins().
00330 { 00331 return (long long) (sampleCount / (long long) binSize_); 00332 }
double RollingSampleCounter::getDuration | ( | double | currentTime = 0.0 |
) |
Returns the amount of time (in seconds) that has elapsed between the time that the earliest sample in the counter was stored and the time that the latest was stored.
Since old samples are discarded to make room for new ones, this does *not* correspond to the time since the first sample was added.
15-Apr-2008, KAB: added time argument. If it is non-zero, then the result of this method is the time between the earliest sample and the specified time.
Definition at line 211 of file RollingSampleCounter.cc.
References accumStyle_, binCount_, binStartTimes_, binStopTimes_, dataMutex_, hasValidResult(), INCLUDE_SAMPLES_IMMEDIATELY, int, processedBinCount_, sl, and workingBinId_.
Referenced by getSampleRate(), and getValueRate().
00212 { 00213 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00214 00215 //if (processedBinCount_ < 1 || ! hasValidResult()) { 00216 if (! hasValidResult()) { 00217 return 0.0; 00218 } 00219 else { 00220 int actualBinCount = binCount_; 00221 if (processedBinCount_ < binCount_) { 00222 actualBinCount = (int) processedBinCount_; 00223 } 00224 long long startBinId = workingBinId_ - actualBinCount; 00225 long long stopBinId = workingBinId_ - 1; 00226 if (accumStyle_ == RollingSampleCounter::INCLUDE_SAMPLES_IMMEDIATELY) { 00227 ++startBinId; 00228 ++stopBinId; 00229 } 00230 00231 int startBinIndex = (int) (startBinId % binCount_); 00232 int stopBinIndex = (int) (stopBinId % binCount_); 00233 00234 if ((*binStopTimes_)[stopBinIndex] > 0.0 && 00235 (*binStartTimes_)[startBinIndex] > 0.0) { 00236 if (currentTime <= 0.0) { 00237 return ((*binStopTimes_)[stopBinIndex] - 00238 (*binStartTimes_)[startBinIndex]); 00239 } 00240 else { 00241 return (currentTime - (*binStartTimes_)[startBinIndex]); 00242 } 00243 } 00244 else { 00245 return 0.0; 00246 } 00247 } 00248 }
int RollingSampleCounter::getSampleCount | ( | ) |
Returns the number of samples stored in the counter.
This value will stop increasing when the full window size is reached.
Definition at line 122 of file RollingSampleCounter.cc.
References accumStyle_, binSize_, dataMutex_, INCLUDE_SAMPLES_IMMEDIATELY, int, HLT_VtxMuL3::result, sampleCount_, sl, and windowSize_.
Referenced by getSampleRate(), and getValueAverage().
00123 { 00124 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00125 00126 int result = windowSize_; 00127 if (sampleCount_ < windowSize_) { 00128 result = (int) sampleCount_; 00129 } 00130 else if (accumStyle_ == INCLUDE_SAMPLES_IMMEDIATELY && binSize_ > 1) { 00131 // NEED TO FIX!!! 00132 result = 1 + (int) ((sampleCount_ - 1) % windowSize_); 00133 } 00134 00135 return result; 00136 }
double RollingSampleCounter::getSampleRate | ( | double | currentTime = 0.0 |
) |
Returns the rate of samples stored in the counter (number of samples divided by duration) or 0.0 if no samples have been stored.
The units of the return value are samples per second.
Definition at line 143 of file RollingSampleCounter.cc.
References dataMutex_, getDuration(), getSampleCount(), and sl.
00144 { 00145 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00146 00147 double duration = getDuration(currentTime); 00148 if (duration > 0.0) { 00149 return ((double) getSampleCount()) / duration; 00150 } 00151 else { 00152 return 0.0; 00153 } 00154 }
double RollingSampleCounter::getValueAverage | ( | ) |
Returns the average value of the samples that have been stored in the counter or 0.0 if no samples have been added.
Definition at line 169 of file RollingSampleCounter.cc.
References currentTotal_, dataMutex_, getSampleCount(), and sl.
00170 { 00171 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00172 00173 if (getSampleCount() > 0) { 00174 return ((double) currentTotal_) / ((double) getSampleCount()); 00175 } 00176 else { 00177 return 0.0; 00178 } 00179 }
double RollingSampleCounter::getValueRate | ( | ) |
Returns the sample value rate (the sum of all sample values stored in the counter divided by the duration) or 0.0 if no samples have been stored.
The units of the return value are [the units of the sample values] per second (e.g. MByte/sec).
Definition at line 187 of file RollingSampleCounter.cc.
References currentTotal_, dataMutex_, getDuration(), and sl.
00188 { 00189 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00190 00191 double duration = getDuration(); 00192 if (duration > 0.0) { 00193 return ((double) currentTotal_) / duration; 00194 } 00195 else { 00196 return 0.0; 00197 } 00198 }
double RollingSampleCounter::getValueSum | ( | ) |
Returns the sum of all sample values stored in the counter.
Definition at line 159 of file RollingSampleCounter.cc.
References currentTotal_, dataMutex_, and sl.
00159 { 00160 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00161 00162 return currentTotal_; 00163 }
bool RollingSampleCounter::hasValidResult | ( | ) |
Tests if the counter has a valid result (enough samples have been stored).
Definition at line 111 of file RollingSampleCounter.cc.
References dataMutex_, processedBinCount_, sl, and validBinCount_.
Referenced by getDuration().
00112 { 00113 boost::recursive_mutex::scoped_lock sl(dataMutex_); 00114 00115 return (processedBinCount_ >= validBinCount_); 00116 }
void RollingSampleCounter::shuffleBins | ( | long long | sampleCount | ) | [private] |
Modifies the internal list of bins so that it correctly reflects the current window.
Definition at line 283 of file RollingSampleCounter.cc.
References accumStyle_, binCount_, currentTotal_, getBinId(), stor::BaseCounter::getCurrentTime(), INCLUDE_SAMPLES_AFTER_BINNING, int, processedBinCount_, workingBinId_, workingBinStartTime_, and workingBinSum_.
Referenced by addSample().
00284 { 00285 // determine the current bin 00286 long long currentBinId = getBinId(sampleCount); 00287 //std::cout << "Shuffle " << workingBinId_ << " " 00288 // << currentBinId << std::endl; 00289 00290 // if we're still working on the current bin, no shuffling is needed 00291 if (currentBinId == workingBinId_) { 00292 return; 00293 } 00294 ++processedBinCount_; 00295 double now = getCurrentTime(); 00296 00297 // handle the different accumulation styles 00298 if (accumStyle_ == INCLUDE_SAMPLES_AFTER_BINNING) { 00299 00300 // move the working bin value into the list 00301 int binIndex = (int) (workingBinId_ % binCount_); 00302 currentTotal_ -= (*binContents_)[binIndex]; 00303 (*binContents_)[binIndex] = workingBinSum_; 00304 (*binStartTimes_)[binIndex] = workingBinStartTime_; 00305 (*binStopTimes_)[binIndex] = now; 00306 currentTotal_ += workingBinSum_; 00307 workingBinSum_ = 0.0; 00308 workingBinStartTime_ = now; 00309 } 00310 00311 else { 00312 00313 // clear out the current bin value so it's ready for new data 00314 int binIndex = (int) (currentBinId % binCount_); 00315 currentTotal_ -= (*binContents_)[binIndex]; 00316 (*binContents_)[binIndex] = 0.0; 00317 00318 int binIndex2 = (int) (workingBinId_ % binCount_); 00319 (*binStartTimes_)[binIndex] = (*binStopTimes_)[binIndex2]; 00320 } 00321 00322 // update the working bin ID to the current bin 00323 workingBinId_ = currentBinId; 00324 }
Definition at line 47 of file RollingSampleCounter.h.
Referenced by addSample(), getDuration(), getSampleCount(), and shuffleBins().
boost::shared_ptr< std::vector<double> > stor::RollingSampleCounter::binContents_ [private] |
Definition at line 65 of file RollingSampleCounter.h.
Referenced by dumpData(), and RollingSampleCounter().
int stor::RollingSampleCounter::binCount_ [private] |
Definition at line 51 of file RollingSampleCounter.h.
Referenced by addSample(), dumpData(), getDuration(), RollingSampleCounter(), and shuffleBins().
int stor::RollingSampleCounter::binSize_ [private] |
Definition at line 50 of file RollingSampleCounter.h.
Referenced by dumpData(), getBinId(), getSampleCount(), and RollingSampleCounter().
boost::shared_ptr< std::vector<double> > stor::RollingSampleCounter::binStartTimes_ [private] |
Definition at line 63 of file RollingSampleCounter.h.
Referenced by dumpData(), getDuration(), and RollingSampleCounter().
boost::shared_ptr< std::vector<double> > stor::RollingSampleCounter::binStopTimes_ [private] |
Definition at line 64 of file RollingSampleCounter.h.
Referenced by dumpData(), getDuration(), and RollingSampleCounter().
double stor::RollingSampleCounter::currentTotal_ [private] |
Definition at line 61 of file RollingSampleCounter.h.
Referenced by addSample(), dumpData(), getValueAverage(), getValueRate(), getValueSum(), and shuffleBins().
boost::recursive_mutex stor::RollingSampleCounter::dataMutex_ [private] |
Definition at line 67 of file RollingSampleCounter.h.
Referenced by addSample(), getDuration(), getSampleCount(), getSampleRate(), getValueAverage(), getValueRate(), getValueSum(), and hasValidResult().
long long stor::RollingSampleCounter::processedBinCount_ [private] |
Definition at line 55 of file RollingSampleCounter.h.
Referenced by dumpData(), getDuration(), hasValidResult(), RollingSampleCounter(), and shuffleBins().
long long stor::RollingSampleCounter::sampleCount_ [private] |
Definition at line 54 of file RollingSampleCounter.h.
Referenced by addSample(), dumpData(), and getSampleCount().
Definition at line 52 of file RollingSampleCounter.h.
Referenced by hasValidResult(), and RollingSampleCounter().
int stor::RollingSampleCounter::windowSize_ [private] |
Definition at line 49 of file RollingSampleCounter.h.
Referenced by dumpData(), getSampleCount(), and RollingSampleCounter().
long long stor::RollingSampleCounter::workingBinId_ [private] |
Definition at line 59 of file RollingSampleCounter.h.
Referenced by addSample(), dumpData(), getDuration(), RollingSampleCounter(), and shuffleBins().
double stor::RollingSampleCounter::workingBinStartTime_ [private] |
double stor::RollingSampleCounter::workingBinSum_ [private] |
Definition at line 57 of file RollingSampleCounter.h.
Referenced by addSample(), dumpData(), and shuffleBins().