1 /* 2 Minetest 3 Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com> 4 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 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU Lesser General Public License for more details. 14 15 You should have received a copy of the GNU Lesser General Public License along 16 with this program; if not, write to the Free Software Foundation, Inc., 17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20 #pragma once 21 22 #include "irrlichttypes.h" 23 #include <cassert> 24 #include <string> 25 #include <map> 26 #include <ostream> 27 28 #include "threading/mutex_auto_lock.h" 29 #include "util/timetaker.h" 30 #include "util/numeric.h" // paging() 31 32 // Global profiler 33 class Profiler; 34 extern Profiler *g_profiler; 35 36 /* 37 Time profiler 38 */ 39 40 class Profiler 41 { 42 public: 43 Profiler(); 44 45 void add(const std::string &name, float value); 46 void avg(const std::string &name, float value); 47 void clear(); 48 49 float getValue(const std::string &name) const; 50 int getAvgCount(const std::string &name) const; 51 u64 getElapsedMs() const; 52 53 typedef std::map<std::string, float> GraphValues; 54 55 // Returns the line count 56 int print(std::ostream &o, u32 page = 1, u32 pagecount = 1); 57 void getPage(GraphValues &o, u32 page, u32 pagecount); 58 59 graphAdd(const std::string & id,float value)60 void graphAdd(const std::string &id, float value) 61 { 62 MutexAutoLock lock(m_mutex); 63 std::map<std::string, float>::iterator i = 64 m_graphvalues.find(id); 65 if(i == m_graphvalues.end()) 66 m_graphvalues[id] = value; 67 else 68 i->second += value; 69 } graphGet(GraphValues & result)70 void graphGet(GraphValues &result) 71 { 72 MutexAutoLock lock(m_mutex); 73 result = m_graphvalues; 74 m_graphvalues.clear(); 75 } 76 remove(const std::string & name)77 void remove(const std::string& name) 78 { 79 MutexAutoLock lock(m_mutex); 80 m_avgcounts.erase(name); 81 m_data.erase(name); 82 } 83 84 private: 85 std::mutex m_mutex; 86 std::map<std::string, float> m_data; 87 std::map<std::string, int> m_avgcounts; 88 std::map<std::string, float> m_graphvalues; 89 u64 m_start_time; 90 }; 91 92 enum ScopeProfilerType{ 93 SPT_ADD, 94 SPT_AVG, 95 SPT_GRAPH_ADD 96 }; 97 98 class ScopeProfiler 99 { 100 public: 101 ScopeProfiler(Profiler *profiler, const std::string &name, 102 ScopeProfilerType type = SPT_ADD); 103 ~ScopeProfiler(); 104 private: 105 Profiler *m_profiler = nullptr; 106 std::string m_name; 107 TimeTaker *m_timer = nullptr; 108 enum ScopeProfilerType m_type; 109 }; 110