]> git.lizzy.rs Git - dragonfireclient.git/blobdiff - src/profiler.h
fix infinite spawners
[dragonfireclient.git] / src / profiler.h
index 56b02688037805cca13443b4b6ca2f062d2d76a4..78d3b08e06493052362b3d443d946e72d7832a23 100644 (file)
@@ -20,13 +20,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 #ifndef PROFILER_HEADER
 #define PROFILER_HEADER
 
-#include "irrlichttypes_bloated.h"
+#include "irrlichttypes.h"
 #include <string>
-#include <jmutex.h>
-#include <jmutexautolock.h>
 #include <map>
+
+#include "jthread/jmutex.h"
+#include "jthread/jmutexautolock.h"
 #include "util/timetaker.h"
-#include "util/numeric.h" // paging()
+#include "util/numeric.h"      // paging()
+#include "debug.h"             // assert()
+
+#define MAX_PROFILER_TEXT_ROWS 20
+
+// Global profiler
+class Profiler;
+extern Profiler *g_profiler;
 
 /*
        Time profiler
@@ -37,7 +45,6 @@ class Profiler
 public:
        Profiler()
        {
-               m_mutex.Init();
        }
 
        void add(const std::string &name, float value)
@@ -66,23 +73,11 @@ class Profiler
        void avg(const std::string &name, float value)
        {
                JMutexAutoLock lock(m_mutex);
-               {
-                       std::map<std::string, int>::iterator n = m_avgcounts.find(name);
-                       if(n == m_avgcounts.end())
-                               m_avgcounts[name] = 1;
-                       else{
-                               /* No add shall have been used */
-                               assert(n->second != -2);
-                               n->second = std::max(n->second, 0) + 1;
-                       }
-               }
-               {
-                       std::map<std::string, float>::iterator n = m_data.find(name);
-                       if(n == m_data.end())
-                               m_data[name] = value;
-                       else
-                               n->second += value;
-               }
+               int &count = m_avgcounts[name];
+
+               assert(count != -2);
+               count = MYMAX(count, 0) + 1;
+               m_data[name] += value;
        }
 
        void clear()
@@ -102,6 +97,21 @@ class Profiler
                printPage(o, 1, 1);
        }
 
+       float getValue(const std::string &name) const
+       {
+               std::map<std::string, float>::const_iterator numerator = m_data.find(name);
+               if (numerator == m_data.end())
+                       return 0.f;
+
+               std::map<std::string, int>::const_iterator denominator = m_avgcounts.find(name);
+               if (denominator != m_avgcounts.end()){
+                       if (denominator->second >= 1)
+                               return numerator->second / denominator->second;
+               }
+
+               return numerator->second;
+       }
+
        void printPage(std::ostream &o, u32 page, u32 pagecount)
        {
                JMutexAutoLock lock(m_mutex);
@@ -164,6 +174,13 @@ class Profiler
                m_graphvalues.clear();
        }
 
+       void remove(const std::string& name)
+       {
+               JMutexAutoLock lock(m_mutex);
+               m_avgcounts.erase(name);
+               m_data.erase(name);
+       }
+
 private:
        JMutex m_mutex;
        std::map<std::string, float> m_data;