profiler.h 6.33 KB
Newer Older
Pascal Palenda's avatar
Pascal Palenda committed
1
2
3
4
5
6
7
8
9
10
11
#ifndef INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_PROFILER_PROFILER
#define INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_PROFILER_PROFILER

// std includes
#include <memory>
#include <valarray>
#include <vector>

// API includes
#include <ITA/simulation_scheduler/definitions.h>

Pascal Palenda's avatar
Pascal Palenda committed
12
13
#include <ITA/simulation_scheduler/profiler/profiler_types.h>

Pascal Palenda's avatar
Pascal Palenda committed
14
15
namespace ITA
{
16
	namespace SimulationScheduler
Pascal Palenda's avatar
Pascal Palenda committed
17
	{
18
	#ifdef WITH_PROFILER
Pascal Palenda's avatar
Pascal Palenda committed
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
		///
		/// \brief Helper functions for the profiler.
		/// \{
		///
	#define PROFILER_STRINGIFY(a) #a
	#define PROFILER_STRINGIFICATION(a) PROFILER_STRINGIFY(a)
	#define PROFILER_TOKEN_JOIN(x, y) x ## y
	#define PROFILER_TOKEN_CONCATENATE(x, y) PROFILER_TOKEN_JOIN(x, y)
	#define PROFILER_UNIQUE_SECTION(x) PROFILER_TOKEN_CONCATENATE(unique_profiler_mark_name_, x)
		///
		/// \}
		///

		///
		/// \brief Issue an event with the name \p NAME.
		///
35
	#define PROFILER_EVENT_COUNT( NAME) Profiler::AddEventCount( NAME, __FUNCTION__, __FILE__ );
Pascal Palenda's avatar
Pascal Palenda committed
36
37
38
39

		///
		/// \brief Add the value \p VALUE to the profiler under the name \p NAME.
		///
40
	#define PROFILER_VALUE( NAME, VALUE) Profiler::AddValue( NAME, VALUE, __FUNCTION__, __FILE__ );
Pascal Palenda's avatar
Pascal Palenda committed
41
42
43
44

		///
		/// \brief Start a section with the name \p NAME.
		///
45
46
	#define PROFILER_SECTION( NAME ) Profiler::CSection PROFILER_UNIQUE_SECTION(__LINE__);\
		Profiler::StartSection(PROFILER_UNIQUE_SECTION(__LINE__), NAME, __FUNCTION__, __FILE__ );
Pascal Palenda's avatar
Pascal Palenda committed
47

48
49
50
		///
		/// \brief Start a section with the name "Full".
		///
51
	#define PROFILER_FUNCTION( ) PROFILER_SECTION( "Full" );
52

Pascal Palenda's avatar
Pascal Palenda committed
53
54
55
		///
		/// \brief End the last opened section in this thread.
		///
56
	#define PROFILER_END_SECTION() Profiler::EndSection();
Pascal Palenda's avatar
Pascal Palenda committed
57

Pascal Palenda's avatar
Pascal Palenda committed
58
59
60
61
62
		///
		/// \brief Name the current thread.
		///
		/// Only the first invocation of this has an effect.
		///
63
	#define PROFILER_NAME_THREAD( NAME ) Profiler::NameThread( NAME );
Pascal Palenda's avatar
Pascal Palenda committed
64
65
66
67
68
69

		///
		/// \brief Name the current thread "Main".
		///
		/// Only the first invocation of this has an effect.
		///
70
	#define PROFILER_NAME_THREAD_MAIN( ) Profiler::NameThread( "Main" );
Pascal Palenda's avatar
Pascal Palenda committed
71

72
		namespace Profiler
Pascal Palenda's avatar
Pascal Palenda committed
73
		{
74
			class ProfileSection;
Pascal Palenda's avatar
Pascal Palenda committed
75
			class CProfiler;
Pascal Palenda's avatar
Pascal Palenda committed
76

Pascal Palenda's avatar
Pascal Palenda committed
77
78
79
80
81
			///
			/// \brief Section object.
			///
			/// This class is used to time a section of code.
			/// It does not have to be ended manually, as the destruction of the object will register as the end of the section.
82
			/// However, the section can still be ended manually by calling ::EndSection().
Pascal Palenda's avatar
Pascal Palenda committed
83
84
85
86
			///
			class ITA_SIMULATION_SCHEDULER_API CSection
			{
				friend CProfiler;
87
				friend ProfileSection;
Pascal Palenda's avatar
Pascal Palenda committed
88
89
90
91

				///
				/// \brief Start the timing for the section.
				///
92
				void Start ( );
Pascal Palenda's avatar
Pascal Palenda committed
93
94
95
96

				///
				/// \brief End the timing for the section.
				///
97
				void End ( );
Pascal Palenda's avatar
Pascal Palenda committed
98
99
100
101

				///
				/// \brief Returns true if the section was ended.
				///
102
				bool Finished ( ) const;
Pascal Palenda's avatar
Pascal Palenda committed
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

				///
				/// \brief Start time of the section.
				///
				double m_dBegin = -1;

				///
				/// \brief End time of the section.
				///
				double m_dEnd = -1;

			public:
				CSection ( ) = default;

				///
				/// \brief Destructor for CSection
				///
				/// This ends the section.
				///
Pascal Palenda's avatar
Pascal Palenda committed
122
				~CSection ( );
Pascal Palenda's avatar
Pascal Palenda committed
123
124
125
126
127
128
129
130
131
			};

			///
			/// \brief Add the CSection \p oSection to the profiler and start the section.
			/// \param oSection the section object to add to the profiler.
			/// \param sName name of the section.
			/// \param sFunctionName name of the function that started the section.
			/// \param sFile name of the file in which the section was started.
			///
132
			void ITA_SIMULATION_SCHEDULER_API StartSection ( CSection& oSection, const std::string& sName, const std::string& sFunctionName, const std::string& sFile );
Pascal Palenda's avatar
Pascal Palenda committed
133
134
135
136

			///
			/// \brief End the last opened section in this thread.
			///
137
			void ITA_SIMULATION_SCHEDULER_API EndSection ( );
Pascal Palenda's avatar
Pascal Palenda committed
138
139
140
141

			///
			/// \brief End all currently open sections.
			///
142
			void ITA_SIMULATION_SCHEDULER_API EndAllSections ( );
Pascal Palenda's avatar
Pascal Palenda committed
143
144
145
146
147
148
149

			///
			/// \brief Add a EventCount to the profiler.
			/// \param sName name of the event.
			/// \param sFunctionName name of the function that issued the event.
			/// \param sFile name of the file in which the event was issued.
			///
150
			void ITA_SIMULATION_SCHEDULER_API AddEventCount ( const std::string& sName, const std::string& sFunctionName, const std::string& sFile );
Pascal Palenda's avatar
Pascal Palenda committed
151

Pascal Palenda's avatar
Pascal Palenda committed
152
153
154
155
156
157
158
159
160
			///
			/// \brief Add a value to the profiler.
			///
			/// The value must be of type: bool, char, int, float, double, string.
			/// \param sName name of the value.
			/// \param oValueData the untyped value data to be saved in the profiler.
			/// \param sFunctionName name of the function that issued the event.
			/// \param sFile name of the file in which the event was issued.
			///
161
			void ITA_SIMULATION_SCHEDULER_API StoreValue ( const std::string& sName, const ValueData& oValueData, const std::string& sFunctionName, const std::string& sFile );
Pascal Palenda's avatar
Pascal Palenda committed
162
163
164
165
166
167
168
169
170
171
172

			///
			/// \brief Add a value to the profiler.
			///
			/// The value must be of type: bool, char, int, float, double, string.
			/// \param sName name of the value.
			/// \param value the value to be saved in the profiler.
			/// \param sFunctionName name of the function that issued the event.
			/// \param sFile name of the file in which the event was issued.
			///
			template <class T>
173
			void ITA_SIMULATION_SCHEDULER_API AddValue ( const std::string& sName, const T& value, const std::string& sFunctionName, const std::string& sFile )
Pascal Palenda's avatar
Pascal Palenda committed
174
175
			{
				ValueData valueData;
176
				valueData.SetValue ( value );
Pascal Palenda's avatar
Pascal Palenda committed
177

178
				StoreValue ( sName, valueData, sFunctionName, sFile );
Pascal Palenda's avatar
Pascal Palenda committed
179
180
			}

Pascal Palenda's avatar
Pascal Palenda committed
181
182
183
184
185
			///
			/// \brief Name the current thread.
			///
			/// Only the first invocation of this has an effect.
			///
186
			void ITA_SIMULATION_SCHEDULER_API NameThread ( const std::string& sName );
Pascal Palenda's avatar
Pascal Palenda committed
187

Pascal Palenda's avatar
Pascal Palenda committed
188
189
190
			///
			/// \brief Parse the profiler data as a json file.
			///
191
			std::stringstream ITA_SIMULATION_SCHEDULER_API ProfilerToJson ( );
192
		} // namespace Profiler
Pascal Palenda's avatar
Pascal Palenda committed
193
	#else
194

Pascal Palenda's avatar
Pascal Palenda committed
195
196
197
198
	#define PROFILER_EVENT_COUNT( ... )
	#define PROFILER_VALUE( ... ) 
	#define PROFILER_SECTION( ... ) 
	#define PROFILER_SECTION( )
199
200
201
202
	#define PROFILER_END_SECTION()
	#define PROFILER_NAME_THREAD( ... )
	#define PROFILER_NAME_THREAD_MAIN( )

Pascal Palenda's avatar
Pascal Palenda committed
203
	#endif
204
205
206
207
208
209

		///
		/// \brief Store the profiler data as a json file.
		/// \param sProfilerFileName name for the json file.
		/// \note The file extension has to be defined by the user.
		/// 
210
		void ITA_SIMULATION_SCHEDULER_API StoreProfilerData ( const std::string& sProfilerFileName );
211
		
212
	} // namespace SimulationScheduler
Pascal Palenda's avatar
Pascal Palenda committed
213
214
215
} // namespace ITA

#endif // INCLUDE_WATCHER_ITA_SIMULATION_SCHEDULER_PROFILER_PROFILER