From b8aeb66d0778205631173958693778591c7d139d Mon Sep 17 00:00:00 2001 From: lisyarus Date: Sun, 7 Aug 2022 11:20:47 +0300 Subject: [PATCH] Small profiler refactoring --- libs/prof/source/profiler.cpp | 44 ++++++++++++++--------------------- 1 file changed, 18 insertions(+), 26 deletions(-) diff --git a/libs/prof/source/profiler.cpp b/libs/prof/source/profiler.cpp index 17a82b1b..bef78992 100644 --- a/libs/prof/source/profiler.cpp +++ b/libs/prof/source/profiler.cpp @@ -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 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()); } }