Thread-safe Timer

- use template arguments instead of run-time variable 'priority'
 - change in paje interface for tracing
This commit is contained in:
Matthias Hochsteger 2021-06-11 09:52:58 +02:00
parent a11294baf0
commit c5639a5706
3 changed files with 82 additions and 49 deletions

View File

@ -85,8 +85,7 @@ namespace ngcore
for(auto & ltask : tasks) for(auto & ltask : tasks)
for(auto & task : ltask) for(auto & task : ltask)
{ {
task.start_time -= start_time; task.time -= start_time;
task.stop_time -= start_time;
} }
for(auto & job : jobs) for(auto & job : jobs)
{ {
@ -635,23 +634,31 @@ namespace ngcore
value_id = job_task_map[jobs[t.id-1].type]; value_id = job_task_map[jobs[t.id-1].type];
if(trace_thread_counter) if(trace_thread_counter)
{ {
paje.AddVariable( t.start_time, variable_type_active_threads, container_jobs, 1.0 ); if(t.is_start)
paje.SubVariable( t.stop_time, variable_type_active_threads, container_jobs, 1.0 ); 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) if(trace_threads)
{ {
paje.PushState( t.start_time, state_type_task, thread_aliases[t.thread_id], value_id, t.additional_value, true ); if(t.is_start)
paje.PopState( t.stop_time, state_type_task, thread_aliases[t.thread_id] ); 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; break;
case Task::ID_TIMER: case Task::ID_TIMER:
value_id = timer_aliases[t.id]; 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 ); if(t.is_start)
paje.PopState( t.stop_time, state_type_timer, thread_aliases[t.thread_id] ); 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; break;
default: default:
paje.PushState( t.start_time, state_type_task, thread_aliases[t.thread_id], value_id, t.additional_value, false ); if(t.is_start)
paje.PopState( t.stop_time, state_type_task, thread_aliases[t.thread_id] ); 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; break;
} }
} }

View File

@ -79,8 +79,8 @@ namespace ngcore
int additional_value; int additional_value;
TTimePoint start_time; TTimePoint time;
TTimePoint stop_time; bool is_start;
static constexpr int ID_NONE = -1; static constexpr int ID_NONE = -1;
static constexpr int ID_JOB = 1; static constexpr int ID_JOB = 1;
@ -178,23 +178,16 @@ namespace ngcore
if(unlikely(tasks[thread_id].size() == max_num_events_per_thread)) if(unlikely(tasks[thread_id].size() == max_num_events_per_thread))
StopTracing(); StopTracing();
int task_num = tasks[thread_id].size(); 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; 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(!trace_threads && !trace_thread_counter) return;
if(task_num>=0) tasks[thread_id].push_back( Task{thread_id, id, id_type, 0, GetTimeCounter(), false} );
tasks[thread_id][task_num].stop_time = GetTimeCounter();
} }
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) void StartJob(int job_id, const std::type_info & type)
{ {
if(!tracing_enabled) return; if(!tracing_enabled) return;

View File

@ -149,13 +149,12 @@ namespace ngcore
class NGCORE_API Timer template<bool DO_TRACING=true, bool DO_TIMING=true>
class Timer
{ {
int timernr; int timernr;
int priority;
public: public:
Timer (const std::string & name, int apriority = 1) Timer (const std::string & name)
: priority(apriority)
{ {
timernr = NgProfiler::CreateTimer (name); timernr = NgProfiler::CreateTimer (name);
} }
@ -165,21 +164,49 @@ namespace ngcore
} }
void Start () void Start ()
{ {
if (priority <= 2) Start(TaskManager::GetThreadId());
NgProfiler::StartTimer (timernr);
if (priority <= 1)
if(trace) trace->StartTimer(timernr);
} }
void Stop () void Stop ()
{ {
if (priority <= 2) Stop(TaskManager::GetThreadId());
NgProfiler::StopTimer (timernr); }
if (priority <= 1) void Start (int tid)
if(trace) trace->StopTimer(timernr); {
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) void AddFlops (double aflops)
{ {
if (priority <= 2) if constexpr(DO_TIMING)
NgProfiler::AddFlops (timernr, aflops); NgProfiler::AddFlops (timernr, aflops);
} }
@ -196,14 +223,21 @@ namespace ngcore
Timer object. Timer object.
Start / stop timer at constructor / destructor. Start / stop timer at constructor / destructor.
*/ */
template<bool DO_TRACING, bool DO_TIMING>
class RegionTimer class RegionTimer
{ {
Timer & timer; Timer<DO_TRACING, DO_TIMING> & timer;
int tid;
public: public:
/// start timer /// start timer
RegionTimer (Timer & atimer) : timer(atimer) { timer.Start(); } RegionTimer (Timer<DO_TRACING, DO_TIMING> & atimer) : timer(atimer)
{
tid = TaskManager::GetThreadId();
timer.Start(tid);
}
/// stop timer /// stop timer
~RegionTimer () { timer.Stop(); } ~RegionTimer () { timer.Stop(tid); }
RegionTimer() = delete; RegionTimer() = delete;
RegionTimer(const RegionTimer &) = delete; RegionTimer(const RegionTimer &) = delete;
@ -235,6 +269,7 @@ namespace ngcore
{ {
int nr; int nr;
int thread_id; int thread_id;
int type;
public: public:
static constexpr int ID_JOB = PajeTrace::Task::ID_JOB; static constexpr int ID_JOB = PajeTrace::Task::ID_JOB;
static constexpr int ID_NONE = PajeTrace::Task::ID_NONE; static constexpr int ID_NONE = PajeTrace::Task::ID_NONE;
@ -251,28 +286,26 @@ namespace ngcore
: thread_id(athread_id) : thread_id(athread_id)
{ {
if (trace) 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 /// start trace with timer
RegionTracer (int athread_id, Timer & timer, int additional_value = -1 ) template<bool DO_TRACING, bool DO_TIMING>
RegionTracer (int athread_id, Timer<DO_TRACING, DO_TIMING> & timer, int additional_value = -1 )
: thread_id(athread_id) : thread_id(athread_id)
{ {
nr = timer;
type = ID_TIMER;
if (trace) if (trace)
nr = trace->StartTask (athread_id, static_cast<int>(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 /// stop trace
~RegionTracer () ~RegionTracer ()
{ {
if (trace) if (trace)
trace->StopTask (thread_id, nr); trace->StopTask (thread_id, nr, type);
} }
}; };