ProfilerStorage.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of ArmarX.
3  *
4  * Copyright (C) 2012-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 MemoryX::component::ProfilerStorage
19  * @author haass ( tobias dot haass at student dot kit dot edu )
20  * @author Manfred Kroehnert ( manfred dot kroehnert at kit dot edu )
21  * @date 2013, 2015
22  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
23  * GNU General Public License
24  */
25 
26 
27 #include "ProfilerStorage.h"
28 
36 
37 #include <IceUtil/Time.h>
38 
39 #include <mongo/client/dbclient.h>
40 
41 
42 namespace memoryx
43 {
44  std::string ProfilerStorage::getDefaultName() const
45  {
46  return "ProfilerStorage";
47  }
48 
49 
51  {
52  setTag("ProfilerStorage");
53 
57 
58  enabled = getProperty<bool>("enabled").getValue();
59 
60  if (enabled)
61  {
62  usingTopic(armarx::Profiler::PROFILER_TOPIC_NAME);
63  }
64 
65  saveTimeout = getProperty<int>("SaveTimeout").getValue();
66  saveToLongtermMemoryTask = new armarx::RunningTask<ProfilerStorage>(this, &ProfilerStorage::saveToLongtermMemory);
67  saveTransitionsToLongtermMemoryTask = new armarx::RunningTask<ProfilerStorage>(this, &ProfilerStorage::saveTransitionsToLongtermMemory);
68 
69  // the initial transition does not have a source state, thus the name of the initial transition is empty
70  memorySnapshot = CreateProfilerMemorySnapshot("", workingMemoryPrx);
71  }
72 
73 
75  {
76  getProxy(commonStoragePrx, getCommonStorageName());
77  getProxy(workingMemoryPrx, getWorkingMemoryName());
78  getProxy(longtermMemoryPrx, getLongtermMemoryName());
79  profilerDataSegmentPrx = longtermMemoryPrx->getProfilerDataSegment();
80 
81  saveToLongtermMemoryTask->start();
82  saveTransitionsToLongtermMemoryTask->start();
83  }
84 
85 
87  {
88  }
89 
90 
92  {
93  enabled = false;
94  saveToLongtermMemoryTask->stop();
95  saveTransitionsToLongtermMemoryTask->stop();
96  }
97 
98 
99  void ProfilerStorage::saveToLongtermMemory()
100  {
101  IceUtil::Time t = IceUtil::Time::now();
102 
103  while (enabled)
104  {
105  size_t logSize = entityLog.size();
106 
107  if (logSize == 0)
108  {
109  continue;
110  }
111 
112  if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
113  {
114  continue;
115  }
116 
117  EntityBaseList entityLogCopy;
118  {
119  std::scoped_lock lock(entityLogMutex);
120  entityLog.swap(entityLogCopy);
121  }
122  t = IceUtil::Time::now();
123  profilerDataSegmentPrx->addEntityList(entityLogCopy);
124  ARMARX_WARNING_S << "Took " << (IceUtil::Time::now() - t).toMilliSeconds() << " [ms] to add " << entityLogCopy.size() << " Entities";
125  t = IceUtil::Time::now();
126  }
127  }
128 
129 
130  void ProfilerStorage::saveTransitionsToLongtermMemory()
131  {
132  IceUtil::Time t = IceUtil::Time::now();
133 
134  while (enabled)
135  {
136  size_t logSize = transitionSnapshotList.size();
137 
138  if (logSize == 0)
139  {
140  continue;
141  }
142 
143  if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
144  {
145  continue;
146  }
147 
148  TransitionSnapshotList transitionSnapshotListCopy;
149  {
150  std::scoped_lock lock(transitionSnapshotListMutex);
151  transitionSnapshotList.swap(transitionSnapshotListCopy);
152  }
153  t = IceUtil::Time::now();
154 
155  for (TransitionSnapshot& snapshot : transitionSnapshotListCopy)
156  {
157  std::string sourceSnapshot = profilerDataSegmentPrx->saveUniqueMemorySnapshot(snapshot.sourceStateMemorySnapshot);
158  std::string targetSnapshot = profilerDataSegmentPrx->saveUniqueMemorySnapshot(snapshot.targetStateMemorySnapshot);
159  EntityRefBasePtr sourceSnapshotRef = profilerDataSegmentPrx->getEntityRefById(sourceSnapshot);
160  EntityRefBasePtr targetSnapshotRef = profilerDataSegmentPrx->getEntityRefById(targetSnapshot);
161  ProfilerTransitionPtr profilerTransition = new memoryx::ProfilerTransition(snapshot.transition.parentStateIdentifier, snapshot.transition.sourceStateIdentifier, snapshot.transition.targetStateIdentifier, sourceSnapshotRef, targetSnapshotRef, 1);
162  std::string entityId = profilerDataSegmentPrx->saveOrUpdateTransition(profilerTransition);
163  }
164  ARMARX_WARNING_S << "Took " << (IceUtil::Time::now() - t).toMilliSeconds() << " [ms] to add " << transitionSnapshotListCopy.size() << " Transitions";
165  t = IceUtil::Time::now();
166  }
167  }
168 
169 
170  void ProfilerStorage::reportNetworkTraffic(const std::string&, const std::string&, Ice::Int, Ice::Int, const Ice::Current&)
171  {
172  // ignore network traffic for now
173  }
174 
175 
176  void ProfilerStorage::reportEvent(const armarx::ProfilerEvent& profilerEvent, const Ice::Current&)
177  {
178  std::scoped_lock lock(entityLogMutex);
179  entityLog.push_back(new memoryx::ProfilerEvent(profilerEvent));
180  }
181 
182 
183  void ProfilerStorage::reportStatechartTransition(const armarx::ProfilerStatechartTransition& transition, const ::Ice::Current& context)
184  {
185  std::scoped_lock lock(transitionSnapshotListMutex);
186  TransitionSnapshot transitionSnapshot;
187  transitionSnapshot.transition = transition;
188  transitionSnapshot.sourceStateMemorySnapshot = memorySnapshot;
189  memorySnapshot = CreateProfilerMemorySnapshot(transition.targetStateIdentifier, workingMemoryPrx);
190  transitionSnapshot.targetStateMemorySnapshot = memorySnapshot;
191  transitionSnapshotList.push_back(transitionSnapshot);
192  }
193 
194 
195  void ProfilerStorage::reportStatechartInputParameters(const armarx::ProfilerStatechartParameters& inputParameters, const Ice::Current&)
196  {
197  }
198 
199 
200  void ProfilerStorage::reportStatechartLocalParameters(const armarx::ProfilerStatechartParameters& localParameters, const Ice::Current&)
201  {
202  }
203 
204 
205  void ProfilerStorage::reportStatechartOutputParameters(const armarx::ProfilerStatechartParameters& outputParameters, const Ice::Current&)
206  {
207  }
208 
209 
210  void ProfilerStorage::reportProcessCpuUsage(const armarx::ProfilerProcessCpuUsage& process, const Ice::Current& context)
211  {
212  std::scoped_lock lock(entityLogMutex);
213  entityLog.push_back(new memoryx::ProfilerProcess(process));
214  }
215 
216  void ProfilerStorage::reportProcessMemoryUsage(const armarx::ProfilerProcessMemoryUsage& memoryUsage, const Ice::Current& context)
217  {
218  std::scoped_lock lock(entityLogMutex);
219  entityLog.push_back(new memoryx::ProfilerMemoryUsage(memoryUsage));
220  }
221 
222 
223 
224 
225  void ProfilerStorage::reportEventList(const armarx::ProfilerEventList& events, const Ice::Current&)
226  {
227  memoryx::EntityBaseList eventEntities(events.size());
228  std::transform(events.begin(), events.end(), eventEntities.begin(),
229  [](const armarx::ProfilerEvent & event)
230  {
231  return new memoryx::ProfilerEvent(event);
232  });
233 
234  {
235  std::scoped_lock lock(entityLogMutex);
236  entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
237  }
238  }
239 
240 
241  void ProfilerStorage::reportStatechartTransitionList(const armarx::ProfilerStatechartTransitionList& transitions, const Ice::Current&)
242  {
243  if (transitions.empty())
244  {
245  return;
246  }
247 
248  TransitionSnapshotList transitionSnapshots(transitions.size());
249 
250  ProfilerMemorySnapshotPtr sourceMemorySnapshot = memorySnapshot->clone();
251 
252  std::transform(transitions.begin(), transitions.end(), transitionSnapshots.begin(),
253  [](const armarx::ProfilerStatechartTransition & transition)
254  {
255  TransitionSnapshot transitionSnapshot = {transition, new ProfilerMemorySnapshot(), new ProfilerMemorySnapshot()};
256  return transitionSnapshot;
257  });
258 
259  transitionSnapshots.front().sourceStateMemorySnapshot = sourceMemorySnapshot;
260  memorySnapshot = ProfilerStorage::CreateProfilerMemorySnapshot(transitionSnapshots.back().transition.targetStateIdentifier, workingMemoryPrx);
261  if (memorySnapshot)
262  {
263  transitionSnapshots.back().targetStateMemorySnapshot = memorySnapshot;
264  }
265  {
266  std::scoped_lock lock(transitionSnapshotListMutex);
267  transitionSnapshotList.insert(transitionSnapshotList.end(), transitionSnapshots.begin(), transitionSnapshots.end());
268  }
269  }
270 
271 
272  void ProfilerStorage::reportStatechartInputParametersList(const armarx::ProfilerStatechartParametersList& inputParametersList, const Ice::Current&)
273  {
274  }
275 
276 
277  void ProfilerStorage::reportStatechartLocalParametersList(const armarx::ProfilerStatechartParametersList& localParametesList, const Ice::Current&)
278  {
279  }
280 
281 
282  void ProfilerStorage::reportStatechartOutputParametersList(const armarx::ProfilerStatechartParametersList& outputParametersList, const Ice::Current&)
283  {
284  }
285 
286 
287  void ProfilerStorage::reportProcessCpuUsageList(const armarx::ProfilerProcessCpuUsageList& processes, const Ice::Current&)
288  {
289  memoryx::EntityBaseList eventEntities(processes.size());
290  std::transform(processes.begin(), processes.end(), eventEntities.begin(),
291  [](const armarx::ProfilerProcessCpuUsage & process)
292  {
293  return new memoryx::ProfilerProcess(process);
294  });
295 
296  {
297  std::scoped_lock lock(entityLogMutex);
298  entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
299  }
300  }
301 
302  void ProfilerStorage::reportProcessMemoryUsageList(const armarx::ProfilerProcessMemoryUsageList& memoryUsages, const Ice::Current&)
303  {
304  memoryx::EntityBaseList eventEntities(memoryUsages.size());
305  std::transform(memoryUsages.begin(), memoryUsages.end(), eventEntities.begin(),
306  [](const armarx::ProfilerProcessMemoryUsage & memoryUsage)
307  {
308  return new memoryx::ProfilerMemoryUsage(memoryUsage);
309  });
310 
311  {
312  std::scoped_lock lock(entityLogMutex);
313  entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
314  }
315  }
316 
317 
318  ProfilerMemorySnapshotPtr ProfilerStorage::CreateProfilerMemorySnapshot(const std::string& stateName, const WorkingMemoryInterfacePrx& workingMemoryProxy)
319  {
320  if (!workingMemoryProxy)
321  {
322  return new ProfilerMemorySnapshot(stateName, Ice::Context());
323  }
324 
325  Ice::Context snapshot;
326 
327  memoryx::ObjectInstancePtr humanInstance = memoryx::ObjectInstancePtr::dynamicCast(workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName("human"));
328  if (humanInstance)
329  {
330  snapshot["human"] = humanInstance->getAttribute("name")->getValue()->getString();
331  if (humanInstance->hasAttribute("humanObject"))
332  {
333  snapshot["humanObject"] = humanInstance->getAttribute("humanObject")->getValue()->getString();
334  }
335 
336  if (humanInstance->hasAttribute("intention"))
337  {
338  snapshot["intention"] = humanInstance->getAttribute("intention")->getValue()->getString();
339  }
340  }
341 
342  memoryx::ObjectInstancePtr objectInstance = memoryx::ObjectInstancePtr::dynamicCast(workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName("taskObject"));
343  if (objectInstance)
344  {
345  snapshot["taskObject"] = objectInstance->getAttribute("name")->getValue()->getString();
346 
347  bool inHand = objectInstance->getAttribute("inHand")->getValue()->getBool();
348  snapshot["objectInHand"] = inHand ? "true" : "false";
349  }
350 
351  return new ProfilerMemorySnapshot(stateName, snapshot);
352  }
353 
354 
355  std::string ProfilerStorage::getCommonStorageName()
356  {
357  return getProperty<std::string>("CommonStorageName").getValue();
358  }
359 
360 
361  std::string ProfilerStorage::getWorkingMemoryName()
362  {
363  return getProperty<std::string>("WorkingMemoryName").getValue();
364  }
365 
366  std::string ProfilerStorage::getLongtermMemoryName()
367  {
368  return getProperty<std::string>("LongtermMemoryName").getValue();
369  }
370 
371 
372  armarx::PropertyDefinitionsPtr ProfilerStorage::createPropertyDefinitions()
373  {
376  getConfigIdentifier()));
377  }
378 
379  void ProfilerStorage::reportStatechartTransitionWithParameters(const armarx::ProfilerStatechartTransitionWithParameters& transition, const Ice::Current& context)
380  {
381 
382  }
383 
384  void ProfilerStorage::reportStatechartTransitionWithParametersList(const armarx::ProfilerStatechartTransitionWithParametersList& transitions, const Ice::Current&)
385  {
386 
387  }
388 
389 
390  ProfilerStoragePropertyDefinitions::ProfilerStoragePropertyDefinitions(std::string prefix) :
391  ComponentPropertyDefinitions(prefix)
392  {
393  defineOptionalProperty<int>("SaveTimeout", 500, "How often should the statistics be saved (ms)");
394  defineOptionalProperty<std::string>("CommonStorageName", "CommonStorage", "Name of the CommonStorage proxy to use");
395  defineOptionalProperty<std::string>("WorkingMemoryName", "WorkingMemory", "Name of the WorkingMemory proxy to use");
396  defineOptionalProperty<std::string>("LongtermMemoryName", "LongtermMemory", "Name of the LongtermMemory proxy to use");
397  defineOptionalProperty<bool>("enabled", true, "disable/enable saver");
398  }
399 }
memoryx::ProfilerStorage::onExitComponent
void onExitComponent() override
Definition: ProfilerStorage.cpp:91
memoryx::ProfilerProcess
Definition: ProfilerProcess.h:38
memoryx::ProfilerStorage::reportStatechartInputParameters
void reportStatechartInputParameters(const armarx::ProfilerStatechartParameters &inputParameters, const Ice::Current &=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:195
memoryx::ProfilerStorage::reportStatechartTransition
void reportStatechartTransition(const armarx::ProfilerStatechartTransition &transition, const ::Ice::Current &context=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:183
memoryx::ProfilerEvent
Definition: ProfilerEvent.h:40
memoryx
VirtualRobot headers.
Definition: CommonPlacesTester.cpp:48
memoryx::ProfilerStorage::reportProcessMemoryUsage
void reportProcessMemoryUsage(const armarx::ProfilerProcessMemoryUsage &memoryUsage, const Ice::Current &context=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:216
memoryx::ProfilerStorage::reportStatechartTransitionList
void reportStatechartTransitionList(const armarx::ProfilerStatechartTransitionList &transitions, const Ice::Current &=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:241
memoryx::ProfilerStorage::reportProcessCpuUsage
void reportProcessCpuUsage(const armarx::ProfilerProcessCpuUsage &process, const Ice::Current &context=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:210
memoryx::ProfilerStorage::onInitComponent
void onInitComponent() override
Definition: ProfilerStorage.cpp:50
memoryx::ProfilerStorage::getWorkingMemoryName
std::string getWorkingMemoryName()
Definition: ProfilerStorage.cpp:361
armarx::RunningTask
Definition: ArmarXMultipleObjectsScheduler.h:35
StatechartObjectFactories.h
IceInternal::Handle< ProfilerMemorySnapshot >
memoryx::ProfilerStorage::getLongtermMemoryName
std::string getLongtermMemoryName()
Definition: ProfilerStorage.cpp:366
memoryx::ProfilerStorage::getDefaultName
std::string getDefaultName() const override
Definition: ProfilerStorage.cpp:44
memoryx::ProfilerStorage::reportNetworkTraffic
void reportNetworkTraffic(const std::string &, const std::string &, Ice::Int, Ice::Int, const Ice::Current &context=Ice::emptyCurrent) override
reportNetworkTraffic
Definition: ProfilerStorage.cpp:170
memoryx::ProfilerStoragePropertyDefinitions
Definition: ProfilerStorage.h:42
memoryx::ProfilerMemorySnapshot
Definition: ProfilerMemorySnapshot.h:41
MemoryXCoreObjectFactories.h
memoryx::ProfilerStorage::reportStatechartOutputParameters
void reportStatechartOutputParameters(const armarx::ProfilerStatechartParameters &outputParameters, const Ice::Current &=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:205
memoryx::ProfilerStorage::onDisconnectComponent
void onDisconnectComponent() override
Definition: ProfilerStorage.cpp:86
ARMARX_WARNING_S
#define ARMARX_WARNING_S
Definition: Logging.h:206
armarx::armem::Time
armarx::core::time::DateTime Time
Definition: forward_declarations.h:13
ProfilerEvent.h
armarx::ManagedIceObject::usingTopic
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Definition: ManagedIceObject.cpp:248
ObjectInstance.h
memoryx::ProfilerStorage::onConnectComponent
void onConnectComponent() override
Definition: ProfilerStorage.cpp:74
IceUtil::Handle< class PropertyDefinitionContainer >
memoryx::ProfilerStorage::getCommonStorageName
std::string getCommonStorageName()
Definition: ProfilerStorage.cpp:355
armarx::transform
auto transform(const Container< InputT, Alloc > &in, OutputT(*func)(InputT const &)) -> Container< OutputT, typename std::allocator_traits< Alloc >::template rebind_alloc< OutputT > >
Convenience function (with less typing) to transform a container of type InputT into the same contain...
Definition: algorithm.h:315
memoryx::ProfilerStorage::reportEventList
void reportEventList(const armarx::ProfilerEventList &events, const Ice::Current &=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:225
armarx::VariantType::Int
const VariantTypeId Int
Definition: Variant.h:916
MemoryXTypesObjectFactories.h
memoryx::ProfilerStorage::reportStatechartLocalParameters
void reportStatechartLocalParameters(const armarx::ProfilerStatechartParameters &localParameters, const Ice::Current &=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:200
memoryx::ProfilerStorage::reportEvent
void reportEvent(const armarx::ProfilerEvent &profilerEvent, const Ice::Current &context=Ice::emptyCurrent) override
Definition: ProfilerStorage.cpp:176
ProfilerStorage.h
armarx::Logging::setTag
void setTag(const LogTag &tag)
Definition: Logging.cpp:55
memoryx::ProfilerMemoryUsage
Definition: ProfilerMemoryUsage.h:38
memoryx::ProfilerTransition
Definition: ProfilerTransition.h:39
ProfilerMemoryUsage.h
armarx::PropertyDefinitionsPtr
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
Definition: forward_declarations.h:34
memoryx::ProfilerTransitionPtr
IceInternal::Handle< ProfilerTransition > ProfilerTransitionPtr
Definition: ProfilerTransition.h:37
armarx::ManagedIceObject::getProxy
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
Definition: ManagedIceObject.cpp:393
armarx::ManagedIceObject::usingProxy
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
Definition: ManagedIceObject.cpp:151
memoryx::ProfilerStorage::CreateProfilerMemorySnapshot
static ProfilerMemorySnapshotPtr CreateProfilerMemorySnapshot(const std::string &stateName, const WorkingMemoryInterfacePrx &workingMemoryProxy)
Definition: ProfilerStorage.cpp:318
ProfilerProcess.h