ProcessWatcher.h
Go to the documentation of this file.
1 /*
2 * This file is part of ArmarX.
3 *
4 * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
5 *
6 * ArmarX is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 *
10 * ArmarX is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 * @package ArmarX::
19 * @author Mirko Waechter ( mirko.waechter at kit dot edu)
20 * @date 2013
21 * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22 * GNU General Public License
23 */
24 
25 #pragma once
26 
27 #include <map> // for map
28 #include <ostream> // for operator<<, size_t
29 #include <string> // for string, operator<<
30 #include <unordered_set>
31 #include <vector> // for vector
32 
33 #include <IceUtil/Time.h> // for Time
34 
36 
37 #include "../services/profiler/Profiler.h" // for ProfilerPtr
38 
39 namespace armarx
40 {
41  struct ThreadUsage
42  {
43  int threadId;
44  int processId;
47  double load;
48  bool operator<(const ThreadUsage& rhs) const;
49  bool operator==(const ThreadUsage& rhs) const;
50  };
51 
52  struct CpuUsage
53  {
54  long processId;
55  int lastUtime;
56  int lastStime;
61  };
62 
63  struct MemoryUsage
64  {
65  int fastbinBlocks; // fsmblocks: space in freed fastbin blocks (bytes)
66  int totalAllocatedSpace; // uordblks: total allocated space (bytes)
67  int totalFreeSpace; // fordblks: total free space (bytes)
68  };
69 } // namespace armarx
70 
71 namespace std
72 {
73  template <>
74  class hash<armarx::ThreadUsage>
75  {
76  public:
77  template <class T>
78  static void
79  hash_combine(std::size_t& seed, const T& v)
80  {
81  std::hash<T> hasher;
82  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
83  }
84 
85  size_t
87  {
88  std::size_t val = 0;
89  hash_combine(val, t.processId);
90  hash_combine(val, t.threadId);
91  return val;
92  }
93  };
94 } // namespace std
95 
96 namespace armarx
97 {
98  /**
99  * @brief The ProcessWatcher class is instantiated once in each armarx::Application an monitors thread, CPU, and memory utilization
100  */
102  {
103  public:
105  void start();
106  void stop();
107 
108 
109  void addThread(int processId, int threadId);
110  void addThread(int threadId);
111  void addAllChildThreads(int processId, int threadId);
112 
113 
114  void removeAllThreads();
115  void removeThread(int processId, int threadId);
116  void removeThread(int threadId);
117 
118  void reportCpuUsage();
119  void reportMemoryUsage();
120 
121  double getThreadLoad(int processId, int threadId);
122  double getThreadLoad(int threadId);
123 
125 
126  static int GetThreadJiffies(int processId, int threadId);
127  static std::map<int, int> GetThreadListJiffies(int processId, std::vector<int> threadIds);
128  static int GetHertz();
129 
130  private:
131  enum PROC_INDEX
132  {
133  eUTime = 13,
134  eSTime = 14,
135  eCUTIME = 15,
136  eCSTIME = 16
137  };
138 
139  void watch();
140  void runThreadWatchList();
141  void cpuUsageFromProcFile();
142  void getMemoryUsage();
143 
144  long Hertz;
145  std::string processCpuDataFilename;
146 
148 
149  std::mutex processCpuUsageMutex;
150  armarx::CpuUsage processCpuUsage;
151 
152  std::mutex processMemoryUsageMutex;
153  armarx::MemoryUsage memoryUsage;
154 
155  std::mutex threadWatchListMutex;
156  std::unordered_set<ThreadUsage> threadWatchList;
157 
158  /**
159  * Holds an instance of armarx::Profiler wich is set in the constructor of CPULoadWatcher.
160  */
161  Profiler::ProfilerPtr profiler;
162  };
163 } // namespace armarx
armarx::ProcessWatcher::stop
void stop()
Definition: ProcessWatcher.cpp:77
armarx::CpuUsage::lastCUtime
int lastCUtime
Definition: ProcessWatcher.h:57
armarx::ThreadUsage::lastUpdate
IceUtil::Time lastUpdate
Definition: ProcessWatcher.h:45
armarx::CpuUsage
Definition: ProcessWatcher.h:52
armarx::ProcessWatcher::removeAllThreads
void removeAllThreads()
armarx::ProcessWatcher::GetThreadJiffies
static int GetThreadJiffies(int processId, int threadId)
Definition: ProcessWatcher.cpp:361
armarx::ProcessWatcher::GetHertz
static int GetHertz()
Definition: ProcessWatcher.cpp:150
armarx::ProcessWatcher::reportMemoryUsage
void reportMemoryUsage()
Definition: ProcessWatcher.cpp:383
armarx::MemoryUsage::fastbinBlocks
int fastbinBlocks
Definition: ProcessWatcher.h:65
armarx::ProcessWatcher::getThreadLoad
double getThreadLoad(int processId, int threadId)
Definition: ProcessWatcher.cpp:396
PeriodicTask.h
armarx::MemoryUsage
Definition: ProcessWatcher.h:63
armarx::ProcessWatcher::addAllChildThreads
void addAllChildThreads(int processId, int threadId)
armarx::ProcessWatcher::removeThread
void removeThread(int processId, int threadId)
Definition: ProcessWatcher.cpp:424
armarx::ThreadUsage::operator<
bool operator<(const ThreadUsage &rhs) const
Definition: ProcessWatcher.cpp:334
armarx::CpuUsage::proc_total_time
double proc_total_time
Definition: ProcessWatcher.h:60
armarx::CpuUsage::lastStime
int lastStime
Definition: ProcessWatcher.h:56
armarx::ProcessWatcher::ProcessWatcher
ProcessWatcher(Profiler::ProfilerPtr profiler)
Definition: ProcessWatcher.cpp:53
armarx::ProcessWatcher::GetThreadListJiffies
static std::map< int, int > GetThreadListJiffies(int processId, std::vector< int > threadIds)
Definition: ProcessWatcher.cpp:268
armarx::ThreadUsage::processId
int processId
Definition: ProcessWatcher.h:44
armarx::ProcessWatcher::addThread
void addThread(int processId, int threadId)
Definition: ProcessWatcher.cpp:84
armarx::ThreadUsage::load
double load
Definition: ProcessWatcher.h:47
std::hash< armarx::ThreadUsage >::operator()
size_t operator()(const armarx::ThreadUsage &t) const
Definition: ProcessWatcher.h:86
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::ProcessWatcher::getProcessCpuUsage
armarx::CpuUsage getProcessCpuUsage()
Definition: ProcessWatcher.cpp:417
armarx::CpuUsage::lastUtime
int lastUtime
Definition: ProcessWatcher.h:55
armarx::ProcessWatcher
The ProcessWatcher class is instantiated once in each armarx::Application an monitors thread,...
Definition: ProcessWatcher.h:101
armarx::CpuUsage::lastCStime
int lastCStime
Definition: ProcessWatcher.h:58
std::hash< armarx::ThreadUsage >::hash_combine
static void hash_combine(std::size_t &seed, const T &v)
Definition: ProcessWatcher.h:79
armarx::ThreadUsage::lastJiffies
int lastJiffies
Definition: ProcessWatcher.h:46
armarx::CpuUsage::processId
long processId
Definition: ProcessWatcher.h:54
armarx::ctrlutil::v
double v(double t, double v0, double a0, double j)
Definition: CtrlUtil.h:39
armarx::ThreadUsage::threadId
int threadId
Definition: ProcessWatcher.h:43
std
Definition: Application.h:66
IceUtil::Handle
Definition: forward_declarations.h:30
armarx::MemoryUsage::totalAllocatedSpace
int totalAllocatedSpace
Definition: ProcessWatcher.h:66
armarx::Profiler::ProfilerPtr
std::shared_ptr< Profiler > ProfilerPtr
Definition: ManagedIceObject.h:74
armarx::ProcessWatcher::reportCpuUsage
void reportCpuUsage()
Definition: ProcessWatcher.cpp:376
armarx::ProcessWatcher::start
void start()
Definition: ProcessWatcher.cpp:65
T
float T
Definition: UnscentedKalmanFilterTest.cpp:38
armarx::ThreadUsage
Definition: ProcessWatcher.h:41
armarx::MemoryUsage::totalFreeSpace
int totalFreeSpace
Definition: ProcessWatcher.h:67
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:27
armarx::CpuUsage::lastUpdate
IceUtil::Time lastUpdate
Definition: ProcessWatcher.h:59
armarx::ThreadUsage::operator==
bool operator==(const ThreadUsage &rhs) const
Definition: ProcessWatcher.cpp:350