Thrill  0.1
profile_thread.cpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/profile_thread.cpp
3  *
4  * A thread running a set of tasks scheduled at regular time intervals. Used in
5  * Thrill for creating profiles of CPU usage, memory, etc.
6  *
7  * Part of Project Thrill - http://project-thrill.org
8  *
9  * Copyright (C) 2016 Timo Bingmann <[email protected]>
10  *
11  * All rights reserved. Published under the BSD-2 license in the LICENSE file.
12  ******************************************************************************/
13 
15 
16 #include <thrill/common/config.hpp>
17 
18 namespace thrill {
19 namespace common {
20 
21 /******************************************************************************/
22 // ProfileThread
23 
26  thread_ = std::thread(&ProfileThread::Worker, this);
27 }
28 
31  std::unique_lock<std::timed_mutex> lock(mutex_);
32  terminate_ = true;
33  cv_.notify_one();
34  lock.unlock();
35  thread_.join();
36  }
37 
38  for (Timer& t : tasks_.container()) {
39  if (t.own_task)
40  delete t.task;
41  }
42 }
43 
45  std::unique_lock<std::timed_mutex> lock(mutex_);
46  return tasks_.erase([task](const Timer& t) { return t.task == task; }) != 0;
47 }
48 
50  std::unique_lock<std::timed_mutex> lock(mutex_);
51 
52  steady_clock::time_point tm = steady_clock::now();
53 
54  while (!terminate_)
55  {
56  if (tasks_.empty()) {
57  while (!terminate_ && tasks_.empty())
58  cv_.wait(mutex_);
59  continue;
60  }
61 
62  while (tasks_.top().next_timeout <= tm) {
63  const Timer& top = tasks_.top();
64  top.task->RunTask(tm);
65 
66  // requeue timeout event again.
68  top.period, top.task, top.own_task);
69  tasks_.pop();
70  }
71 
72  tm = tasks_.top().next_timeout;
73  cv_.wait_until(mutex_, tm);
74  tm = steady_clock::now();
75  }
76 }
77 
78 /******************************************************************************/
79 // ProfileThread::Timer
80 
81 ProfileThread::Timer::Timer(const steady_clock::time_point& _next_timeout,
82  const milliseconds& _period,
83  ProfileTask* _task, bool _own_task)
84  : next_timeout(_next_timeout), period(_period),
85  task(_task), own_task(_own_task) { }
86 
88  return next_timeout > b.next_timeout;
89 }
90 
91 /******************************************************************************/
92 // ProfileTaskRegistration
93 
95  const std::chrono::milliseconds& period,
96  ProfileThread& profiler, ProfileTask* task)
97  : profiler_(profiler), task_(task) {
98  profiler_.Add(period, task);
99 }
100 
103 }
104 
105 } // namespace common
106 } // namespace thrill
107 
108 /******************************************************************************/
void emplace(Args &&... args)
add an items in the PQ.
Definition: binary_heap.hpp:57
ProfileTask * task_
task to register and unregister
std::thread thread_
thread for profiling (only run on top-level loggers)
Timer(const steady_clock::time_point &_next_timeout, const milliseconds &_period, ProfileTask *_task, bool _own_task)
bool operator<(const Timer &b) const
static constexpr bool g_profile_thread
global flag to enable background profiler thread
Definition: config.hpp:35
bool own_task
delete task on deletion
void pop()
remove the top item in the PQ
Definition: binary_heap.hpp:63
bool Remove(ProfileTask *task)
Unregister a regularly scheduled callback.
milliseconds period
interval period for rescheduling
virtual void RunTask(const std::chrono::steady_clock::time_point &tp)=0
method called by ProfileThread.
steady_clock::time_point next_timeout
timepoint of next run
void Worker()
the thread worker function
std::timed_mutex mutex_
cv/mutex pair to signal thread to terminate
ProfileThread & profiler_
profiler at which the task was registered
const_reference top() const
return reference to top item in PQ
Definition: binary_heap.hpp:53
void Add(const Period &period, ProfileTask *task, bool own_task=false)
Register a regularly scheduled callback.
std::condition_variable_any cv_
cv/mutex pair to signal thread to terminate
std::atomic< bool > terminate_
flag to terminate profiling thread
struct for timer callbacks
ProfileTaskRegistration(const std::chrono::milliseconds &period, ProfileThread &profiler, ProfileTask *task)
Container & container()
direct access to heap container
Definition: binary_heap.hpp:74
TimerPQ tasks_
priority queue of interval scheduled callbacks
bool empty() const
check if PQ is empty
Definition: binary_heap.hpp:47
size_t erase(Functor &&f)
Definition: binary_heap.hpp:79
std::chrono::milliseconds milliseconds