Profiler.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. /*
  2. * Profiler.h
  3. * ----------
  4. * Purpose: Performance measuring
  5. * Notes : (currently none)
  6. * Authors: OpenMPT Devs
  7. * The OpenMPT source code is released under the BSD license. Read LICENSE for more details.
  8. */
  9. #pragma once
  10. #include "openmpt/all/BuildSettings.hpp"
  11. #include "mpt/mutex/mutex.hpp"
  12. #include <string>
  13. #include <vector>
  14. OPENMPT_NAMESPACE_BEGIN
  15. #if defined(MODPLUG_TRACKER)
  16. //#define USE_PROFILER
  17. #endif
  18. #ifdef USE_PROFILER
  19. class Profiler
  20. {
  21. public:
  22. enum Category
  23. {
  24. GUI,
  25. Audio,
  26. Notify,
  27. CategoriesCount
  28. };
  29. static std::vector<std::string> GetCategoryNames()
  30. {
  31. std::vector<std::string> ret;
  32. ret.push_back("GUI");
  33. ret.push_back("Audio");
  34. ret.push_back("Notify");
  35. return ret;
  36. }
  37. public:
  38. static void Update();
  39. static std::string DumpProfiles();
  40. static std::vector<double> DumpCategories();
  41. };
  42. class Profile
  43. {
  44. private:
  45. mutable mpt::mutex datamutex;
  46. public:
  47. struct Data
  48. {
  49. uint64 Calls;
  50. uint64 Sum;
  51. int64 Overhead;
  52. uint64 Start;
  53. };
  54. public:
  55. Data data;
  56. uint64 EnterTime;
  57. Profiler::Category Category;
  58. const char * const Name;
  59. uint64 GetTime() const;
  60. uint64 GetFrequency() const;
  61. public:
  62. Profile(Profiler::Category category, const char *name);
  63. ~Profile();
  64. void Reset();
  65. void Enter();
  66. void Leave();
  67. class Scope
  68. {
  69. private:
  70. Profile &profile;
  71. public:
  72. Scope(Profile &p) : profile(p) { profile.Enter(); }
  73. ~Scope() { profile.Leave(); }
  74. };
  75. public:
  76. Data GetAndResetData();
  77. };
  78. #define OPENMPT_PROFILE_SCOPE(cat, name) \
  79. static Profile OPENMPT_PROFILE_VAR(cat, name);\
  80. Profile::Scope OPENMPT_PROFILE_SCOPE_VAR(OPENMPT_PROFILE_VAR); \
  81. /**/
  82. #define OPENMPT_PROFILE_FUNCTION(cat) OPENMPT_PROFILE_SCOPE(cat, __func__)
  83. #else // !USE_PROFILER
  84. class Profiler
  85. {
  86. public:
  87. enum Category
  88. {
  89. CategoriesCount
  90. };
  91. static std::vector<std::string> GetCategoryNames() { return std::vector<std::string>(); }
  92. public:
  93. static void Update() { }
  94. static std::string DumpProfiles() { return std::string(); }
  95. static std::vector<double> DumpCategories() { return std::vector<double>(); }
  96. };
  97. #define OPENMPT_PROFILE_SCOPE(cat, name) do { } while(0)
  98. #define OPENMPT_PROFILE_FUNCTION(cat) do { } while(0)
  99. #endif // USE_PROFILER
  100. OPENMPT_NAMESPACE_END