Luanti 5.10.0-dev
 
Loading...
Searching...
No Matches
profiler.h
Go to the documentation of this file.
1// Luanti
2// SPDX-License-Identifier: LGPL-2.1-or-later
3// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
4
5#pragma once
6
7#include "irrlichttypes.h"
8#include <cassert>
9#include <string>
10#include <map>
11#include <ostream>
12
14#include "util/timetaker.h"
15#include "util/numeric.h" // paging()
16
17// Global profiler
18class Profiler;
19extern Profiler *g_profiler;
20
21/*
22 Time profiler
23*/
24
26{
27public:
28 Profiler();
29
30 void add(const std::string &name, float value);
31 void avg(const std::string &name, float value);
32 void max(const std::string &name, float value);
33 void clear();
34
35 float getValue(const std::string &name) const;
36 int getAvgCount(const std::string &name) const;
37 u64 getElapsedMs() const;
38
39 typedef std::map<std::string, float> GraphValues;
40
41 // Returns the line count
42 int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
43 void getPage(GraphValues &o, u32 page, u32 pagecount);
44
45
46 void graphSet(const std::string &id, float value)
47 {
49 m_graphvalues[id] = value;
50 }
51 void graphAdd(const std::string &id, float value)
52 {
54 auto it = m_graphvalues.find(id);
55 if (it == m_graphvalues.end())
56 m_graphvalues.emplace(id, value);
57 else
58 it->second += value;
59 }
60 void graphPop(GraphValues &result)
61 {
63 assert(result.empty());
64 std::swap(result, m_graphvalues);
65 }
66
67 void remove(const std::string& name)
68 {
70 m_data.erase(name);
71 }
72
73private:
74 struct DataPair {
75 float value = 0;
76 int avgcount = 0;
77
78 inline void reset() {
79 value = 0;
80 // negative values are used for type checking, so leave them alone
81 if (avgcount >= 1)
82 avgcount = 0;
83 }
84 inline float getValue() const {
85 return avgcount >= 1 ? (value / avgcount) : value;
86 }
87 };
88
89 std::mutex m_mutex;
90 std::map<std::string, DataPair> m_data;
91 std::map<std::string, float> m_graphvalues;
93};
94
102
103// Note: this class should be kept lightweight.
104
106{
107public:
108 ScopeProfiler(Profiler *profiler, const std::string &name,
110 TimePrecision precision = PRECISION_MILLI);
112
113private:
115 std::string m_name;
119};
Definition profiler.h:26
void graphPop(GraphValues &result)
Definition profiler.h:60
u64 m_start_time
Definition profiler.h:92
void graphAdd(const std::string &id, float value)
Definition profiler.h:51
void getPage(GraphValues &o, u32 page, u32 pagecount)
Definition profiler.cpp:148
void max(const std::string &name, float value)
Definition profiler.cpp:62
void avg(const std::string &name, float value)
Definition profiler.cpp:76
void add(const std::string &name, float value)
Definition profiler.cpp:48
void remove(const std::string &name)
Definition profiler.h:67
int getAvgCount(const std::string &name) const
Definition profiler.cpp:106
Profiler()
Definition profiler.cpp:43
void clear()
Definition profiler.cpp:90
void graphSet(const std::string &id, float value)
Definition profiler.h:46
float getValue(const std::string &name) const
Definition profiler.cpp:98
std::mutex m_mutex
Definition profiler.h:89
u64 getElapsedMs() const
Definition profiler.cpp:115
int print(std::ostream &o, u32 page=1, u32 pagecount=1)
Definition profiler.cpp:120
std::map< std::string, float > m_graphvalues
Definition profiler.h:91
std::map< std::string, DataPair > m_data
Definition profiler.h:90
std::map< std::string, float > GraphValues
Definition profiler.h:39
Definition profiler.h:106
~ScopeProfiler()
Definition profiler.cpp:20
u64 m_time1
Definition profiler.h:116
std::string m_name
Definition profiler.h:115
Profiler * m_profiler
Definition profiler.h:114
ScopeProfiler(Profiler *profiler, const std::string &name, ScopeProfilerType type=SPT_ADD, TimePrecision precision=PRECISION_MILLI)
Definition profiler.cpp:11
TimePrecision m_precision
Definition profiler.h:118
ScopeProfilerType m_type
Definition profiler.h:117
std::lock_guard< std::mutex > MutexAutoLock
Definition mutex_auto_lock.h:31
ScopeProfilerType
Definition profiler.h:96
@ SPT_MAX
Definition profiler.h:100
@ SPT_AVG
Definition profiler.h:98
@ SPT_ADD
Definition profiler.h:97
@ SPT_GRAPH_ADD
Definition profiler.h:99
Profiler * g_profiler
Definition profiler.cpp:9
Definition profiler.h:74
float value
Definition profiler.h:75
int avgcount
Definition profiler.h:76
void reset()
Definition profiler.h:78
float getValue() const
Definition profiler.h:84
TimePrecision
Definition timetaker.h:11
@ PRECISION_MILLI
Definition timetaker.h:13