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 <string>
23#include <vector>
24#include <boost/unordered_map.hpp>
25
26#include <iosfwd>
27
28#include <memory>
29
31class MLTimeProfile;
32
33typedef std::shared_ptr<MLCallGraphFunction> MLCallGraphFunctionPtr;
34
36{
37public:
38 std::string function;
39 std::string file;
40 int line;
41
43 inline MLGlobalFunctionKey() : line(0) {}
44
46 inline MLGlobalFunctionKey(const std::string& function_, const std::string& file_, int line_)
47 {
49 file = file_;
50 line = line_;
51 }
52
53 inline bool operator==(const MLGlobalFunctionKey& other) const {
54 return (line == other.line) && (function == other.function) && (file == other.file);
55 }
56};
57
59inline size_t hash_value(MLGlobalFunctionKey const& key)
60{
61 size_t hash = boost::hash_value(key.function);
62 boost::hash_combine(hash, key.file);
63 boost::hash_combine(hash, key.line);
64 return hash;
65}
66
67typedef double MLProfilingTimeType;
68
91
92
94{
95public:
96 MLCallGraphNode(MLTimeProfile* timeProfile = nullptr,
97 MLCallGraphNode* parent = nullptr,
98 void* userData = nullptr,
100 const std::string& nodeInfo = "");
101
103
104 MLCallGraphNode* createChild(MLTimeProfile* timeProfile, const MLCallGraphFunctionPtr& function, void* userData, const std::string& nodeInfo);
105
106 const MLTimer& timer() const { return _timer; }
107 const MLTimeProfile* timeProfile() const { return _timeProfile; }
108 const MLCallGraphFunctionPtr& function() const { return _function; }
110 const std::vector<MLCallGraphNode*>& children() const { return _children; }
111 MLCallGraphNode* parent() const { return _parent; }
112
115 inline bool isRecursive(const MLTimeProfile* timeProfile) const;
116
118 inline bool isRecursive() const;
119
120 void print(std::ostream& out, const std::string& indent = "") const;
121
123 void printTrace(std::ostream& out) const;
124
129
131 const MLTimeStatistics& statistics() const { return _statistics; }
132
134 void* userData() const { return _userData; }
135
137 const std::string& nodeInfo() const { return _nodeInfo; }
138
140 void clearUserData() { _userData = nullptr; }
141
142private:
143 MLTimer _timer;
144 MLCallGraphFunctionPtr _function;
145 MLCallGraphNode* _parent;
146 std::vector<MLCallGraphNode*> _children;
147 MLTimeProfile* _timeProfile;
148 MLTimeStatistics _statistics;
149 void* _userData;
150 std::string _nodeInfo;
151
152 friend class MLProfilingManager;
153 friend class MLTimeProfile;
154};
155
157{
158public:
159 typedef boost::unordered_map<MLCallGraphFunction*, MLTimeStatistics> CallGraphFunctionMap;
160 typedef boost::unordered_map<const MLTimeProfile*, MLTimeStatistics> TimeStatisticsMap;
161
162public:
163 MLCallGraphFunction(const std::string& name, const std::string& filename,
164 int linenumber, int type);
166
168 int type() const { return _type; }
170 const std::string& name() const { return _name; }
172 const std::string& filename() const { return _filename; }
174 int linenumber() const { return _linenumber; }
175
181 const std::vector<MLCallGraphNode*>& callGraphNodes() const { return _callGraphNodes; }
183 std::vector<MLCallGraphNode*>& callGraphNodes() { return _callGraphNodes; }
184
187
189 const CallGraphFunctionMap& callers() const { return _callers; }
190
192 const CallGraphFunctionMap& callees() const { return _callees; }
193
196
199
202
205
208 MLTimeStatistics& statistics() { return _statistics; }
209 const MLTimeStatistics& statistics() const { return _statistics; }
211
214 MLTimeStatistics& statistics(const MLTimeProfile* timeProfile) { return _statisticsByTimeProfile[timeProfile]; }
215 const MLTimeStatistics& statistics(const MLTimeProfile* timeProfile) const { return const_cast< TimeStatisticsMap& >(_statisticsByTimeProfile)[timeProfile]; }
217
218 bool hasStatisticsForTimeProfile(const MLTimeProfile* timeProfile) const;
219
220 const TimeStatisticsMap& statisticsByTimeProfile() const { return _statisticsByTimeProfile; }
221
222
223private:
224 bool removeFunctionFromMap(CallGraphFunctionMap& map, MLCallGraphFunction* function);
225
226private:
227 std::vector<MLCallGraphNode*> _callGraphNodes;
228 std::string _name;
229 std::string _filename;
230 int _linenumber;
231 int _type;
232
233 MLTimeStatistics _statistics;
234 TimeStatisticsMap _statisticsByTimeProfile;
235
236 CallGraphFunctionMap _callers;
237 CallGraphFunctionMap _callees;
238
239 friend class MLCallGraphNode;
240 friend class MLTimeProfile;
241};
242
243
245{
246 MLCallGraphNode* next = _parent;
247 while (next) {
248 if (_function == next->_function) {
249 return true;
250 }
251 next = next->_parent;
252 }
253 return false;
254}
255
256
257
259{
260 MLCallGraphNode* next = _parent;
261 while (next) {
262 if ( (_function == next->_function) && (timeProfile_ == next->_timeProfile) ) {
263 return true;
264 }
265 next = next->_parent;
266 }
267 return false;
268}
269
270#endif // _ML_TIMER_TREE_H_
void addCallGraphNode(MLCallGraphNode *node)
Adds a call graph node that represents a function call to this function.
const std::string & name() const
Returns the name of the function.
int type() const
Returns the user id.
const TimeStatisticsMap & statisticsByTimeProfile() 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.
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.
MLCallGraphFunction * getCaller(MLCallGraphFunction *caller)
Returns the caller object for the given function.
int linenumber() const
Returns the line number of the function.
MLCallGraphFunction * getCallee(MLCallGraphFunction *callee)
Returns the callee object for the given 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
@
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 info.
void print(std::ostream &out, const std::string &indent="") const
MLCallGraphNode * parent() const
void clearFunctionRecursive()
const MLCallGraphFunctionPtr & function() const
const MLTimeProfile * timeProfile() const
void clearUserData()
Clears the user data.
void * userData() const
Returns the user data.
std::string function
Definition mlCallGraph.h:38
MLGlobalFunctionKey()
Initializes line to 0.
Definition mlCallGraph.h:43
MLGlobalFunctionKey(const std::string &function_, const std::string &file_, int line_)
Initializes _id with id and _fileHandle with fileName.
Definition mlCallGraph.h:46
bool operator==(const MLGlobalFunctionKey &other) const
Definition mlCallGraph.h:53
double MLProfilingTimeType
Definition mlCallGraph.h:67
size_t hash_value(MLGlobalFunctionKey const &key)
Calculates the has key from last string segment.
Definition mlCallGraph.h:59
std::shared_ptr< MLCallGraphFunction > MLCallGraphFunctionPtr
Definition mlCallGraph.h:33
#define MLPROFILINGMANAGER_EXPORT
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
MLProfilingTimeType minimum
Definition mlCallGraph.h:82
MLProfilingTimeType maximum
Definition mlCallGraph.h:83
MLProfilingTimeType total
Definition mlCallGraph.h:84
void add(MLProfilingTimeType time)
Values & operator+=(const Values &other)
Time is stored in seconds.
Definition mlCallGraph.h:71
unsigned int count
Definition mlCallGraph.h:89
MLTimeStatistics & operator+=(const MLTimeStatistics &other)