Profiler.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 "Profiler.h"
26
28
29#include "IceLoggingStrategy.h"
30#include "LoggingStrategy.h"
31
32std::string
34{
35 ARMARX_CHECK_EXPRESSION(Profiler::GetEventTypeMap().count(eventType));
36 return Profiler::GetEventTypeMap().at(eventType);
37}
38
43
47
48void
50{
51 this->profilerName = profilerName;
52 std::unique_lock lock(loggerMutex);
53 logger->setId(profilerName);
54}
55
56void
58{
59 if (!loggingStrategy)
60 {
61 return;
62 }
63
64 std::unique_lock lock(loggerMutex);
65 logger = loggingStrategy;
66 logger->setId(profilerName);
67}
68
69void
71 const std::string& parentName,
72 const std::string& functionName)
73{
74 std::unique_lock lock(loggerMutex);
75 logger->logEvent(::getpid(),
76 IceUtil::Time::now().toMicroSeconds(),
78 timestampUnit,
79 Profiler::GetEventName(eventType),
80 parentName,
81 functionName);
82}
83
84void
85armarx::Profiler::Profiler::logStatechartTransition(const std::string& parentStateIdentifier,
86 const StateIceBasePtr& sourceState,
87 const StateIceBasePtr& destinationState,
88 const std::string& eventName)
89{
90 ARMARX_CHECK_EXPRESSION(destinationState);
91 ProfilerStatechartTransition transition;
92 transition.processId = static_cast<Ice::Long>(::getpid());
93 transition.timestamp = IceUtil::Time::now().toMicroSeconds();
94 transition.parentStateIdentifier = parentStateIdentifier;
95 transition.sourceStateIdentifier = sourceState ? sourceState->globalStateIdentifier : "";
96 transition.targetStateIdentifier = destinationState->globalStateIdentifier;
97 transition.targetStateType = destinationState->stateType;
98 transition.eventName = eventName;
99
100 std::unique_lock lock(loggerMutex);
101 logger->logStatechartTransition(transition);
102}
103
104void
106 const std::string& stateIdentifier,
107 const armarx::StateParameterMap& inputParameterMap)
108{
109 std::unique_lock lock(loggerMutex);
110 logger->logStatechartInputParameters(
111 ::getpid(), IceUtil::Time::now().toMicroSeconds(), stateIdentifier, inputParameterMap);
112}
113
114void
116 const std::string& stateIdentifier,
117 const armarx::StateParameterMap& localParameterMap)
118{
119 std::unique_lock lock(loggerMutex);
120 logger->logStatechartLocalParameters(
121 ::getpid(), IceUtil::Time::now().toMicroSeconds(), stateIdentifier, localParameterMap);
122}
123
124void
126 const std::string& stateIdentifier,
127 const armarx::StateParameterMap& outputParameterMap)
128{
129 std::unique_lock lock(loggerMutex);
130 logger->logStatechartOutputParameters(
131 ::getpid(), IceUtil::Time::now().toMicroSeconds(), stateIdentifier, outputParameterMap);
132}
133
134void
136{
137 std::unique_lock lock(loggerMutex);
138 logger->logProcessCpuUsage(::getpid(), IceUtil::Time::now().toMicroSeconds(), cpuUsage);
139}
140
141void
143{
144 std::unique_lock lock(loggerMutex);
145 logger->logProcessMemoryUsage(::getpid(), IceUtil::Time::now().toMicroSeconds(), memoryUsage);
146}
147
149armarx::Profiler::Profiler::GetEventTypeMap()
150{
151 const static EventTypeMap evenTypeNameMap{
152 {Profiler::eFunctionStart, "FUNCTION_START"},
153 {Profiler::eFunctionReturn, "FUNCTION_RETURN"},
154 {Profiler::eFunctionBreak, "FUNCTION_RETURN"},
155 };
156 return evenTypeNameMap;
157}
158
159void
161 const armarx::TransitionIceBase& transition)
162{
163 std::unique_lock lock(loggerMutex);
164 logger->logStatechartTransitionWithParameters(
165 ::getpid(), IceUtil::Time::now().toMicroSeconds(), transition);
166}
void logStatechartInputParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap)
Definition Profiler.cpp:105
void logStatechartTransitionWithParameters(const TransitionIceBase &transition)
Definition Profiler.cpp:160
void logStatechartTransition(const std::string &parentStateIdentifier, const StateIceBasePtr &sourceState, const StateIceBasePtr &destinationState, const std::string &eventName)
Definition Profiler.cpp:85
static std::string GetEventName(Profiler::EventType eventType)
getEventName maps enum values from armarx::Profiler::Profiler::EventType to strings
Definition Profiler.cpp:33
EventType
The EventType enum provides symbolic names for the different events which can be logged via armarx::P...
Definition Profiler.h:95
void logProcessCpuUsage(float cpuUsage)
Definition Profiler.cpp:135
std::map< Profiler::EventType, std::string > EventTypeMap
Definition Profiler.h:102
void setName(const std::string &profilerName)
Definition Profiler.cpp:49
void logProcessMemoryUsage(int memoryUsage)
Definition Profiler.cpp:142
void logEvent(Profiler::EventType eventType, const std::string &parentName, const std::string &functionName)
Definition Profiler.cpp:70
void logStatechartOutputParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap)
Definition Profiler.cpp:125
void setLoggingStrategy(LoggingStrategyPtr loggingStrategy)
Definition Profiler.cpp:57
void logStatechartLocalParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap)
Definition Profiler.cpp:115
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
std::shared_ptr< LoggingStrategy > LoggingStrategyPtr
const std::string & to_string(const std::string &s)