StatCollector.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // SPDX-FileCopyrightText: 2021 Jorrit Rouwe
  2. // SPDX-License-Identifier: MIT
  3. #pragma once
  4. #include <Core/Mutex.h>
  5. #include <Core/NonCopyable.h>
  6. #include <map>
  7. namespace JPH {
  8. #ifdef JPH_STAT_COLLECTOR
  9. /// Singleton class for collacting simple stat values
  10. ///
  11. /// Usage:
  12. ///
  13. /// To start recording call:
  14. ///
  15. /// JPH_STAT_COLLECTOR_START_CAPTURE()
  16. ///
  17. /// Then start the next frame:
  18. ///
  19. /// JPH_STAT_COLLECTOR_SET_NEXT_FRAME()
  20. ///
  21. /// To add a stat:
  22. ///
  23. /// STAT_COLLETOR_ADD("Path.To.Name", <value>)
  24. ///
  25. /// where value is an int, float, bool, Vec3 or Quat
  26. ///
  27. /// To dump the stats to a file call:
  28. ///
  29. /// JPH_STAT_COLLECTOR_STOP_CAPTURE()
  30. class StatCollector : public NonCopyable
  31. {
  32. public:
  33. /// Reset all stats
  34. void Reset();
  35. /// Start / stop capture
  36. void StartCapture();
  37. void StopCapture(const char *inFileName);
  38. bool IsCapturing() const { return mIsCapturing; }
  39. /// Increments the frame counter
  40. void SetNextFrame();
  41. /// Helper class that stores data points
  42. class Variant
  43. {
  44. public:
  45. enum class EType
  46. {
  47. Undefined,
  48. Float,
  49. Int,
  50. Bool,
  51. };
  52. Variant() : mType(EType::Undefined) { }
  53. Variant(float inFloat) : mType(EType::Float), mFloat(inFloat) { }
  54. Variant(int inInt) : mType(EType::Int), mInt(inInt) { }
  55. Variant(bool inBool) : mType(EType::Bool), mBool(inBool) { }
  56. string ToString() const;
  57. private:
  58. EType mType;
  59. union
  60. {
  61. float mFloat;
  62. int mInt;
  63. bool mBool;
  64. };
  65. };
  66. /// Add an item
  67. void AddItem(const string &inName, const Variant &inValue);
  68. void AddItem(const string &inName, Vec3Arg inValue);
  69. void AddItem(const string &inName, QuatArg inValue);
  70. /// Singleton instance
  71. static StatCollector sInstance;
  72. private:
  73. /// Internal variant of Reset that does not take the lock
  74. void ResetInternal();
  75. using KeyValueMap = map<int, Variant>;
  76. using FrameMap = map<int, KeyValueMap>;
  77. using KeyIDMap = map<string, int>;
  78. Mutex mMutex;
  79. bool mIsCapturing = false;
  80. FrameMap mFrames;
  81. KeyIDMap mKeys;
  82. int mNextKey = 0;
  83. int mCurrentFrameNumber = 0;
  84. KeyValueMap * mCurrentFrame = nullptr;
  85. };
  86. #define JPH_IF_STAT_COLLECTOR(...) __VA_ARGS__
  87. #define JPH_STAT_COLLECTOR_SET_NEXT_FRAME() StatCollector::sInstance.SetNextFrame()
  88. #define JPH_STAT_COLLECTOR_ADD(name, value) StatCollector::sInstance.AddItem(name, value)
  89. #define JPH_STAT_COLLECTOR_START_CAPTURE() StatCollector::sInstance.StartCapture()
  90. #define JPH_STAT_COLLECTOR_STOP_CAPTURE(file_name) StatCollector::sInstance.StopCapture(file_name)
  91. #define JPH_STAT_COLLECTOR_IS_CAPTURING() StatCollector::sInstance.IsCapturing()
  92. #define JPH_STAT_COLLECTOR_RESET() StatCollector::sInstance.Reset()
  93. #else
  94. #define JPH_IF_STAT_COLLECTOR(...)
  95. #define JPH_STAT_COLLECTOR_SET_NEXT_FRAME()
  96. #define JPH_STAT_COLLECTOR_ADD(name, value)
  97. #define JPH_STAT_COLLECTOR_START_CAPTURE()
  98. #define JPH_STAT_COLLECTOR_STOP_CAPTURE(file_name)
  99. #define JPH_STAT_COLLECTOR_IS_CAPTURING() false
  100. #define JPH_STAT_COLLECTOR_RESET()
  101. #endif
  102. } // JPH