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 "../services/profiler/Profiler.h" // for ProfilerPtr
29 #include <IceUtil/Time.h> // for Time
30 
31 #include <map> // for map
32 #include <string> // for string, operator<<
33 #include <vector> // for vector
34 #include <ostream> // for operator<<, size_t
35 #include <unordered_set>
36 
37 namespace armarx
38 {
39  struct ThreadUsage
40  {
41  int threadId;
42  int processId;
45  double load;
46  bool operator <(const ThreadUsage& rhs) const;
47  bool operator ==(const ThreadUsage& rhs) const;
48  };
49 
50  struct CpuUsage
51  {
52  long processId;
53  int lastUtime;
54  int lastStime;
59  };
60 
61  struct MemoryUsage
62  {
63  int fastbinBlocks; // fsmblocks: space in freed fastbin blocks (bytes)
64  int totalAllocatedSpace; // uordblks: total allocated space (bytes)
65  int totalFreeSpace; // fordblks: total free space (bytes)
66  };
67 }
68 
69 namespace std
70 {
71  template<>
72  class hash<armarx::ThreadUsage>
73  {
74  public:
75  template <class T>
76  static void hash_combine(std::size_t& seed, const T& v)
77  {
78  std::hash<T> hasher;
79  seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
80  }
81  size_t operator()(const armarx::ThreadUsage& t) const
82  {
83  std::size_t val = 0;
84  hash_combine(val, t.processId);
85  hash_combine(val, t.threadId);
86  return val;
87  }
88  };
89 }
90 
91 namespace armarx
92 {
93  /**
94  * @brief The ProcessWatcher class is instantiated once in each armarx::Application an monitors thread, CPU, and memory utilization
95  */
97  {
98  public:
100  void start();
101  void stop();
102 
103 
104  void addThread(int processId, int threadId);
105  void addThread(int threadId);
106  void addAllChildThreads(int processId, int threadId);
107 
108 
109  void removeAllThreads();
110  void removeThread(int processId, int threadId);
111  void removeThread(int threadId);
112 
113  void reportCpuUsage();
114  void reportMemoryUsage();
115 
116  double getThreadLoad(int processId, int threadId);
117  double getThreadLoad(int threadId);
118 
120 
121  static int GetThreadJiffies(int processId, int threadId);
122  static std::map<int, int> GetThreadListJiffies(int processId, std::vector<int> threadIds);
123  static int GetHertz();
124  private:
125  enum PROC_INDEX
126  {
127  eUTime = 13,
128  eSTime = 14,
129  eCUTIME = 15,
130  eCSTIME = 16
131  };
132 
133  void watch();
134  void runThreadWatchList();
135  void cpuUsageFromProcFile();
136  void getMemoryUsage();
137 
138  long Hertz;
139  std::string processCpuDataFilename;
140 
142 
143  std::mutex processCpuUsageMutex;
144  armarx::CpuUsage processCpuUsage;
145 
146  std::mutex processMemoryUsageMutex;
147  armarx::MemoryUsage memoryUsage;
148 
149  std::mutex threadWatchListMutex;
150  std::unordered_set<ThreadUsage> threadWatchList;
151 
152  /**
153  * Holds an instance of armarx::Profiler wich is set in the constructor of CPULoadWatcher.
154  */
155  Profiler::ProfilerPtr profiler;
156 
157  };
158 }
159 
160 
161 
armarx::ProcessWatcher::stop
void stop()
Definition: ProcessWatcher.cpp:75
armarx::CpuUsage::lastCUtime
int lastCUtime
Definition: ProcessWatcher.h:55
armarx::ThreadUsage::lastUpdate
IceUtil::Time lastUpdate
Definition: ProcessWatcher.h:43
armarx::CpuUsage
Definition: ProcessWatcher.h:50
armarx::ProcessWatcher::removeAllThreads
void removeAllThreads()
armarx::ProcessWatcher::GetThreadJiffies
static int GetThreadJiffies(int processId, int threadId)
Definition: ProcessWatcher.cpp:340
armarx::ProcessWatcher::GetHertz
static int GetHertz()
Definition: ProcessWatcher.cpp:142
armarx::ProcessWatcher::reportMemoryUsage
void reportMemoryUsage()
Definition: ProcessWatcher.cpp:361
armarx::MemoryUsage::fastbinBlocks
int fastbinBlocks
Definition: ProcessWatcher.h:63
armarx::ProcessWatcher::getThreadLoad
double getThreadLoad(int processId, int threadId)
Definition: ProcessWatcher.cpp:373
PeriodicTask.h
armarx::MemoryUsage
Definition: ProcessWatcher.h:61
armarx::ProcessWatcher::addAllChildThreads
void addAllChildThreads(int processId, int threadId)
armarx::ProcessWatcher::removeThread
void removeThread(int processId, int threadId)
Definition: ProcessWatcher.cpp:400
armarx::ThreadUsage::operator<
bool operator<(const ThreadUsage &rhs) const
Definition: ProcessWatcher.cpp:315
armarx::CpuUsage::proc_total_time
double proc_total_time
Definition: ProcessWatcher.h:58
armarx::CpuUsage::lastStime
int lastStime
Definition: ProcessWatcher.h:54
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:255
armarx::ThreadUsage::processId
int processId
Definition: ProcessWatcher.h:42
armarx::ProcessWatcher::addThread
void addThread(int processId, int threadId)
Definition: ProcessWatcher.cpp:81
armarx::ThreadUsage::load
double load
Definition: ProcessWatcher.h:45
std::hash< armarx::ThreadUsage >::operator()
size_t operator()(const armarx::ThreadUsage &t) const
Definition: ProcessWatcher.h:81
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
armarx::ProcessWatcher::getProcessCpuUsage
armarx::CpuUsage getProcessCpuUsage()
Definition: ProcessWatcher.cpp:393
armarx::CpuUsage::lastUtime
int lastUtime
Definition: ProcessWatcher.h:53
armarx::ProcessWatcher
The ProcessWatcher class is instantiated once in each armarx::Application an monitors thread,...
Definition: ProcessWatcher.h:96
armarx::CpuUsage::lastCStime
int lastCStime
Definition: ProcessWatcher.h:56
std::hash< armarx::ThreadUsage >::hash_combine
static void hash_combine(std::size_t &seed, const T &v)
Definition: ProcessWatcher.h:76
armarx::ThreadUsage::lastJiffies
int lastJiffies
Definition: ProcessWatcher.h:44
armarx::CpuUsage::processId
long processId
Definition: ProcessWatcher.h:52
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:41
std
Definition: Application.h:66
IceUtil::Handle
Definition: forward_declarations.h:29
armarx::MemoryUsage::totalAllocatedSpace
int totalAllocatedSpace
Definition: ProcessWatcher.h:64
armarx::Profiler::ProfilerPtr
std::shared_ptr< Profiler > ProfilerPtr
Definition: ManagedIceObject.h:75
armarx::ProcessWatcher::reportCpuUsage
void reportCpuUsage()
Definition: ProcessWatcher.cpp:355
armarx::ProcessWatcher::start
void start()
Definition: ProcessWatcher.cpp:65
T
float T
Definition: UnscentedKalmanFilterTest.cpp:35
armarx::ThreadUsage
Definition: ProcessWatcher.h:39
armarx::MemoryUsage::totalFreeSpace
int totalFreeSpace
Definition: ProcessWatcher.h:65
armarx
This file offers overloads of toIce() and fromIce() functions for STL container types.
Definition: ArmarXTimeserver.cpp:28
armarx::CpuUsage::lastUpdate
IceUtil::Time lastUpdate
Definition: ProcessWatcher.h:57
armarx::ThreadUsage::operator==
bool operator==(const ThreadUsage &rhs) const
Definition: ProcessWatcher.cpp:330