profiler.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /*
  2. Minetest
  3. Copyright (C) 2015 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. #include "profiler.h"
  17. #include "porting.h"
  18. static Profiler main_profiler;
  19. Profiler *g_profiler = &main_profiler;
  20. ScopeProfiler::ScopeProfiler(
  21. Profiler *profiler, const std::string &name, ScopeProfilerType type) :
  22. m_profiler(profiler),
  23. m_name(name), m_type(type)
  24. {
  25. m_name.append(" [ms]");
  26. if (m_profiler)
  27. m_timer = new TimeTaker(m_name, nullptr, PRECISION_MILLI);
  28. }
  29. ScopeProfiler::~ScopeProfiler()
  30. {
  31. if (!m_timer)
  32. return;
  33. float duration_ms = m_timer->stop(true);
  34. float duration = duration_ms;
  35. if (m_profiler) {
  36. switch (m_type) {
  37. case SPT_ADD:
  38. m_profiler->add(m_name, duration);
  39. break;
  40. case SPT_AVG:
  41. m_profiler->avg(m_name, duration);
  42. break;
  43. case SPT_GRAPH_ADD:
  44. m_profiler->graphAdd(m_name, duration);
  45. break;
  46. }
  47. }
  48. delete m_timer;
  49. }
  50. Profiler::Profiler()
  51. {
  52. m_start_time = porting::getTimeMs();
  53. }
  54. void Profiler::add(const std::string &name, float value)
  55. {
  56. MutexAutoLock lock(m_mutex);
  57. {
  58. /* No average shall have been used; mark add used as -2 */
  59. std::map<std::string, int>::iterator n = m_avgcounts.find(name);
  60. if (n == m_avgcounts.end()) {
  61. m_avgcounts[name] = -2;
  62. } else {
  63. if (n->second == -1)
  64. n->second = -2;
  65. assert(n->second == -2);
  66. }
  67. }
  68. {
  69. std::map<std::string, float>::iterator n = m_data.find(name);
  70. if (n == m_data.end())
  71. m_data[name] = value;
  72. else
  73. n->second += value;
  74. }
  75. }
  76. void Profiler::avg(const std::string &name, float value)
  77. {
  78. MutexAutoLock lock(m_mutex);
  79. int &count = m_avgcounts[name];
  80. assert(count != -2);
  81. count = MYMAX(count, 0) + 1;
  82. m_data[name] += value;
  83. }
  84. void Profiler::clear()
  85. {
  86. MutexAutoLock lock(m_mutex);
  87. for (auto &it : m_data) {
  88. it.second = 0;
  89. }
  90. m_avgcounts.clear();
  91. m_start_time = porting::getTimeMs();
  92. }
  93. float Profiler::getValue(const std::string &name) const
  94. {
  95. auto numerator = m_data.find(name);
  96. if (numerator == m_data.end())
  97. return 0.f;
  98. auto denominator = m_avgcounts.find(name);
  99. if (denominator != m_avgcounts.end()) {
  100. if (denominator->second >= 1)
  101. return numerator->second / denominator->second;
  102. }
  103. return numerator->second;
  104. }
  105. int Profiler::getAvgCount(const std::string &name) const
  106. {
  107. auto n = m_avgcounts.find(name);
  108. if (n != m_avgcounts.end() && n->second >= 1)
  109. return n->second;
  110. return 1;
  111. }
  112. u64 Profiler::getElapsedMs() const
  113. {
  114. return porting::getTimeMs() - m_start_time;
  115. }
  116. int Profiler::print(std::ostream &o, u32 page, u32 pagecount)
  117. {
  118. GraphValues values;
  119. getPage(values, page, pagecount);
  120. char num_buf[50];
  121. for (const auto &i : values) {
  122. o << " " << i.first << " ";
  123. if (i.second == 0) {
  124. o << std::endl;
  125. continue;
  126. }
  127. s32 space = 44 - i.first.size();
  128. for (s32 j = 0; j < space; j++) {
  129. if ((j & 1) && j < space - 1)
  130. o << ".";
  131. else
  132. o << " ";
  133. }
  134. porting::mt_snprintf(num_buf, sizeof(num_buf), "% 4ix % 3g",
  135. getAvgCount(i.first), i.second);
  136. o << num_buf << std::endl;
  137. }
  138. return values.size();
  139. }
  140. void Profiler::getPage(GraphValues &o, u32 page, u32 pagecount)
  141. {
  142. MutexAutoLock lock(m_mutex);
  143. u32 minindex, maxindex;
  144. paging(m_data.size(), page, pagecount, minindex, maxindex);
  145. for (const auto &i : m_data) {
  146. if (maxindex == 0)
  147. break;
  148. maxindex--;
  149. if (minindex != 0) {
  150. minindex--;
  151. continue;
  152. }
  153. o[i.first] = i.second / getAvgCount(i.first);
  154. }
  155. }