profiler.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. #define MAX_PROFILER_TEXT_ROWS 20
  26. // Global profiler
  27. class Profiler;
  28. extern Profiler *g_profiler;
  29. /*
  30. Time profiler
  31. */
  32. class Profiler
  33. {
  34. public:
  35. Profiler() = default;
  36. void add(const std::string &name, float value)
  37. {
  38. MutexAutoLock lock(m_mutex);
  39. {
  40. /* No average shall have been used; mark add used as -2 */
  41. std::map<std::string, int>::iterator n = m_avgcounts.find(name);
  42. if(n == m_avgcounts.end())
  43. m_avgcounts[name] = -2;
  44. else{
  45. if(n->second == -1)
  46. n->second = -2;
  47. assert(n->second == -2);
  48. }
  49. }
  50. {
  51. std::map<std::string, float>::iterator n = m_data.find(name);
  52. if(n == m_data.end())
  53. m_data[name] = value;
  54. else
  55. n->second += value;
  56. }
  57. }
  58. void avg(const std::string &name, float value)
  59. {
  60. MutexAutoLock lock(m_mutex);
  61. int &count = m_avgcounts[name];
  62. assert(count != -2);
  63. count = MYMAX(count, 0) + 1;
  64. m_data[name] += value;
  65. }
  66. void clear()
  67. {
  68. MutexAutoLock lock(m_mutex);
  69. for (auto &it : m_data) {
  70. it.second = 0;
  71. }
  72. m_avgcounts.clear();
  73. }
  74. void print(std::ostream &o)
  75. {
  76. printPage(o, 1, 1);
  77. }
  78. float getValue(const std::string &name) const
  79. {
  80. std::map<std::string, float>::const_iterator numerator = m_data.find(name);
  81. if (numerator == m_data.end())
  82. return 0.f;
  83. std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
  84. if (denominator != m_avgcounts.end()){
  85. if (denominator->second >= 1)
  86. return numerator->second / denominator->second;
  87. }
  88. return numerator->second;
  89. }
  90. void printPage(std::ostream &o, u32 page, u32 pagecount)
  91. {
  92. MutexAutoLock lock(m_mutex);
  93. u32 minindex, maxindex;
  94. paging(m_data.size(), page, pagecount, minindex, maxindex);
  95. for (std::map<std::string, float>::const_iterator i = m_data.begin();
  96. i != m_data.end(); ++i) {
  97. if (maxindex == 0)
  98. break;
  99. maxindex--;
  100. if (minindex != 0) {
  101. minindex--;
  102. continue;
  103. }
  104. int avgcount = 1;
  105. std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
  106. if (n != m_avgcounts.end()) {
  107. if(n->second >= 1)
  108. avgcount = n->second;
  109. }
  110. o << " " << i->first << ": ";
  111. s32 clampsize = 40;
  112. s32 space = clampsize - i->first.size();
  113. for(s32 j = 0; j < space; j++) {
  114. if (j % 2 == 0 && j < space - 1)
  115. o << "-";
  116. else
  117. o << " ";
  118. }
  119. o << (i->second / avgcount);
  120. o << std::endl;
  121. }
  122. }
  123. typedef std::map<std::string, float> GraphValues;
  124. void graphAdd(const std::string &id, float value)
  125. {
  126. MutexAutoLock lock(m_mutex);
  127. std::map<std::string, float>::iterator i =
  128. m_graphvalues.find(id);
  129. if(i == m_graphvalues.end())
  130. m_graphvalues[id] = value;
  131. else
  132. i->second += value;
  133. }
  134. void graphGet(GraphValues &result)
  135. {
  136. MutexAutoLock lock(m_mutex);
  137. result = m_graphvalues;
  138. m_graphvalues.clear();
  139. }
  140. void remove(const std::string& name)
  141. {
  142. MutexAutoLock lock(m_mutex);
  143. m_avgcounts.erase(name);
  144. m_data.erase(name);
  145. }
  146. private:
  147. std::mutex m_mutex;
  148. std::map<std::string, float> m_data;
  149. std::map<std::string, int> m_avgcounts;
  150. std::map<std::string, float> m_graphvalues;
  151. };
  152. enum ScopeProfilerType{
  153. SPT_ADD,
  154. SPT_AVG,
  155. SPT_GRAPH_ADD
  156. };
  157. class ScopeProfiler
  158. {
  159. public:
  160. ScopeProfiler(Profiler *profiler, const std::string &name,
  161. ScopeProfilerType type = SPT_ADD);
  162. ~ScopeProfiler();
  163. private:
  164. Profiler *m_profiler = nullptr;
  165. std::string m_name;
  166. TimeTaker *m_timer = nullptr;
  167. enum ScopeProfilerType m_type;
  168. };