2020-12-06 17:34:16 +01:00
|
|
|
#pragma once
|
2020-10-18 14:00:10 +02:00
|
|
|
|
2020-12-12 13:01:29 +01:00
|
|
|
#include "util/types.hpp"
|
2020-10-18 14:00:10 +02:00
|
|
|
#include "util/logs.hpp"
|
2025-10-05 18:28:03 +02:00
|
|
|
#include "rx/tsc.hpp"
|
2020-10-18 14:00:10 +02:00
|
|
|
#include "system_config.h"
|
|
|
|
|
#include <array>
|
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
|
|
LOG_CHANNEL(perf_log, "PERF");
|
|
|
|
|
|
|
|
|
|
// TODO: constexpr with the help of bitcast
|
|
|
|
|
template <auto Name>
|
|
|
|
|
inline const auto perf_name = []
|
|
|
|
|
{
|
|
|
|
|
constexpr auto short_name = Name;
|
|
|
|
|
std::array<char, sizeof(Name) + 1> result{};
|
|
|
|
|
std::memcpy(result.data(), &short_name, sizeof(Name));
|
|
|
|
|
return result;
|
|
|
|
|
}();
|
|
|
|
|
|
|
|
|
|
class perf_stat_base
|
|
|
|
|
{
|
|
|
|
|
atomic_t<u64> m_log[66]{};
|
|
|
|
|
|
|
|
|
|
protected:
|
|
|
|
|
// Print accumulated values
|
2021-04-09 21:12:47 +02:00
|
|
|
void print(const char* name) const noexcept;
|
2020-10-18 14:00:10 +02:00
|
|
|
|
|
|
|
|
// Accumulate values from a thread
|
|
|
|
|
void push(u64 ns[66]) noexcept;
|
|
|
|
|
|
2020-12-21 15:12:05 +01:00
|
|
|
// Get end time; accumulate value to the TLS
|
2021-04-09 21:12:47 +02:00
|
|
|
static void push(u64 data[66], u64 start_time, const char* name) noexcept;
|
2020-12-21 15:12:05 +01:00
|
|
|
|
2020-11-19 09:12:59 +01:00
|
|
|
// Register TLS storage for stats
|
|
|
|
|
static void add(u64 ns[66], const char* name) noexcept;
|
|
|
|
|
|
|
|
|
|
// Unregister TLS storage and drain its data
|
|
|
|
|
static void remove(u64 ns[66], const char* name) noexcept;
|
|
|
|
|
|
2020-10-18 14:00:10 +02:00
|
|
|
public:
|
|
|
|
|
perf_stat_base() noexcept = default;
|
|
|
|
|
|
|
|
|
|
perf_stat_base(const perf_stat_base&) = delete;
|
|
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
perf_stat_base& operator=(const perf_stat_base&) = delete;
|
2020-10-18 14:00:10 +02:00
|
|
|
|
|
|
|
|
~perf_stat_base() {}
|
2020-11-19 09:12:59 +01:00
|
|
|
|
|
|
|
|
// Collect all data, report it, and clean
|
|
|
|
|
static void report() noexcept;
|
2020-10-18 14:00:10 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Object that prints event length stats at the end
|
|
|
|
|
template <auto ShortName>
|
|
|
|
|
class perf_stat final : public perf_stat_base
|
|
|
|
|
{
|
|
|
|
|
static inline thread_local struct perf_stat_local
|
|
|
|
|
{
|
|
|
|
|
// Local non-atomic values for increments
|
|
|
|
|
u64 m_log[66]{};
|
|
|
|
|
|
2020-11-19 09:12:59 +01:00
|
|
|
perf_stat_local() noexcept
|
|
|
|
|
{
|
|
|
|
|
perf_stat_base::add(m_log, perf_name<ShortName>.data());
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 14:00:10 +02:00
|
|
|
~perf_stat_local()
|
|
|
|
|
{
|
2020-11-19 09:12:59 +01:00
|
|
|
perf_stat_base::remove(m_log, perf_name<ShortName>.data());
|
2020-10-18 14:00:10 +02:00
|
|
|
}
|
2020-11-19 09:12:59 +01:00
|
|
|
|
2020-10-18 14:00:10 +02:00
|
|
|
} g_tls_perf_stat;
|
|
|
|
|
|
|
|
|
|
public:
|
2021-02-09 10:33:50 +01:00
|
|
|
static FORCE_INLINE SAFE_BUFFERS(void) push(u64 start_time) noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
2020-12-21 15:12:05 +01:00
|
|
|
perf_stat_base::push(g_tls_perf_stat.m_log, start_time, perf_name<ShortName>.data());
|
2020-10-18 14:00:10 +02:00
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// Object that prints event length at the end
|
|
|
|
|
template <auto ShortName, auto... SubEvents>
|
|
|
|
|
class perf_meter
|
|
|
|
|
{
|
|
|
|
|
// Initialize array (possibly only 1 element) with timestamp
|
2020-10-30 01:19:13 +01:00
|
|
|
u64 m_timestamps[1 + sizeof...(SubEvents)];
|
2020-10-18 14:00:10 +02:00
|
|
|
|
|
|
|
|
public:
|
2021-02-09 10:33:50 +01:00
|
|
|
FORCE_INLINE SAFE_BUFFERS() perf_meter() noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
2020-10-30 01:19:13 +01:00
|
|
|
restart();
|
2020-10-18 14:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
2022-10-05 17:30:12 +02:00
|
|
|
FORCE_INLINE SAFE_BUFFERS() perf_meter(int) noexcept
|
|
|
|
|
{
|
|
|
|
|
std::fill(std::begin(m_timestamps), std::end(m_timestamps), 0);
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
FORCE_INLINE SAFE_BUFFERS(operator bool)() const noexcept
|
2022-10-05 17:30:12 +02:00
|
|
|
{
|
|
|
|
|
return m_timestamps[0] != 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-18 14:00:10 +02:00
|
|
|
// Copy first timestamp
|
|
|
|
|
template <auto SN, auto... S>
|
2021-02-09 10:33:50 +01:00
|
|
|
FORCE_INLINE SAFE_BUFFERS() perf_meter(const perf_meter<SN, S...>& r) noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
m_timestamps[0] = r.get();
|
|
|
|
|
std::memset(m_timestamps + 1, 0, sizeof(m_timestamps) - sizeof(u64));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <auto SN, auto... S>
|
2025-04-05 21:50:45 +02:00
|
|
|
SAFE_BUFFERS()
|
|
|
|
|
perf_meter(perf_meter<SN, S...>&& r) noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
m_timestamps[0] = r.get();
|
|
|
|
|
r.reset();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Copy first timestamp
|
|
|
|
|
template <auto SN, auto... S>
|
2025-04-05 21:50:45 +02:00
|
|
|
SAFE_BUFFERS(perf_meter&)
|
|
|
|
|
operator=(const perf_meter<SN, S...>& r) noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
m_timestamps[0] = r.get();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
template <auto SN, auto... S>
|
2025-04-05 21:50:45 +02:00
|
|
|
SAFE_BUFFERS(perf_meter&)
|
|
|
|
|
operator=(perf_meter<SN, S...>& r) noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
m_timestamps[0] = r.get();
|
|
|
|
|
r.reset();
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Push subevent data in array
|
2020-12-18 08:39:54 +01:00
|
|
|
template <auto Event, usz Index = 0>
|
2025-04-05 21:50:45 +02:00
|
|
|
SAFE_BUFFERS(void)
|
|
|
|
|
push() noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
// TODO: should use more efficient search with type comparison, then value comparison, or pattern matching
|
|
|
|
|
if constexpr (std::array<bool, sizeof...(SubEvents)>{(SubEvents == Event)...}[Index])
|
|
|
|
|
{
|
|
|
|
|
// Push actual timestamp into an array
|
2025-10-05 18:28:03 +02:00
|
|
|
m_timestamps[Index + 1] = rx::get_tsc();
|
2020-10-18 14:00:10 +02:00
|
|
|
}
|
|
|
|
|
else if constexpr (Index < sizeof...(SubEvents))
|
|
|
|
|
{
|
|
|
|
|
// Proceed search recursively
|
|
|
|
|
push<Event, Index + 1>();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Obtain initial timestamp
|
|
|
|
|
u64 get() const noexcept
|
|
|
|
|
{
|
|
|
|
|
return m_timestamps[0];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Disable this counter
|
2021-02-09 10:33:50 +01:00
|
|
|
FORCE_INLINE SAFE_BUFFERS(void) reset() noexcept
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
m_timestamps[0] = 0;
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-30 01:19:13 +01:00
|
|
|
// Re-initialize first timestamp
|
2021-02-09 10:33:50 +01:00
|
|
|
FORCE_INLINE SAFE_BUFFERS(void) restart() noexcept
|
2020-10-30 01:19:13 +01:00
|
|
|
{
|
2025-10-05 18:28:03 +02:00
|
|
|
m_timestamps[0] = rx::get_tsc();
|
2020-10-30 01:19:13 +01:00
|
|
|
std::memset(m_timestamps + 1, 0, sizeof(m_timestamps) - sizeof(u64));
|
|
|
|
|
}
|
|
|
|
|
|
2025-04-05 21:50:45 +02:00
|
|
|
SAFE_BUFFERS()
|
|
|
|
|
~perf_meter()
|
2020-10-18 14:00:10 +02:00
|
|
|
{
|
|
|
|
|
// Disabled counter
|
|
|
|
|
if (!m_timestamps[0]) [[unlikely]]
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!g_cfg.core.perf_report) [[likely]]
|
|
|
|
|
{
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Register perf stat in nanoseconds
|
2020-11-25 05:26:37 +01:00
|
|
|
perf_stat<ShortName>::push(m_timestamps[0]);
|
2020-10-18 14:00:10 +02:00
|
|
|
|
|
|
|
|
// TODO: handle push(), currently ignored
|
|
|
|
|
}
|
|
|
|
|
};
|