profiler.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  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 clear();
  38. float getValue(const std::string &name) const;
  39. int getAvgCount(const std::string &name) const;
  40. u64 getElapsedMs() const;
  41. typedef std::map<std::string, float> GraphValues;
  42. // Returns the line count
  43. int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
  44. void getPage(GraphValues &o, u32 page, u32 pagecount);
  45. void graphAdd(const std::string &id, float value)
  46. {
  47. MutexAutoLock lock(m_mutex);
  48. std::map<std::string, float>::iterator i =
  49. m_graphvalues.find(id);
  50. if(i == m_graphvalues.end())
  51. m_graphvalues[id] = value;
  52. else
  53. i->second += value;
  54. }
  55. void graphGet(GraphValues &result)
  56. {
  57. MutexAutoLock lock(m_mutex);
  58. result = m_graphvalues;
  59. m_graphvalues.clear();
  60. }
  61. void remove(const std::string& name)
  62. {
  63. MutexAutoLock lock(m_mutex);
  64. m_avgcounts.erase(name);
  65. m_data.erase(name);
  66. }
  67. private:
  68. std::mutex m_mutex;
  69. std::map<std::string, float> m_data;
  70. std::map<std::string, int> m_avgcounts;
  71. std::map<std::string, float> m_graphvalues;
  72. u64 m_start_time;
  73. };
  74. enum ScopeProfilerType{
  75. SPT_ADD,
  76. SPT_AVG,
  77. SPT_GRAPH_ADD
  78. };
  79. class ScopeProfiler
  80. {
  81. public:
  82. ScopeProfiler(Profiler *profiler, const std::string &name,
  83. ScopeProfilerType type = SPT_ADD);
  84. ~ScopeProfiler();
  85. private:
  86. Profiler *m_profiler = nullptr;
  87. std::string m_name;
  88. TimeTaker *m_timer = nullptr;
  89. enum ScopeProfilerType m_type;
  90. };