Support move operations in prof::profiler

This commit is contained in:
Nikita Lisitsa 2025-04-13 17:36:05 +03:00
parent 6a8563cce9
commit c75809aa75
2 changed files with 38 additions and 6 deletions

View file

@ -3,7 +3,7 @@
#include <psemek/util/clock.hpp> #include <psemek/util/clock.hpp>
#include <psemek/log/level.hpp> #include <psemek/log/level.hpp>
#include <string_view> #include <string>
namespace psemek::prof namespace psemek::prof
{ {
@ -13,8 +13,13 @@ namespace psemek::prof
struct profiler struct profiler
{ {
profiler(std::string const & name); profiler(std::string const & name);
profiler(profiler && other);
profiler(profiler const & other) = delete;
~ profiler(); profiler & operator = (profiler && other);
profiler & operator = (profiler const & other) = delete;
~profiler();
static void start_frame(std::string const & name); static void start_frame(std::string const & name);
static void push(std::string const & name, std::chrono::duration<double> duration, std::size_t count = 1); static void push(std::string const & name, std::chrono::duration<double> duration, std::size_t count = 1);
@ -33,6 +38,9 @@ namespace psemek::prof
private: private:
util::clock<std::chrono::duration<double>, std::chrono::high_resolution_clock> clock_; util::clock<std::chrono::duration<double>, std::chrono::high_resolution_clock> clock_;
bool moved_out_ = false;
void report();
}; };
} }

View file

@ -183,12 +183,25 @@ namespace psemek::prof
clock_.restart(); clock_.restart();
} }
profiler::profiler(profiler && other)
: clock_(other.clock_)
, moved_out_(other.moved_out_)
{
other.moved_out_ = true;
}
profiler & profiler::operator = (profiler && other)
{
report();
clock_ = other.clock_;
moved_out_ = other.moved_out_;
other.moved_out_ = true;
return *this;
}
profiler::~profiler() profiler::~profiler()
{ {
auto count = clock_.count(); report();
std::lock_guard lock{*(current->mutex)};
current->execution_time.push(count);
current = current->parent;
} }
void profiler::start_frame(std::string const & name) void profiler::start_frame(std::string const & name)
@ -214,4 +227,15 @@ namespace psemek::prof
current = current->parent; current = current->parent;
} }
void profiler::report()
{
if (moved_out_)
return;
auto count = clock_.count();
std::lock_guard lock{*(current->mutex)};
current->execution_time.push(count);
current = current->parent;
}
} }