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