MIRA
ThreadMonitor.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2012 by
3  * MetraLabs GmbH (MLAB), GERMANY
4  * and
5  * Neuroinformatics and Cognitive Robotics Labs (NICR) at TU Ilmenau, GERMANY
6  * All rights reserved.
7  *
8  * Contact: info@mira-project.org
9  *
10  * Commercial Usage:
11  * Licensees holding valid commercial licenses may use this file in
12  * accordance with the commercial license agreement provided with the
13  * software or, alternatively, in accordance with the terms contained in
14  * a written agreement between you and MLAB or NICR.
15  *
16  * GNU General Public License Usage:
17  * Alternatively, this file may be used under the terms of the GNU
18  * General Public License version 3.0 as published by the Free Software
19  * Foundation and appearing in the file LICENSE.GPL3 included in the
20  * packaging of this file. Please review the following information to
21  * ensure the GNU General Public License version 3.0 requirements will be
22  * met: http://www.gnu.org/copyleft/gpl.html.
23  * Alternatively you may (at your option) use any later version of the GNU
24  * General Public License if such license has been publicly approved by
25  * MLAB and NICR (or its successors, if any).
26  *
27  * IN NO EVENT SHALL "MLAB" OR "NICR" BE LIABLE TO ANY PARTY FOR DIRECT,
28  * INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT OF
29  * THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF "MLAB" OR
30  * "NICR" HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  *
32  * "MLAB" AND "NICR" SPECIFICALLY DISCLAIM ANY WARRANTIES, INCLUDING,
33  * BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
34  * FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
35  * ON AN "AS IS" BASIS, AND "MLAB" AND "NICR" HAVE NO OBLIGATION TO
36  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS OR MODIFICATIONS.
37  */
38 
48 #ifndef _MIRA_THREADMONITOR_H_
49 #define _MIRA_THREADMONITOR_H_
50 
51 #include <map>
52 #include <vector>
53 
54 #include <platform/Platform.h>
55 #include <utils/Singleton.h>
56 #include <utils/Time.h>
57 
58 #include <thread/Thread.h>
59 #include <thread/ThreadID.h>
60 
61 #include <serialization/adapters/std/vector>
62 
63 namespace mira {
64 
66 
116  public LazySingleton<ThreadMonitor>
117 {
118 public:
121 
123  ThreadMonitor();
124 
126  virtual ~ThreadMonitor();
127 
129 
130 public:
133 
138  void addThisThread(const std::string& pName);
139 
143  void removeThisThread();
144 
149  void removeThread(const boost::thread& pThread);
150 
155  void removeThread(const boost::thread::id& pThreadID);
156 
161  void removeThread(ThreadID pThreadID);
162 
164 
165 public:
168 
171  {
172  template<typename Reflector>
173  void reflect(Reflector& r)
174  {
175  r.property("ID", id, "The native thread id (number)");
176  r.property("Name", name, "The thread's name");
177  }
178 
180  std::string name;
181  };
182 
188  {
189  boost::thread::id bid;
190  };
191 
194  {
195  template<typename Reflector>
196  void reflect(Reflector& r)
197  {
198  r.property("CreateTime", create_time, "Thread creation time, UTC");
199  r.property("UserTime", user_time, "Time spent in user mode");
200  r.property("KernelTime", kernel_time, "Time spent in kernel mode");
201  }
202 
206  };
207 
209  template<typename TID>
211  {
212  ThreadInfoTempl() = default;
213 
215  template<typename OtherTID>
217  : ThreadInfoBase(other), known(other.known), identity(other.identity)
218  {}
219 
220  template<typename Reflector>
221  void reflect(Reflector& r)
222  {
223  r.property("Known", known, "Thread is registered to the ThreadMonitor?");
224  r.property("Identity", identity, "Thread identity info");
225  r.template reflectBase<ThreadInfoBase>(*this);
226  }
227 
228  bool known = false;
230  TID identity;
231  };
232 
236  using BasicThreadInfoVector = std::vector<BasicThreadInfo>;
237 
241  using ThreadInfoVector = std::vector<ThreadInfo>;
242 
243 public:
248  ThreadInfoVector getThreadInformation();
249 
255  BasicThreadInfoVector getBasicThreadInformation();
256 
258 
259 private:
260  typedef std::map<ThreadID, ThreadIdentity> ThreadMap;
261 
262  boost::mutex mThreadsMutex;
263  ThreadMap mThreads;
264 };
265 
267 
268 }
269 
270 #endif
Duration kernel_time
The time amount in kernel-mode.
Definition: ThreadMonitor.h:205
void reflect(Reflector &r)
Definition: ThreadMonitor.h:196
specialize cv::DataType for our ImgPixel and inherit from cv::DataType<Vec>
Definition: IOService.h:67
std::vector< BasicThreadInfo > BasicThreadInfoVector
Vector of serializable thread info.
Definition: ThreadMonitor.h:236
Time and Duration wrapper class.
Combine thread identity (template param) and runtime info.
Definition: ThreadMonitor.h:210
uint32 ThreadID
Platform independent thread ID.
Definition: ThreadID.h:68
std::string name
Name of the thread.
Definition: ThreadMonitor.h:180
Provided for convenience.
Definition: Singleton.h:564
boost::thread::id bid
Boost thread ID.
Definition: ThreadMonitor.h:189
Wrapper class for boost::posix_time::ptime for adding more functionality to it.
Definition: Time.h:418
ThreadInfoTempl(const ThreadInfoTempl< OtherTID > &other)
implement conversion between different identity types
Definition: ThreadMonitor.h:216
OS independent thread id.
TID identity
Thread identity data.
Definition: ThreadMonitor.h:230
Includes, defines and functions for threads.
void reflect(Reflector &r)
Definition: ThreadMonitor.h:173
A singleton class that can be freely configured using policies that control the creation, instantiation, lifetime and thread-safety.
Use this class to represent time durations.
Definition: Time.h:106
void reflect(Reflector &r)
Definition: ThreadMonitor.h:221
std::vector< ThreadInfo > ThreadInfoVector
Vector of local id thread data.
Definition: ThreadMonitor.h:241
Time create_time
Thread creation time in UTC.
Definition: ThreadMonitor.h:203
Serializable thread id.
Definition: ThreadMonitor.h:170
Extend basic thread id by boost thread id (not serializable).
Definition: ThreadMonitor.h:187
#define MIRA_BASE_EXPORT
This is required because on windows there is a macro defined called ERROR.
Definition: Platform.h:153
ThreadID id
Native thread ID (not boost::thread::native_handle_type!)
Definition: ThreadMonitor.h:179
A thread monitor class.
Definition: ThreadMonitor.h:115
Runtime thread info.
Definition: ThreadMonitor.h:193
Platform dependent defines and macros.
Duration user_time
The time amount in user-mode.
Definition: ThreadMonitor.h:204