1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-07-27 17:28:41 +00:00

Some more profiler stuff to get the hang on what really uses CPU

This commit is contained in:
Perttu Ahola 2011-10-16 22:39:35 +03:00
parent b8f2dcea4d
commit 2419d0029a
5 changed files with 66 additions and 24 deletions

View file

@ -41,28 +41,48 @@ public:
void add(const std::string &name, float value)
{
JMutexAutoLock lock(m_mutex);
core::map<std::string, float>::Node *n = m_data.find(name);
if(n == NULL)
{
m_data[name] = value;
/* No average shall have been used; mark add used as -2 */
core::map<std::string, int>::Node *n = m_avgcounts.find(name);
if(n == NULL)
m_avgcounts[name] = -2;
else{
if(n->getValue() == -1)
n->setValue(-2);
assert(n->getValue() == -2);
}
}
else
{
n->setValue(n->getValue() + value);
core::map<std::string, float>::Node *n = m_data.find(name);
if(n == NULL)
m_data[name] = value;
else
n->setValue(n->getValue() + value);
}
}
void lowpass(const std::string &name, float value, float factor)
void avg(const std::string &name, float value)
{
JMutexAutoLock lock(m_mutex);
core::map<std::string, float>::Node *n = m_data.find(name);
if(n == NULL)
{
m_data[name] = value;
core::map<std::string, int>::Node *n = m_avgcounts.find(name);
if(n == NULL)
m_avgcounts[name] = 1;
else{
/* No add shall have been used */
assert(n->getValue() != -2);
if(n->getValue() <= 0)
n->setValue(1);
else
n->setValue(n->getValue() + 1);
}
}
else
{
n->setValue(n->getValue() * (1.0 - 1.0/factor) + value / factor);
core::map<std::string, float>::Node *n = m_data.find(name);
if(n == NULL)
m_data[name] = value;
else
n->setValue(n->getValue() + value);
}
}
@ -75,6 +95,7 @@ public:
{
i.getNode()->setValue(0);
}
m_avgcounts.clear();
}
void print(std::ostream &o)
@ -85,6 +106,12 @@ public:
i.atEnd() == false; i++)
{
std::string name = i.getNode()->getKey();
int avgcount = 1;
core::map<std::string, int>::Node *n = m_avgcounts.find(name);
if(n){
if(n->getValue() >= 1)
avgcount = n->getValue();
}
o<<" "<<name<<": ";
s32 clampsize = 40;
s32 space = clampsize - name.size();
@ -95,7 +122,7 @@ public:
else
o<<" ";
}
o<<i.getNode()->getValue();
o<<(i.getNode()->getValue() / avgcount);
o<<std::endl;
}
}
@ -103,11 +130,12 @@ public:
private:
JMutex m_mutex;
core::map<std::string, float> m_data;
core::map<std::string, int> m_avgcounts;
};
enum ScopeProfilerType{
SPT_ADD,
SPT_LOWPASS
SPT_AVG
};
class ScopeProfiler
@ -138,14 +166,15 @@ public:
{
if(m_timer)
{
u32 duration = m_timer->stop(true);
float duration_ms = m_timer->stop(true);
float duration = duration_ms / 1000.0;
if(m_profiler){
switch(m_type){
case SPT_ADD:
m_profiler->add(m_name, duration);
break;
case SPT_LOWPASS:
m_profiler->lowpass(m_name, duration, 20.0);
case SPT_AVG:
m_profiler->avg(m_name, duration);
break;
}
}