Profiler.h
Go to the documentation of this file.
1 #pragma once
2 
3 /*
4  * This file is part of ArmarX.
5  *
6  * Copyright (C) 2011-2016, High Performance Humanoid Technologies (H2T), Karlsruhe Institute of Technology (KIT), all rights reserved.
7  *
8  * ArmarX is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License version 2 as
10  * published by the Free Software Foundation.
11  *
12  * ArmarX is distributed in the hope that it will be useful, but
13  * WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  * @package ArmarXCore::core::services::profiler
21  * @author Manfred Kroehnert ( manfred dot kroehnert at dot kit dot edu )
22  * @date 2015
23  * @copyright http://www.gnu.org/licenses/gpl-2.0.txt
24  * GNU General Public License
25  */
26 
27 #include <ArmarXCore/interface/statechart/StatechartIce.h>
28 
29 #include <IceUtil/Time.h>
30 
31 #include <memory>
32 #include <string>
33 #include <mutex>
34 #include <map>
35 #include <set>
36 
37 namespace armarx::Profiler
38 {
39  class Profiler;
40  using ProfilerPtr = std::shared_ptr<Profiler>;
41  using ProfilerSet = std::set<ProfilerPtr>;
42 
43  class LoggingStrategy;
44  using LoggingStrategyPtr = std::shared_ptr<LoggingStrategy>;
45 
46  /**
47  * \page armarx-profiling-doc ArmarX Profiling
48  *
49  * \section armarx-profiling-overview ArmarX Profiling Overview
50  *
51  * ArmarXCore provides a basic armarx::Profiler::Profiler facility for profiling applications.
52  *
53  * The API documentation can be found here: \ref Profiling
54  *
55  * The armarx::Profiler::Profiler can be activated during application startup via the following commandline parameters
56  *
57  * \li ArmarX.NetworkStats : sends out information about amount of data sent and received by the enabled application
58  * \li ArmarX.EnableProfiling : sends out information about CPU utilization of the enabled application
59  * \li ArmarX.$COMPONENT_NAME.EnableProfiling : sends out profiling events and state transitions (in case of statecharts) of the enabled application
60  *
61  * To display the profiled data via ArmarXGui, the armarx::ProfilerObserver component can be executed and its output visualized using the Plotter gui.
62  *
63  * \subsection armarx-profiling-api ArmarX Profiling API
64  *
65  * The profiler is built into each armarx::ManagedIceObject and can be accessed via armarx::ManagedIceObject::getProfiler().
66  * armarx::Component instances provide an "EnableProfiling" property which activates the profiler via armarx::ManagedIceObject::enableProfiler().
67  *
68  * After obtaining an instance of armarx::Profiler::Profiler the following methods can be called
69  *
70  * \li armarx::Profiler::Profiler::logEvent()
71  * \li armarx::Profiler::Profiler::logStatechartTransition() (this is automatically called in any statechart if the property "EnableProfiling" and "ProfilingDepth" are set)
72  *
73  * \defgroup Profiling
74  * \ingroup core-utility
75  */
76 
77 
78  /**
79  * \class Profiler
80  * \ingroup Profiling
81  * \brief The armarx::Profiler::Profiler class can be used for timing executions within the ArmarX framework
82  *
83  * The armarx::Profiler::Profiler class provides methods for logging events and statechart transitions.
84  * Changing the logging behavior requires calling armarx::Profiler::Profiler::setLoggingStrategy() with a specialized implementation
85  * of armarx::Profiler::LoggingStrategy.
86  * By default, all calls to log functions are ignored.
87  */
88  class Profiler
89  {
90  public:
91  /**
92  * @brief The EventType enum provides symbolic names for the different events which can be logged via armarx::Profiler::Profiler::logEvent()
93  */
94  enum EventType
95  {
100  };
101 
102  using EventTypeMap = std::map<Profiler::EventType, std::string>;
103 
104  /**
105  * @brief getEventName maps enum values from armarx::Profiler::Profiler::EventType to strings
106  * @param eventType
107  * @return string representation of the enum value
108  */
109  static std::string GetEventName(Profiler::EventType eventType);
110 
111  Profiler();
112  ~Profiler();
113 
114  void setName(const std::string& profilerName);
115 
116  void setLoggingStrategy(LoggingStrategyPtr loggingStrategy);
117 
118  void logProcessCpuUsage(float cpuUsage);
119  void logProcessMemoryUsage(int memoryUsage);
120  void logEvent(Profiler::EventType eventType, const std::string& parentName, const std::string& functionName);
121  void logStatechartTransition(const std::string& parentStateIdentifier, const StateIceBasePtr& sourceState, const StateIceBasePtr& destinationState, const std::string& eventName);
122  void logStatechartInputParameters(const std::string& stateIdentifier, const armarx::StateParameterMap& inputParameterMap);
123  void logStatechartLocalParameters(const std::string& stateIdentifier, const armarx::StateParameterMap& localParameterMap);
124  void logStatechartOutputParameters(const std::string& stateIdentifier, const armarx::StateParameterMap& outputParameterMap);
125 
126  void logStatechartTransitionWithParameters(const TransitionIceBase& transition);
127 
128  /**
129  * @brief reset reinitializes armarx::Profiler::Profiler::startTime with the current time.
130  */
131  inline void reset();
132  protected:
133  std::string profilerName;
134 
135  private:
136  /**
137  * @brief GetEventTypeMap ensures that elements are only put once into armarx::Profiler::Profiler::evenTypeNameMap
138  * @return reference to armarx::Profiler::Profiler::evenTypeNameMap
139  */
140  static const EventTypeMap& GetEventTypeMap();
141 
142  /**
143  * @brief logger holds an armarx::Profiler::LoggingStrategy subclass that defines what armarx::Profiler::Profiler should do with the collected data
144  */
145  LoggingStrategyPtr logger;
146 
147  /**
148  * @brief loggerMutex is used to prevent race conditions when armarx::Profiler::Profiler::logger is exchanged.
149  */
150  std::mutex loggerMutex;
151 
152  /**
153  * @brief contains a string with the unit of the timestamp (must be changed if Profiler::getTimestamp() is changed)
154  */
155  const std::string timestampUnit{"us"};
156  };
157 }
armarx::Profiler::Profiler::reset
void reset()
reset reinitializes armarx::Profiler::Profiler::startTime with the current time.
armarx::Profiler::Profiler::logProcessCpuUsage
void logProcessCpuUsage(float cpuUsage)
Definition: Profiler.cpp:111
armarx::Profiler
Definition: ManagedIceObject.h:72
armarx::Profiler::Profiler::eFunctionBreak
@ eFunctionBreak
Definition: Profiler.h:98
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::ProfilerSet
std::set< ProfilerPtr > ProfilerSet
Definition: Profiler.h:41
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::eFunctionStart
@ eFunctionStart
Definition: Profiler.h:96
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
armarx::Profiler::Profiler::eNumberEventTypes
@ eNumberEventTypes
Definition: Profiler.h:99
armarx::Profiler::Profiler::~Profiler
~Profiler()
Definition: Profiler.cpp:45
armarx::Profiler::Profiler::eFunctionReturn
@ eFunctionReturn
Definition: Profiler.h:97
armarx::statechartmodel::StateParameterMap
QMap< QString, StateParameterPtr > StateParameterMap
Definition: StateParameter.h:46
armarx::Profiler::Profiler
The armarx::Profiler::Profiler class can be used for timing executions within the ArmarX framework.
Definition: Profiler.h:88
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::Profiler::Profiler::profilerName
std::string profilerName
Definition: Profiler.h:133
armarx::Profiler::Profiler::logStatechartTransitionWithParameters
void logStatechartTransitionWithParameters(const TransitionIceBase &transition)
Definition: Profiler.cpp:135
armarx::Profiler::Profiler::EventTypeMap
std::map< Profiler::EventType, std::string > EventTypeMap
Definition: Profiler.h:102
armarx::Profiler::ProfilerPtr
std::shared_ptr< Profiler > ProfilerPtr
Definition: ManagedIceObject.h:75
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