29 #include <IceUtil/Time.h>
39 #include <mongo/client/dbclient.h>
46 return "ProfilerStorage";
58 enabled = getProperty<bool>(
"enabled").getValue();
62 usingTopic(armarx::Profiler::PROFILER_TOPIC_NAME);
65 saveTimeout = getProperty<int>(
"SaveTimeout").getValue();
66 saveToLongtermMemoryTask =
69 this, &ProfilerStorage::saveTransitionsToLongtermMemory);
81 profilerDataSegmentPrx = longtermMemoryPrx->getProfilerDataSegment();
83 saveToLongtermMemoryTask->start();
84 saveTransitionsToLongtermMemoryTask->start();
96 saveToLongtermMemoryTask->stop();
97 saveTransitionsToLongtermMemoryTask->stop();
101 ProfilerStorage::saveToLongtermMemory()
107 size_t logSize = entityLog.size();
114 if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
119 EntityBaseList entityLogCopy;
121 std::scoped_lock lock(entityLogMutex);
122 entityLog.swap(entityLogCopy);
124 t = IceUtil::Time::now();
125 profilerDataSegmentPrx->addEntityList(entityLogCopy);
127 <<
" [ms] to add " << entityLogCopy.size() <<
" Entities";
128 t = IceUtil::Time::now();
133 ProfilerStorage::saveTransitionsToLongtermMemory()
139 size_t logSize = transitionSnapshotList.size();
146 if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
151 TransitionSnapshotList transitionSnapshotListCopy;
153 std::scoped_lock lock(transitionSnapshotListMutex);
154 transitionSnapshotList.swap(transitionSnapshotListCopy);
156 t = IceUtil::Time::now();
158 for (TransitionSnapshot& snapshot : transitionSnapshotListCopy)
160 std::string sourceSnapshot = profilerDataSegmentPrx->saveUniqueMemorySnapshot(
161 snapshot.sourceStateMemorySnapshot);
162 std::string targetSnapshot = profilerDataSegmentPrx->saveUniqueMemorySnapshot(
163 snapshot.targetStateMemorySnapshot);
164 EntityRefBasePtr sourceSnapshotRef =
165 profilerDataSegmentPrx->getEntityRefById(sourceSnapshot);
166 EntityRefBasePtr targetSnapshotRef =
167 profilerDataSegmentPrx->getEntityRefById(targetSnapshot);
170 snapshot.transition.sourceStateIdentifier,
171 snapshot.transition.targetStateIdentifier,
175 std::string entityId =
176 profilerDataSegmentPrx->saveOrUpdateTransition(profilerTransition);
179 <<
" [ms] to add " << transitionSnapshotListCopy.size()
181 t = IceUtil::Time::now();
198 std::scoped_lock lock(entityLogMutex);
204 const armarx::ProfilerStatechartTransition& transition,
205 const ::Ice::Current& context)
207 std::scoped_lock lock(transitionSnapshotListMutex);
208 TransitionSnapshot transitionSnapshot;
209 transitionSnapshot.transition = transition;
210 transitionSnapshot.sourceStateMemorySnapshot = memorySnapshot;
213 transitionSnapshot.targetStateMemorySnapshot = memorySnapshot;
214 transitionSnapshotList.push_back(transitionSnapshot);
219 const armarx::ProfilerStatechartParameters& inputParameters,
226 const armarx::ProfilerStatechartParameters& localParameters,
233 const armarx::ProfilerStatechartParameters& outputParameters,
240 const Ice::Current& context)
242 std::scoped_lock lock(entityLogMutex);
248 const Ice::Current& context)
250 std::scoped_lock lock(entityLogMutex);
257 memoryx::EntityBaseList eventEntities(events.size());
260 eventEntities.begin(),
261 [](
const armarx::ProfilerEvent& event)
262 { return new memoryx::ProfilerEvent(event); });
265 std::scoped_lock lock(entityLogMutex);
266 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
272 const armarx::ProfilerStatechartTransitionList& transitions,
275 if (transitions.empty())
280 TransitionSnapshotList transitionSnapshots(transitions.size());
286 transitionSnapshots.begin(),
287 [](
const armarx::ProfilerStatechartTransition& transition)
289 TransitionSnapshot transitionSnapshot = {transition,
290 new ProfilerMemorySnapshot(),
291 new ProfilerMemorySnapshot()};
292 return transitionSnapshot;
295 transitionSnapshots.front().sourceStateMemorySnapshot = sourceMemorySnapshot;
297 transitionSnapshots.back().transition.targetStateIdentifier, workingMemoryPrx);
300 transitionSnapshots.back().targetStateMemorySnapshot = memorySnapshot;
303 std::scoped_lock lock(transitionSnapshotListMutex);
304 transitionSnapshotList.insert(transitionSnapshotList.end(),
305 transitionSnapshots.begin(),
306 transitionSnapshots.end());
311 ProfilerStorage::reportStatechartInputParametersList(
312 const armarx::ProfilerStatechartParametersList& inputParametersList,
318 ProfilerStorage::reportStatechartLocalParametersList(
319 const armarx::ProfilerStatechartParametersList& localParametesList,
325 ProfilerStorage::reportStatechartOutputParametersList(
326 const armarx::ProfilerStatechartParametersList& outputParametersList,
332 ProfilerStorage::reportProcessCpuUsageList(
const armarx::ProfilerProcessCpuUsageList& processes,
335 memoryx::EntityBaseList eventEntities(processes.size());
338 eventEntities.begin(),
339 [](
const armarx::ProfilerProcessCpuUsage& process)
340 { return new memoryx::ProfilerProcess(process); });
343 std::scoped_lock lock(entityLogMutex);
344 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
349 ProfilerStorage::reportProcessMemoryUsageList(
350 const armarx::ProfilerProcessMemoryUsageList& memoryUsages,
353 memoryx::EntityBaseList eventEntities(memoryUsages.size());
356 eventEntities.begin(),
357 [](
const armarx::ProfilerProcessMemoryUsage& memoryUsage)
358 { return new memoryx::ProfilerMemoryUsage(memoryUsage); });
361 std::scoped_lock lock(entityLogMutex);
362 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
367 ProfilerStorage::CreateProfilerMemorySnapshot(
368 const std::string& stateName,
369 const WorkingMemoryInterfacePrx& workingMemoryProxy)
371 if (!workingMemoryProxy)
376 Ice::Context snapshot;
379 workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName(
"human"));
382 snapshot[
"human"] = humanInstance->getAttribute(
"name")->getValue()->getString();
383 if (humanInstance->hasAttribute(
"humanObject"))
385 snapshot[
"humanObject"] =
386 humanInstance->getAttribute(
"humanObject")->getValue()->getString();
389 if (humanInstance->hasAttribute(
"intention"))
391 snapshot[
"intention"] =
392 humanInstance->getAttribute(
"intention")->getValue()->getString();
397 workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName(
"taskObject"));
400 snapshot[
"taskObject"] = objectInstance->getAttribute(
"name")->getValue()->getString();
402 bool inHand = objectInstance->getAttribute(
"inHand")->getValue()->getBool();
403 snapshot[
"objectInHand"] = inHand ?
"true" :
"false";
410 ProfilerStorage::getCommonStorageName()
412 return getProperty<std::string>(
"CommonStorageName").getValue();
416 ProfilerStorage::getWorkingMemoryName()
418 return getProperty<std::string>(
"WorkingMemoryName").getValue();
422 ProfilerStorage::getLongtermMemoryName()
424 return getProperty<std::string>(
"LongtermMemoryName").getValue();
428 ProfilerStorage::createPropertyDefinitions()
435 ProfilerStorage::reportStatechartTransitionWithParameters(
436 const armarx::ProfilerStatechartTransitionWithParameters& transition,
437 const Ice::Current& context)
442 ProfilerStorage::reportStatechartTransitionWithParametersList(
443 const armarx::ProfilerStatechartTransitionWithParametersList& transitions,
448 ProfilerStoragePropertyDefinitions::ProfilerStoragePropertyDefinitions(std::string prefix) :
449 ComponentPropertyDefinitions(prefix)
451 defineOptionalProperty<int>(
452 "SaveTimeout", 500,
"How often should the statistics be saved (ms)");
453 defineOptionalProperty<std::string>(
454 "CommonStorageName",
"CommonStorage",
"Name of the CommonStorage proxy to use");
455 defineOptionalProperty<std::string>(
456 "WorkingMemoryName",
"WorkingMemory",
"Name of the WorkingMemory proxy to use");
457 defineOptionalProperty<std::string>(
458 "LongtermMemoryName",
"LongtermMemory",
"Name of the LongtermMemory proxy to use");
459 defineOptionalProperty<bool>(
"enabled",
true,
"disable/enable saver");