| 1 | /* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield |
|---|
| 2 | * |
|---|
| 3 | * This library is open source and may be redistributed and/or modified under |
|---|
| 4 | * the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or |
|---|
| 5 | * (at your option) any later version. The full license is in LICENSE file |
|---|
| 6 | * included with this distribution, and on the openscenegraph.org website. |
|---|
| 7 | * |
|---|
| 8 | * This library is distributed in the hope that it will be useful, |
|---|
| 9 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 10 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 11 | * OpenSceneGraph Public License for more details. |
|---|
| 12 | */ |
|---|
| 13 | |
|---|
| 14 | #ifndef OSG_STATS |
|---|
| 15 | #define OSG_STATS 1 |
|---|
| 16 | |
|---|
| 17 | #include <osg/Referenced> |
|---|
| 18 | #include <OpenThreads/Mutex> |
|---|
| 19 | #include <OpenThreads/ScopedLock> |
|---|
| 20 | |
|---|
| 21 | #include <string> |
|---|
| 22 | #include <map> |
|---|
| 23 | #include <vector> |
|---|
| 24 | #include <ostream> |
|---|
| 25 | |
|---|
| 26 | namespace osg { |
|---|
| 27 | |
|---|
| 28 | class OSG_EXPORT Stats : public osg::Referenced |
|---|
| 29 | { |
|---|
| 30 | public: |
|---|
| 31 | |
|---|
| 32 | Stats(const std::string& name); |
|---|
| 33 | |
|---|
| 34 | Stats(const std::string& name, unsigned int numberOfFrames); |
|---|
| 35 | |
|---|
| 36 | void setName(const std::string& name) { _name = name; } |
|---|
| 37 | const std::string& getName() const { return _name; } |
|---|
| 38 | |
|---|
| 39 | void allocate(unsigned int numberOfFrames); |
|---|
| 40 | |
|---|
| 41 | unsigned int getEarliestFrameNumber() const { return _latestFrameNumber < static_cast<unsigned int>(_attributeMapList.size()) ? 0 : _latestFrameNumber - static_cast<unsigned int>(_attributeMapList.size()) + 1; } |
|---|
| 42 | unsigned int getLatestFrameNumber() const { return _latestFrameNumber; } |
|---|
| 43 | |
|---|
| 44 | typedef std::map<std::string, double> AttributeMap; |
|---|
| 45 | typedef std::vector<AttributeMap> AttributeMapList; |
|---|
| 46 | |
|---|
| 47 | bool setAttribute(unsigned int frameNumber, const std::string& attributeName, double value); |
|---|
| 48 | |
|---|
| 49 | inline bool getAttribute(unsigned int frameNumber, const std::string& attributeName, double& value) const |
|---|
| 50 | { |
|---|
| 51 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 52 | return getAttributeNoMutex(frameNumber, attributeName, value); |
|---|
| 53 | } |
|---|
| 54 | |
|---|
| 55 | bool getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace=false) const; |
|---|
| 56 | |
|---|
| 57 | bool getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace=false) const; |
|---|
| 58 | |
|---|
| 59 | inline AttributeMap& getAttributeMap(unsigned int frameNumber) |
|---|
| 60 | { |
|---|
| 61 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 62 | return getAttributeMapNoMutex(frameNumber); |
|---|
| 63 | } |
|---|
| 64 | |
|---|
| 65 | inline const AttributeMap& getAttributeMap(unsigned int frameNumber) const |
|---|
| 66 | { |
|---|
| 67 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 68 | return getAttributeMapNoMutex(frameNumber); |
|---|
| 69 | } |
|---|
| 70 | |
|---|
| 71 | typedef std::map<std::string, bool> CollectMap; |
|---|
| 72 | |
|---|
| 73 | void collectStats(const std::string& str, bool flag) { _collectMap[str] = flag; } |
|---|
| 74 | |
|---|
| 75 | inline bool collectStats(const std::string& str) const |
|---|
| 76 | { |
|---|
| 77 | OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex); |
|---|
| 78 | |
|---|
| 79 | CollectMap::const_iterator itr = _collectMap.find(str); |
|---|
| 80 | return (itr != _collectMap.end()) ? itr->second : false; |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | void report(std::ostream& out, const char* indent=0) const; |
|---|
| 84 | void report(std::ostream& out, unsigned int frameNumber, const char* indent=0) const; |
|---|
| 85 | |
|---|
| 86 | protected: |
|---|
| 87 | |
|---|
| 88 | virtual ~Stats() {} |
|---|
| 89 | |
|---|
| 90 | bool getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const; |
|---|
| 91 | |
|---|
| 92 | AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber); |
|---|
| 93 | const AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber) const; |
|---|
| 94 | |
|---|
| 95 | |
|---|
| 96 | int getIndex(unsigned int frameNumber) const |
|---|
| 97 | { |
|---|
| 98 | // reject frame that are in the future |
|---|
| 99 | if (frameNumber > _latestFrameNumber) return -1; |
|---|
| 100 | |
|---|
| 101 | // reject frames that are too early |
|---|
| 102 | if (frameNumber < getEarliestFrameNumber()) return -1; |
|---|
| 103 | |
|---|
| 104 | if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber; |
|---|
| 105 | else return static_cast<int>(_attributeMapList.size()) - (_baseFrameNumber-frameNumber); |
|---|
| 106 | } |
|---|
| 107 | |
|---|
| 108 | std::string _name; |
|---|
| 109 | |
|---|
| 110 | mutable OpenThreads::Mutex _mutex; |
|---|
| 111 | |
|---|
| 112 | unsigned int _baseFrameNumber; |
|---|
| 113 | unsigned int _latestFrameNumber; |
|---|
| 114 | |
|---|
| 115 | AttributeMapList _attributeMapList; |
|---|
| 116 | AttributeMap _invalidAttributeMap; |
|---|
| 117 | |
|---|
| 118 | CollectMap _collectMap; |
|---|
| 119 | |
|---|
| 120 | }; |
|---|
| 121 | |
|---|
| 122 | |
|---|
| 123 | } |
|---|
| 124 | |
|---|
| 125 | #endif |
|---|