ThreadList.cpp
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 2012
21* @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22* GNU General Public License
23*/
24
25#include "ThreadList.h"
26
27#include <mutex>
28#include <set>
29
31#include "PeriodicTask.h"
32
33namespace armarx
34{
36 {
37 std::set<RunningTaskIceBase*> activeRunningTaskList;
38 std::set<PeriodicTaskIceBase*> activePeriodicTaskList;
39 std::mutex runListMutex;
42
43 std::shared_ptr<ProcessWatcher> processWatcher;
44 };
45
47 {
48 impl->processWatcher.reset(new ProcessWatcher(getProfiler()));
49 }
50
54
55 void
59
60 void
62 {
63 impl->processWatcher->start();
64 }
65
66 void
68 {
69 impl->processWatcher->stop();
70 }
71
72 void
76
77 Ice::StringSeq
79 {
80 std::unique_lock lock(impl->runListMutex);
81 Ice::StringSeq result;
82
83 for (std::set<RunningTaskIceBase*>::const_iterator it = impl->activeRunningTaskList.begin();
84 it != impl->activeRunningTaskList.end();
85 ++it)
86 {
87 result.push_back((*it)->name);
88 }
89
90 return result;
91 }
92
93 Ice::StringSeq
95 {
96 std::unique_lock lock(impl->periodicListMutex);
97
98 Ice::StringSeq result;
99
100 for (std::set<PeriodicTaskIceBase*>::const_iterator it =
101 impl->activePeriodicTaskList.begin();
102 it != impl->activePeriodicTaskList.end();
103 ++it)
104 {
105 result.push_back((*it)->name);
106 }
107
108 return result;
109 }
110
111 double
112 ThreadList::getCpuUsage(const Ice::Current& c)
113 {
114 std::unique_lock lock(impl->procTotalTimeMutex);
115 return impl->processWatcher->getProcessCpuUsage().proc_total_time;
116 }
117
118 RunningTaskList
119 ThreadList::getRunningTasks(const Ice::Current& c)
120 {
121 std::unique_lock lock(impl->runListMutex);
122 RunningTaskList resultList;
123 std::set<RunningTaskIceBase*>::const_iterator it = impl->activeRunningTaskList.begin();
124
125 for (; it != impl->activeRunningTaskList.end(); it++)
126 {
127 resultList.push_back(**it);
128 resultList.rbegin()->workload = impl->processWatcher->getThreadLoad((*it)->threadId);
129 }
130
131 return resultList;
132 }
133
134 PeriodicTaskList
135 ThreadList::getPeriodicTasks(const Ice::Current& c)
136 {
137 std::unique_lock lock(impl->periodicListMutex);
138 PeriodicTaskList resultList;
139 std::set<PeriodicTaskIceBase*>::const_iterator it = impl->activePeriodicTaskList.begin();
140
141 for (; it != impl->activePeriodicTaskList.end(); it++)
142 {
143 resultList.push_back(**it);
144 }
145
146 return resultList;
147 }
148
149 void
150 ThreadList::addRunningTask(RunningTaskIceBase* threadPtr)
151 {
152 std::unique_lock lock(impl->runListMutex);
153 impl->processWatcher->addThread(threadPtr->threadId);
154
155 if (impl->activeRunningTaskList.find(threadPtr) != impl->activeRunningTaskList.end())
156 {
157 return;
158 }
159
160 impl->activeRunningTaskList.insert(threadPtr);
161 }
162
163 bool
164 ThreadList::removeRunningTask(RunningTaskIceBase* threadPtr)
165 {
166 std::unique_lock lock(impl->runListMutex);
167 impl->processWatcher->removeThread(threadPtr->threadId);
168
169 if (impl->activeRunningTaskList.find(threadPtr) == impl->activeRunningTaskList.end())
170 {
171 return false;
172 }
173
174 impl->activeRunningTaskList.erase(threadPtr);
175 return true;
176 }
177
178 void
179 ThreadList::addPeriodicTask(PeriodicTaskIceBase* threadPtr)
180 {
181 std::unique_lock lock(impl->periodicListMutex);
182
183 if (impl->activePeriodicTaskList.find(threadPtr) != impl->activePeriodicTaskList.end())
184 {
185 return;
186 }
187
188 impl->activePeriodicTaskList.insert(threadPtr);
189 }
190
191 bool
192 ThreadList::removePeriodicTask(PeriodicTaskIceBase* threadPtr)
193 {
194 std::unique_lock lock(impl->periodicListMutex);
195
196 if (impl->activePeriodicTaskList.find(threadPtr) == impl->activePeriodicTaskList.end())
197 {
198 return false;
199 }
200
201 impl->activePeriodicTaskList.erase(threadPtr);
202 return true;
203 }
204
207 {
208 static ThreadListPtr applicationThreadList = new ThreadList();
209 return applicationThreadList;
210 }
211
212 void
213 ThreadList::setApplicationThreadListName(const std::string& threadListName)
214 {
215 getApplicationThreadList()->setName(threadListName);
216 }
217} // namespace armarx
218
219std::ostream&
220std::operator<<(std::ostream& stream, const armarx::RunningTaskIceBase& task)
221{
222 stream << "Threadname: " << task.name << " \n ";
223 stream << "\tWorldload: " << task.workload << " \n ";
224 stream << "\tRunning: " << task.running << " \n ";
225 stream << "\tStartTime: " << IceUtil::Time::microSeconds(task.startTime).toDateTime() << " \n ";
226 stream << "\tLastFeedbackTime: "
227 << IceUtil::Time::microSeconds(task.lastFeedbackTime).toDateTime() << " \n ";
228 stream << "\tStopped: " << task.stopped << " \n ";
229 stream << "\tFinished: " << task.finished << "";
230
231 return stream;
232}
constexpr T c
Profiler::ProfilerPtr getProfiler() const
getProfiler returns an instance of armarx::Profiler
The ProcessWatcher class is instantiated once in each armarx::Application an monitors thread,...
void addRunningTask(RunningTaskIceBase *threadPtr)
add RunningTask instance to this thread list
void onInitComponent() override
Pure virtual hook for the subclass.
RunningTaskList getRunningTasks(const Ice::Current &c=Ice::emptyCurrent) override
PeriodicTaskList getPeriodicTasks(const Ice::Current &c=Ice::emptyCurrent) override
bool removePeriodicTask(PeriodicTaskIceBase *threadPtr)
remove PeriodicTask instance from this thread list
Ice::StringSeq getPeriodicTaskNames(const Ice::Current &c=Ice::emptyCurrent) override
void onDisconnectComponent() override
Hook for subclass.
static ThreadListPtr getApplicationThreadList()
getApplicationThreadList retrieves the ThreadList, that contains all TimerTasks and PeriodicTasks in ...
void setApplicationThreadListName(const std::string &threadListName)
bool removeRunningTask(RunningTaskIceBase *threadPtr)
remove RunningTask instance from this thread list
void addPeriodicTask(PeriodicTaskIceBase *threadPtr)
add PeriodicTask instance to this thread list
void onConnectComponent() override
Pure virtual hook for the subclass.
Ice::StringSeq getRunningTaskNames(const Ice::Current &c=Ice::emptyCurrent) override
double getCpuUsage(const Ice::Current &c=Ice::emptyCurrent) override
This file offers overloads of toIce() and fromIce() functions for STL container types.
IceInternal::Handle< ThreadList > ThreadListPtr
Definition RunningTask.h:39
ARMARXCORE_IMPORT_EXPORT ostream & operator<<(ostream &stream, const armarx::RunningTaskIceBase &task)
std::set< RunningTaskIceBase * > activeRunningTaskList
std::shared_ptr< ProcessWatcher > processWatcher
std::set< PeriodicTaskIceBase * > activePeriodicTaskList