29#include <IceUtil/Time.h>
40#include <mongo/client/dbclient.h>
47 return "ProfilerStorage";
53 return "ProfilerStorage";
69 usingTopic(armarx::Profiler::PROFILER_TOPIC_NAME);
73 saveToLongtermMemoryTask =
76 this, &ProfilerStorage::saveTransitionsToLongtermMemory);
88 profilerDataSegmentPrx = longtermMemoryPrx->getProfilerDataSegment();
90 saveToLongtermMemoryTask->start();
91 saveTransitionsToLongtermMemoryTask->start();
103 saveToLongtermMemoryTask->stop();
104 saveTransitionsToLongtermMemoryTask->stop();
108 ProfilerStorage::saveToLongtermMemory()
110 IceUtil::Time t = IceUtil::Time::now();
114 size_t logSize = entityLog.size();
121 if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
126 EntityBaseList entityLogCopy;
128 std::scoped_lock lock(entityLogMutex);
129 entityLog.swap(entityLogCopy);
131 t = IceUtil::Time::now();
132 profilerDataSegmentPrx->addEntityList(entityLogCopy);
134 <<
" [ms] to add " << entityLogCopy.size() <<
" Entities";
135 t = IceUtil::Time::now();
140 ProfilerStorage::saveTransitionsToLongtermMemory()
142 IceUtil::Time t = IceUtil::Time::now();
146 size_t logSize = transitionSnapshotList.size();
153 if ((logSize < 1000) && ((IceUtil::Time::now() - t).toMilliSeconds() < saveTimeout))
158 TransitionSnapshotList transitionSnapshotListCopy;
160 std::scoped_lock lock(transitionSnapshotListMutex);
161 transitionSnapshotList.swap(transitionSnapshotListCopy);
163 t = IceUtil::Time::now();
165 for (TransitionSnapshot& snapshot : transitionSnapshotListCopy)
167 std::string sourceSnapshot = profilerDataSegmentPrx->saveUniqueMemorySnapshot(
168 snapshot.sourceStateMemorySnapshot);
169 std::string targetSnapshot = profilerDataSegmentPrx->saveUniqueMemorySnapshot(
170 snapshot.targetStateMemorySnapshot);
171 EntityRefBasePtr sourceSnapshotRef =
172 profilerDataSegmentPrx->getEntityRefById(sourceSnapshot);
173 EntityRefBasePtr targetSnapshotRef =
174 profilerDataSegmentPrx->getEntityRefById(targetSnapshot);
176 new memoryx::ProfilerTransition(snapshot.transition.parentStateIdentifier,
177 snapshot.transition.sourceStateIdentifier,
178 snapshot.transition.targetStateIdentifier,
182 std::string entityId =
183 profilerDataSegmentPrx->saveOrUpdateTransition(profilerTransition);
186 <<
" [ms] to add " << transitionSnapshotListCopy.size()
188 t = IceUtil::Time::now();
205 std::scoped_lock lock(entityLogMutex);
211 const armarx::ProfilerStatechartTransition& transition,
212 const ::Ice::Current& context)
214 std::scoped_lock lock(transitionSnapshotListMutex);
215 TransitionSnapshot transitionSnapshot;
216 transitionSnapshot.transition = transition;
217 transitionSnapshot.sourceStateMemorySnapshot = memorySnapshot;
220 transitionSnapshot.targetStateMemorySnapshot = memorySnapshot;
221 transitionSnapshotList.push_back(transitionSnapshot);
226 const armarx::ProfilerStatechartParameters& inputParameters,
233 const armarx::ProfilerStatechartParameters& localParameters,
240 const armarx::ProfilerStatechartParameters& outputParameters,
247 const Ice::Current& context)
249 std::scoped_lock lock(entityLogMutex);
255 const Ice::Current& context)
257 std::scoped_lock lock(entityLogMutex);
264 memoryx::EntityBaseList eventEntities(events.size());
265 std::transform(events.begin(),
267 eventEntities.begin(),
268 [](
const armarx::ProfilerEvent& event)
269 { return new memoryx::ProfilerEvent(event); });
272 std::scoped_lock lock(entityLogMutex);
273 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
279 const armarx::ProfilerStatechartTransitionList& transitions,
282 if (transitions.empty())
287 TransitionSnapshotList transitionSnapshots(transitions.size());
291 std::transform(transitions.begin(),
293 transitionSnapshots.begin(),
294 [](
const armarx::ProfilerStatechartTransition& transition)
296 TransitionSnapshot transitionSnapshot = {transition,
297 new ProfilerMemorySnapshot(),
298 new ProfilerMemorySnapshot()};
299 return transitionSnapshot;
302 transitionSnapshots.front().sourceStateMemorySnapshot = sourceMemorySnapshot;
304 transitionSnapshots.back().transition.targetStateIdentifier, workingMemoryPrx);
307 transitionSnapshots.back().targetStateMemorySnapshot = memorySnapshot;
310 std::scoped_lock lock(transitionSnapshotListMutex);
311 transitionSnapshotList.insert(transitionSnapshotList.end(),
312 transitionSnapshots.begin(),
313 transitionSnapshots.end());
319 const armarx::ProfilerStatechartParametersList& inputParametersList,
326 const armarx::ProfilerStatechartParametersList& localParametesList,
333 const armarx::ProfilerStatechartParametersList& outputParametersList,
342 memoryx::EntityBaseList eventEntities(processes.size());
343 std::transform(processes.begin(),
345 eventEntities.begin(),
346 [](
const armarx::ProfilerProcessCpuUsage& process)
347 { return new memoryx::ProfilerProcess(process); });
350 std::scoped_lock lock(entityLogMutex);
351 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
357 const armarx::ProfilerProcessMemoryUsageList& memoryUsages,
360 memoryx::EntityBaseList eventEntities(memoryUsages.size());
361 std::transform(memoryUsages.begin(),
363 eventEntities.begin(),
364 [](
const armarx::ProfilerProcessMemoryUsage& memoryUsage)
365 { return new memoryx::ProfilerMemoryUsage(memoryUsage); });
368 std::scoped_lock lock(entityLogMutex);
369 entityLog.insert(entityLog.end(), eventEntities.begin(), eventEntities.end());
375 const std::string& stateName,
376 const WorkingMemoryInterfacePrx& workingMemoryProxy)
378 if (!workingMemoryProxy)
383 Ice::Context snapshot;
386 workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName(
"human"));
389 snapshot[
"human"] = humanInstance->getAttribute(
"name")->getValue()->getString();
390 if (humanInstance->hasAttribute(
"humanObject"))
392 snapshot[
"humanObject"] =
393 humanInstance->getAttribute(
"humanObject")->getValue()->getString();
396 if (humanInstance->hasAttribute(
"intention"))
398 snapshot[
"intention"] =
399 humanInstance->getAttribute(
"intention")->getValue()->getString();
404 workingMemoryProxy->getObjectInstancesSegment()->getObjectInstanceByName(
"taskObject"));
407 snapshot[
"taskObject"] = objectInstance->getAttribute(
"name")->getValue()->getString();
409 bool inHand = objectInstance->getAttribute(
"inHand")->getValue()->getBool();
410 snapshot[
"objectInHand"] = inHand ?
"true" :
"false";
443 const armarx::ProfilerStatechartTransitionWithParameters& transition,
444 const Ice::Current& context)
450 const armarx::ProfilerStatechartTransitionWithParametersList& transitions,
459 "SaveTimeout", 500,
"How often should the statistics be saved (ms)");
461 "CommonStorageName",
"CommonStorage",
"Name of the CommonStorage proxy to use");
463 "WorkingMemoryName",
"WorkingMemory",
"Name of the WorkingMemory proxy to use");
465 "LongtermMemoryName",
"LongtermMemory",
"Name of the LongtermMemory proxy to use");
#define ARMARX_REGISTER_COMPONENT_EXECUTABLE(ComponentT, applicationName)
ComponentPropertyDefinitions(std::string prefix, bool hasObjectNameParameter=true)
std::string getConfigIdentifier()
Retrieve config identifier for this component as set in constructor.
Property< PropertyType > getProperty(const std::string &name)
void setTag(const LogTag &tag)
bool usingProxy(const std::string &name, const std::string &endpoints="")
Registers a proxy for retrieval after initialization and adds it to the dependency list.
void usingTopic(const std::string &name, bool orderedPublishing=false)
Registers a proxy for subscription after initialization.
Ice::ObjectPrx getProxy(long timeoutMs=0, bool waitForScheduler=true) const
Returns the proxy of this object (optionally it waits for the proxy)
std::string prefix
Prefix of the properties such as namespace, domain, component name, etc.
PropertyDefinition< PropertyType > & defineOptionalProperty(const std::string &name, PropertyType defaultValue, const std::string &description="", PropertyDefinitionBase::PropertyConstness constness=PropertyDefinitionBase::eConstant)
ProfilerStoragePropertyDefinitions(std::string prefix)
void reportProcessCpuUsage(const armarx::ProfilerProcessCpuUsage &process, const Ice::Current &context=Ice::emptyCurrent) override
void reportStatechartOutputParametersList(const armarx::ProfilerStatechartParametersList &outputParametersList, const Ice::Current &=Ice::emptyCurrent) override
void onInitComponent() override
std::string getCommonStorageName()
void reportStatechartTransitionWithParameters(const armarx::ProfilerStatechartTransitionWithParameters &transition, const ::Ice::Current &context=Ice::emptyCurrent) override
void reportProcessMemoryUsage(const armarx::ProfilerProcessMemoryUsage &memoryUsage, const Ice::Current &context=Ice::emptyCurrent) override
void reportStatechartTransitionWithParametersList(const armarx::ProfilerStatechartTransitionWithParametersList &transitions, const Ice::Current &=Ice::emptyCurrent) override
void onDisconnectComponent() override
void reportStatechartInputParametersList(const armarx::ProfilerStatechartParametersList &inputParametersList, const Ice::Current &=Ice::emptyCurrent) override
armarx::PropertyDefinitionsPtr createPropertyDefinitions() override
void reportStatechartOutputParameters(const armarx::ProfilerStatechartParameters &outputParameters, const Ice::Current &=Ice::emptyCurrent) override
void reportStatechartTransitionList(const armarx::ProfilerStatechartTransitionList &transitions, const Ice::Current &=Ice::emptyCurrent) override
std::string getLongtermMemoryName()
void reportStatechartLocalParametersList(const armarx::ProfilerStatechartParametersList &localParametesList, const Ice::Current &=Ice::emptyCurrent) override
void reportStatechartTransition(const armarx::ProfilerStatechartTransition &transition, const ::Ice::Current &context=Ice::emptyCurrent) override
void reportNetworkTraffic(const std::string &, const std::string &, Ice::Int, Ice::Int, const Ice::Current &context=Ice::emptyCurrent) override
reportNetworkTraffic
void reportEventList(const armarx::ProfilerEventList &events, const Ice::Current &=Ice::emptyCurrent) override
void onConnectComponent() override
static std::string GetDefaultName()
static ProfilerMemorySnapshotPtr CreateProfilerMemorySnapshot(const std::string &stateName, const WorkingMemoryInterfacePrx &workingMemoryProxy)
void reportProcessMemoryUsageList(const armarx::ProfilerProcessMemoryUsageList &memoryUsages, const Ice::Current &=Ice::emptyCurrent) override
void reportEvent(const armarx::ProfilerEvent &profilerEvent, const Ice::Current &context=Ice::emptyCurrent) override
void onExitComponent() override
void reportStatechartInputParameters(const armarx::ProfilerStatechartParameters &inputParameters, const Ice::Current &=Ice::emptyCurrent) override
void reportStatechartLocalParameters(const armarx::ProfilerStatechartParameters &localParameters, const Ice::Current &=Ice::emptyCurrent) override
std::string getWorkingMemoryName()
void reportProcessCpuUsageList(const armarx::ProfilerProcessCpuUsageList &processes, const Ice::Current &=Ice::emptyCurrent) override
std::string getDefaultName() const override
#define ARMARX_WARNING_S
The logging level for unexpected behaviour, but not a serious problem.
IceUtil::Handle< class PropertyDefinitionContainer > PropertyDefinitionsPtr
PropertyDefinitions smart pointer type.
IceInternal::Handle< ProfilerMemorySnapshot > ProfilerMemorySnapshotPtr
IceInternal::Handle< ProfilerTransition > ProfilerTransitionPtr
IceInternal::Handle< ObjectInstance > ObjectInstancePtr