From c5639a5706ea567da4b87335c2f09cc20ab7e2b8 Mon Sep 17 00:00:00 2001 From: Matthias Hochsteger Date: Fri, 11 Jun 2021 09:52:58 +0200 Subject: [PATCH] Thread-safe Timer - use template arguments instead of run-time variable 'priority' - change in paje interface for tracing --- libsrc/core/paje_trace.cpp | 27 +++++++----- libsrc/core/paje_trace.hpp | 17 +++----- libsrc/core/profiler.hpp | 87 ++++++++++++++++++++++++++------------ 3 files changed, 82 insertions(+), 49 deletions(-) diff --git a/libsrc/core/paje_trace.cpp b/libsrc/core/paje_trace.cpp index 56fb0c12..eb0dd06d 100644 --- a/libsrc/core/paje_trace.cpp +++ b/libsrc/core/paje_trace.cpp @@ -85,8 +85,7 @@ namespace ngcore for(auto & ltask : tasks) for(auto & task : ltask) { - task.start_time -= start_time; - task.stop_time -= start_time; + task.time -= start_time; } for(auto & job : jobs) { @@ -635,23 +634,31 @@ namespace ngcore value_id = job_task_map[jobs[t.id-1].type]; if(trace_thread_counter) { - paje.AddVariable( t.start_time, variable_type_active_threads, container_jobs, 1.0 ); - paje.SubVariable( t.stop_time, variable_type_active_threads, container_jobs, 1.0 ); + if(t.is_start) + paje.AddVariable( t.time, variable_type_active_threads, container_jobs, 1.0 ); + else + paje.SubVariable( t.time, variable_type_active_threads, container_jobs, 1.0 ); } if(trace_threads) { - paje.PushState( t.start_time, state_type_task, thread_aliases[t.thread_id], value_id, t.additional_value, true ); - paje.PopState( t.stop_time, state_type_task, thread_aliases[t.thread_id] ); + if(t.is_start) + paje.PushState( t.time, state_type_task, thread_aliases[t.thread_id], value_id, t.additional_value, true ); + else + paje.PopState( t.time, state_type_task, thread_aliases[t.thread_id] ); } break; case Task::ID_TIMER: value_id = timer_aliases[t.id]; - paje.PushState( t.start_time, state_type_timer, thread_aliases[t.thread_id], value_id, t.additional_value, true ); - paje.PopState( t.stop_time, state_type_timer, thread_aliases[t.thread_id] ); + if(t.is_start) + paje.PushState( t.time, state_type_timer, thread_aliases[t.thread_id], value_id, t.additional_value, true ); + else + paje.PopState( t.time, state_type_timer, thread_aliases[t.thread_id] ); break; default: - paje.PushState( t.start_time, state_type_task, thread_aliases[t.thread_id], value_id, t.additional_value, false ); - paje.PopState( t.stop_time, state_type_task, thread_aliases[t.thread_id] ); + if(t.is_start) + paje.PushState( t.time, state_type_task, thread_aliases[t.thread_id], value_id, t.additional_value, false ); + else + paje.PopState( t.time, state_type_task, thread_aliases[t.thread_id] ); break; } } diff --git a/libsrc/core/paje_trace.hpp b/libsrc/core/paje_trace.hpp index 5444a96c..c85c040f 100644 --- a/libsrc/core/paje_trace.hpp +++ b/libsrc/core/paje_trace.hpp @@ -79,8 +79,8 @@ namespace ngcore int additional_value; - TTimePoint start_time; - TTimePoint stop_time; + TTimePoint time; + bool is_start; static constexpr int ID_NONE = -1; static constexpr int ID_JOB = 1; @@ -178,23 +178,16 @@ namespace ngcore if(unlikely(tasks[thread_id].size() == max_num_events_per_thread)) StopTracing(); int task_num = tasks[thread_id].size(); - tasks[thread_id].push_back( Task{thread_id, id, id_type, additional_value, GetTimeCounter()} ); + tasks[thread_id].push_back( Task{thread_id, id, id_type, additional_value, GetTimeCounter(), true} ); return task_num; } - void StopTask(int thread_id, int task_num) + void StopTask(int thread_id, int id, int id_type = Task::ID_NONE) { if(!trace_threads && !trace_thread_counter) return; - if(task_num>=0) - tasks[thread_id][task_num].stop_time = GetTimeCounter(); + tasks[thread_id].push_back( Task{thread_id, id, id_type, 0, GetTimeCounter(), false} ); } - void SetTask(int thread_id, int task_num, int additional_value) { - if(!trace_threads && !trace_thread_counter) return; - if(task_num>=0) - tasks[thread_id][task_num].additional_value = additional_value; - } - void StartJob(int job_id, const std::type_info & type) { if(!tracing_enabled) return; diff --git a/libsrc/core/profiler.hpp b/libsrc/core/profiler.hpp index 294928d3..e6795f69 100644 --- a/libsrc/core/profiler.hpp +++ b/libsrc/core/profiler.hpp @@ -149,13 +149,12 @@ namespace ngcore - class NGCORE_API Timer + template + class Timer { int timernr; - int priority; public: - Timer (const std::string & name, int apriority = 1) - : priority(apriority) + Timer (const std::string & name) { timernr = NgProfiler::CreateTimer (name); } @@ -165,21 +164,49 @@ namespace ngcore } void Start () { - if (priority <= 2) - NgProfiler::StartTimer (timernr); - if (priority <= 1) - if(trace) trace->StartTimer(timernr); + Start(TaskManager::GetThreadId()); } void Stop () { - if (priority <= 2) - NgProfiler::StopTimer (timernr); - if (priority <= 1) - if(trace) trace->StopTimer(timernr); + Stop(TaskManager::GetThreadId()); + } + void Start (int tid) + { + if(tid==0) + { + if constexpr(DO_TIMING) + NgProfiler::StartTimer (timernr); + if constexpr(DO_TRACING) + if(trace) trace->StartTimer(timernr); + } + else + { + if constexpr(DO_TIMING) + NgProfiler::StartThreadTimer(timernr, tid); + if constexpr(DO_TRACING) + trace->StartTask (tid, timernr, PajeTrace::Task::ID_TIMER); + } + } + void Stop (int tid) + { + if(tid==0) + { + if constexpr(DO_TIMING) + NgProfiler::StopTimer (timernr); + if constexpr(DO_TRACING) + if(trace) trace->StopTimer(timernr); + } + else + { + if constexpr(DO_TIMING) + NgProfiler::StopThreadTimer(timernr, tid); + if constexpr(DO_TRACING) + trace->StopTask (tid, timernr, PajeTrace::Task::ID_TIMER); + } } void AddFlops (double aflops) { - if (priority <= 2) + if constexpr(DO_TIMING) NgProfiler::AddFlops (timernr, aflops); } @@ -196,14 +223,21 @@ namespace ngcore Timer object. Start / stop timer at constructor / destructor. */ + template class RegionTimer { - Timer & timer; + Timer & timer; + int tid; public: /// start timer - RegionTimer (Timer & atimer) : timer(atimer) { timer.Start(); } + RegionTimer (Timer & atimer) : timer(atimer) + { + tid = TaskManager::GetThreadId(); + timer.Start(tid); + } + /// stop timer - ~RegionTimer () { timer.Stop(); } + ~RegionTimer () { timer.Stop(tid); } RegionTimer() = delete; RegionTimer(const RegionTimer &) = delete; @@ -235,6 +269,7 @@ namespace ngcore { int nr; int thread_id; + int type; public: static constexpr int ID_JOB = PajeTrace::Task::ID_JOB; static constexpr int ID_NONE = PajeTrace::Task::ID_NONE; @@ -251,28 +286,26 @@ namespace ngcore : thread_id(athread_id) { if (trace) - nr = trace->StartTask (athread_id, region_id, id_type, additional_value); + trace->StartTask (athread_id, region_id, id_type, additional_value); + type = id_type; + nr = region_id; } /// start trace with timer - RegionTracer (int athread_id, Timer & timer, int additional_value = -1 ) + template + RegionTracer (int athread_id, Timer & timer, int additional_value = -1 ) : thread_id(athread_id) { + nr = timer; + type = ID_TIMER; if (trace) - nr = trace->StartTask (athread_id, static_cast(timer), ID_TIMER, additional_value); + trace->StartTask (athread_id, nr, type, additional_value); } - /// set user defined value - void SetValue( int additional_value ) - { - if (trace) - trace->SetTask( thread_id, nr, additional_value ); - } - /// stop trace ~RegionTracer () { if (trace) - trace->StopTask (thread_id, nr); + trace->StopTask (thread_id, nr, type); } };