2023-02-28 01:15:26 +00:00
|
|
|
// AUTOGENERATED COPYRIGHT HEADER START
|
|
|
|
// Copyright (C) 2020-2023 Michael Fabian 'Xaymar' Dirks <info@xaymar.com>
|
|
|
|
// AUTOGENERATED COPYRIGHT HEADER END
|
2020-03-26 13:57:24 +00:00
|
|
|
|
|
|
|
#include "util-profiler.hpp"
|
2022-08-29 10:29:44 +00:00
|
|
|
|
|
|
|
#include "warning-disable.hpp"
|
2020-03-26 13:57:24 +00:00
|
|
|
#include <iterator>
|
2022-08-29 10:29:44 +00:00
|
|
|
#include "warning-enable.hpp"
|
2020-03-26 13:57:24 +00:00
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
streamfx::util::profiler::profiler() {}
|
2020-03-26 13:57:24 +00:00
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
streamfx::util::profiler::~profiler() {}
|
2020-03-26 13:57:24 +00:00
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
std::shared_ptr<streamfx::util::profiler::instance> streamfx::util::profiler::track()
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
2021-06-23 11:03:40 +00:00
|
|
|
return std::make_shared<streamfx::util::profiler::instance>(shared_from_this());
|
2020-03-26 13:57:24 +00:00
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
void streamfx::util::profiler::track(std::chrono::nanoseconds duration)
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> ul(_timings_lock);
|
|
|
|
auto itr = _timings.find(duration);
|
|
|
|
if (itr == _timings.end()) {
|
2020-07-12 16:41:50 +00:00
|
|
|
_timings.emplace(duration, 1u);
|
2020-03-26 13:57:24 +00:00
|
|
|
} else {
|
|
|
|
itr->second++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
uint64_t streamfx::util::profiler::count()
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
|
|
|
uint64_t count = 0;
|
|
|
|
|
|
|
|
std::map<std::chrono::nanoseconds, size_t> copy_timings;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> ul(_timings_lock);
|
|
|
|
copy(_timings.begin(), _timings.end(), std::inserter(copy_timings, copy_timings.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto kv : copy_timings) {
|
|
|
|
count += kv.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
std::chrono::nanoseconds streamfx::util::profiler::total_duration()
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
|
|
|
std::chrono::nanoseconds duration{0};
|
|
|
|
|
|
|
|
std::map<std::chrono::nanoseconds, size_t> copy_timings;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> ul(_timings_lock);
|
|
|
|
copy(_timings.begin(), _timings.end(), std::inserter(copy_timings, copy_timings.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto kv : copy_timings) {
|
|
|
|
duration += kv.first * kv.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return duration;
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
double_t streamfx::util::profiler::average_duration()
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
|
|
|
std::chrono::nanoseconds duration{0};
|
|
|
|
uint64_t count = 0;
|
|
|
|
|
|
|
|
std::map<std::chrono::nanoseconds, size_t> copy_timings;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> ul(_timings_lock);
|
|
|
|
copy(_timings.begin(), _timings.end(), std::inserter(copy_timings, copy_timings.end()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto kv : copy_timings) {
|
|
|
|
duration += kv.first * kv.second;
|
|
|
|
count += kv.second;
|
|
|
|
}
|
|
|
|
|
|
|
|
return double_t(duration.count()) / double_t(count);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
inline bool is_equal(T a, T b, T c)
|
|
|
|
{
|
|
|
|
return (a == b) || ((a >= (b - c)) && (a <= (b + c)));
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
std::chrono::nanoseconds streamfx::util::profiler::percentile(double_t percentile, bool by_time)
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
2020-04-05 16:52:06 +00:00
|
|
|
constexpr double_t edge = 0.00005;
|
|
|
|
uint64_t calls = count();
|
2020-03-26 13:57:24 +00:00
|
|
|
|
|
|
|
std::map<std::chrono::nanoseconds, size_t> copy_timings;
|
|
|
|
{
|
|
|
|
std::unique_lock<std::mutex> ul(_timings_lock);
|
|
|
|
copy(_timings.begin(), _timings.end(), inserter(copy_timings, copy_timings.end()));
|
|
|
|
}
|
|
|
|
if (by_time) { // Return by time percentile.
|
|
|
|
// Find largest and smallest time.
|
|
|
|
std::chrono::nanoseconds smallest = copy_timings.begin()->first;
|
|
|
|
std::chrono::nanoseconds largest = copy_timings.rbegin()->first;
|
|
|
|
std::chrono::nanoseconds variance = largest - smallest;
|
|
|
|
|
|
|
|
for (auto kv : copy_timings) {
|
|
|
|
double_t kv_pct = double_t((kv.first - smallest).count()) / double_t(variance.count());
|
2020-04-18 14:40:53 +00:00
|
|
|
if (is_equal<double_t>(kv_pct, percentile, edge) || (kv_pct > percentile)) {
|
2020-03-26 13:57:24 +00:00
|
|
|
return std::chrono::nanoseconds(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else { // Return by call percentile.
|
|
|
|
if (percentile == 0.0) {
|
|
|
|
return copy_timings.begin()->first;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint64_t accu_calls_now = 0;
|
|
|
|
for (auto kv : copy_timings) {
|
|
|
|
uint64_t accu_calls_last = accu_calls_now;
|
|
|
|
accu_calls_now += kv.second;
|
|
|
|
|
|
|
|
double_t percentile_last = double_t(accu_calls_last) / double_t(calls);
|
|
|
|
double_t percentile_now = double_t(accu_calls_now) / double_t(calls);
|
|
|
|
|
2020-04-18 14:40:53 +00:00
|
|
|
if (is_equal<double_t>(percentile, percentile_now, edge)
|
2020-03-26 13:57:24 +00:00
|
|
|
|| ((percentile_last < percentile) && (percentile_now > percentile))) {
|
|
|
|
return std::chrono::nanoseconds(kv.first);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return std::chrono::nanoseconds(-1);
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
streamfx::util::profiler::instance::instance(std::shared_ptr<streamfx::util::profiler> parent)
|
2020-03-26 13:57:24 +00:00
|
|
|
: _parent(parent), _start(std::chrono::high_resolution_clock::now())
|
|
|
|
{}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
streamfx::util::profiler::instance::~instance()
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
|
|
|
auto end = std::chrono::high_resolution_clock::now();
|
|
|
|
auto dur = end - _start;
|
|
|
|
if (_parent) {
|
|
|
|
_parent->track(dur);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
void streamfx::util::profiler::instance::cancel()
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
|
|
|
_parent.reset();
|
|
|
|
}
|
|
|
|
|
2021-06-23 11:03:40 +00:00
|
|
|
void streamfx::util::profiler::instance::reparent(std::shared_ptr<streamfx::util::profiler> parent)
|
2020-03-26 13:57:24 +00:00
|
|
|
{
|
2020-04-23 23:58:14 +00:00
|
|
|
_parent = parent;
|
2020-03-26 13:57:24 +00:00
|
|
|
}
|