| 1 | |
|---|
| 2 | |
|---|
| 3 | |
|---|
| 4 | |
|---|
| 5 | |
|---|
| 6 | |
|---|
| 7 | |
|---|
| 8 | |
|---|
| 9 | |
|---|
| 10 | |
|---|
| 11 | |
|---|
| 12 | |
|---|
| 13 | |
|---|
| 14 | #include <osg/Stats> |
|---|
| 15 | #include <osg/Notify> |
|---|
| 16 | |
|---|
| 17 | using namespace osg; |
|---|
| 18 | |
|---|
| 19 | Stats::Stats(const std::string& name): |
|---|
| 20 | _name(name) |
|---|
| 21 | { |
|---|
| 22 | allocate(25); |
|---|
| 23 | } |
|---|
| 24 | |
|---|
| 25 | |
|---|
| 26 | Stats::Stats(const std::string& name, unsigned int numberOfFrames): |
|---|
| 27 | _name(name) |
|---|
| 28 | { |
|---|
| 29 | allocate(numberOfFrames); |
|---|
| 30 | } |
|---|
| 31 | |
|---|
| 32 | void Stats::allocate(unsigned int numberOfFrames) |
|---|
| 33 | { |
|---|
| 34 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 35 | |
|---|
| 36 | _baseFrameNumber = 0; |
|---|
| 37 | _latestFrameNumber = 0; |
|---|
| 38 | _attributeMapList.clear(); |
|---|
| 39 | _attributeMapList.resize(numberOfFrames); |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | |
|---|
| 43 | bool Stats::setAttribute(unsigned int frameNumber, const std::string& attributeName, double value) |
|---|
| 44 | { |
|---|
| 45 | if (frameNumber<getEarliestFrameNumber()) return false; |
|---|
| 46 | |
|---|
| 47 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 48 | |
|---|
| 49 | if (frameNumber>_latestFrameNumber) |
|---|
| 50 | { |
|---|
| 51 | |
|---|
| 52 | |
|---|
| 53 | |
|---|
| 54 | for(unsigned int i = _latestFrameNumber+1; i<= frameNumber; ++i) |
|---|
| 55 | { |
|---|
| 56 | unsigned int index = (i - _baseFrameNumber) % _attributeMapList.size(); |
|---|
| 57 | _attributeMapList[index].clear(); |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | if ( (frameNumber-_baseFrameNumber) >= static_cast<unsigned int>(_attributeMapList.size())) |
|---|
| 61 | { |
|---|
| 62 | _baseFrameNumber = (frameNumber/_attributeMapList.size())*_attributeMapList.size(); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | _latestFrameNumber = frameNumber; |
|---|
| 66 | |
|---|
| 67 | } |
|---|
| 68 | |
|---|
| 69 | int index = getIndex(frameNumber); |
|---|
| 70 | if (index<0) |
|---|
| 71 | { |
|---|
| 72 | OSG_NOTICE<<"Failed to assing valid index for Stats::setAttribute("<<frameNumber<<","<<attributeName<<","<<value<<")"<<std::endl; |
|---|
| 73 | return false; |
|---|
| 74 | } |
|---|
| 75 | |
|---|
| 76 | AttributeMap& attributeMap = _attributeMapList[index]; |
|---|
| 77 | attributeMap[attributeName] = value; |
|---|
| 78 | |
|---|
| 79 | return true; |
|---|
| 80 | } |
|---|
| 81 | |
|---|
| 82 | bool Stats::getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const |
|---|
| 83 | { |
|---|
| 84 | int index = getIndex(frameNumber); |
|---|
| 85 | if (index<0) return false; |
|---|
| 86 | |
|---|
| 87 | const AttributeMap& attributeMap = _attributeMapList[index]; |
|---|
| 88 | AttributeMap::const_iterator itr = attributeMap.find(attributeName); |
|---|
| 89 | if (itr == attributeMap.end()) return false; |
|---|
| 90 | |
|---|
| 91 | value = itr->second; |
|---|
| 92 | return true; |
|---|
| 93 | } |
|---|
| 94 | |
|---|
| 95 | bool Stats::getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace) const |
|---|
| 96 | { |
|---|
| 97 | return getAveragedAttribute(getEarliestFrameNumber(), getLatestFrameNumber(), attributeName, value, averageInInverseSpace); |
|---|
| 98 | } |
|---|
| 99 | |
|---|
| 100 | bool Stats::getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace) const |
|---|
| 101 | { |
|---|
| 102 | if (endFrameNumber<startFrameNumber) |
|---|
| 103 | { |
|---|
| 104 | std::swap(endFrameNumber, startFrameNumber); |
|---|
| 105 | } |
|---|
| 106 | |
|---|
| 107 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 108 | |
|---|
| 109 | double total = 0.0; |
|---|
| 110 | double numValidSamples = 0.0; |
|---|
| 111 | for(unsigned int i = startFrameNumber; i<=endFrameNumber; ++i) |
|---|
| 112 | { |
|---|
| 113 | double v = 0.0; |
|---|
| 114 | if (getAttributeNoMutex(i,attributeName,v)) |
|---|
| 115 | { |
|---|
| 116 | if (averageInInverseSpace) total += 1.0/v; |
|---|
| 117 | else total += v; |
|---|
| 118 | numValidSamples += 1.0; |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | if (numValidSamples>0.0) |
|---|
| 122 | { |
|---|
| 123 | if (averageInInverseSpace) value = numValidSamples/total; |
|---|
| 124 | else value = total/numValidSamples; |
|---|
| 125 | return true; |
|---|
| 126 | } |
|---|
| 127 | else return false; |
|---|
| 128 | } |
|---|
| 129 | |
|---|
| 130 | Stats::AttributeMap& Stats::getAttributeMapNoMutex(unsigned int frameNumber) |
|---|
| 131 | { |
|---|
| 132 | int index = getIndex(frameNumber); |
|---|
| 133 | if (index<0) return _invalidAttributeMap; |
|---|
| 134 | |
|---|
| 135 | return _attributeMapList[index]; |
|---|
| 136 | } |
|---|
| 137 | |
|---|
| 138 | const Stats::AttributeMap& Stats::getAttributeMapNoMutex(unsigned int frameNumber) const |
|---|
| 139 | { |
|---|
| 140 | int index = getIndex(frameNumber); |
|---|
| 141 | if (index<0) return _invalidAttributeMap; |
|---|
| 142 | |
|---|
| 143 | return _attributeMapList[index]; |
|---|
| 144 | } |
|---|
| 145 | |
|---|
| 146 | void Stats::report(std::ostream& out, const char* indent) const |
|---|
| 147 | { |
|---|
| 148 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 149 | |
|---|
| 150 | if (indent) out<<indent; |
|---|
| 151 | out<<"Stats "<<_name<<std::endl; |
|---|
| 152 | for(unsigned int i = getEarliestFrameNumber(); i<= getLatestFrameNumber(); ++i) |
|---|
| 153 | { |
|---|
| 154 | out<<" FrameNumber "<<i<<std::endl; |
|---|
| 155 | const osg::Stats::AttributeMap& attributes = getAttributeMapNoMutex(i); |
|---|
| 156 | for(osg::Stats::AttributeMap::const_iterator itr = attributes.begin(); |
|---|
| 157 | itr != attributes.end(); |
|---|
| 158 | ++itr) |
|---|
| 159 | { |
|---|
| 160 | if (indent) out<<indent; |
|---|
| 161 | out<<" "<<itr->first<<"\t"<<itr->second<<std::endl; |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | } |
|---|
| 165 | } |
|---|
| 166 | |
|---|
| 167 | void Stats::report(std::ostream& out, unsigned int frameNumber, const char* indent) const |
|---|
| 168 | { |
|---|
| 169 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 170 | |
|---|
| 171 | if (indent) out<<indent; |
|---|
| 172 | out<<"Stats "<<_name<<" FrameNumber "<<frameNumber<<std::endl; |
|---|
| 173 | const osg::Stats::AttributeMap& attributes = getAttributeMapNoMutex(frameNumber); |
|---|
| 174 | for(osg::Stats::AttributeMap::const_iterator itr = attributes.begin(); |
|---|
| 175 | itr != attributes.end(); |
|---|
| 176 | ++itr) |
|---|
| 177 | { |
|---|
| 178 | if (indent) out<<indent; |
|---|
| 179 | out<<" "<<itr->first<<"\t"<<itr->second<<std::endl; |
|---|
| 180 | } |
|---|
| 181 | } |
|---|