OpenDDS  Snapshot(2023/04/28-20:55)
Classes | Public Types | Public Member Functions | Private Member Functions | Static Private Member Functions | Private Attributes | List of all members
OpenDDS::DCPS::ThreadStatusManager Class Reference

#include <ThreadStatusManager.h>

Collaboration diagram for OpenDDS::DCPS::ThreadStatusManager:
Collaboration graph
[legend]

Classes

class  Event
 
class  Sleeper
 
class  Start
 
class  Thread
 

Public Types

typedef String ThreadId
 

Public Member Functions

typedef OPENDDS_MAP (ThreadId, Thread) Map
 
typedef OPENDDS_LIST (Thread) List
 
void thread_status_interval (const TimeDuration &thread_status_interval)
 
const TimeDurationthread_status_interval () const
 
bool update_thread_status () const
 
void harvest (const MonotonicTimePoint &start, List &running, List &finished) const
 

Private Member Functions

void add_thread (const String &name)
 
void active (bool nested=false)
 
void idle (bool nested=false)
 
void finished ()
 
void cleanup (const MonotonicTimePoint &now)
 

Static Private Member Functions

static ThreadId get_thread_id ()
 

Private Attributes

TimeDuration thread_status_interval_
 
TimeDuration bucket_limit_
 
Map map_
 
List list_
 
ACE_Thread_Mutex lock_
 

Detailed Description

Definition at line 19 of file ThreadStatusManager.h.

Member Typedef Documentation

◆ ThreadId

Definition at line 29 of file ThreadStatusManager.h.

Member Function Documentation

◆ active()

void OpenDDS::DCPS::ThreadStatusManager::active ( bool  nested = false)
private

Definition at line 117 of file ThreadStatusManager.cpp.

References ACE_GUARD, bucket_limit_, cleanup(), get_thread_id(), lock_, map_, OpenDDS::DCPS::TimePoint_T< SystemClock >::now(), OpenDDS::DCPS::TimePoint_T< MonotonicClock >::now(), OpenDDS::DCPS::ThreadStatusManager::Thread::ThreadStatus_Active, and update_thread_status().

118 {
119  if (!update_thread_status()) {
120  return;
121  }
122 
124 
126  const SystemTimePoint s_now = SystemTimePoint::now();
127  const ThreadId thread_id = get_thread_id();
128 
129  const Map::iterator pos = map_.find(thread_id);
130  if (pos != map_.end()) {
131  pos->second.update(m_now, s_now, Thread::ThreadStatus_Active, bucket_limit_, nested);
132  }
133 
134  cleanup(m_now);
135 }
#define ACE_GUARD(MUTEX, OBJ, LOCK)
TimePoint_T< SystemClock > SystemTimePoint
Definition: TimeTypes.h:32
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
TimePoint_T< MonotonicClock > MonotonicTimePoint
Definition: TimeTypes.h:51
void cleanup(const MonotonicTimePoint &now)

◆ add_thread()

void OpenDDS::DCPS::ThreadStatusManager::add_thread ( const String name)
private

Definition at line 94 of file ThreadStatusManager.cpp.

References ACE_DEBUG, ACE_GUARD, OpenDDS::DCPS::ThreadStatusManager::Thread::bit_key(), OpenDDS::DCPS::DCPS_debug_level, get_thread_id(), LM_DEBUG, lock_, map_, OpenDDS::DCPS::ThreadStatusManager::Thread::Thread(), OpenDDS::DCPS::to_dds_string(), and update_thread_status().

