Small profiler refactoring

This commit is contained in:
Nikita Lisitsa 2022-08-07 11:20:47 +03:00
parent c7923f356e
commit b8aeb66d07

View file

@ -33,6 +33,8 @@ namespace psemek::prof
if (it == children.end()) if (it == children.end())
{ {
it = children.insert({name, profiler_tree{}}).first; it = children.insert({name, profiler_tree{}}).first;
it->second.parent = this;
it->second.mutex = mutex;
children_list.push_back(it); children_list.push_back(it);
} }
return &(it->second); return &(it->second);
@ -45,6 +47,18 @@ namespace psemek::prof
static thread_local profiler_tree * current = nullptr; static thread_local profiler_tree * current = nullptr;
void get_current()
{
if (!current)
{
auto const id = std::this_thread::get_id();
std::lock_guard lock{roots_mutex};
current = &roots[id];
current->mutex = &thread_mutex[id];
}
}
struct pretty_impl struct pretty_impl
{ {
double value; double value;
@ -163,21 +177,10 @@ namespace psemek::prof
profiler::profiler(std::string const & name) profiler::profiler(std::string const & name)
{ {
auto const id = std::this_thread::get_id(); get_current();
if (!current)
{
std::lock_guard lock{roots_mutex};
current = &roots[id];
current->mutex = &thread_mutex[id];
}
std::lock_guard lock{*(current->mutex)}; std::lock_guard lock{*(current->mutex)};
current = current->child(name);
auto next = current->child(name);
next->parent = current;
next->mutex = current->mutex;
current = next;
clock_.restart(); clock_.restart();
} }
@ -191,21 +194,10 @@ namespace psemek::prof
void profiler::push(std::string const & name, std::chrono::duration<double> duration) void profiler::push(std::string const & name, std::chrono::duration<double> duration)
{ {
auto const id = std::this_thread::get_id(); get_current();
if (!current)
{
std::lock_guard lock{roots_mutex};
current = &roots[id];
current->mutex = &thread_mutex[id];
}
std::lock_guard lock{*(current->mutex)}; std::lock_guard lock{*(current->mutex)};
current->child(name)->execution_time.push(duration.count());
auto child = current->child(name);
child->parent = current;
child->mutex = current->mutex;
child->execution_time.push(duration.count());
} }
} }