MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
mlTimeProfile.h
Go to the documentation of this file.
1/*************************************************************************************
2**
3** Copyright 2009, MeVis Medical Solutions AG
4**
5** The user may use this file in accordance with the license agreement provided with
6** the Software or, alternatively, in accordance with the terms contained in a
7** written agreement between the user and MeVis Medical Solutions AG.
8**
9** For further information use the contact form at https://www.mevislab.de/contact
10**
11**************************************************************************************/
12
13// Class for collecting time profiling information.
14
15#ifndef ML_TIME_PROFILE_H
16#define ML_TIME_PROFILE_H
17
19
21
22#include <boost/thread/thread.hpp>
23#include <boost/unordered_map.hpp>
24#include <unordered_set>
25#include <vector>
26#include <memory>
27
28class MLMetaProfile;
29class MLTimeProfile;
30
31typedef std::shared_ptr<MLCallGraphFunction> MLCallGraphFunctionPtr;
32
33typedef std::vector<MLCallGraphNode*> MLCallGraphNodesVector;
34typedef std::unordered_set<MLCallGraphFunctionPtr> MLCallGraphFunctionSet;
35typedef std::vector<MLCallGraphFunctionPtr> MLCallGraphFunctionList;
36typedef boost::unordered_map<MLGlobalFunctionKey, MLCallGraphFunctionPtr> MLCallGraphGlobalFunctionMap;
37
39{
40public:
42
43private:
45
46 MLCallGraphNode* _callGraphNode;
47
48 friend class MLTimeProfile;
49 friend class ProfilingTest_TestCallGraphIsRecursive_Test;
50 friend class ProfilingTest_TestProfileAccumulation_Test;
51};
52
53
55{
56public:
57 MLTimeProfile(const MLMetaProfile* metaProfile = nullptr);
59
63 MLTimeProfileHandle startMeasuring(const std::string& function, int userId, void* userData,
64 const std::string& filename, int linenumber, const std::string& nodeInfo = "");
65
68 void stopMeasuring(MLTimeProfileHandle& handle, bool ignoreIfEmpty = false);
69
73 void addTimeToFunction(const std::string& function, int userId, const std::string& filename, int linenumber,
75
77 void reset();
78
80
82 const MLMetaProfile* metaProfile() const;
83
86 {
87 return _allElapsedTime;
88 }
89
92 {
93 return _allConsumedTime;
94 }
95
98 {
99 return _allElapsedTimeSnapshot;
100 }
101
104 {
105 return _allConsumedTimeSnapshot;
106 }
107
110
113
115 static void refreshCallGraphCopy();
116
119 {
120 return _usedFunctions;
121 }
122
125 {
126 return getGlobalFunctionMap();
127 }
128
131 {
132 return getGlobalFunctionList();
133 }
134
137 {
138 return refreshGlobalFunctionListCopy();
139 }
140
141
143 static MLCallGraphFunctionPtr getFunctionGlobal(const std::string& function, const std::string& filename,
144 int linenumber);
145
147 MLCallGraphFunctionPtr getFunctionForTesting(const std::string& function) const;
148
151
154 {
155 _reduceCallGraph = reduceCallGraph;
156 };
157
160 {
161 _allowParameterEvaluation = allowParameterEvaluation;
162 };
163
166 {
167 return _reduceCallGraph;
168 };
169
172 {
173 return _allowParameterEvaluation;
174 };
175
177 static void initialize();
178
180 static void deinitialize();
181
183 static void enableTracing(const char* filename);
184
186 static void disableTracing();
187
189 static inline bool isTracingEnabled()
190 {
191 return _traceStream != nullptr;
192 }
193
195
196private:
198 MLCallGraphFunctionPtr getOrCreateGlobalFunction(const std::string& function, const std::string& filename,
199 int linenumber, int userId);
200
204 void deleteFunction(const MLCallGraphFunctionPtr& function);
205
207 static void addGlobalFunction(const MLCallGraphFunctionPtr& function);
208
212 static void deleteGlobalFunction(const MLCallGraphFunctionPtr& function);
213
216 void removeCallGraphNode(MLCallGraphNode* node);
217
219 void updateStatistics(MLTimeStatistics& statistics, bool updateElapsedTime, MLProfilingTimeType elapsedTime,
221
223 static void resetGlobalData();
224
225public:
227
228private:
229 MLCallGraphFunctionSet _usedFunctions;
230 static MLCallGraphFunctionList _globalFunctionListCopy;
231
232 MLProfilingTimeType _allElapsedTime;
233 MLProfilingTimeType _allConsumedTime;
234
235 MLProfilingTimeType _allElapsedTimeSnapshot;
236 MLProfilingTimeType _allConsumedTimeSnapshot;
237
238 const MLMetaProfile* _metaProfile;
239
240 MLCallGraphNodesVector _callGraphNodes;
241
242 static bool _reduceCallGraph;
243 static bool _allowParameterEvaluation;
244
245 static MLCallGraphGlobalFunctionMap& getGlobalFunctionMap();
246 // Duplicates the global function map values, but is required for performance reasons
247 static MLCallGraphFunctionList& getGlobalFunctionList();
248 static MLCallGraphFunctionList& refreshGlobalFunctionListCopy();
249
250 static MLCallGraphNode* _callGraphRoot;
251 static MLCallGraphNode* _callGraphRootCopy;
252 static MLCallGraphNode* _lastCallGraphNode;
253
254 static std::ofstream* _traceStream;
255
257 friend class MLCallGraphNode;
258 friend class MLProfilingManager;
259
260 friend class MLABModuleProfile;
261};
262
263
264#endif // _ML_TIME_PROFILE_H_
static bool shouldAllowParameterEvaluation()
Returns whether the call graph should be reduced.
static MLCallGraphFunctionPtr getFunctionGlobal(const std::string &function, const std::string &filename, int linenumber)
Returns the function pointer or NULL.
void stopMeasuring(MLTimeProfileHandle &handle, bool ignoreIfEmpty=false)
Stops measuring time and associates the elapsed time.
const MLMetaProfile * metaProfile() const
Returns the metaprofile. Do not assume that the pointer is valid!
MLCallGraphFunctionPtr getFunctionForTesting(const std::string &function) const
Returns the function pointer or NULL.
static void setAllowParameterEvaluation(bool allowParameterEvaluation)
Sets whether function parameters should be evaluated.
void reset()
Resets the time profile.
static bool shouldReduceCallGraph()
Returns whether the call graph should be reduced.
static void resetGlobalFunctionListCopy()
static MLCallGraphNode * callGraphCopy()
Returns the timer copy tree root.
static void deinitialize()
Static deinitialization, is called by the profiling manager.
static bool isTracingEnabled()
Returns whether call tracing is enabled.
static MLCallGraphNode & callGraph()
Returns the timer tree root.
MLTimeProfileHandle startMeasuring(const std::string &function, int userId, void *userData, const std::string &filename, int linenumber, const std::string &nodeInfo="")
Starts measuring time.
static void initialize()
Static initialization, is called by the profiling manager.
static void refreshCallGraphCopy()
Resets the timer copy tree root.
void makeSnapshot()
void addTimeToFunction(const std::string &function, int userId, const std::string &filename, int linenumber, MLProfilingTimeType time, int callCount)
Adds time to the given function, without measuring and without putting the function onto the call sta...
static const MLCallGraphFunctionList & globalFunctionList()
Returns the global function list.
MLProfilingTimeType allConsumedTime() const
Returns the sum of the consumed time of all functions in seconds.
static void disableTracing()
Disables call tracing.
static const MLCallGraphGlobalFunctionMap & globalFunctionMap()
Returns the global function map.
const MLCallGraphFunctionSet & usedFunctions() const
Returns the function map.
MLProfilingTimeType allConsumedTimeSnapshot() const
Returns snapshot of the sum of the consumed time of all functions in seconds.
MLTimeProfile(const MLMetaProfile *metaProfile=nullptr)
static void setReduceCallGraph(bool reduceCallGraph)
Sets whether the call graph should be reduced.
MLProfilingTimeType allElapsedTimeSnapshot() const
Returns snapshot of the sum of the elapsed time of all functions in seconds.
static MLProfilingTimeType globalElapsedTime()
Returns the global elapsed time in seconds.
static MLCallGraphFunctionPtr mainFunction
static void enableTracing(const char *filename)
Enables call tracing and set the trace filname.
static const MLCallGraphFunctionList & globalFunctionListCopy()
Returns copy of the global function list.
MLProfilingTimeType allElapsedTime() const
Returns the sum of the elapsed time of all functions in seconds.
double MLProfilingTimeType
Definition mlCallGraph.h:71
#define MLPROFILINGMANAGER_EXPORT
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
std::unordered_set< MLCallGraphFunctionPtr > MLCallGraphFunctionSet
std::vector< MLCallGraphFunctionPtr > MLCallGraphFunctionList
boost::unordered_map< MLGlobalFunctionKey, MLCallGraphFunctionPtr > MLCallGraphGlobalFunctionMap
std::vector< MLCallGraphNode * > MLCallGraphNodesVector
std::shared_ptr< MLCallGraphFunction > MLCallGraphFunctionPtr
Time is stored in seconds.
Definition mlCallGraph.h:75