95 {
96  if (!update_thread_status()) {
97  return;
98  }
99 
100  const ThreadId thread_id = get_thread_id();
101 
102  String bit_key = to_dds_string(thread_id);
103 
104  if (name.length()) {
105  bit_key += " (" + name + ")";
106  }
107 
108  if (DCPS_debug_level > 4) {
109  ACE_DEBUG((LM_DEBUG, "(%P|%t) ThreadStatusManager::add_thread: "
110  "adding thread %C\n", bit_key.c_str()));
111  }
112 
114  map_.insert(std::make_pair(thread_id, Thread(bit_key)));
115 }
#define ACE_DEBUG(X)
#define ACE_GUARD(MUTEX, OBJ, LOCK)
std::string String
String to_dds_string(unsigned short to_convert)
const char *const name
Definition: debug.cpp:60
OpenDDS_Dcps_Export unsigned int DCPS_debug_level
Definition: debug.cpp:30

◆ cleanup()

void OpenDDS::DCPS::ThreadStatusManager::cleanup ( const MonotonicTimePoint now)
private

Definition at line 199 of file ThreadStatusManager.cpp.

References list_, OPENDDS_END_VERSIONED_NAMESPACE_DECL, and thread_status_interval_.

Referenced by active(), finished(), and idle().

200 {
201  const MonotonicTimePoint cutoff = now - 10 * thread_status_interval_;
202 
203  while (!list_.empty() && list_.front().last_update() < cutoff) {
204  list_.pop_front();
205  }
206 }
TimePoint_T< MonotonicClock > MonotonicTimePoint
Definition: TimeTypes.h:51

◆ finished()

void OpenDDS::DCPS::ThreadStatusManager::finished ( )
private

Definition at line 157 of file ThreadStatusManager.cpp.

References ACE_GUARD, bucket_limit_, cleanup(), get_thread_id(), list_, lock_, map_, OpenDDS::DCPS::TimePoint_T< SystemClock >::now(), OpenDDS::DCPS::TimePoint_T< MonotonicClock >::now(), OpenDDS::DCPS::ThreadStatusManager::Thread::ThreadStatus_Idle, and update_thread_status().

158 {
159  if (!update_thread_status()) {
160  return;
161  }
162 
164 
166  const SystemTimePoint s_now = SystemTimePoint::now();
167  const ThreadId thread_id = get_thread_id();
168 
169  const Map::iterator pos = map_.find(thread_id);
170  if (pos != map_.end()) {
171  pos->second.update(m_now, s_now, Thread::ThreadStatus_Idle, bucket_limit_, false);
172 
173  list_.push_back(pos->second);
174  map_.erase(pos);
175  }
176 
177  cleanup(m_now);
178 }
#define ACE_GUARD(MUTEX, OBJ, LOCK)
TimePoint_T< SystemClock > SystemTimePoint
Definition: TimeTypes.h:32
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
TimePoint_T< MonotonicClock > MonotonicTimePoint
Definition: TimeTypes.h:51
void cleanup(const MonotonicTimePoint &now)

◆ get_thread_id()

ThreadStatusManager::ThreadId OpenDDS::DCPS::ThreadStatusManager::get_thread_id ( void  )
staticprivate

Definition at line 79 of file ThreadStatusManager.cpp.

References ACE_Thread::self(), ACE_OS::thr_gettid(), and ACE_OS::thr_id().

Referenced by active(), add_thread(), finished(), and idle().

80 {
81 #if defined (ACE_WIN32)
82  return static_cast<unsigned>(ACE_Thread::self());
83 #else
84 # ifdef ACE_HAS_GETTID
85  return ACE_OS::thr_gettid();
86 # else
87  char buffer[32];
88  const size_t len = ACE_OS::thr_id(buffer, 32);
89  return String(buffer, len);
90 # endif
91 #endif /* ACE_WIN32 */
92 }
pid_t thr_gettid()
std::string String
ssize_t thr_id(char buffer[], size_t buffer_length)
static ACE_thread_t self(void)

◆ harvest()

void OpenDDS::DCPS::ThreadStatusManager::harvest ( const MonotonicTimePoint start,
List &  running,
List &  finished 
) const

Copy active and idle threads to running and finished threads to finished. Only threads updated after start are considered.

Definition at line 180 of file ThreadStatusManager.cpp.

References ACE_GUARD, list_, lock_, and map_.

