MeVisLab Toolbox Reference
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
20#include "../Include/mlCallGraph.h"
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,
64 int userId,
65 void* userData,
66 const std::string& filename,
67 int linenumber,
68 const std::string& nodeInfo = "");
69
72 void stopMeasuring(MLTimeProfileHandle& handle, bool ignoreIfEmpty = false);
73
77 void addTimeToFunction(const std::string& function,
78 int userId,
79 const std::string& filename,
80 int linenumber,
81 MLProfilingTimeType time, int callCount);
82
84 void reset();
85
87
89 const MLMetaProfile* metaProfile() const;
90
92 MLProfilingTimeType allElapsedTime() const { return _allElapsedTime; }
93
95 MLProfilingTimeType allConsumedTime() const { return _allConsumedTime; }
96
99 {
100 return _allElapsedTimeSnapshot;
101 }
102
105 {
106 return _allConsumedTimeSnapshot;
107 }
108
111
114
116 static void refreshCallGraphCopy();
117
119 const MLCallGraphFunctionSet& usedFunctions() const { return _usedFunctions; }
120
122 static const MLCallGraphGlobalFunctionMap& globalFunctionMap() { return getGlobalFunctionMap(); }
123
125 static const MLCallGraphFunctionList& globalFunctionList() { return getGlobalFunctionList(); }
126
128 static const MLCallGraphFunctionList& globalFunctionListCopy() { return refreshGlobalFunctionListCopy(); }
129
130
132 static MLCallGraphFunctionPtr getFunctionGlobal(const std::string& function, const std::string& filename, int linenumber);
133
135 MLCallGraphFunctionPtr getFunctionForTesting(const std::string& function) const;
136
139
141 static void setReduceCallGraph(bool reduceCallGraph) { _reduceCallGraph = reduceCallGraph; };
142
144 static bool shouldReduceCallGraph() { return _reduceCallGraph; };
145
147 static void initialize();
148
150 static void deinitialize();
151
153 static void enableTracing(const char* filename);
154
156 static void disableTracing();
157
159 static inline bool isTracingEnabled() { return _traceStream != nullptr; }
160
162
163private:
165 MLCallGraphFunctionPtr getOrCreateGlobalFunction(const std::string& function, const std::string& filename, int linenumber, int userId);
166
170 void deleteFunction(const MLCallGraphFunctionPtr& function);
171
173 static void addGlobalFunction(const MLCallGraphFunctionPtr& function);
174
178 static void deleteGlobalFunction(const MLCallGraphFunctionPtr& function);
179
182 void removeCallGraphNode(MLCallGraphNode* node);
183
185 void updateStatistics(MLTimeStatistics& statistics,
186 bool updateElapsedTime,
187 MLProfilingTimeType elapsedTime,
188 MLProfilingTimeType consumedTime,
189 int callCount = 1);
190
192 static void resetGlobalData();
193
194public:
196
197private:
198 MLCallGraphFunctionSet _usedFunctions;
199 static MLCallGraphFunctionList _globalFunctionListCopy;
200
201 MLProfilingTimeType _allElapsedTime;
202 MLProfilingTimeType _allConsumedTime;
203
204 MLProfilingTimeType _allElapsedTimeSnapshot;
205 MLProfilingTimeType _allConsumedTimeSnapshot;
206
207 const MLMetaProfile* _metaProfile;
208
209 MLCallGraphNodesVector _callGraphNodes;
210
211 static bool _reduceCallGraph;
212
213 static MLCallGraphGlobalFunctionMap& getGlobalFunctionMap();
214 // Duplicates the global function map values, but is required for performance reasons
215 static MLCallGraphFunctionList& getGlobalFunctionList();
216 static MLCallGraphFunctionList& refreshGlobalFunctionListCopy();
217
218 static MLCallGraphNode* _callGraphRoot;
219 static MLCallGraphNode* _callGraphRootCopy;
220 static MLCallGraphNode* _lastCallGraphNode;
221
222 static std::ofstream* _traceStream;
223
225 friend class MLCallGraphNode;
226 friend class MLProfilingManager;
227
228 friend class MLABModuleProfile;
229};
230
231
232#endif // _ML_TIME_PROFILE_H_
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.
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:68
#define MLPROFILINGMANAGER_EXPORT
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:72