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
39namespace armarx
40{
42 {
45 IceUtil::Time lastUpdate;
47 double load;
48 bool operator<(const ThreadUsage& rhs) const;
49 bool operator==(const ThreadUsage& rhs) const;
50 };
51
52 struct CpuUsage
53 {
59 IceUtil::Time lastUpdate;
61 };
62
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
71namespace 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
96namespace 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
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
IceUtil::Handle< PeriodicTask< T > > pointer_type
Shared pointer type for convenience.
ProcessWatcher(Profiler::ProfilerPtr profiler)
void removeThread(int processId, int threadId)
static std::map< int, int > GetThreadListJiffies(int processId, std::vector< int > threadIds)
static int GetThreadJiffies(int processId, int threadId)
armarx::CpuUsage getProcessCpuUsage()
double getThreadLoad(int processId, int threadId)
void addThread(int processId, int threadId)
void addAllChildThreads(int processId, int threadId)
size_t operator()(const armarx::ThreadUsage &t) const
static void hash_combine(std::size_t &seed, const T &v)
std::shared_ptr< Profiler > ProfilerPtr
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceUtil::Time lastUpdate
bool operator==(const ThreadUsage &rhs) const
bool operator<(const ThreadUsage &rhs) const
IceUtil::Time lastUpdate