OpenDDS  Snapshot(2023/04/28-20:55)
ThreadStatusManager.h
Go to the documentation of this file.
1 /*
2  *
3  *
4  * Distributed under the OpenDDS License.
5  * See: http://www.opendds.org/license.html
6  */
7 
8 #ifndef OPENDDS_DCPS_THREADSTATUSMANAGER_H
9 #define OPENDDS_DCPS_THREADSTATUSMANAGER_H
10 
11 #include "dcps_export.h"
12 #include "TimeTypes.h"
13 
15 
16 namespace OpenDDS {
17 namespace DCPS {
18 
20 public:
21 
22 #if defined (ACE_WIN32)
23  typedef unsigned ThreadId;
24 #else
25 
26 # ifdef ACE_HAS_GETTID
27  typedef pid_t ThreadId;
28 # else
29  typedef String ThreadId;
30 # endif
31 #endif /* ACE_WIN32 */
32 
34  public:
35  enum ThreadStatus {
38  };
39 
40  Thread(const String& bit_key)
41  : bit_key_(bit_key)
42  , timestamp_(SystemTimePoint::now())
43  , status_(ThreadStatus_Active)
44  , last_update_(MonotonicTimePoint::now())
45  , current_bucket_(0)
46  , nesting_depth_(0)
47  {}
48 
49  const String& bit_key() const { return bit_key_; }
50  const SystemTimePoint& timestamp() const { return timestamp_; }
51  const MonotonicTimePoint& last_update() const { return last_update_; }
52 
53  void update(const MonotonicTimePoint& m_now,
54  const SystemTimePoint& s_now,
55  ThreadStatus next_status,
56  const TimeDuration& bucket_limit,
57  bool nested);
58  double utilization(const MonotonicTimePoint& now) const;
59 
60  static const size_t bucket_count = 8;
61 
62  private:
66 
70  };
73  Bucket bucket_[bucket_count];
76  };
77  typedef OPENDDS_MAP(ThreadId, Thread) Map;
78  typedef OPENDDS_LIST(Thread) List;
79 
80  void thread_status_interval(const TimeDuration& thread_status_interval)
81  {
82  thread_status_interval_ = thread_status_interval;
83  bucket_limit_ = thread_status_interval / static_cast<double>(Thread::bucket_count);
84  }
85 
87  {
88  return thread_status_interval_;
89  }
90 
91  bool update_thread_status() const
92  {
93  return thread_status_interval_ > TimeDuration::zero_value;
94  }
95 
96  /// Add the calling thread with the manager.
97  /// name is for a more human-friendly name that will be appended to the BIT key.
98  /// Implicitly makes the thread active and finishes the thread on destruction.
99  class Start {
100  public:
101  Start(ThreadStatusManager& thread_status_manager, const String& name)
102  : thread_status_manager_(thread_status_manager)
103  {
104  thread_status_manager_.add_thread(name);
105  }
106 
108  {
109  thread_status_manager_.finished();
110  }
111 
112  private:
114  };
115 
116  class Event {
117  public:
118  Event(ThreadStatusManager& thread_status_manager)
119  : thread_status_manager_(thread_status_manager)
120  {
121  thread_status_manager_.active(true);
122  }
123 
125  {
126  thread_status_manager_.idle(true);
127  }
128 
129  private:
131  };
132 
133  class Sleeper {
134  public:
135  Sleeper(ThreadStatusManager& thread_status_manager)
136  : thread_status_manager_(thread_status_manager)
137  {
138  thread_status_manager_.idle();
139  }
140 
142  {
143  thread_status_manager_.active();
144  }
145 
146  private:
148  };
149 
150  /// Copy active and idle threads to running and finished threads to
151  /// finished. Only threads updated after start are considered.
152  void harvest(const MonotonicTimePoint& start,
153  List& running,
154  List& finished) const;
155 
156 #ifdef ACE_HAS_GETTID
157  static inline pid_t gettid()
158  {
159  return syscall(SYS_gettid);
160  }
161 #endif
162 
163 private:
164  static ThreadId get_thread_id();
165  void add_thread(const String& name);
166  void active(bool nested = false);
167  void idle(bool nested = false);
168  void finished();
169 
170  void cleanup(const MonotonicTimePoint& now);
171 
174  Map map_;
175  List list_;
176 
178 };
179 
180 } // namespace DCPS
181 } // namespace OpenDDS
182 
184 
185 #endif /* OPENDDS_DCPS_THREADSTATUSMANAGER_H */
void thread_status_interval(const TimeDuration &thread_status_interval)
const TimeDuration & thread_status_interval() const
std::string String
Start(ThreadStatusManager &thread_status_manager, const String &name)
#define OpenDDS_Dcps_Export
Definition: dcps_export.h:24
const MonotonicTimePoint & last_update() const
Sleeper(ThreadStatusManager &thread_status_manager)
Event(ThreadStatusManager &thread_status_manager)
const char *const name
Definition: debug.cpp:60
static const TimeDuration zero_value
Definition: TimeDuration.h:31
#define OPENDDS_END_VERSIONED_NAMESPACE_DECL
The Internal API and Implementation of OpenDDS.
Definition: AddressCache.h:28
typedef OPENDDS_MAP(OPENDDS_STRING, OPENDDS_STRING) ValueMap
Helper types and functions for config file parsing.
typedef OPENDDS_LIST(SubsectionPair) KeyList
const SystemTimePoint & timestamp() const