Stats
Go to the documentation of this file.
/* -*-c++-*- OpenSceneGraph - Copyright (C) 1998-2007 Robert Osfield
*
* This library is open source and may be redistributed and/or modified under
* the terms of the OpenSceneGraph Public License (OSGPL) version 0.0 or
* (at your option) any later version. The full license is in LICENSE file
* included with this distribution, and on the openscenegraph.org website.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* OpenSceneGraph Public License for more details.
*/
#ifndef OSG_STATS
#define OSG_STATS 1
#include <osg/Referenced>
#include <OpenThreads/Mutex>
#include <OpenThreads/ScopedLock>
#include <string>
#include <map>
#include <vector>
#include <ostream>
namespace osg {
class OSG_EXPORT Stats : public osg::Referenced
{
public:
Stats(const std::string& name);
Stats(const std::string& name, unsigned int numberOfFrames);
void setName(const std::string& name) { _name = name; }
const std::string& getName() const { return _name; }
void allocate(unsigned int numberOfFrames);
unsigned int getEarliestFrameNumber() const { return _latestFrameNumber < static_cast<unsigned int>(_attributeMapList.size()) ? 0 : _latestFrameNumber - static_cast<unsigned int>(_attributeMapList.size()) + 1; }
unsigned int getLatestFrameNumber() const { return _latestFrameNumber; }
typedef std::map<std::string, double> AttributeMap;
typedef std::vector<AttributeMap> AttributeMapList;
bool setAttribute(unsigned int frameNumber, const std::string& attributeName, double value);
inline bool getAttribute(unsigned int frameNumber, const std::string& attributeName, double& value) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return getAttributeNoMutex(frameNumber, attributeName, value);
}
bool getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
bool getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
inline AttributeMap& getAttributeMap(unsigned int frameNumber)
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return getAttributeMapNoMutex(frameNumber);
}
inline const AttributeMap& getAttributeMap(unsigned int frameNumber) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
return getAttributeMapNoMutex(frameNumber);
}
typedef std::map<std::string, bool> CollectMap;
void collectStats(const std::string& str, bool flag) { _collectMap[str] = flag; }
inline bool collectStats(const std::string& str) const
{
OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
CollectMap::const_iterator itr = _collectMap.find(str);
return (itr != _collectMap.end()) ? itr->second : false;
}
void report(std::ostream& out, const char* indent=0) const;
void report(std::ostream& out, unsigned int frameNumber, const char* indent=0) const;
protected:
virtual ~Stats() {}
bool getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const;
AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber);
const AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber) const;
int getIndex(unsigned int frameNumber) const
{
// reject frame that are in the future
if (frameNumber > _latestFrameNumber) return -1;
// reject frames that are too early
if (frameNumber < getEarliestFrameNumber()) return -1;
if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber;
else return static_cast<int>(_attributeMapList.size()) - (_baseFrameNumber-frameNumber);
}
std::string _name;
mutable OpenThreads::Mutex _mutex;
unsigned int _baseFrameNumber;
unsigned int _latestFrameNumber;
AttributeMapList _attributeMapList;
AttributeMap _invalidAttributeMap;
CollectMap _collectMap;
};
}
#endif
Mutex
Referenced
ScopedLock
OpenThreads::Mutex
This class provides an object-oriented thread mutex interface.
Definition Mutex:31
OpenThreads::ScopedLock
Definition ScopedLock:25
osg::Referenced
Definition Referenced:44
osg::Stats::getAttributeNoMutex
bool getAttributeNoMutex(unsigned int frameNumber, const std::string &attributeName, double &value) const
osg::Stats::getIndex
int getIndex(unsigned int frameNumber) const
Definition Stats:96
osg::Stats::report
void report(std::ostream &out, unsigned int frameNumber, const char *indent=0) const
osg::Stats::getLatestFrameNumber
unsigned int getLatestFrameNumber() const
Definition Stats:42
osg::Stats::CollectMap
std::map< std::string, bool > CollectMap
Definition Stats:71
osg::Stats::getAttribute
bool getAttribute(unsigned int frameNumber, const std::string &attributeName, double &value) const
Definition Stats:49
osg::Stats::collectStats
void collectStats(const std::string &str, bool flag)
Definition Stats:73
osg::Stats::collectStats
bool collectStats(const std::string &str) const
Definition Stats:75
osg::Stats::_invalidAttributeMap
AttributeMap _invalidAttributeMap
Definition Stats:116
osg::Stats::_name
std::string _name
Definition Stats:108
osg::Stats::_mutex
OpenThreads::Mutex _mutex
Definition Stats:110
osg::Stats::allocate
void allocate(unsigned int numberOfFrames)
osg::Stats::AttributeMap
std::map< std::string, double > AttributeMap
Definition Stats:44
osg::Stats::AttributeMapList
std::vector< AttributeMap > AttributeMapList
Definition Stats:45
osg::Stats::setName
void setName(const std::string &name)
Definition Stats:36
osg::Stats::getName
const std::string & getName() const
Definition Stats:37
osg::Stats::Stats
Stats(const std::string &name)
osg::Stats::_collectMap
CollectMap _collectMap
Definition Stats:118
osg::Stats::_latestFrameNumber
unsigned int _latestFrameNumber
Definition Stats:113
osg::Stats::_baseFrameNumber
unsigned int _baseFrameNumber
Definition Stats:112
osg::Stats::getEarliestFrameNumber
unsigned int getEarliestFrameNumber() const
Definition Stats:41
osg::Stats::setAttribute
bool setAttribute(unsigned int frameNumber, const std::string &attributeName, double value)
osg::Stats::getAttributeMap
AttributeMap & getAttributeMap(unsigned int frameNumber)
Definition Stats:59
osg::Stats::getAveragedAttribute
bool getAveragedAttribute(const std::string &attributeName, double &value, bool averageInInverseSpace=false) const
osg::Stats::report
void report(std::ostream &out, const char *indent=0) const
osg::Stats::getAveragedAttribute
bool getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string &attributeName, double &value, bool averageInInverseSpace=false) const
osg::Stats::getAttributeMap
const AttributeMap & getAttributeMap(unsigned int frameNumber) const
Definition Stats:65
osg::Stats::~Stats
virtual ~Stats()
Definition Stats:88
osg::Stats::getAttributeMapNoMutex
const AttributeMap & getAttributeMapNoMutex(unsigned int frameNumber) const
osg::Stats::getAttributeMapNoMutex
AttributeMap & getAttributeMapNoMutex(unsigned int frameNumber)
osg::Stats::Stats
Stats(const std::string &name, unsigned int numberOfFrames)
osg::Stats::_attributeMapList
AttributeMapList _attributeMapList
Definition Stats:115
osg
Definition AlphaFunc:19
OSG_EXPORT
#define OSG_EXPORT
Definition osg/Export:39