profiler.h 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. Minetest
  3. Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 2.1 of the License, or
  7. (at your option) any later version.
  8. This program 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. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License along
  13. with this program; if not, write to the Free Software Foundation, Inc.,
  14. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  15. */
  16. #pragma once
  17. #include "irrlichttypes.h"
  18. #include <cassert>
  19. #include <string>
  20. #include <map>
  21. #include <ostream>
  22. #include "threading/mutex_auto_lock.h"
  23. #include "util/timetaker.h"
  24. #include "util/numeric.h" // paging()
  25. // Global profiler
  26. class Profiler;
  27. extern Profiler *g_profiler;
  28. /*
  29. Time profiler
  30. */
  31. class Profiler
  32. {
  33. public:
  34. Profiler();
  35. void add(const std::string &name, float value);
  36. void avg(const std::string &name, float value);
  37. void max(const std::string &name, float value);
  38. void clear();
  39. float getValue(const std::string &name) const;
  40. int getAvgCount(const std::string &name) const;
  41. u64 getElapsedMs() const;
  42. typedef std::map<std::string, float> GraphValues;
  43. // Returns the line count
  44. int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
  45. void getPage(GraphValues &o, u32 page, u32 pagecount);
  46. void graphSet(const std::string &id, float value)
  47. {
  48. MutexAutoLock lock(m_mutex);
  49. m_graphvalues[id] = value;
  50. }
  51. void graphAdd(const std::string &id, float value)
  52. {
  53. MutexAutoLock lock(m_mutex);
  54. auto it = m_graphvalues.find(id);
  55. if (it == m_graphvalues.end())
  56. m_graphvalues.emplace(id, value);
  57. else
  58. it->second += value;
  59. }
  60. void graphPop(GraphValues &result)
  61. {
  62. MutexAutoLock lock(m_mutex);
  63. assert(result.empty());
  64. std::swap(result, m_graphvalues);
  65. }
  66. void remove(const std::string& name)
  67. {
  68. MutexAutoLock lock(m_mutex);
  69. m_data.erase(name);
  70. }
  71. private:
  72. struct DataPair {
  73. float value = 0;
  74. int avgcount = 0;
  75. inline void reset() {
  76. value = 0;
  77. // negative values are used for type checking, so leave them alone
  78. if (avgcount >= 1)
  79. avgcount = 0;
  80. }
  81. inline float getValue() const {
  82. return avgcount >= 1 ? (value / avgcount) : value;
  83. }
  84. };
  85. std::mutex m_mutex;
  86. std::map<std::string, DataPair> m_data;
  87. std::map<std::string, float> m_graphvalues;
  88. u64 m_start_time;
  89. };
  90. enum ScopeProfilerType : u8
  91. {
  92. SPT_ADD = 1,
  93. SPT_AVG,
  94. SPT_GRAPH_ADD,
  95. SPT_MAX
  96. };
  97. // Note: this class should be kept lightweight.
  98. class ScopeProfiler
  99. {
  100. public:
  101. ScopeProfiler(Profiler *profiler, const std::string &name,
  102. ScopeProfilerType type = SPT_ADD,
  103. TimePrecision precision = PRECISION_MILLI);
  104. ~ScopeProfiler();
  105. private:
  106. Profiler *m_profiler = nullptr;
  107. std::string m_name;
  108. u64 m_time1;
  109. ScopeProfilerType m_type;
  110. TimePrecision m_precision;
  111. };