mirror of
https://github.com/luanti-org/luanti.git
synced 2025-08-31 18:31:04 +00:00
Better F6 profiler (#8750)
Update the profiler names to make more sense of what they actually represent Move the profiler code from header to its source file Use monospace font to align lines Format the statistics line to align better with surrounding values Refresh the profiler each 3 seconds (roughly)
This commit is contained in:
parent
e9ceead81d
commit
539f016c1b
19 changed files with 256 additions and 324 deletions
114
src/profiler.h
114
src/profiler.h
|
@ -29,8 +29,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||
#include "util/timetaker.h"
|
||||
#include "util/numeric.h" // paging()
|
||||
|
||||
#define MAX_PROFILER_TEXT_ROWS 20
|
||||
|
||||
// Global profiler
|
||||
class Profiler;
|
||||
extern Profiler *g_profiler;
|
||||
|
@ -42,110 +40,23 @@ extern Profiler *g_profiler;
|
|||
class Profiler
|
||||
{
|
||||
public:
|
||||
Profiler() = default;
|
||||
Profiler();
|
||||
|
||||
void add(const std::string &name, float value)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
{
|
||||
/* No average shall have been used; mark add used as -2 */
|
||||
std::map<std::string, int>::iterator n = m_avgcounts.find(name);
|
||||
if(n == m_avgcounts.end())
|
||||
m_avgcounts[name] = -2;
|
||||
else{
|
||||
if(n->second == -1)
|
||||
n->second = -2;
|
||||
assert(n->second == -2);
|
||||
}
|
||||
}
|
||||
{
|
||||
std::map<std::string, float>::iterator n = m_data.find(name);
|
||||
if(n == m_data.end())
|
||||
m_data[name] = value;
|
||||
else
|
||||
n->second += value;
|
||||
}
|
||||
}
|
||||
void add(const std::string &name, float value);
|
||||
void avg(const std::string &name, float value);
|
||||
void clear();
|
||||
|
||||
void avg(const std::string &name, float value)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
int &count = m_avgcounts[name];
|
||||
|
||||
assert(count != -2);
|
||||
count = MYMAX(count, 0) + 1;
|
||||
m_data[name] += value;
|
||||
}
|
||||
|
||||
void clear()
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
for (auto &it : m_data) {
|
||||
it.second = 0;
|
||||
}
|
||||
m_avgcounts.clear();
|
||||
}
|
||||
|
||||
void print(std::ostream &o)
|
||||
{
|
||||
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)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
|
||||
u32 minindex, maxindex;
|
||||
paging(m_data.size(), page, pagecount, minindex, maxindex);
|
||||
|
||||
for (std::map<std::string, float>::const_iterator i = m_data.begin();
|
||||
i != m_data.end(); ++i) {
|
||||
if (maxindex == 0)
|
||||
break;
|
||||
maxindex--;
|
||||
|
||||
if (minindex != 0) {
|
||||
minindex--;
|
||||
continue;
|
||||
}
|
||||
|
||||
int avgcount = 1;
|
||||
std::map<std::string, int>::const_iterator n = m_avgcounts.find(i->first);
|
||||
if (n != m_avgcounts.end()) {
|
||||
if(n->second >= 1)
|
||||
avgcount = n->second;
|
||||
}
|
||||
o << " " << i->first << ": ";
|
||||
s32 clampsize = 40;
|
||||
s32 space = clampsize - i->first.size();
|
||||
for(s32 j = 0; j < space; j++) {
|
||||
if (j % 2 == 0 && j < space - 1)
|
||||
o << "-";
|
||||
else
|
||||
o << " ";
|
||||
}
|
||||
o << (i->second / avgcount);
|
||||
o << std::endl;
|
||||
}
|
||||
}
|
||||
float getValue(const std::string &name) const;
|
||||
int getAvgCount(const std::string &name) const;
|
||||
u64 getElapsedMs() const;
|
||||
|
||||
typedef std::map<std::string, float> GraphValues;
|
||||
|
||||
// Returns the line count
|
||||
int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
|
||||
void getPage(GraphValues &o, u32 page, u32 pagecount);
|
||||
|
||||
|
||||
void graphAdd(const std::string &id, float value)
|
||||
{
|
||||
MutexAutoLock lock(m_mutex);
|
||||
|
@ -175,6 +86,7 @@ private:
|
|||
std::map<std::string, float> m_data;
|
||||
std::map<std::string, int> m_avgcounts;
|
||||
std::map<std::string, float> m_graphvalues;
|
||||
u64 m_start_time;
|
||||
};
|
||||
|
||||
enum ScopeProfilerType{
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue