2024-10-28 15:57:39 +01:00
|
|
|
// Luanti
|
|
|
|
// SPDX-License-Identifier: LGPL-2.1-or-later
|
|
|
|
// Copyright (C) 2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2011-05-31 00:15:43 +03:00
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2011-05-31 00:15:43 +03:00
|
|
|
|
2013-08-11 04:09:45 +02:00
|
|
|
#include "irrlichttypes.h"
|
2017-08-18 19:25:07 +02:00
|
|
|
#include <cassert>
|
2011-05-31 00:15:43 +03:00
|
|
|
#include <string>
|
2014-06-29 16:57:50 +02:00
|
|
|
#include <map>
|
2017-08-18 19:25:07 +02:00
|
|
|
#include <ostream>
|
2014-06-29 16:57:50 +02:00
|
|
|
|
2015-04-07 06:13:12 -04:00
|
|
|
#include "threading/mutex_auto_lock.h"
|
2012-06-17 02:40:36 +03:00
|
|
|
#include "util/timetaker.h"
|
2015-04-01 23:01:28 +10:00
|
|
|
#include "util/numeric.h" // paging()
|
2011-05-31 00:15:43 +03:00
|
|
|
|
2015-04-01 23:01:28 +10:00
|
|
|
// Global profiler
|
|
|
|
class Profiler;
|
|
|
|
extern Profiler *g_profiler;
|
|
|
|
|
2011-05-31 00:15:43 +03:00
|
|
|
/*
|
|
|
|
Time profiler
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Profiler
|
|
|
|
{
|
|
|
|
public:
|
2019-08-13 19:56:55 +02:00
|
|
|
Profiler();
|
2011-05-31 00:15:43 +03:00
|
|
|
|
2019-08-13 19:56:55 +02:00
|
|
|
void add(const std::string &name, float value);
|
|
|
|
void avg(const std::string &name, float value);
|
2023-04-09 07:44:43 -10:00
|
|
|
void max(const std::string &name, float value);
|
2019-08-13 19:56:55 +02:00
|
|
|
void clear();
|
2011-10-16 21:16:44 +03:00
|
|
|
|
2019-08-13 19:56:55 +02:00
|
|
|
float getValue(const std::string &name) const;
|
|
|
|
int getAvgCount(const std::string &name) const;
|
|
|
|
u64 getElapsedMs() const;
|
2015-01-22 00:25:06 +11:00
|
|
|
|
2019-08-13 19:56:55 +02:00
|
|
|
typedef std::map<std::string, float> GraphValues;
|
2012-02-01 00:56:30 +01:00
|
|
|
|
2019-08-13 19:56:55 +02:00
|
|
|
// Returns the line count
|
|
|
|
int print(std::ostream &o, u32 page = 1, u32 pagecount = 1);
|
|
|
|
void getPage(GraphValues &o, u32 page, u32 pagecount);
|
2011-05-31 00:15:43 +03:00
|
|
|
|
2012-03-21 03:33:02 +02:00
|
|
|
|
2024-04-10 23:56:34 +02:00
|
|
|
void graphSet(const std::string &id, float value)
|
|
|
|
{
|
|
|
|
MutexAutoLock lock(m_mutex);
|
|
|
|
m_graphvalues[id] = value;
|
|
|
|
}
|
2012-03-21 03:33:02 +02:00
|
|
|
void graphAdd(const std::string &id, float value)
|
|
|
|
{
|
2015-04-07 06:13:12 -04:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2024-04-10 23:56:34 +02:00
|
|
|
auto it = m_graphvalues.find(id);
|
|
|
|
if (it == m_graphvalues.end())
|
|
|
|
m_graphvalues.emplace(id, value);
|
2012-03-21 03:33:02 +02:00
|
|
|
else
|
2024-04-10 23:56:34 +02:00
|
|
|
it->second += value;
|
2012-03-21 03:33:02 +02:00
|
|
|
}
|
2024-04-10 23:56:34 +02:00
|
|
|
void graphPop(GraphValues &result)
|
2012-03-21 03:33:02 +02:00
|
|
|
{
|
2015-04-07 06:13:12 -04:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2024-04-10 23:56:34 +02:00
|
|
|
assert(result.empty());
|
|
|
|
std::swap(result, m_graphvalues);
|
2012-03-21 03:33:02 +02:00
|
|
|
}
|
|
|
|
|
2014-01-06 15:21:22 +01:00
|
|
|
void remove(const std::string& name)
|
|
|
|
{
|
2015-04-07 06:13:12 -04:00
|
|
|
MutexAutoLock lock(m_mutex);
|
2014-01-06 15:21:22 +01:00
|
|
|
m_data.erase(name);
|
|
|
|
}
|
|
|
|
|
2011-05-31 00:15:43 +03:00
|
|
|
private:
|
2024-04-10 23:56:34 +02:00
|
|
|
struct DataPair {
|
|
|
|
float value = 0;
|
|
|
|
int avgcount = 0;
|
|
|
|
|
2024-04-20 14:32:35 +02:00
|
|
|
inline void reset() {
|
|
|
|
value = 0;
|
|
|
|
// negative values are used for type checking, so leave them alone
|
|
|
|
if (avgcount >= 1)
|
|
|
|
avgcount = 0;
|
|
|
|
}
|
2024-04-10 23:56:34 +02:00
|
|
|
inline float getValue() const {
|
|
|
|
return avgcount >= 1 ? (value / avgcount) : value;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-06 16:29:28 +02:00
|
|
|
std::mutex m_mutex;
|
2024-04-10 23:56:34 +02:00
|
|
|
std::map<std::string, DataPair> m_data;
|
2012-03-21 03:33:02 +02:00
|
|
|
std::map<std::string, float> m_graphvalues;
|
2019-08-13 19:56:55 +02:00
|
|
|
u64 m_start_time;
|
2011-10-16 21:16:44 +03:00
|
|
|
};
|
|
|
|
|
2024-04-10 23:56:34 +02:00
|
|
|
enum ScopeProfilerType : u8
|
|
|
|
{
|
|
|
|
SPT_ADD = 1,
|
2012-03-21 15:38:24 +02:00
|
|
|
SPT_AVG,
|
2023-04-09 07:44:43 -10:00
|
|
|
SPT_GRAPH_ADD,
|
|
|
|
SPT_MAX
|
2011-05-31 00:15:43 +03:00
|
|
|
};
|
|
|
|
|
2024-04-10 23:56:34 +02:00
|
|
|
// Note: this class should be kept lightweight.
|
|
|
|
|
2011-05-31 00:15:43 +03:00
|
|
|
class ScopeProfiler
|
|
|
|
{
|
|
|
|
public:
|
2011-10-16 21:16:44 +03:00
|
|
|
ScopeProfiler(Profiler *profiler, const std::string &name,
|
2024-04-10 23:56:34 +02:00
|
|
|
ScopeProfilerType type = SPT_ADD,
|
|
|
|
TimePrecision precision = PRECISION_MILLI);
|
2017-05-26 14:03:36 +02:00
|
|
|
~ScopeProfiler();
|
2024-04-10 23:56:34 +02:00
|
|
|
|
2011-05-31 00:15:43 +03:00
|
|
|
private:
|
2017-06-18 19:55:15 +02:00
|
|
|
Profiler *m_profiler = nullptr;
|
2011-05-31 00:15:43 +03:00
|
|
|
std::string m_name;
|
2024-04-10 23:56:34 +02:00
|
|
|
u64 m_time1;
|
|
|
|
ScopeProfilerType m_type;
|
|
|
|
TimePrecision m_precision;
|
2011-05-31 00:15:43 +03:00
|
|
|
};
|