Make Connection::Send cancel silently if peer doesn't exist.
[oweals/minetest.git] / src / profiler.h
1 /*
2 Minetest-c55
3 Copyright (C) 2011 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 General Public License as published by
7 the Free Software Foundation; either version 2 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 General Public License for more details.
14
15 You should have received a copy of the GNU 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 #ifndef PROFILER_HEADER
21 #define PROFILER_HEADER
22
23 #include "common_irrlicht.h"
24 #include <string>
25 #include "utility.h"
26 #include <jmutex.h>
27 #include <jmutexautolock.h>
28
29 /*
30         Time profiler
31 */
32
33 class Profiler
34 {
35 public:
36         Profiler()
37         {
38                 m_mutex.Init();
39         }
40
41         void add(const std::string &name, u32 duration)
42         {
43                 JMutexAutoLock lock(m_mutex);
44                 core::map<std::string, u32>::Node *n = m_data.find(name);
45                 if(n == NULL)
46                 {
47                         m_data[name] = duration;
48                 }
49                 else
50                 {
51                         n->setValue(n->getValue()+duration);
52                 }
53         }
54
55         void clear()
56         {
57                 JMutexAutoLock lock(m_mutex);
58                 for(core::map<std::string, u32>::Iterator
59                                 i = m_data.getIterator();
60                                 i.atEnd() == false; i++)
61                 {
62                         i.getNode()->setValue(0);
63                 }
64         }
65
66         void print(std::ostream &o)
67         {
68                 JMutexAutoLock lock(m_mutex);
69                 for(core::map<std::string, u32>::Iterator
70                                 i = m_data.getIterator();
71                                 i.atEnd() == false; i++)
72                 {
73                         std::string name = i.getNode()->getKey();
74                         o<<name<<": ";
75                         s32 clampsize = 40;
76                         s32 space = clampsize-name.size();
77                         for(s32 j=0; j<space; j++)
78                         {
79                                 if(j%2 == 0 && j < space - 1)
80                                         o<<"-";
81                                 else
82                                         o<<" ";
83                         }
84                         o<<i.getNode()->getValue();
85                         o<<std::endl;
86                 }
87         }
88
89 private:
90         JMutex m_mutex;
91         core::map<std::string, u32> m_data;
92 };
93
94 class ScopeProfiler
95 {
96 public:
97         ScopeProfiler(Profiler *profiler, const std::string &name):
98                 m_profiler(profiler),
99                 m_name(name),
100                 m_timer(NULL)
101         {
102                 if(m_profiler)
103                         m_timer = new TimeTaker(m_name.c_str());
104         }
105         // name is copied
106         ScopeProfiler(Profiler *profiler, const char *name):
107                 m_profiler(profiler),
108                 m_name(name),
109                 m_timer(NULL)
110         {
111                 if(m_profiler)
112                         m_timer = new TimeTaker(m_name.c_str());
113         }
114         ~ScopeProfiler()
115         {
116                 if(m_timer)
117                 {
118                         u32 duration = m_timer->stop(true);
119                         if(m_profiler)
120                                 m_profiler->add(m_name, duration);
121                         delete m_timer;
122                 }
123         }
124 private:
125         Profiler *m_profiler;
126         std::string m_name;
127         TimeTaker *m_timer;
128 };
129
130 #endif
131