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 <map>
25 #include <vector>
26 #include <memory>
27 
28 class MLMetaProfile;
29 class MLTimeProfile;
30 
31 typedef std::shared_ptr<MLCallGraphFunction> MLCallGraphFunctionPtr;
32 
33 typedef std::vector<MLCallGraphNode*> MLCallGraphNodesVector;
34 typedef boost::unordered_map<MLCallGraphFunction*, MLCallGraphFunctionPtr> MLCallGraphFunctionMap;
35 typedef std::vector<MLCallGraphFunctionPtr> MLCallGraphFunctionList;
36 typedef boost::unordered_map<MLGlobalFunctionKey, MLCallGraphFunctionPtr> MLCallGraphGlobalFunctionMap;
37 
39 {
40 public:
42 
43 private:
44  MLTimeProfileHandle(MLCallGraphNode* callGraphNode);
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 {
56 public:
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  const MLMetaProfile* metaProfile() const;
88 
90  MLProfilingTimeType allElapsedTime() const { return _allElapsedTime; }
91 
93  MLProfilingTimeType allConsumedTime() const { return _allConsumedTime; }
94 
97 
99  const MLCallGraphFunctionMap& functionMap() const { return _functionMap; }
100 
102  static const MLCallGraphGlobalFunctionMap& globalFunctionMap() { return getGlobalFunctionMap(); }
103 
105  static const MLCallGraphFunctionList& globalFunctionList() { return getGlobalFunctionList(); }
106 
108  static MLCallGraphFunctionPtr getFunctionGlobal(const std::string& function, const std::string& filename, int linenumber);
109 
111  MLCallGraphFunctionPtr getFunctionForTesting(const std::string& function) const;
112 
115 
117  static void setReduceCallGraph(bool reduceCallGraph) { _reduceCallGraph = reduceCallGraph; };
118 
120  static bool shouldReduceCallGraph() { return _reduceCallGraph; };
121 
123  static void initialize();
124 
126  static void deinitialize();
127 
129  static void enableTracing(const char* filename);
130 
132  static void disableTracing();
133 
135  static inline bool isTracingEnabled() { return _traceStream != nullptr; }
136 
137 private:
139  MLCallGraphFunctionPtr getOrCreateGlobalFunction(const std::string& function, const std::string& filename, int linenumber, int userId);
140 
144  void deleteFunction(const MLCallGraphFunctionPtr& function);
145 
147  static void addGlobalFunction(const MLCallGraphFunctionPtr& function);
148 
152  static void deleteGlobalFunction(const MLCallGraphFunctionPtr& function);
153 
156  void removeCallGraphNode(MLCallGraphNode* node);
157 
159  void updateStatistics(MLTimeStatistics& statistics,
160  bool updateElapsedTime,
161  MLProfilingTimeType elapsedTime,
162  MLProfilingTimeType consumedTime,
163  int callCount = 1);
164 
166  static void resetGlobalData();
167 
168 public:
170 
171 private:
172  MLCallGraphFunctionMap _functionMap;
173 
174  MLProfilingTimeType _allElapsedTime;
175  MLProfilingTimeType _allConsumedTime;
176 
177  const MLMetaProfile* _metaProfile;
178 
179  MLCallGraphNodesVector _callGraphNodes;
180 
181  static bool _reduceCallGraph;
182 
183  static MLCallGraphGlobalFunctionMap& getGlobalFunctionMap();
184  // duplicates the global function map values, but is required for performance reasons
185  static MLCallGraphFunctionList& getGlobalFunctionList();
186 
187  static MLCallGraphNode* _callGraphRoot;
188  static MLCallGraphNode* _lastCallGraphNode;
189 
190  static std::ofstream* _traceStream;
191 
192  friend class MLAccumulatedProfile;
193  friend class MLCallGraphNode;
194  friend class MLProfilingManager;
195 
196  friend class MLABModuleProfile;
197 };
198 
199 
200 #endif // _ML_TIME_PROFILE_H_
static const MLCallGraphFunctionList & globalFunctionList()
Returns the global function list.
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.
MLCallGraphFunctionPtr getFunctionForTesting(const std::string &function) const
Returns the function pointer or NULL.
void reset()
Resets the time profile.
static bool shouldReduceCallGraph()
Gets if the call graph should be reduced.
const MLCallGraphFunctionMap & functionMap() const
Returns the function map.
Definition: mlTimeProfile.h:99
static void deinitialize()
Static deinitialization, is called by the profiling manager.
static bool isTracingEnabled()
Returns if 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 const MLCallGraphGlobalFunctionMap & globalFunctionMap()
Returns the global function map.
static void initialize()
Static initialization, is called by the profiling manager.
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...
MLProfilingTimeType allConsumedTime() const
Returns the sum of the consumed time of all functions in seconds.
Definition: mlTimeProfile.h:93
static void disableTracing()
Disables call tracing.
MLTimeProfile(const MLMetaProfile *metaProfile=nullptr)
const MLMetaProfile * metaProfile() const
Returns the meta profile. Don't assume that the pointer is valid!
static void setReduceCallGraph(bool reduceCallGraph)
Sets if the call graph should be reduced.
static MLProfilingTimeType globalElapsedTime()
Returns the global elapsed time in seconds.
static MLCallGraphFunctionPtr mainFunction
static void enableTracing(const char *filename)
Enable call tracing and set the trace filname.
MLProfilingTimeType allElapsedTime() const
Returns the sum of the elapsed time of all functions in seconds.
Definition: mlTimeProfile.h:90
double MLProfilingTimeType
Definition: mlCallGraph.h:67
std::shared_ptr< MLCallGraphFunction > MLCallGraphFunctionPtr
Definition: mlCallGraph.h:31
#define MLPROFILINGMANAGER_EXPORT
boost::unordered_map< MLCallGraphFunction *, MLCallGraphFunctionPtr > MLCallGraphFunctionMap
Definition: mlTimeProfile.h:34
std::vector< MLCallGraphFunctionPtr > MLCallGraphFunctionList
Definition: mlTimeProfile.h:35
boost::unordered_map< MLGlobalFunctionKey, MLCallGraphFunctionPtr > MLCallGraphGlobalFunctionMap
Definition: mlTimeProfile.h:36
std::vector< MLCallGraphNode * > MLCallGraphNodesVector
Definition: mlTimeProfile.h:33
std::shared_ptr< MLCallGraphFunction > MLCallGraphFunctionPtr
Definition: mlTimeProfile.h:29
Time is stored in seconds.
Definition: mlCallGraph.h:71