mirror of
https://github.com/minetest/minetest.git
synced 2024-10-04 00:43:17 +02:00
Add documentation to profiler and profiler graph
This commit is contained in:
parent
85878d894a
commit
7be7d15a02
@ -30,11 +30,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
class ProfilerGraph
|
class ProfilerGraph
|
||||||
{
|
{
|
||||||
private:
|
private:
|
||||||
|
// One-frame slice of graph values.
|
||||||
struct Piece
|
struct Piece
|
||||||
{
|
{
|
||||||
Piece(Profiler::GraphValues v) : values(std::move(v)) {}
|
Piece(Profiler::GraphValues v) : values(std::move(v)) {}
|
||||||
Profiler::GraphValues values;
|
Profiler::GraphValues values;
|
||||||
};
|
};
|
||||||
|
// Data for drawing a one graph. Updates every frame.
|
||||||
struct Meta
|
struct Meta
|
||||||
{
|
{
|
||||||
float min;
|
float min;
|
||||||
@ -54,6 +56,8 @@ public:
|
|||||||
|
|
||||||
ProfilerGraph() = default;
|
ProfilerGraph() = default;
|
||||||
|
|
||||||
|
// Adds graph values to the end of graph (rendered at right side) and
|
||||||
|
// removes the oldest ones (beyond the `m_log_max_size`).
|
||||||
void put(const Profiler::GraphValues &values);
|
void put(const Profiler::GraphValues &values);
|
||||||
|
|
||||||
void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
|
void draw(s32 x_left, s32 y_bottom, video::IVideoDriver *driver,
|
||||||
|
@ -29,21 +29,55 @@ with this program; if not, write to the Free Software Foundation, Inc.,
|
|||||||
#include "util/timetaker.h"
|
#include "util/timetaker.h"
|
||||||
#include "util/numeric.h" // paging()
|
#include "util/numeric.h" // paging()
|
||||||
|
|
||||||
// Global profiler
|
|
||||||
class Profiler;
|
class Profiler;
|
||||||
|
// Global profiler. Its data is available for user.
|
||||||
extern Profiler *g_profiler;
|
extern Profiler *g_profiler;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Time profiler
|
Time profiler
|
||||||
*/
|
|
||||||
|
|
||||||
|
Collects data (time intervals and other) in two independent cycles.
|
||||||
|
|
||||||
|
- Cycle for profiler graph lasts for one frame and managed in
|
||||||
|
`Game::updateProfilerGraphs()`, result is displayed by `ProfilerGraph`.
|
||||||
|
|
||||||
|
- Cycle for all other data lasts for `profiler_print_interval` seconds (user
|
||||||
|
setting) and managed in `Game::updateProfilers()`, result is given in text
|
||||||
|
form by `print` function. User can view it via in-game profiler menu or
|
||||||
|
"/profiler" command.
|
||||||
|
|
||||||
|
All data is cleared at cycle end.
|
||||||
|
*/
|
||||||
class Profiler
|
class Profiler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
Profiler();
|
Profiler();
|
||||||
|
|
||||||
|
/*
|
||||||
|
Adds value to given profiler entry (`name`). Does not change count
|
||||||
|
in the entry.
|
||||||
|
|
||||||
|
As a resut, profiler will give *sum* of record values as value and "1" as avgCount.
|
||||||
|
(If only this method is used for given entry between profiler updates)
|
||||||
|
*/
|
||||||
void add(const std::string &name, float value);
|
void add(const std::string &name, float value);
|
||||||
|
|
||||||
|
/*
|
||||||
|
Adds value to given profiler entry (`name`) and increases its record count
|
||||||
|
by 1.
|
||||||
|
|
||||||
|
As a resut, profiler will give *average* of record vaues as value and count of
|
||||||
|
records as avgCount.
|
||||||
|
(If only this method is used for given entry between profiler updates)
|
||||||
|
*/
|
||||||
void avg(const std::string &name, float value);
|
void avg(const std::string &name, float value);
|
||||||
|
/*
|
||||||
|
Sets value to given profiler entry (`name`) if it is larger then existing one and
|
||||||
|
increases record coutn by 1.
|
||||||
|
|
||||||
|
As a resut, profiler will give *max* record value as value and count of records as avgCount.
|
||||||
|
(If only this method is used for given entry between profiler updates)
|
||||||
|
*/
|
||||||
void max(const std::string &name, float value);
|
void max(const std::string &name, float value);
|
||||||
void clear();
|
void clear();
|
||||||
|
|
||||||
@ -55,6 +89,7 @@ public:
|
|||||||
|
|
||||||
// Returns the line count
|
// Returns the line count
|
||||||
int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
|
int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
|
||||||
|
// Writes values on page into `o`.
|
||||||
void getPage(GraphValues &o, u32 page, u32 pagecount);
|
void getPage(GraphValues &o, u32 page, u32 pagecount);
|
||||||
|
|
||||||
|
|
||||||
@ -87,7 +122,10 @@ public:
|
|||||||
|
|
||||||
private:
|
private:
|
||||||
struct DataPair {
|
struct DataPair {
|
||||||
|
// Recorded value. Recording method depends on used functions (add/avg/max)
|
||||||
|
// of profiler
|
||||||
float value = 0;
|
float value = 0;
|
||||||
|
// Count of records.
|
||||||
int avgcount = 0;
|
int avgcount = 0;
|
||||||
|
|
||||||
inline void reset() {
|
inline void reset() {
|
||||||
@ -102,33 +140,53 @@ private:
|
|||||||
};
|
};
|
||||||
|
|
||||||
std::mutex m_mutex;
|
std::mutex m_mutex;
|
||||||
|
// All the profiler entries, stored untill `clear()` call.
|
||||||
std::map<std::string, DataPair> m_data;
|
std::map<std::string, DataPair> m_data;
|
||||||
|
// Values for profiler graph collected untill next frame draw. Value history
|
||||||
|
// are stored in `ProfilerGraph`.
|
||||||
std::map<std::string, float> m_graphvalues;
|
std::map<std::string, float> m_graphvalues;
|
||||||
u64 m_start_time;
|
u64 m_start_time;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
enum ScopeProfilerType : u8
|
enum ScopeProfilerType : u8
|
||||||
{
|
{
|
||||||
|
// Accumulate measurements untill profiler update.
|
||||||
|
// Profiler will print sum as value and "1" as count.
|
||||||
SPT_ADD = 1,
|
SPT_ADD = 1,
|
||||||
|
// Accumulate measurements and record count untill profiler update.
|
||||||
|
// Profiler will print average value as value and record count as count.
|
||||||
SPT_AVG,
|
SPT_AVG,
|
||||||
|
// Accumulate measurements until frame update and then add sum to profiler graph.
|
||||||
SPT_GRAPH_ADD,
|
SPT_GRAPH_ADD,
|
||||||
|
// Save only the largest recorded value. Profiler will print them as value and "1" as count.
|
||||||
SPT_MAX
|
SPT_MAX
|
||||||
};
|
};
|
||||||
|
|
||||||
// Note: this class should be kept lightweight.
|
/*
|
||||||
|
A class for time measurements. Each created object records time from its
|
||||||
|
construction till scope end.
|
||||||
|
|
||||||
|
Note: this class should be kept lightweight.
|
||||||
|
*/
|
||||||
class ScopeProfiler
|
class ScopeProfiler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
/*
|
||||||
|
Begins record untill scope end. Result will be added to `name` profiling
|
||||||
|
entry in `profiler` (profiling entries are created/deleted automatically).
|
||||||
|
*/
|
||||||
ScopeProfiler(Profiler *profiler, const std::string &name,
|
ScopeProfiler(Profiler *profiler, const std::string &name,
|
||||||
ScopeProfilerType type = SPT_ADD,
|
ScopeProfilerType type = SPT_ADD,
|
||||||
TimePrecision precision = PRECISION_MILLI);
|
TimePrecision precision = PRECISION_MILLI);
|
||||||
|
|
||||||
|
// Ends record and logs result in profiler.
|
||||||
~ScopeProfiler();
|
~ScopeProfiler();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Profiler *m_profiler = nullptr;
|
Profiler *m_profiler = nullptr;
|
||||||
std::string m_name;
|
std::string m_name;
|
||||||
u64 m_time1;
|
u64 m_time1; // Record start time
|
||||||
ScopeProfilerType m_type;
|
ScopeProfilerType m_type;
|
||||||
TimePrecision m_precision;
|
TimePrecision m_precision;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user