MeVisLab Toolbox Reference
Loading...
Searching...
No Matches
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
45 : line(0)
46 {}
47
49 inline MLGlobalFunctionKey(const std::string& function_, const std::string& file_, int line_)
50 {
52 file = file_;
53 line = line_;
54 }
55
56 inline bool operator==(const MLGlobalFunctionKey& other) const
57 {
58 return (line == other.line) && (function == other.function) && (file == other.file);
59 }
60};
61
63inline size_t hash_value(MLGlobalFunctionKey const& key)
64{
65 size_t hash = boost::hash_value(key.function);
66 boost::hash_combine(hash, key.file);
67 boost::hash_combine(hash, key.line);
68 return hash;
69}
70
71typedef double MLProfilingTimeType;
72
102
103
105{
106public:
107 typedef std::unordered_map<MLCallGraphFunction*, MLCallGraphFunction*> CallGraphCopyFunctionMap;
108
109public:
110 MLCallGraphNode(MLTimeProfile* timeProfile = nullptr, MLCallGraphNode* parent = nullptr, void* userData = nullptr,
112 const std::string& nodeInfo = "");
113
115
116 MLCallGraphNode* createChild(MLTimeProfile* timeProfile, const MLCallGraphFunctionPtr& function, void* userData,
117 const std::string& nodeInfo);
118
119 const MLTimer& timer() const
120 {
121 return _timer;
122 }
124 {
125 return _timeProfile;
126 }
128 {
129 return _function;
130 }
132 const std::vector<MLCallGraphNode*>& children() const
133 {
134 return _children;
135 }
137 {
138 return _parent;
139 }
140
143 inline bool isRecursive(const MLTimeProfile* timeProfile) const;
144
146 inline bool isRecursive() const;
147
148 void print(std::ostream& out, const std::string& indent = "") const;
149
151 void printTrace(std::ostream& out) const;
152
157
160 {
161 return _statistics;
162 }
163
165 void* userData() const
166 {
167 return _userData;
168 }
169
171 const std::string& nodeInfo() const
172 {
173 return _nodeInfo;
174 }
175
178 {
179 _userData = nullptr;
180 }
181
182
184
185private:
186 MLTimer _timer;
187 MLCallGraphFunctionPtr _function;
188 MLCallGraphNode* _parent;
189 std::vector<MLCallGraphNode*> _children;
190 MLTimeProfile* _timeProfile;
191 MLTimeStatistics _statistics;
192 void* _userData;
193 std::string _nodeInfo;
194
195 friend class MLProfilingManager;
196 friend class MLTimeProfile;
197};
198
200{
201public:
202 typedef boost::unordered_map<MLCallGraphFunction*, MLTimeStatistics> CallGraphFunctionMap;
203 typedef boost::unordered_map<const MLTimeProfile*, MLTimeStatistics> TimeStatisticsMap;
204
205public:
206 MLCallGraphFunction(const std::string& name, const std::string& filename, int linenumber, int type);
209
211 int type() const
212 {
213 return _type;
214 }
216 const std::string& name() const
217 {
218 return _name;
219 }
221 const std::string& filename() const
222 {
223 return _filename;
224 }
226 int linenumber() const
227 {
228 return _linenumber;
229 }
230
232
234 {
235 return _copiedFrom;
236 }
238 {
239 return _copiedTo;
240 }
247 const std::vector<MLCallGraphNode*>& callGraphNodes() const
248 {
249 return _callGraphNodes;
250 }
252 std::vector<MLCallGraphNode*>& callGraphNodes()
253 {
254 return _callGraphNodes;
255 }
256
259
262 {
263 return _callers;
264 }
265
268 {
269 return _callees;
270 }
271
274
277
281 {
282 return _statistics;
283 }
285 {
286 return _statistics;
287 }
289
293 {
294 return _statisticsByTimeProfile[timeProfile];
295 }
296 const MLTimeStatistics& statistics(const MLTimeProfile* timeProfile) const
297 {
298 return const_cast<TimeStatisticsMap&>(_statisticsByTimeProfile)[timeProfile];
299 }
301
302 bool hasStatisticsForTimeProfile(const MLTimeProfile* timeProfile) const;
303
305 {
306 return _statisticsByTimeProfile;
307 }
308
309
310private:
311 void removeFunctionFromMap(CallGraphFunctionMap& map, MLCallGraphFunction* function);
312
313private:
314 std::vector<MLCallGraphNode*> _callGraphNodes;
315 std::string _name;
316 std::string _filename;
317 int _linenumber;
318 int _type;
319
320 MLTimeStatistics _statistics;
321 TimeStatisticsMap _statisticsByTimeProfile;
322
323 CallGraphFunctionMap _callers;
324 CallGraphFunctionMap _callees;
325 MLCallGraphFunction* _copiedFrom{nullptr};
326 MLCallGraphFunction* _copiedTo{nullptr};
327
328 friend class MLCallGraphNode;
329 friend class MLTimeProfile;
330};
331
332
334{
335 MLCallGraphNode* next = _parent;
336 while (next) {
337 if (_function == next->_function) {
338 return true;
339 }
340 next = next->_parent;
341 }
342 return false;
343}
344
345
347{
348 MLCallGraphNode* next = _parent;
349 while (next) {
350 if ((_function == next->_function) && (timeProfile_ == next->_timeProfile)) {
351 return true;
352 }
353 next = next->_parent;
354 }
355 return false;
356}
357
358#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
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:49
bool operator==(const MLGlobalFunctionKey &other) const
Definition mlCallGraph.h:56
double MLProfilingTimeType
Definition mlCallGraph.h:71
size_t hash_value(MLGlobalFunctionKey const &key)
Calculates the hash key from last string segment.
Definition mlCallGraph.h:63
std::shared_ptr< MLCallGraphFunction > MLCallGraphFunctionPtr
Definition mlCallGraph.h:34
#define MLPROFILINGMANAGER_EXPORT
Target mlrange_cast(Source arg)
Generic version of checked ML casts.
MLProfilingTimeType minimum
Definition mlCallGraph.h:93
MLProfilingTimeType maximum
Definition mlCallGraph.h:94
MLProfilingTimeType total
Definition mlCallGraph.h:95
void add(MLProfilingTimeType time)
Values & operator+=(const Values &other)
Time is stored in seconds.
Definition mlCallGraph.h:75
unsigned int count
MLTimeStatistics & operator+=(const MLTimeStatistics &other)
MLTimeStatistics copy() const