MeVisLab Toolbox Reference
mlCallGraph.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// Tree structure for holding timers.
14
15#ifndef ML_CALL_GRAPH_H
16#define ML_CALL_GRAPH_H
17
19#include "mlTimer.h"
20
21#include <map>
22#include <unordered_map>
23#include <string>
24#include <vector>
25#include <boost/unordered_map.hpp>
26
27#include <iosfwd>
28
29#include <memory>
30
32class MLTimeProfile;
33
34typedef std::shared_ptr<MLCallGraphFunction> MLCallGraphFunctionPtr;
35
37{
38public:
39 std::string function;
40 std::string file;
41 int line;
42
44 inline MLGlobalFunctionKey() : line(0) {}
45
47 inline MLGlobalFunctionKey(const std::string& function_, const std::string& file_, int line_)
48 {
49 function = function_;
50 file = file_;
51 line = line_;
52 }
53
54 inline bool operator==(const MLGlobalFunctionKey& other) const {
55 return (line == other.line) && (function == other.function) && (file == other.file);
56 }
57};
58
60inline size_t hash_value(MLGlobalFunctionKey const& key)
61{
62 size_t hash = boost::hash_value(key.function);
63 boost::hash_combine(hash, key.file);
64 boost::hash_combine(hash, key.line);
65 return hash;
66}
67
68typedef double MLProfilingTimeType;
69
93
94
96{
97public:
98 typedef std::unordered_map<MLCallGraphFunction*, MLCallGraphFunction*> CallGraphCopyFunctionMap;
99
100public:
101 MLCallGraphNode(MLTimeProfile* timeProfile = nullptr,
102 MLCallGraphNode* parent = nullptr,
103 void* userData = nullptr,
104 const MLCallGraphFunctionPtr& callGraphFunction = MLCallGraphFunctionPtr(),
105 const std::string& nodeInfo = "");
106
108
109 MLCallGraphNode* createChild(MLTimeProfile* timeProfile, const MLCallGraphFunctionPtr& function, void* userData, const std::string& nodeInfo);
110
111 const MLTimer& timer() const { return _timer; }
112 const MLTimeProfile* timeProfile() const { return _timeProfile; }
113 const MLCallGraphFunctionPtr& function() const { return _function; }
115 const std::vector<MLCallGraphNode*>& children() const { return _children; }
116 MLCallGraphNode* parent() const { return _parent; }
117
120 inline bool isRecursive(const MLTimeProfile* timeProfile) const;
121
123 inline bool isRecursive() const;
124
125 void print(std::ostream& out, const std::string& indent = "") const;
126
128 void printTrace(std::ostream& out) const;
129
134
137 {
138 return _statistics;
139 }
140
142 void* userData() const { return _userData; }
143
145 const std::string& nodeInfo() const { return _nodeInfo; }
146
148 void clearUserData() { _userData = nullptr; }
149
150
152
153private:
154 MLTimer _timer;
155 MLCallGraphFunctionPtr _function;
156 MLCallGraphNode* _parent;
157 std::vector<MLCallGraphNode*> _children;
158 MLTimeProfile* _timeProfile;
159 MLTimeStatistics _statistics;
160 void* _userData;
161 std::string _nodeInfo;
162
163 friend class MLProfilingManager;
164 friend class MLTimeProfile;
165};
166
168{
169public:
170 typedef boost::unordered_map<MLCallGraphFunction*, MLTimeStatistics> CallGraphFunctionMap;
171 typedef boost::unordered_map<const MLTimeProfile*, MLTimeStatistics> TimeStatisticsMap;
172
173public:
174 MLCallGraphFunction(const std::string& name, const std::string& filename,
175 int linenumber, int type);
178
180 int type() const { return _type; }
182 const std::string& name() const { return _name; }
184 const std::string& filename() const { return _filename; }
186 int linenumber() const { return _linenumber; }
187
189
191 {
192 return _copiedFrom;
193 }
195 {
196 return _copiedTo;
197 }
204 const std::vector<MLCallGraphNode*>& callGraphNodes() const { return _callGraphNodes; }
206 std::vector<MLCallGraphNode*>& callGraphNodes() { return _callGraphNodes; }
207
210
212 const CallGraphFunctionMap& callers() const { return _callers; }
213
215 const CallGraphFunctionMap& callees() const { return _callees; }
216
219
222
225 MLTimeStatistics& statistics() { return _statistics; }
226 const MLTimeStatistics& statistics() const { return _statistics; }
228
231 MLTimeStatistics& statistics(const MLTimeProfile* timeProfile) { return _statisticsByTimeProfile[timeProfile]; }
232 const MLTimeStatistics& statistics(const MLTimeProfile* timeProfile) const { return const_cast< TimeStatisticsMap& >(_statisticsByTimeProfile)[timeProfile]; }
234
235 bool hasStatisticsForTimeProfile(const MLTimeProfile* timeProfile) const;
236
237 const TimeStatisticsMap& statisticsByTimeProfile() const { return _statisticsByTimeProfile; }
238
239
240private:
241 void removeFunctionFromMap(CallGraphFunctionMap& map, MLCallGraphFunction* function);
242
243private:
244 std::vector<MLCallGraphNode*> _callGraphNodes;
245 std::string _name;
246 std::string _filename;
247 int _linenumber;
248 int _type;
249
250 MLTimeStatistics _statistics;
251 TimeStatisticsMap _statisticsByTimeProfile;
252
253 CallGraphFunctionMap _callers;
254 CallGraphFunctionMap _callees;
255 MLCallGraphFunction* _copiedFrom{nullptr};
256 MLCallGraphFunction* _copiedTo{nullptr};
257
258 friend class MLCallGraphNode;
259 friend class MLTimeProfile;
260};
261
262
264{
265 MLCallGraphNode* next = _parent;
266 while (next) {
267 if (_function == next->_function) {
268 return true;
269 }
270 next = next->_parent;
271 }
272 return false;
273}
274
275
276
277inline bool MLCallGraphNode::isRecursive(const MLTimeProfile* timeProfile_) const
278{
279 MLCallGraphNode* next = _parent;
280 while (next) {
281 if ( (_function == next->_function) && (timeProfile_ == next->_timeProfile) ) {
282 return true;
283 }
284 next = next->_parent;
285 }
286 return false;
287}
288
289#endif // _ML_TIMER_TREE_H_
void addCallGraphNode(MLCallGraphNode *node)
Returns the line number of the function.
const std::string & name() const
Returns the name of the function.
int type() const
Returns the user ID.
const TimeStatisticsMap & statisticsByTimeProfile() const
MLCallGraphFunction * copiedFrom() const
const MLTimeStatistics * statisticsByCallee(MLCallGraphFunction *callee)
Returns the statistics of the given callee.
const MLTimeStatistics & statistics() const
const MLTimeStatistics * statisticsByCaller(MLCallGraphFunction *caller)
Returns the statistics of the given caller.
void addCaller(MLCallGraphFunction *function, MLProfilingTimeType elapsed, MLProfilingTimeType consumed)
Adds a caller function. If it is already added, then only the statistics are updated.
void removeCallGraphNode(MLCallGraphNode *node)
Removes a call graph node that represents a function call to this function.
MLCallGraphFunction * copiedTo() const
MLTimeStatistics & statistics(const MLTimeProfile *timeProfile)
@
const std::string & filename() const
Returns the filename of the function.
std::vector< MLCallGraphNode * > & callGraphNodes()
Returns all call graph nodes.
int linenumber() const
Returns the line number of the function.
MLTimeStatistics & statistics()
Returns the time statistics.
const CallGraphFunctionMap & callees() const
Returns the IDs of the functions that are called by this function.
MLCallGraphFunction(const std::string &name, const std::string &filename, int linenumber, int type)
const std::vector< MLCallGraphNode * > & callGraphNodes() const
Returns all call graph nodes.
const CallGraphFunctionMap & callers() const
Returns the IDs of the functions that called this function.
boost::unordered_map< const MLTimeProfile *, MLTimeStatistics > TimeStatisticsMap
bool hasStatisticsForTimeProfile(const MLTimeProfile *timeProfile) const
@
MLCallGraphFunction * copyWithStatistics() const
MLCallGraphFunction * cachedCopy(MLCallGraphNode::CallGraphCopyFunctionMap *copyFunctionMap)
boost::unordered_map< MLCallGraphFunction *, MLTimeStatistics > CallGraphFunctionMap
const MLTimeStatistics & statistics(const MLTimeProfile *timeProfile) const
MLCallGraphNode * createChild(MLTimeProfile *timeProfile, const MLCallGraphFunctionPtr &function, void *userData, const std::string &nodeInfo)
void printTrace(std::ostream &out) const
Prints the current call graph node for tracing.
const std::vector< MLCallGraphNode * > & children() const
const MLTimeStatistics & statistics() const
Returns the time statistics of the call graph node.
const MLTimer & timer() const
bool isRecursive() const
Returns true if the node is a recursive call of its function.
MLCallGraphNode(MLTimeProfile *timeProfile=nullptr, MLCallGraphNode *parent=nullptr, void *userData=nullptr, const MLCallGraphFunctionPtr &callGraphFunction=MLCallGraphFunctionPtr(), const std::string &nodeInfo="")
void removeFromParent()
Removes the node from its parent.
const std::string & nodeInfo() const
Returns the node information.
void print(std::ostream &out, const std::string &indent="") const
MLCallGraphNode * parent() const
void clearFunctionRecursive()
const MLCallGraphFunctionPtr & function() const
MLCallGraphNode * deepCopy(CallGraphCopyFunctionMap *copyFunctionMap) const
const MLTimeProfile * timeProfile() const
void clearUserData()
Clears the user data.
std::unordered_map< MLCallGraphFunction *, MLCallGraphFunction * > CallGraphCopyFunctionMap
Definition mlCallGraph.h:98
void * userData() const
Returns the user data.
std::string function
Definition mlCallGraph.h:39
MLGlobalFunctionKey()
Initializes line to 0.
Definition mlCallGraph.h:44
MLGlobalFunctionKey(const std::string &function_, const std::string &file_, int line_)
Initializes internal variables with the given ones.
Definition mlCallGraph.h:47
bool operator==(const MLGlobalFunctionKey &other) const
Definition mlCallGraph.h:54
double MLProfilingTimeType
Definition mlCallGraph.h:68
size_t hash_value(MLGlobalFunctionKey const &key)
Calculates the hash key from last string segment.
Definition mlCallGraph.h:60
std::shared_ptr< MLCallGraphFunction > MLCallGraphFunctionPtr
Definition mlCallGraph.h:34
#define MLPROFILINGMANAGER_EXPORT
MLProfilingTimeType minimum
Definition mlCallGraph.h:84
MLProfilingTimeType maximum
Definition mlCallGraph.h:85
MLProfilingTimeType total
Definition mlCallGraph.h:86
void add(MLProfilingTimeType time)
Values & operator+=(const Values &other)
Time is stored in seconds.
Definition mlCallGraph.h:72
unsigned int count
Definition mlCallGraph.h:91
MLTimeStatistics & operator+=(const MLTimeStatistics &other)
MLTimeStatistics copy() const