Stats 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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. #ifndef OSG_STATS
  14. #define OSG_STATS 1
  15. #include <osg/Referenced>
  16. #include <OpenThreads/Mutex>
  17. #include <OpenThreads/ScopedLock>
  18. #include <string>
  19. #include <map>
  20. #include <vector>
  21. #include <ostream>
  22. namespace osg {
  23. class OSG_EXPORT Stats : public osg::Referenced
  24. {
  25. public:
  26. Stats(const std::string& name);
  27. Stats(const std::string& name, unsigned int numberOfFrames);
  28. void setName(const std::string& name) { _name = name; }
  29. const std::string& getName() const { return _name; }
  30. void allocate(unsigned int numberOfFrames);
  31. unsigned int getEarliestFrameNumber() const { return _latestFrameNumber < static_cast<unsigned int>(_attributeMapList.size()) ? 0 : _latestFrameNumber - static_cast<unsigned int>(_attributeMapList.size()) + 1; }
  32. unsigned int getLatestFrameNumber() const { return _latestFrameNumber; }
  33. typedef std::map<std::string, double> AttributeMap;
  34. typedef std::vector<AttributeMap> AttributeMapList;
  35. bool setAttribute(unsigned int frameNumber, const std::string& attributeName, double value);
  36. inline bool getAttribute(unsigned int frameNumber, const std::string& attributeName, double& value) const
  37. {
  38. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
  39. return getAttributeNoMutex(frameNumber, attributeName, value);
  40. }
  41. bool getAveragedAttribute(const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
  42. bool getAveragedAttribute(unsigned int startFrameNumber, unsigned int endFrameNumber, const std::string& attributeName, double& value, bool averageInInverseSpace=false) const;
  43. inline AttributeMap& getAttributeMap(unsigned int frameNumber)
  44. {
  45. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
  46. return getAttributeMapNoMutex(frameNumber);
  47. }
  48. inline const AttributeMap& getAttributeMap(unsigned int frameNumber) const
  49. {
  50. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
  51. return getAttributeMapNoMutex(frameNumber);
  52. }
  53. typedef std::map<std::string, bool> CollectMap;
  54. void collectStats(const std::string& str, bool flag) { _collectMap[str] = flag; }
  55. inline bool collectStats(const std::string& str) const
  56. {
  57. OpenThreads::ScopedLock<OpenThreads::Mutex> lock(_mutex);
  58. CollectMap::const_iterator itr = _collectMap.find(str);
  59. return (itr != _collectMap.end()) ? itr->second : false;
  60. }
  61. void report(std::ostream& out, const char* indent=0) const;
  62. void report(std::ostream& out, unsigned int frameNumber, const char* indent=0) const;
  63. protected:
  64. virtual ~Stats() {}
  65. bool getAttributeNoMutex(unsigned int frameNumber, const std::string& attributeName, double& value) const;
  66. AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber);
  67. const AttributeMap& getAttributeMapNoMutex(unsigned int frameNumber) const;
  68. int getIndex(unsigned int frameNumber) const
  69. {
  70. // reject frame that are in the future
  71. if (frameNumber > _latestFrameNumber) return -1;
  72. // reject frames that are too early
  73. if (frameNumber < getEarliestFrameNumber()) return -1;
  74. if (frameNumber >= _baseFrameNumber) return frameNumber - _baseFrameNumber;
  75. else return static_cast<int>(_attributeMapList.size()) - (_baseFrameNumber-frameNumber);
  76. }
  77. std::string _name;
  78. mutable OpenThreads::Mutex _mutex;
  79. unsigned int _baseFrameNumber;
  80. unsigned int _latestFrameNumber;
  81. AttributeMapList _attributeMapList;
  82. AttributeMap _invalidAttributeMap;
  83. CollectMap _collectMap;
  84. };
  85. }
  86. #endif