IceLoggingStrategy.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 ArmarXCore::core::services::profiler
19  * @author Manfred Kroehnert ( manfred dot kroehnert at dot kit dot edu )
20  * @date 2015
21  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
22  * GNU General Public License
23  */
24 
25 #include "IceLoggingStrategy.h"
26 
27 #include <map> // for _Rb_tree_const_iterator, etc
28 #include <utility> // for pair
29 
30 #include <Ice/Config.h> // for Long
31 #include <Ice/Handle.h> // for Handle
32 #include <Ice/Object.h> // for Object
33 #include <IceUtil/Handle.h> // for HandleBase
34 
36 #include "ArmarXCore/interface/core/Profiler.h"
37 #include "ArmarXCore/interface/core/ThreadingIceBase.h" // for upCast
38 #include <ArmarXCore/core/application/Application.h> // for Application
39 
41  armarx::ProfilerListenerPrx profilerTopic) :
42  profilerListenerPrx(profilerTopic)
43 {
44 }
45 
47 {
48 }
49 
50 void
52  uint64_t timestamp,
53  const std::string& executableName,
54  const std::string& timestampUnit,
55  const std::string& eventName,
56  const std::string& parentName,
57  const std::string& functionName)
58 {
59  ProfilerEvent event = {processId,
60  Ice::Long(timestamp),
61  timestampUnit,
62  executableName,
63  eventName,
64  parentName,
65  functionName};
66  profilerListenerPrx->reportEvent(event);
67 }
68 
69 void
71  const ProfilerStatechartTransition& transition)
72 {
73  profilerListenerPrx->reportStatechartTransition(transition);
74 }
75 
76 void
78  pid_t processId,
79  uint64_t timestamp,
80  const std::string& stateIdentifier,
81  const armarx::StateParameterMap& inputParameterMap)
82 {
83  ProfilerStatechartParameters parameters = {
84  processId, Ice::Long(timestamp), stateIdentifier, inputParameterMap};
85  profilerListenerPrx->reportStatechartInputParameters(parameters);
86 }
87 
88 void
90  pid_t processId,
91  uint64_t timestamp,
92  const std::string& stateIdentifier,
93  const armarx::StateParameterMap& localParameterMap)
94 {
95  ProfilerStatechartParameters parameters = {
96  processId, Ice::Long(timestamp), stateIdentifier, localParameterMap};
97  profilerListenerPrx->reportStatechartLocalParameters(parameters);
98 }
99 
100 void
102  pid_t processId,
103  uint64_t timestamp,
104  const std::string& stateIdentifier,
105  const armarx::StateParameterMap& outputParameterMap)
106 {
107  ProfilerStatechartParameters parameters = {
108  processId, Ice::Long(timestamp), stateIdentifier, outputParameterMap};
109  profilerListenerPrx->reportStatechartOutputParameters(parameters);
110 }
111 
112 void
114  pid_t processId,
115  uint64_t timestamp,
116  const TransitionIceBase& transition)
117 {
118  ARMARX_TRACE;
119 
120  ProfilerStatechartTransitionWithParameters profilerTransition =
122  processId, timestamp, transition);
123  profilerListenerPrx->reportStatechartTransitionWithParameters(profilerTransition);
124 }
125 
126 void
128  uint64_t timestamp,
129  float cpuUsage)
130 {
131  ProfilerProcessCpuUsage process = {
132  processId, Ice::Long(timestamp), ::armarx::Application::getInstance()->getName(), cpuUsage};
133  profilerListenerPrx->reportProcessCpuUsage(process);
134 }
135 
136 void
138  u_int64_t timestamp,
139  int memoryUsage)
140 {
141  ProfilerProcessMemoryUsage processMemoryUsage = {
142  processId,
143  Ice::Long(timestamp),
145  memoryUsage};
146  profilerListenerPrx->reportProcessMemoryUsage(processMemoryUsage);
147 }
148 
149 #define ARMARX_ICE_LOGGING_BUFFER_SIZE 500
150 
152  armarx::ProfilerListenerPrx profilerTopic) :
153  profilerListenerPrx(profilerTopic)
154 {
163 
166  publisherTask->start();
167 }
168 
170 {
171  publisherTask->stop();
172 }
173 
177 {
178  armarx::StateParameterMap destination;
179 
180  for (armarx::StateParameterMap::const_iterator it = source.begin(); it != source.end(); it++)
181  {
182  destination[it->first] =
183  armarx::StateParameterIceBasePtr::dynamicCast(it->second->ice_clone());
184  }
185  return destination;
186 }
187 
188 armarx::ProfilerStatechartTransitionWithParameters
190  pid_t processId,
191  uint64_t timestamp,
192  const armarx::TransitionIceBase& transition)
193 {
194  StateIceBasePtr destinationState = StateIceBasePtr::dynamicCast(transition.destinationState);
195  ARMARX_CHECK_NOT_NULL(destinationState);
196 
197  StateParameterMap inputCopy = copyDictionary(destinationState->inputParameters);
198  StateParameterMap localCopy = copyDictionary(destinationState->localParameters);
199  StateParameterMap outputCopy;
200  if (transition.sourceState)
201  {
202  outputCopy =
203  copyDictionary(StateIceBasePtr::dynamicCast(transition.sourceState)->outputParameters);
204  }
205 
206  ProfilerStatechartTransitionWithParameters profilerTransition;
207  profilerTransition.processId = processId;
208  profilerTransition.timestamp = timestamp;
209 
210  profilerTransition.sourceStateIdentifier =
211  transition.sourceState
212  ? StateIceBasePtr::dynamicCast(transition.sourceState)->globalStateIdentifier
213  : "";
214  profilerTransition.targetStateIdentifier = destinationState->globalStateIdentifier;
215  profilerTransition.targetStateType = destinationState->stateType;
216  profilerTransition.eventName = transition.evt->eventName;
217 
218  profilerTransition.inputParameters = inputCopy;
219  profilerTransition.localParameters = localCopy;
220  profilerTransition.outputParameters = outputCopy;
221 
222  return profilerTransition;
223 }
224 
225 void
227  uint64_t timestamp,
228  const std::string& executableName,
229  const std::string& timestampUnit,
230  const std::string& eventName,
231  const std::string& parentName,
232  const std::string& functionName)
233 {
234  ProfilerEvent event = {processId,
235  Ice::Long(timestamp),
236  executableName,
237  timestampUnit,
238  eventName,
239  parentName,
240  functionName};
241  {
242  std::unique_lock lock(profilerEventsMutex);
243  profilerEvents.push_back(event);
244  }
245 }
246 
247 void
249  const ProfilerStatechartTransition& transition)
250 {
251  std::unique_lock lock(profilerStatechartTransitionsMutex);
252  profilerStatechartTransitions.push_back(transition);
253 }
254 
255 void
257  pid_t processId,
258  uint64_t timestamp,
259  const std::string& stateIdentifier,
260  const armarx::StateParameterMap& inputParameterMap)
261 {
262  armarx::StateParameterMap copy = copyDictionary(inputParameterMap);
263 
264  ProfilerStatechartParameters parameters = {
265  processId, Ice::Long(timestamp), stateIdentifier, copy};
266  {
267  std::unique_lock lock(profilerStatechartInputParametersMutex);
268  profilerStatechartInputParameters.push_back(parameters);
269  }
270 }
271 
272 void
274  pid_t processId,
275  uint64_t timestamp,
276  const std::string& stateIdentifier,
277  const armarx::StateParameterMap& localParameterMap)
278 {
279  armarx::StateParameterMap copy = copyDictionary(localParameterMap);
280  ProfilerStatechartParameters parameters = {
281  processId, Ice::Long(timestamp), stateIdentifier, copy};
282  {
283  std::unique_lock lock(profilerStatechartLocalParametersMutex);
284  profilerStatechartLocalParameters.push_back(parameters);
285  }
286 }
287 
288 void
290  pid_t processId,
291  uint64_t timestamp,
292  const std::string& stateIdentifier,
293  const armarx::StateParameterMap& outputParameterMap)
294 {
295  armarx::StateParameterMap copy = copyDictionary(outputParameterMap);
296  ProfilerStatechartParameters parameters = {
297  processId, Ice::Long(timestamp), stateIdentifier, copy};
298  {
299  std::unique_lock lock(profilerStatechartOutputParametersMutex);
300  profilerStatechartOutputParameters.push_back(parameters);
301  }
302 }
303 
304 void
306  pid_t processId,
307  uint64_t timestamp,
308  const TransitionIceBase& transition)
309 {
310  ARMARX_TRACE;
311 
312  ProfilerStatechartTransitionWithParameters profilerTransition =
313  toProfilerTransition(processId, timestamp, transition);
314  {
315  std::unique_lock lock(profilerStatechartTransitionsWithParametersMutex);
316  profilerStatechartTransitionsWithParameters.push_back(profilerTransition);
317  }
318 }
319 
320 void
322  uint64_t timestamp,
323  float cpuUsage)
324 {
325  ProfilerProcessCpuUsage process = {
326  processId, Ice::Long(timestamp), ::armarx::Application::getInstance()->getName(), cpuUsage};
327  {
328  std::unique_lock lock(profilerCpuUsagesMutex);
329  profilerProcessCpuUsages.push_back(process);
330  }
331 }
332 
333 void
335  uint64_t timestamp,
336  int memoryUsage)
337 {
338  ProfilerProcessMemoryUsage processMemoryUsage = {
339  processId,
340  Ice::Long(timestamp),
342  memoryUsage};
343  {
344  std::unique_lock lock(profilerProcessMemoryUsagesMutex);
345  profilerProcessMemoryUsages.push_back(processMemoryUsage);
346  }
347 }
348 
349 void
351 {
352  if (!profilerEvents.empty())
353  {
354  ProfilerEventList eventsCopy;
355  {
356  std::unique_lock lock(profilerEventsMutex);
357  profilerEvents.swap(eventsCopy);
358  }
359  profilerListenerPrx->reportEventList(eventsCopy);
360  }
361 
362  if (!profilerStatechartTransitions.empty())
363  {
364  ProfilerStatechartTransitionList transitionsCopy;
365  {
366  std::unique_lock lock(profilerStatechartTransitionsMutex);
367  profilerStatechartTransitions.swap(transitionsCopy);
368  }
369  profilerListenerPrx->reportStatechartTransitionList(transitionsCopy);
370  }
371 
372  if (!profilerStatechartInputParameters.empty())
373  {
374  ProfilerStatechartParametersList parametersCopy;
375  {
376  std::unique_lock lock(profilerStatechartInputParametersMutex);
377  profilerStatechartInputParameters.swap(parametersCopy);
378  }
379  profilerListenerPrx->reportStatechartInputParametersList(parametersCopy);
380  }
381 
382  if (!profilerStatechartLocalParameters.empty())
383  {
384  ProfilerStatechartParametersList parametersCopy;
385  {
386  std::unique_lock lock(profilerStatechartLocalParametersMutex);
387  profilerStatechartLocalParameters.swap(parametersCopy);
388  }
389  profilerListenerPrx->reportStatechartLocalParametersList(parametersCopy);
390  }
391 
392  if (!profilerStatechartOutputParameters.empty())
393  {
394  ProfilerStatechartParametersList parametersCopy;
395  {
396  std::unique_lock lock(profilerStatechartOutputParametersMutex);
397  profilerStatechartOutputParameters.swap(parametersCopy);
398  }
399  profilerListenerPrx->reportStatechartOutputParametersList(parametersCopy);
400  }
401 
402  if (!profilerStatechartTransitionsWithParameters.empty())
403  {
404  ProfilerStatechartTransitionWithParametersList transitionsWithParametersCopy;
405  {
406  std::unique_lock lock(profilerStatechartTransitionsWithParametersMutex);
407  profilerStatechartTransitionsWithParameters.swap(transitionsWithParametersCopy);
408  }
409  profilerListenerPrx->reportStatechartTransitionWithParametersList(
410  transitionsWithParametersCopy);
411  }
412 
413  if (!profilerProcessCpuUsages.empty())
414  {
415  ProfilerProcessCpuUsageList cpuUsagesCopy;
416  {
417  std::unique_lock lock(profilerCpuUsagesMutex);
418  profilerProcessCpuUsages.swap(cpuUsagesCopy);
419  }
420  profilerListenerPrx->reportProcessCpuUsageList(cpuUsagesCopy);
421  }
422 
423  if (!profilerProcessMemoryUsages.empty())
424  {
425  ProfilerProcessMemoryUsageList memoryUsageCopy;
426  {
427  std::unique_lock lock(profilerProcessMemoryUsagesMutex);
428  profilerProcessMemoryUsages.swap(memoryUsageCopy);
429  }
430 
431  profilerListenerPrx->reportProcessMemoryUsageList(memoryUsageCopy);
432  }
433 }
armarx::Profiler::IceLoggingStrategy::logEvent
void logEvent(pid_t processId, uint64_t timestamp, const std::string &executableName, const std::string &timestampUnit, const std::string &eventName, const std::string &parentName, const std::string &functionName) override
Definition: IceLoggingStrategy.cpp:51
armarx::Profiler::IceLoggingStrategy::logProcessCpuUsage
void logProcessCpuUsage(pid_t processId, uint64_t timestamp, float cpuUsage) override
Definition: IceLoggingStrategy.cpp:127
armarx::Profiler::IceBufferedLoggingStrategy::profilerProcessMemoryUsages
ProfilerProcessMemoryUsageList profilerProcessMemoryUsages
Definition: IceLoggingStrategy.h:176
armarx::Profiler::IceLoggingStrategy::~IceLoggingStrategy
~IceLoggingStrategy() override
Definition: IceLoggingStrategy.cpp:46
armarx::Profiler::IceLoggingStrategy::logStatechartInputParameters
void logStatechartInputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap) override
Definition: IceLoggingStrategy.cpp:77
ARMARX_CHECK_NOT_NULL
#define ARMARX_CHECK_NOT_NULL(ptr)
This macro evaluates whether ptr is not null and if it turns out to be false it will throw an Express...
Definition: ExpressionException.h:206
armarx::Profiler::IceBufferedLoggingStrategy::IceBufferedLoggingStrategy
IceBufferedLoggingStrategy(ProfilerListenerPrx profilerTopic)
Definition: IceLoggingStrategy.cpp:151
PeriodicTask.h
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartInputParameters
ProfilerStatechartParametersList profilerStatechartInputParameters
Definition: IceLoggingStrategy.h:161
armarx::Profiler::IceBufferedLoggingStrategy::publisherTask
PeriodicTask< IceBufferedLoggingStrategy >::pointer_type publisherTask
Definition: IceLoggingStrategy.h:180
armarx::Profiler::IceBufferedLoggingStrategy::profilerProcessCpuUsages
ProfilerProcessCpuUsageList profilerProcessCpuUsages
Definition: IceLoggingStrategy.h:173
armarx::Profiler::IceLoggingStrategy::IceLoggingStrategy
IceLoggingStrategy(ProfilerListenerPrx profilerTopic)
Definition: IceLoggingStrategy.cpp:40
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartLocalParameters
void logStatechartLocalParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap) override
Definition: IceLoggingStrategy.cpp:273
IceLoggingStrategy.h
armarx::Profiler::IceBufferedLoggingStrategy::copyDictionary
static armarx::StateParameterMap copyDictionary(const armarx::StateParameterMap &source)
Definition: IceLoggingStrategy.cpp:175
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartLocalParameters
ProfilerStatechartParametersList profilerStatechartLocalParameters
Definition: IceLoggingStrategy.h:164
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:77
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartOutputParameters
ProfilerStatechartParametersList profilerStatechartOutputParameters
Definition: IceLoggingStrategy.h:167
copy
Use of this software is granted under one of the following two to be chosen freely by the user Boost Software License Version Marcin Kalicinski Permission is hereby free of to any person or organization obtaining a copy of the software and accompanying documentation covered by this and transmit the and to prepare derivative works of the and to permit third parties to whom the Software is furnished to do all subject to the including the above license this restriction and the following must be included in all copies of the in whole or in and all derivative works of the unless such copies or derivative works are solely in the form of machine executable object code generated by a source language processor THE SOFTWARE IS PROVIDED AS WITHOUT WARRANTY OF ANY EXPRESS OR INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF FITNESS FOR A PARTICULAR TITLE AND NON INFRINGEMENT IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE FOR ANY DAMAGES OR OTHER WHETHER IN TORT OR ARISING OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE The MIT Marcin Kalicinski Permission is hereby free of to any person obtaining a copy of this software and associated documentation to deal in the Software without including without limitation the rights to copy
Definition: license.txt:39
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartTransition
void logStatechartTransition(const ProfilerStatechartTransition &transition) override
Definition: IceLoggingStrategy.cpp:248
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::Profiler::IceBufferedLoggingStrategy::toProfilerTransition
static ProfilerStatechartTransitionWithParameters toProfilerTransition(pid_t processId, uint64_t timestamp, const TransitionIceBase &transition)
Definition: IceLoggingStrategy.cpp:189
armarx::StateUtilFunctions::copyDictionary
void copyDictionary(const StringVariantContainerBaseMap &source, StringVariantContainerBaseMap &destination)
Clears the destination map and copies the parameters of the source in it.
Definition: StateUtilFunctions.cpp:197
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(pid_t processId, uint64_t timestamp, const TransitionIceBase &transition) override
Definition: IceLoggingStrategy.cpp:305
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
armarx::Profiler::IceLoggingStrategy::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(pid_t processId, uint64_t timestamp, const TransitionIceBase &transition) override
Definition: IceLoggingStrategy.cpp:113
armarx::Profiler::IceLoggingStrategy::logStatechartOutputParameters
void logStatechartOutputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap) override
Definition: IceLoggingStrategy.cpp:101
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartTransitions
ProfilerStatechartTransitionList profilerStatechartTransitions
Definition: IceLoggingStrategy.h:158
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartInputParameters
void logStatechartInputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap) override
Definition: IceLoggingStrategy.cpp:256
armarx::Application::getInstance
static ApplicationPtr getInstance()
Retrieve shared pointer to the application object.
Definition: Application.cpp:315
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:661
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartOutputParameters
void logStatechartOutputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap) override
Definition: IceLoggingStrategy.cpp:289
ARMARX_ICE_LOGGING_BUFFER_SIZE
#define ARMARX_ICE_LOGGING_BUFFER_SIZE
Definition: IceLoggingStrategy.cpp:149
armarx::Profiler::IceBufferedLoggingStrategy::logProcessMemoryUsage
void logProcessMemoryUsage(pid_t processId, u_int64_t timestamp, int memoryUsage) override
Definition: IceLoggingStrategy.cpp:334
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartTransitionsWithParameters
ProfilerStatechartTransitionWithParametersList profilerStatechartTransitionsWithParameters
Definition: IceLoggingStrategy.h:170
armarx::Profiler::IceBufferedLoggingStrategy::logEvent
void logEvent(pid_t processId, uint64_t timestamp, const std::string &executableName, const std::string &timestampUnit, const std::string &eventName, const std::string &parentName, const std::string &functionName) override
Definition: IceLoggingStrategy.cpp:226
armarx::Profiler::IceBufferedLoggingStrategy::publishData
void publishData()
Definition: IceLoggingStrategy.cpp:350
armarx::Profiler::IceBufferedLoggingStrategy::profilerEvents
ProfilerEventList profilerEvents
Definition: IceLoggingStrategy.h:155
armarx::PeriodicTask
Definition: ArmarXManager.h:70
armarx::Profiler::IceBufferedLoggingStrategy::~IceBufferedLoggingStrategy
~IceBufferedLoggingStrategy() override
Definition: IceLoggingStrategy.cpp:169
armarx::Profiler::IceLoggingStrategy::logProcessMemoryUsage
void logProcessMemoryUsage(pid_t processId, u_int64_t timestamp, int memoryUsage) override
Definition: IceLoggingStrategy.cpp:137
armarx::Profiler::IceLoggingStrategy::logStatechartLocalParameters
void logStatechartLocalParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap) override
Definition: IceLoggingStrategy.cpp:89
armarx::Profiler::IceBufferedLoggingStrategy::logProcessCpuUsage
void logProcessCpuUsage(pid_t processId, uint64_t timestamp, float cpuUsage) override
Definition: IceLoggingStrategy.cpp:321
Application.h
armarx::Profiler::IceLoggingStrategy::logStatechartTransition
void logStatechartTransition(const ProfilerStatechartTransition &transition) override
Definition: IceLoggingStrategy.cpp:70