profilergraph.cpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. Minetest
  3. Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
  4. Copyright (C) 2018 nerzhul, Loic Blot <loic.blot@unix-experience.fr>
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU Lesser General Public License as published by
  7. the Free Software Foundation; either version 2.1 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License along
  14. with this program; if not, write to the Free Software Foundation, Inc.,
  15. 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  16. */
  17. #include "porting.h"
  18. #include "profilergraph.h"
  19. #include "util/string.h"
  20. void ProfilerGraph::put(const Profiler::GraphValues &values)
  21. {
  22. m_log.emplace_back(values);
  23. while (m_log.size() > m_log_max_size)
  24. m_log.erase(m_log.begin());
  25. }
  26. void ProfilerGraph::draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
  27. gui::IGUIFont *font) const
  28. {
  29. // Do *not* use UNORDERED_MAP here as the order needs
  30. // to be the same for each call to prevent flickering
  31. std::map<std::string, Meta> m_meta;
  32. for (const Piece &piece : m_log) {
  33. for (const auto &i : piece.values) {
  34. const std::string &id = i.first;
  35. const float &value = i.second;
  36. std::map<std::string, Meta>::iterator j = m_meta.find(id);
  37. if (j == m_meta.end()) {
  38. m_meta[id] = Meta(value);
  39. continue;
  40. }
  41. if (value < j->second.min)
  42. j->second.min = value;
  43. if (value > j->second.max)
  44. j->second.max = value;
  45. }
  46. }
  47. // Assign colors
  48. static const video::SColor usable_colors[] = {video::SColor(255, 255, 100, 100),
  49. video::SColor(255, 90, 225, 90),
  50. video::SColor(255, 100, 100, 255),
  51. video::SColor(255, 255, 150, 50),
  52. video::SColor(255, 220, 220, 100)};
  53. static const u32 usable_colors_count =
  54. sizeof(usable_colors) / sizeof(*usable_colors);
  55. u32 next_color_i = 0;
  56. for (auto &i : m_meta) {
  57. Meta &meta = i.second;
  58. video::SColor color(255, 200, 200, 200);
  59. if (next_color_i < usable_colors_count)
  60. color = usable_colors[next_color_i++];
  61. meta.color = color;
  62. }
  63. s32 graphh = 50;
  64. s32 textx = x_left + m_log_max_size + 15;
  65. s32 textx2 = textx + 200 - 15;
  66. s32 meta_i = 0;
  67. for (const auto &p : m_meta) {
  68. const std::string &id = p.first;
  69. const Meta &meta = p.second;
  70. s32 x = x_left;
  71. s32 y = y_bottom - meta_i * 50;
  72. float show_min = meta.min;
  73. float show_max = meta.max;
  74. if (show_min >= -0.0001 && show_max >= -0.0001) {
  75. if (show_min <= show_max * 0.5)
  76. show_min = 0;
  77. }
  78. const s32 texth = 15;
  79. char buf[10];
  80. if (floorf(show_max) == show_max)
  81. porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_max);
  82. else
  83. porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_max);
  84. font->draw(utf8_to_wide(buf).c_str(),
  85. core::rect<s32>(textx, y - graphh, textx2,
  86. y - graphh + texth),
  87. meta.color);
  88. if (floorf(show_min) == show_min)
  89. porting::mt_snprintf(buf, sizeof(buf), "%.5g", show_min);
  90. else
  91. porting::mt_snprintf(buf, sizeof(buf), "%.3g", show_min);
  92. font->draw(utf8_to_wide(buf).c_str(),
  93. core::rect<s32>(textx, y - texth, textx2, y), meta.color);
  94. font->draw(utf8_to_wide(id).c_str(),
  95. core::rect<s32>(textx, y - graphh / 2 - texth / 2, textx2,
  96. y - graphh / 2 + texth / 2),
  97. meta.color);
  98. s32 graph1y = y;
  99. s32 graph1h = graphh;
  100. bool relativegraph = (show_min != 0 && show_min != show_max);
  101. float lastscaledvalue = 0.0;
  102. bool lastscaledvalue_exists = false;
  103. for (const Piece &piece : m_log) {
  104. float value = 0;
  105. bool value_exists = false;
  106. Profiler::GraphValues::const_iterator k = piece.values.find(id);
  107. if (k != piece.values.end()) {
  108. value = k->second;
  109. value_exists = true;
  110. }
  111. if (!value_exists) {
  112. x++;
  113. lastscaledvalue_exists = false;
  114. continue;
  115. }
  116. float scaledvalue = 1.0;
  117. if (show_max != show_min)
  118. scaledvalue = (value - show_min) / (show_max - show_min);
  119. if (scaledvalue == 1.0 && value == 0) {
  120. x++;
  121. lastscaledvalue_exists = false;
  122. continue;
  123. }
  124. if (relativegraph) {
  125. if (lastscaledvalue_exists) {
  126. s32 ivalue1 = lastscaledvalue * graph1h;
  127. s32 ivalue2 = scaledvalue * graph1h;
  128. driver->draw2DLine(
  129. v2s32(x - 1, graph1y - ivalue1),
  130. v2s32(x, graph1y - ivalue2),
  131. meta.color);
  132. }
  133. lastscaledvalue = scaledvalue;
  134. lastscaledvalue_exists = true;
  135. } else {
  136. s32 ivalue = scaledvalue * graph1h;
  137. driver->draw2DLine(v2s32(x, graph1y),
  138. v2s32(x, graph1y - ivalue), meta.color);
  139. }
  140. x++;
  141. }
  142. meta_i++;
  143. }
  144. }