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