Thrill  0.1
profile_thread.hpp
Go to the documentation of this file.
1 /*******************************************************************************
2  * thrill/common/profile_thread.hpp
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 
14 #pragma once
15 #ifndef THRILL_COMMON_PROFILE_THREAD_HEADER
16 #define THRILL_COMMON_PROFILE_THREAD_HEADER
17 
20 #include <tlx/delegate.hpp>
21 
22 #include <atomic>
23 #include <chrono>
24 #include <condition_variable>
25 #include <mutex>
26 #include <queue>
27 #include <thread>
28 
29 namespace thrill {
30 namespace common {
31 
33 {
34 public:
35  using milliseconds = std::chrono::milliseconds;
36  using steady_clock = std::chrono::steady_clock;
37 
38  ProfileThread();
39 
40  //! non-copyable: delete copy-constructor
41  ProfileThread(const ProfileThread&) = delete;
42  //! non-copyable: delete assignment operator
43  ProfileThread& operator = (const ProfileThread&) = delete;
44 
46 
47  //! Register a regularly scheduled callback
48  template <typename Period>
49  void Add(const Period& period, ProfileTask* task, bool own_task = false) {
50  std::unique_lock<std::timed_mutex> lock(mutex_);
51  tasks_.emplace(steady_clock::now() + period,
52  std::chrono::duration_cast<milliseconds>(period),
53  task, own_task);
54  cv_.notify_one();
55  }
56 
57  //! Unregister a regularly scheduled callback
58  bool Remove(ProfileTask* task);
59 
60 private:
61  //! thread for profiling (only run on top-level loggers)
62  std::thread thread_;
63 
64  //! flag to terminate profiling thread
65  std::atomic<bool> terminate_ { false };
66 
67  //! cv/mutex pair to signal thread to terminate
68  std::timed_mutex mutex_;
69 
70  //! cv/mutex pair to signal thread to terminate
71  std::condition_variable_any cv_;
72 
73  //! struct for timer callbacks
74  struct Timer {
75  //! timepoint of next run
76  steady_clock::time_point next_timeout;
77  //! interval period for rescheduling
79  //! callback
81  //! delete task on deletion
82  bool own_task;
83 
84  Timer(const steady_clock::time_point& _next_timeout,
85  const milliseconds& _period,
86  ProfileTask* _task, bool _own_task);
87 
88  bool operator < (const Timer& b) const;
89  };
90 
91  //! priority queue of interval scheduled callbacks
93 
94  //! priority queue of interval scheduled callbacks
96 
97  //! the thread worker function
98  void Worker();
99 };
100 
101 } // namespace common
102 } // namespace thrill
103 
104 #endif // !THRILL_COMMON_PROFILE_THREAD_HEADER
105 
106 /******************************************************************************/
void emplace(Args &&... args)
add an items in the PQ.
Definition: binary_heap.hpp:57
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
bool own_task
delete task on deletion
bool Remove(ProfileTask *task)
Unregister a regularly scheduled callback.
std::chrono::steady_clock steady_clock
milliseconds period
interval period for rescheduling
void Worker()
the thread worker function
steady_clock::time_point next_timeout
timepoint of next run
std::timed_mutex mutex_
cv/mutex pair to signal thread to terminate
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
ProfileThread & operator=(const ProfileThread &)=delete
non-copyable: delete assignment operator
TimerPQ tasks_
priority queue of interval scheduled callbacks
std::chrono::milliseconds milliseconds