profiler.h 4.4 KB

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