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 #include "LoggingStrategy.h"
27 #include "IceLoggingStrategy.h"
28 
30 
32 {
33  ARMARX_CHECK_EXPRESSION(Profiler::GetEventTypeMap().count(eventType));
34  return Profiler::GetEventTypeMap().at(eventType);
35 }
36 
37 
39  profilerName(std::to_string(getpid())),
40  logger(new LoggingStrategy)
41 {
42 }
43 
44 
46 {
47 }
48 
49 
50 void armarx::Profiler::Profiler::setName(const std::string& profilerName)
51 {
52  this->profilerName = profilerName;
53  std::unique_lock lock(loggerMutex);
54  logger->setId(profilerName);
55 }
56 
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 
70 void armarx::Profiler::Profiler::logEvent(armarx::Profiler::Profiler::EventType eventType, const std::string& parentName, const std::string& functionName)
71 {
72  std::unique_lock lock(loggerMutex);
73  logger->logEvent(::getpid(), IceUtil::Time::now().toMicroSeconds(), profilerName, timestampUnit, Profiler::GetEventName(eventType), parentName, functionName);
74 }
75 
76 
77 void armarx::Profiler::Profiler::logStatechartTransition(const std::string& parentStateIdentifier, const StateIceBasePtr& sourceState, const StateIceBasePtr& destinationState, const std::string& eventName)
78 {
79  ARMARX_CHECK_EXPRESSION(destinationState);
80  ProfilerStatechartTransition transition;
81  transition.processId = static_cast<Ice::Long>(::getpid());
82  transition.timestamp = IceUtil::Time::now().toMicroSeconds();
83  transition.parentStateIdentifier = parentStateIdentifier;
84  transition.sourceStateIdentifier = sourceState ? sourceState->globalStateIdentifier : "";
85  transition.targetStateIdentifier = destinationState->globalStateIdentifier;
86  transition.targetStateType = destinationState->stateType;
87  transition.eventName = eventName;
88 
89  std::unique_lock lock(loggerMutex);
90  logger->logStatechartTransition(transition);
91 }
92 
93 void armarx::Profiler::Profiler::logStatechartInputParameters(const std::string& stateIdentifier, const armarx::StateParameterMap& inputParameterMap)
94 {
95  std::unique_lock lock(loggerMutex);
96  logger->logStatechartInputParameters(::getpid(), IceUtil::Time::now().toMicroSeconds(), stateIdentifier, inputParameterMap);
97 }
98 
99 void armarx::Profiler::Profiler::logStatechartLocalParameters(const std::string& stateIdentifier, const armarx::StateParameterMap& localParameterMap)
100 {
101  std::unique_lock lock(loggerMutex);
102  logger->logStatechartLocalParameters(::getpid(), IceUtil::Time::now().toMicroSeconds(), stateIdentifier, localParameterMap);
103 }
104 
105 void armarx::Profiler::Profiler::logStatechartOutputParameters(const std::string& stateIdentifier, const armarx::StateParameterMap& outputParameterMap)
106 {
107  std::unique_lock lock(loggerMutex);
108  logger->logStatechartOutputParameters(::getpid(), IceUtil::Time::now().toMicroSeconds(), stateIdentifier, outputParameterMap);
109 }
110 
112 {
113  std::unique_lock lock(loggerMutex);
114  logger->logProcessCpuUsage(::getpid(), IceUtil::Time::now().toMicroSeconds(), cpuUsage);
115 }
116 
118 {
119  std::unique_lock lock(loggerMutex);
120  logger->logProcessMemoryUsage(::getpid(), IceUtil::Time::now().toMicroSeconds(), memoryUsage);
121 }
122 
123 
124 const armarx::Profiler::Profiler::EventTypeMap& armarx::Profiler::Profiler::GetEventTypeMap()
125 {
126  const static EventTypeMap evenTypeNameMap
127  {
128  {Profiler::eFunctionStart, "FUNCTION_START"},
129  {Profiler::eFunctionReturn, "FUNCTION_RETURN"},
130  {Profiler::eFunctionBreak, "FUNCTION_RETURN"},
131  };
132  return evenTypeNameMap;
133 }
134 
135 void armarx::Profiler::Profiler::logStatechartTransitionWithParameters(const armarx::TransitionIceBase& transition)
136 {
137  std::unique_lock lock(loggerMutex);
138  logger->logStatechartTransitionWithParameters(::getpid(), IceUtil::Time::now().toMicroSeconds(), transition);
139 }
armarx::Profiler::Profiler::logProcessCpuUsage
void logProcessCpuUsage(float cpuUsage)
Definition: Profiler.cpp:111
armarx::Profiler::Profiler::logStatechartTransition
void logStatechartTransition(const std::string &parentStateIdentifier, const StateIceBasePtr &sourceState, const StateIceBasePtr &destinationState, const std::string &eventName)
Definition: Profiler.cpp:77
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:50
armarx::Profiler::Profiler::logStatechartInputParameters
void logStatechartInputParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &inputParameterMap)
Definition: Profiler.cpp:93
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:31
armarx::Profiler::Profiler::logStatechartLocalParameters
void logStatechartLocalParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap)
Definition: Profiler.cpp:99
armarx::Profiler::LoggingStrategyPtr
std::shared_ptr< LoggingStrategy > LoggingStrategyPtr
Definition: LoggingStrategy.h:42
armarx::Profiler::Profiler::logProcessMemoryUsage
void logProcessMemoryUsage(int memoryUsage)
Definition: Profiler.cpp:117
armarx::Profiler::Profiler::setName
void setName(const std::string &profilerName)
Definition: Profiler.cpp:50
IceLoggingStrategy.h
armarx::Profiler::Profiler::~Profiler
~Profiler()
Definition: Profiler.cpp:45
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::VariantType::Long
const VariantTypeId Long
Definition: Variant.h:917
armarx::Profiler::Profiler::logStatechartOutputParameters
void logStatechartOutputParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &outputParameterMap)
Definition: Profiler.cpp:105
armarx::Profiler::Profiler::Profiler
Profiler()
Definition: Profiler.cpp:38
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:40
LoggingStrategy.h
ExpressionException.h
Profiler.h
armarx::Profiler::Profiler::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(const TransitionIceBase &transition)
Definition: Profiler.cpp:135
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