183 {
185 
186  for (Map::const_iterator pos = map_.begin(), limit = map_.end(); pos != limit; ++pos) {
187  if (pos->second.last_update() > start) {
188  running.push_back(pos->second);
189  }
190  }
191 
192  for (List::const_iterator pos = list_.begin(), limit = list_.end(); pos != limit; ++pos) {
193  if (pos->last_update() > start) {
194  finished.push_back(*pos);
195  }
196  }
197 }
#define ACE_GUARD(MUTEX, OBJ, LOCK)

◆ idle()

void OpenDDS::DCPS::ThreadStatusManager::idle ( bool  nested = false)
private

Definition at line 137 of file ThreadStatusManager.cpp.

References ACE_GUARD, bucket_limit_, cleanup(), get_thread_id(), lock_, map_, OpenDDS::DCPS::TimePoint_T< SystemClock >::now(), OpenDDS::DCPS::TimePoint_T< MonotonicClock >::now(), OpenDDS::DCPS::ThreadStatusManager::Thread::ThreadStatus_Idle, and update_thread_status().

138 {
139  if (!update_thread_status()) {
140  return;
141  }
142 
144 
146  const SystemTimePoint s_now = SystemTimePoint::now();
147  const ThreadId thread_id = get_thread_id();
148 
149  const Map::iterator pos = map_.find(thread_id);
150  if (pos != map_.end()) {
151  pos->second.update(m_now, s_now, Thread::ThreadStatus_Idle, bucket_limit_, nested);
152  }
153 
154  cleanup(m_now);
155 }
#define ACE_GUARD(MUTEX, OBJ, LOCK)
TimePoint_T< SystemClock > SystemTimePoint
Definition: TimeTypes.h:32
static TimePoint_T< MonotonicClock > now()
Definition: TimePoint_T.inl:41
TimePoint_T< MonotonicClock > MonotonicTimePoint
Definition: TimeTypes.h:51
void cleanup(const MonotonicTimePoint &now)

◆ OPENDDS_LIST()

typedef OpenDDS::DCPS::ThreadStatusManager::OPENDDS_LIST ( Thread  )

◆ OPENDDS_MAP()

typedef OpenDDS::DCPS::ThreadStatusManager::OPENDDS_MAP ( ThreadId  ,
Thread   
)

◆ thread_status_interval() [1/2]

void OpenDDS::DCPS::ThreadStatusManager::thread_status_interval ( const TimeDuration thread_status_interval)
inline

◆ thread_status_interval() [2/2]

const TimeDuration& OpenDDS::DCPS::ThreadStatusManager::thread_status_interval ( ) const
inline

Definition at line 86 of file ThreadStatusManager.h.

87  {
89  }

◆ update_thread_status()

bool OpenDDS::DCPS::ThreadStatusManager::update_thread_status ( ) const
inline

Member Data Documentation

◆ bucket_limit_

TimeDuration OpenDDS::DCPS::ThreadStatusManager::bucket_limit_
private

Definition at line 173 of file ThreadStatusManager.h.

Referenced by active(), finished(), and idle().

◆ list_

List OpenDDS::DCPS::ThreadStatusManager::list_
private

Definition at line 175 of file ThreadStatusManager.h.

Referenced by cleanup(), finished(), and harvest().

◆ lock_

ACE_Thread_Mutex OpenDDS::DCPS::ThreadStatusManager::lock_
mutableprivate

Definition at line 177 of file ThreadStatusManager.h.

Referenced by active(), add_thread(), finished(), harvest(), and idle().

◆ map_

Map OpenDDS::DCPS::ThreadStatusManager::map_
private

Definition at line 174 of file ThreadStatusManager.h.

Referenced by active(), add_thread(), finished(), harvest(), and idle().

◆ thread_status_interval_

TimeDuration OpenDDS::DCPS::ThreadStatusManager::thread_status_interval_
private

Definition at line 172 of file ThreadStatusManager.h.

Referenced by cleanup().


The documentation for this class was generated from the following files: