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 <ArmarXCore/core/application/Application.h> // for Application
26 #include <Ice/Config.h> // for Long
27 #include <Ice/Handle.h> // for Handle
28 #include <Ice/Object.h> // for Object
29 #include <IceUtil/Handle.h> // for HandleBase
30 #include <map> // for _Rb_tree_const_iterator, etc
31 #include <utility> // for pair
32 
34 #include "ArmarXCore/interface/core/Profiler.h"
35 #include "ArmarXCore/interface/core/ThreadingIceBase.h" // for upCast
36 #include "IceLoggingStrategy.h"
37 
38 
39 armarx::Profiler::IceLoggingStrategy::IceLoggingStrategy(armarx::ProfilerListenerPrx profilerTopic) :
40  profilerListenerPrx(profilerTopic)
41 {
42 }
43 
45 {
46 }
47 
48 void armarx::Profiler::IceLoggingStrategy::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)
49 {
50  ProfilerEvent event = {processId, Ice::Long(timestamp), timestampUnit, executableName, eventName, parentName, functionName};
51  profilerListenerPrx->reportEvent(event);
52 }
53 
54 void armarx::Profiler::IceLoggingStrategy::logStatechartTransition(const ProfilerStatechartTransition& transition)
55 {
56  profilerListenerPrx->reportStatechartTransition(transition);
57 }
58 
59 void armarx::Profiler::IceLoggingStrategy::logStatechartInputParameters(pid_t processId, uint64_t timestamp, const std::string& stateIdentifier, const armarx::StateParameterMap& inputParameterMap)
60 {
61  ProfilerStatechartParameters parameters = {processId, Ice::Long(timestamp), stateIdentifier, inputParameterMap};
62  profilerListenerPrx->reportStatechartInputParameters(parameters);
63 }
64 
65 void armarx::Profiler::IceLoggingStrategy::logStatechartLocalParameters(pid_t processId, uint64_t timestamp, const std::string& stateIdentifier, const armarx::StateParameterMap& localParameterMap)
66 {
67  ProfilerStatechartParameters parameters = {processId, Ice::Long(timestamp), stateIdentifier, localParameterMap};
68  profilerListenerPrx->reportStatechartLocalParameters(parameters);
69 }
70 
71 void armarx::Profiler::IceLoggingStrategy::logStatechartOutputParameters(pid_t processId, uint64_t timestamp, const std::string& stateIdentifier, const armarx::StateParameterMap& outputParameterMap)
72 {
73  ProfilerStatechartParameters parameters = {processId, Ice::Long(timestamp), stateIdentifier, outputParameterMap};
74  profilerListenerPrx->reportStatechartOutputParameters(parameters);
75 }
76 
77 void armarx::Profiler::IceLoggingStrategy::logStatechartTransitionWithParameters(pid_t processId, uint64_t timestamp, const TransitionIceBase& transition)
78 {
80 
81  ProfilerStatechartTransitionWithParameters profilerTransition = armarx::Profiler::IceBufferedLoggingStrategy::toProfilerTransition(processId, timestamp, transition);
82  profilerListenerPrx->reportStatechartTransitionWithParameters(profilerTransition);
83 }
84 
85 void armarx::Profiler::IceLoggingStrategy::logProcessCpuUsage(pid_t processId, uint64_t timestamp, float cpuUsage)
86 {
87  ProfilerProcessCpuUsage process = {processId, Ice::Long(timestamp), ::armarx::Application::getInstance()->getName(), cpuUsage};
88  profilerListenerPrx->reportProcessCpuUsage(process);
89 }
90 
91 void armarx::Profiler::IceLoggingStrategy::logProcessMemoryUsage(pid_t processId, u_int64_t timestamp, int memoryUsage)
92 {
93  ProfilerProcessMemoryUsage processMemoryUsage = {processId, Ice::Long(timestamp), ::armarx::Application::getInstance()->getName(), memoryUsage};
94  profilerListenerPrx->reportProcessMemoryUsage(processMemoryUsage);
95 }
96 
97 
98 
99 #define ARMARX_ICE_LOGGING_BUFFER_SIZE 500
100 
102  profilerListenerPrx(profilerTopic)
103 {
112 
114  publisherTask->start();
115 }
116 
118 {
119  publisherTask->stop();
120 }
121 
123 {
124  armarx::StateParameterMap destination;
125 
126  for (armarx::StateParameterMap::const_iterator it = source.begin(); it != source.end(); it++)
127  {
128  destination[it->first] = armarx::StateParameterIceBasePtr::dynamicCast(it->second->ice_clone());
129  }
130  return destination;
131 }
132 
133 armarx::ProfilerStatechartTransitionWithParameters
135  const armarx::TransitionIceBase& transition)
136 {
137  StateIceBasePtr destinationState = StateIceBasePtr::dynamicCast(transition.destinationState);
138  ARMARX_CHECK_NOT_NULL(destinationState);
139 
140  StateParameterMap inputCopy = copyDictionary(destinationState->inputParameters);
141  StateParameterMap localCopy = copyDictionary(destinationState->localParameters);
142  StateParameterMap outputCopy;
143  if (transition.sourceState)
144  {
145  outputCopy = copyDictionary(StateIceBasePtr::dynamicCast(transition.sourceState)->outputParameters);
146  }
147 
148  ProfilerStatechartTransitionWithParameters profilerTransition;
149  profilerTransition.processId = processId;
150  profilerTransition.timestamp = timestamp;
151 
152  profilerTransition.sourceStateIdentifier = transition.sourceState ? StateIceBasePtr::dynamicCast(transition.sourceState)->globalStateIdentifier : "";
153  profilerTransition.targetStateIdentifier = destinationState->globalStateIdentifier;
154  profilerTransition.targetStateType = destinationState->stateType;
155  profilerTransition.eventName = transition.evt->eventName;
156 
157  profilerTransition.inputParameters = inputCopy;
158  profilerTransition.localParameters = localCopy;
159  profilerTransition.outputParameters = outputCopy;
160 
161  return profilerTransition;
162 }
163 
164 
165 void armarx::Profiler::IceBufferedLoggingStrategy::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)
166 {
167  ProfilerEvent event = { processId, Ice::Long(timestamp), executableName, timestampUnit, eventName, parentName, functionName};
168  {
169  std::unique_lock lock(profilerEventsMutex);
170  profilerEvents.push_back(event);
171  }
172 }
173 
174 void armarx::Profiler::IceBufferedLoggingStrategy::logStatechartTransition(const ProfilerStatechartTransition& transition)
175 {
176  std::unique_lock lock(profilerStatechartTransitionsMutex);
177  profilerStatechartTransitions.push_back(transition);
178 }
179 
180 void armarx::Profiler::IceBufferedLoggingStrategy::logStatechartInputParameters(pid_t processId, uint64_t timestamp, const std::string& stateIdentifier, const armarx::StateParameterMap& inputParameterMap)
181 {
182  armarx::StateParameterMap copy = copyDictionary(inputParameterMap);
183 
184  ProfilerStatechartParameters parameters = { processId, Ice::Long(timestamp), stateIdentifier, copy};
185  {
186  std::unique_lock lock(profilerStatechartInputParametersMutex);
187  profilerStatechartInputParameters.push_back(parameters);
188  }
189 }
190 
191 void armarx::Profiler::IceBufferedLoggingStrategy::logStatechartLocalParameters(pid_t processId, uint64_t timestamp, const std::string& stateIdentifier, const armarx::StateParameterMap& localParameterMap)
192 {
193  armarx::StateParameterMap copy = copyDictionary(localParameterMap);
194  ProfilerStatechartParameters parameters = { processId, Ice::Long(timestamp), stateIdentifier, copy};
195  {
196  std::unique_lock lock(profilerStatechartLocalParametersMutex);
197  profilerStatechartLocalParameters.push_back(parameters);
198  }
199 }
200 
201 void armarx::Profiler::IceBufferedLoggingStrategy::logStatechartOutputParameters(pid_t processId, uint64_t timestamp, const std::string& stateIdentifier, const armarx::StateParameterMap& outputParameterMap)
202 {
203  armarx::StateParameterMap copy = copyDictionary(outputParameterMap);
204  ProfilerStatechartParameters parameters = { processId, Ice::Long(timestamp), stateIdentifier, copy};
205  {
206  std::unique_lock lock(profilerStatechartOutputParametersMutex);
207  profilerStatechartOutputParameters.push_back(parameters);
208  }
209 }
210 
211 void armarx::Profiler::IceBufferedLoggingStrategy::logStatechartTransitionWithParameters(pid_t processId, uint64_t timestamp, const TransitionIceBase& transition)
212 {
213  ARMARX_TRACE;
214 
215  ProfilerStatechartTransitionWithParameters profilerTransition = toProfilerTransition(processId, timestamp, transition);
216  {
217  std::unique_lock lock(profilerStatechartTransitionsWithParametersMutex);
218  profilerStatechartTransitionsWithParameters.push_back(profilerTransition);
219  }
220 }
221 
222 
223 void armarx::Profiler::IceBufferedLoggingStrategy::logProcessCpuUsage(pid_t processId, uint64_t timestamp, float cpuUsage)
224 {
225  ProfilerProcessCpuUsage process = {processId, Ice::Long(timestamp), ::armarx::Application::getInstance()->getName(), cpuUsage};
226  {
227  std::unique_lock lock(profilerCpuUsagesMutex);
228  profilerProcessCpuUsages.push_back(process);
229  }
230 }
231 
232 void armarx::Profiler::IceBufferedLoggingStrategy::logProcessMemoryUsage(pid_t processId, uint64_t timestamp, int memoryUsage)
233 {
234  ProfilerProcessMemoryUsage processMemoryUsage = {processId, Ice::Long(timestamp), ::armarx::Application::getInstance()->getName(), memoryUsage};
235  {
236  std::unique_lock lock(profilerProcessMemoryUsagesMutex);
237  profilerProcessMemoryUsages.push_back(processMemoryUsage);
238  }
239 }
240 
242 {
243  if (!profilerEvents.empty())
244  {
245  ProfilerEventList eventsCopy;
246  {
247  std::unique_lock lock(profilerEventsMutex);
248  profilerEvents.swap(eventsCopy);
249  }
250  profilerListenerPrx->reportEventList(eventsCopy);
251  }
252 
253  if (!profilerStatechartTransitions.empty())
254  {
255  ProfilerStatechartTransitionList transitionsCopy;
256  {
257  std::unique_lock lock(profilerStatechartTransitionsMutex);
258  profilerStatechartTransitions.swap(transitionsCopy);
259  }
260  profilerListenerPrx->reportStatechartTransitionList(transitionsCopy);
261  }
262 
263  if (!profilerStatechartInputParameters.empty())
264  {
265  ProfilerStatechartParametersList parametersCopy;
266  {
267  std::unique_lock lock(profilerStatechartInputParametersMutex);
268  profilerStatechartInputParameters.swap(parametersCopy);
269  }
270  profilerListenerPrx->reportStatechartInputParametersList(parametersCopy);
271  }
272 
273  if (!profilerStatechartLocalParameters.empty())
274  {
275  ProfilerStatechartParametersList parametersCopy;
276  {
277  std::unique_lock lock(profilerStatechartLocalParametersMutex);
278  profilerStatechartLocalParameters.swap(parametersCopy);
279  }
280  profilerListenerPrx->reportStatechartLocalParametersList(parametersCopy);
281  }
282 
283  if (!profilerStatechartOutputParameters.empty())
284  {
285  ProfilerStatechartParametersList parametersCopy;
286  {
287  std::unique_lock lock(profilerStatechartOutputParametersMutex);
288  profilerStatechartOutputParameters.swap(parametersCopy);
289  }
290  profilerListenerPrx->reportStatechartOutputParametersList(parametersCopy);
291  }
292 
293  if (!profilerStatechartTransitionsWithParameters.empty())
294  {
295  ProfilerStatechartTransitionWithParametersList transitionsWithParametersCopy;
296  {
297  std::unique_lock lock(profilerStatechartTransitionsWithParametersMutex);
298  profilerStatechartTransitionsWithParameters.swap(transitionsWithParametersCopy);
299  }
300  profilerListenerPrx->reportStatechartTransitionWithParametersList(transitionsWithParametersCopy);
301  }
302 
303  if (!profilerProcessCpuUsages.empty())
304  {
305  ProfilerProcessCpuUsageList cpuUsagesCopy;
306  {
307  std::unique_lock lock(profilerCpuUsagesMutex);
308  profilerProcessCpuUsages.swap(cpuUsagesCopy);
309  }
310  profilerListenerPrx->reportProcessCpuUsageList(cpuUsagesCopy);
311  }
312 
313  if (!profilerProcessMemoryUsages.empty())
314  {
315  ProfilerProcessMemoryUsageList memoryUsageCopy;
316  {
317  std::unique_lock lock(profilerProcessMemoryUsagesMutex);
318  profilerProcessMemoryUsages.swap(memoryUsageCopy);
319  }
320 
321  profilerListenerPrx->reportProcessMemoryUsageList(memoryUsageCopy);
322  }
323 }
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:48
armarx::Profiler::IceLoggingStrategy::logProcessCpuUsage
void logProcessCpuUsage(pid_t processId, uint64_t timestamp, float cpuUsage) override
Definition: IceLoggingStrategy.cpp:85
armarx::Profiler::IceBufferedLoggingStrategy::profilerProcessMemoryUsages
ProfilerProcessMemoryUsageList profilerProcessMemoryUsages
Definition: IceLoggingStrategy.h:133
armarx::Profiler::IceLoggingStrategy::~IceLoggingStrategy
~IceLoggingStrategy() override
Definition: IceLoggingStrategy.cpp:44
armarx::Profiler::IceLoggingStrategy::logStatechartInputParameters
void logStatechartInputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap) override
Definition: IceLoggingStrategy.cpp:59
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:101
PeriodicTask.h
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartInputParameters
ProfilerStatechartParametersList profilerStatechartInputParameters
Definition: IceLoggingStrategy.h:118
armarx::Profiler::IceBufferedLoggingStrategy::publisherTask
PeriodicTask< IceBufferedLoggingStrategy >::pointer_type publisherTask
Definition: IceLoggingStrategy.h:137
armarx::Profiler::IceBufferedLoggingStrategy::profilerProcessCpuUsages
ProfilerProcessCpuUsageList profilerProcessCpuUsages
Definition: IceLoggingStrategy.h:130
armarx::Profiler::IceLoggingStrategy::IceLoggingStrategy
IceLoggingStrategy(ProfilerListenerPrx profilerTopic)
Definition: IceLoggingStrategy.cpp:39
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartLocalParameters
void logStatechartLocalParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap) override
Definition: IceLoggingStrategy.cpp:191
IceLoggingStrategy.h
armarx::Profiler::IceBufferedLoggingStrategy::copyDictionary
static armarx::StateParameterMap copyDictionary(const armarx::StateParameterMap &source)
Definition: IceLoggingStrategy.cpp:122
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartLocalParameters
ProfilerStatechartParametersList profilerStatechartLocalParameters
Definition: IceLoggingStrategy.h:121
ARMARX_TRACE
#define ARMARX_TRACE
Definition: trace.h:69
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartOutputParameters
ProfilerStatechartParametersList profilerStatechartOutputParameters
Definition: IceLoggingStrategy.h:124
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:174
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:134
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:184
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(pid_t processId, uint64_t timestamp, const TransitionIceBase &transition) override
Definition: IceLoggingStrategy.cpp:211
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::Profiler::IceLoggingStrategy::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(pid_t processId, uint64_t timestamp, const TransitionIceBase &transition) override
Definition: IceLoggingStrategy.cpp:77
armarx::Profiler::IceLoggingStrategy::logStatechartOutputParameters
void logStatechartOutputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap) override
Definition: IceLoggingStrategy.cpp:71
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartTransitions
ProfilerStatechartTransitionList profilerStatechartTransitions
Definition: IceLoggingStrategy.h:115
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartInputParameters
void logStatechartInputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap) override
Definition: IceLoggingStrategy.cpp:180
armarx::Application::getInstance
static ApplicationPtr getInstance()
Retrieve shared pointer to the application object.
Definition: Application.cpp:289
boost::source
Vertex source(const detail::edge_base< Directed, Vertex > &e, const PCG &)
Definition: point_cloud_graph.h:681
armarx::Profiler::IceBufferedLoggingStrategy::logStatechartOutputParameters
void logStatechartOutputParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap) override
Definition: IceLoggingStrategy.cpp:201
ARMARX_ICE_LOGGING_BUFFER_SIZE
#define ARMARX_ICE_LOGGING_BUFFER_SIZE
Definition: IceLoggingStrategy.cpp:99
armarx::Profiler::IceBufferedLoggingStrategy::logProcessMemoryUsage
void logProcessMemoryUsage(pid_t processId, u_int64_t timestamp, int memoryUsage) override
Definition: IceLoggingStrategy.cpp:232
armarx::Profiler::IceBufferedLoggingStrategy::profilerStatechartTransitionsWithParameters
ProfilerStatechartTransitionWithParametersList profilerStatechartTransitionsWithParameters
Definition: IceLoggingStrategy.h:127
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:165
armarx::Profiler::IceBufferedLoggingStrategy::publishData
void publishData()
Definition: IceLoggingStrategy.cpp:241
armarx::Profiler::IceBufferedLoggingStrategy::profilerEvents
ProfilerEventList profilerEvents
Definition: IceLoggingStrategy.h:112
armarx::PeriodicTask
Definition: ArmarXManager.h:70
armarx::Profiler::IceBufferedLoggingStrategy::~IceBufferedLoggingStrategy
~IceBufferedLoggingStrategy() override
Definition: IceLoggingStrategy.cpp:117
armarx::Profiler::IceLoggingStrategy::logProcessMemoryUsage
void logProcessMemoryUsage(pid_t processId, u_int64_t timestamp, int memoryUsage) override
Definition: IceLoggingStrategy.cpp:91
armarx::Profiler::IceLoggingStrategy::logStatechartLocalParameters
void logStatechartLocalParameters(pid_t processId, uint64_t timestamp, const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap) override
Definition: IceLoggingStrategy.cpp:65
armarx::Profiler::IceBufferedLoggingStrategy::logProcessCpuUsage
void logProcessCpuUsage(pid_t processId, uint64_t timestamp, float cpuUsage) override
Definition: IceLoggingStrategy.cpp:223
Application.h
armarx::Profiler::IceLoggingStrategy::logStatechartTransition
void logStatechartTransition(const ProfilerStatechartTransition &transition) override
Definition: IceLoggingStrategy.cpp:54