37 #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();
79 profilerDataSegmentPrx = longtermMemoryPrx->getProfilerDataSegment();
81 saveToLongtermMemoryTask->start();
82 saveTransitionsToLongtermMemoryTask->start();
94 saveToLongtermMemoryTask->stop();
95 saveTransitionsToLongtermMemoryTask->stop();
99 void ProfilerStorage::saveToLongtermMemory()
105 size_t logSize = entityLog.size();
112 if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
117 EntityBaseList entityLogCopy;
119 std::scoped_lock lock(entityLogMutex);
120 entityLog.swap(entityLogCopy);
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();
130 void ProfilerStorage::saveTransitionsToLongtermMemory()
136 size_t logSize = transitionSnapshotList.size();
143 if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
148 TransitionSnapshotList transitionSnapshotListCopy;
150 std::scoped_lock lock(transitionSnapshotListMutex);
151 transitionSnapshotList.swap(transitionSnapshotListCopy);
153 t = IceUtil::Time::now();
155 for (TransitionSnapshot& snapshot : transitionSnapshotListCopy)
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);
164 ARMARX_WARNING_S <<
"Took " << (IceUtil::Time::now() - t).toMilliSeconds() <<
" [ms] to add " << transitionSnapshotListCopy.size() <<
" Transitions";
165 t = IceUtil::Time::now();
178 std::scoped_lock lock(entityLogMutex);
185 std::scoped_lock lock(transitionSnapshotListMutex);
186 TransitionSnapshot transitionSnapshot;
187 transitionSnapshot.transition = transition;
188 transitionSnapshot.sourceStateMemorySnapshot = memorySnapshot;
190 transitionSnapshot.targetStateMemorySnapshot = memorySnapshot;
191 transitionSnapshotList.push_back(transitionSnapshot);
212 std::scoped_lock lock(entityLogMutex);
218 std::scoped_lock lock(entityLogMutex);
227 memoryx::EntityBaseList eventEntities(events.size());
228 std::transform(events.begin(), events.end(), eventEntities.begin(),
229 [](
const armarx::ProfilerEvent & event)
231 return new memoryx::ProfilerEvent(event);
235 std::scoped_lock lock(entityLogMutex);
236 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
243 if (transitions.empty())
248 TransitionSnapshotList transitionSnapshots(transitions.size());
252 std::transform(transitions.begin(), transitions.end(), transitionSnapshots.begin(),
253 [](
const armarx::ProfilerStatechartTransition & transition)
255 TransitionSnapshot transitionSnapshot = {transition, new ProfilerMemorySnapshot(), new ProfilerMemorySnapshot()};
256 return transitionSnapshot;
259 transitionSnapshots.front().sourceStateMemorySnapshot = sourceMemorySnapshot;
263 transitionSnapshots.back().targetStateMemorySnapshot = memorySnapshot;
266 std::scoped_lock lock(transitionSnapshotListMutex);
267 transitionSnapshotList.insert(transitionSnapshotList.end(), transitionSnapshots.begin(), transitionSnapshots.end());
272 void ProfilerStorage::reportStatechartInputParametersList(
const armarx::ProfilerStatechartParametersList& inputParametersList,
const Ice::Current&)
277 void ProfilerStorage::reportStatechartLocalParametersList(
const armarx::ProfilerStatechartParametersList& localParametesList,
const Ice::Current&)
282 void ProfilerStorage::reportStatechartOutputParametersList(
const armarx::ProfilerStatechartParametersList& outputParametersList,
const Ice::Current&)
287 void ProfilerStorage::reportProcessCpuUsageList(
const armarx::ProfilerProcessCpuUsageList& processes,
const Ice::Current&)
289 memoryx::EntityBaseList eventEntities(processes.size());
290 std::transform(processes.begin(), processes.end(), eventEntities.begin(),
291 [](
const armarx::ProfilerProcessCpuUsage & process)
293 return new memoryx::ProfilerProcess(process);
297 std::scoped_lock lock(entityLogMutex);
298 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
302 void ProfilerStorage::reportProcessMemoryUsageList(
const armarx::ProfilerProcessMemoryUsageList& memoryUsages,
const Ice::Current&)
304 memoryx::EntityBaseList eventEntities(memoryUsages.size());
305 std::transform(memoryUsages.begin(), memoryUsages.end(), eventEntities.begin(),
306 [](
const armarx::ProfilerProcessMemoryUsage & memoryUsage)
308 return new memoryx::ProfilerMemoryUsage(memoryUsage);
312 std::scoped_lock lock(entityLogMutex);
313 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
318 ProfilerMemorySnapshotPtr ProfilerStorage::CreateProfilerMemorySnapshot(
const std::string& stateName,
const WorkingMemoryInterfacePrx& workingMemoryProxy)
320 if (!workingMemoryProxy)
325 Ice::Context snapshot;
327 memoryx::ObjectInstancePtr humanInstance = memoryx::ObjectInstancePtr::dynamicCast(workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName(
"human"));
330 snapshot[
"human"] = humanInstance->getAttribute(
"name")->getValue()->getString();
331 if (humanInstance->hasAttribute(
"humanObject"))
333 snapshot[
"humanObject"] = humanInstance->getAttribute(
"humanObject")->getValue()->getString();
336 if (humanInstance->hasAttribute(
"intention"))
338 snapshot[
"intention"] = humanInstance->getAttribute(
"intention")->getValue()->getString();
342 memoryx::ObjectInstancePtr objectInstance = memoryx::ObjectInstancePtr::dynamicCast(workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName(
"taskObject"));
345 snapshot[
"taskObject"] = objectInstance->getAttribute(
"name")->getValue()->getString();
347 bool inHand = objectInstance->getAttribute(
"inHand")->getValue()->getBool();
348 snapshot[
"objectInHand"] = inHand ?
"true" :
"false";
355 std::string ProfilerStorage::getCommonStorageName()
357 return getProperty<std::string>(
"CommonStorageName").getValue();
361 std::string ProfilerStorage::getWorkingMemoryName()
363 return getProperty<std::string>(
"WorkingMemoryName").getValue();
366 std::string ProfilerStorage::getLongtermMemoryName()
368 return getProperty<std::string>(
"LongtermMemoryName").getValue();
376 getConfigIdentifier()));
379 void ProfilerStorage::reportStatechartTransitionWithParameters(
const armarx::ProfilerStatechartTransitionWithParameters& transition,
const Ice::Current& context)
384 void ProfilerStorage::reportStatechartTransitionWithParametersList(
const armarx::ProfilerStatechartTransitionWithParametersList& transitions,
const Ice::Current&)
390 ProfilerStoragePropertyDefinitions::ProfilerStoragePropertyDefinitions(std::string prefix) :
391 ComponentPropertyDefinitions(prefix)
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");