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 <map>
28#include <memory>
29#include <mutex>
30#include <set>
31#include <string>
32
33#include <IceUtil/Time.h>
34
35#include <ArmarXCore/interface/statechart/StatechartIce.h>
36
37namespace 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 */
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 */
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,
121 const std::string& parentName,
122 const std::string& functionName);
123 void logStatechartTransition(const std::string& parentStateIdentifier,
124 const StateIceBasePtr& sourceState,
125 const StateIceBasePtr& destinationState,
126 const std::string& eventName);
127 void logStatechartInputParameters(const std::string& stateIdentifier,
128 const armarx::StateParameterMap& inputParameterMap);
129 void logStatechartLocalParameters(const std::string& stateIdentifier,
130 const armarx::StateParameterMap& localParameterMap);
131 void logStatechartOutputParameters(const std::string& stateIdentifier,
132 const armarx::StateParameterMap& outputParameterMap);
133
134 void logStatechartTransitionWithParameters(const TransitionIceBase& transition);
135
136 /**
137 * @brief reset reinitializes armarx::Profiler::Profiler::startTime with the current time.
138 */
139 inline void reset();
140
141 protected:
142 std::string profilerName;
143
144 private:
145 /**
146 * @brief GetEventTypeMap ensures that elements are only put once into armarx::Profiler::Profiler::evenTypeNameMap
147 * @return reference to armarx::Profiler::Profiler::evenTypeNameMap
148 */
149 static const EventTypeMap& GetEventTypeMap();
150
151 /**
152 * @brief logger holds an armarx::Profiler::LoggingStrategy subclass that defines what armarx::Profiler::Profiler should do with the collected data
153 */
154 LoggingStrategyPtr logger;
155
156 /**
157 * @brief loggerMutex is used to prevent race conditions when armarx::Profiler::Profiler::logger is exchanged.
158 */
159 std::mutex loggerMutex;
160
161 /**
162 * @brief contains a string with the unit of the timestamp (must be changed if Profiler::getTimestamp() is changed)
163 */
164 const std::string timestampUnit{"us"};
165 };
166} // namespace armarx::Profiler
The armarx::Profiler::Profiler class can be used for timing executions within the ArmarX framework.
Definition Profiler.h:89
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 reset()
reset reinitializes armarx::Profiler::Profiler::startTime with the current time.
void logStatechartLocalParameters(const std::string &stateIdentifier, const armarx::StateParameterMap &localParameterMap)
Definition Profiler.cpp:115
std::shared_ptr< LoggingStrategy > LoggingStrategyPtr
std::shared_ptr< Profiler > ProfilerPtr
std::set< ProfilerPtr > ProfilerSet
Definition Profiler.h:41