]> git.lizzy.rs Git - minetest.git/blobdiff - src/profiler.h
Fix client profiler print interval
[minetest.git] / src / profiler.h
index 8eaf18df5effb290e88ec49d2a37758d94e82121..129118ef6216c567281044f1e8b8741f009f764a 100644 (file)
@@ -41,28 +41,48 @@ class Profiler
        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 @@ class Profiler
                {
                        i.getNode()->setValue(0);
                }
+               m_avgcounts.clear();
        }
 
        void print(std::ostream &o)
@@ -85,6 +106,12 @@ class Profiler
                                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 @@ class Profiler
                                else
                                        o<<" ";
                        }
-                       o<<i.getNode()->getValue();
+                       o<<(i.getNode()->getValue() / avgcount);
                        o<<std::endl;
                }
        }
@@ -103,11 +130,12 @@ class Profiler
 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 @@ class ScopeProfiler
        {
                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;
                                }
                        }