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 
32 std::string
34 {
35  ARMARX_CHECK_EXPRESSION(Profiler::GetEventTypeMap().count(eventType));
36  return Profiler::GetEventTypeMap().at(eventType);
37 }
38 
40  profilerName(std::to_string(getpid())), logger(new LoggingStrategy)
41 {
42 }
43 
45 {
46 }
47 
48 void
49 armarx::Profiler::Profiler::setName(const std::string& profilerName)
50 {
51  this->profilerName = profilerName;
52  std::unique_lock lock(loggerMutex);
53  logger->setId(profilerName);
54 }
55 
56 void
58 {
59  if (!loggingStrategy)
60  {
61  return;
62  }
63 
64  std::unique_lock lock(loggerMutex);
65  logger = loggingStrategy;
66  logger->setId(profilerName);
67 }
68 
69 void
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(),
77  profilerName,
78  timestampUnit,
79  Profiler::GetEventName(eventType),
80  parentName,
81  functionName);
82 }
83 
84 void
85 armarx::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 
104 void
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 
114 void
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 
124 void
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 
134 void
136 {
137  std::unique_lock lock(loggerMutex);
138  logger->logProcessCpuUsage(::getpid(), IceUtil::Time::now().toMicroSeconds(), cpuUsage);
139 }
140 
141 void
143 {
144  std::unique_lock lock(loggerMutex);
145  logger->logProcessMemoryUsage(::getpid(), IceUtil::Time::now().toMicroSeconds(), memoryUsage);
146 }
147 
149 armarx::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 
159 void
161  const armarx::TransitionIceBase& transition)
162 {
163  std::unique_lock lock(loggerMutex);
164  logger->logStatechartTransitionWithParameters(
165  ::getpid(), IceUtil::Time::now().toMicroSeconds(), transition);
166 }
armarx::Profiler::Profiler::logProcessCpuUsage
void logProcessCpuUsage(float cpuUsage)
Definition: Profiler.cpp:135
armarx::Profiler::Profiler::logStatechartTransition
void logStatechartTransition(const std::string &parentStateIdentifier, const StateIceBasePtr &sourceState, const StateIceBasePtr &destinationState, const std::string &eventName)
Definition: Profiler.cpp:85
armarx::Profiler::Profiler::logEvent
void logEvent(Profiler::EventType eventType, const std::string &parentName, const std::string &functionName)
Definition: Profiler.cpp:70
armarx::Profiler::LoggingStrategy
A brief description.
Definition: LoggingStrategy.h:51
armarx::Profiler::Profiler::logStatechartInputParameters
void logStatechartInputParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap)
Definition: Profiler.cpp:105
armarx::Profiler::Profiler::GetEventName
static std::string GetEventName(Profiler::EventType eventType)
getEventName maps enum values from armarx::Profiler::Profiler::EventType to strings
Definition: Profiler.cpp:33
armarx::Profiler::Profiler::logStatechartLocalParameters
void logStatechartLocalParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap)
Definition: Profiler.cpp:115
armarx::Profiler::LoggingStrategyPtr
std::shared_ptr< LoggingStrategy > LoggingStrategyPtr
Definition: LoggingStrategy.h:42
armarx::Profiler::Profiler::logProcessMemoryUsage
void logProcessMemoryUsage(int memoryUsage)
Definition: Profiler.cpp:142
armarx::Profiler::Profiler::setName
void setName(const std::string &profilerName)
Definition: Profiler.cpp:49
IceLoggingStrategy.h
armarx::Profiler::Profiler::~Profiler
~Profiler()
Definition: Profiler.cpp:44
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:918
armarx::Profiler::Profiler::logStatechartOutputParameters
void logStatechartOutputParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap)
Definition: Profiler.cpp:125
armarx::Profiler::Profiler::Profiler
Profiler()
Definition: Profiler.cpp:39
armarx::Profiler::Profiler::setLoggingStrategy
void setLoggingStrategy(LoggingStrategyPtr loggingStrategy)
Definition: Profiler.cpp:57
armarx::to_string
const std::string & to_string(const std::string &s)
Definition: StringHelpers.h:41
LoggingStrategy.h
ExpressionException.h
Profiler.h
armarx::Profiler::Profiler::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(const TransitionIceBase &transition)
Definition: Profiler.cpp:160
ARMARX_CHECK_EXPRESSION
#define ARMARX_CHECK_EXPRESSION(expression)
This macro evaluates the expression and if it turns out to be false it will throw an ExpressionExcept...
Definition: ExpressionException.h:73
std
Definition: Application.h:66
armarx::Profiler::Profiler::EventTypeMap
std::map< Profiler::EventType, std::string > EventTypeMap
Definition: Profiler.h:102
armarx::Profiler::Profiler::EventType
EventType
The EventType enum provides symbolic names for the different events which can be logged via armarx::P...
Definition: Profiler.h:94