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 
31 class MLTimeProfile;
32 
33 typedef std::shared_ptr<MLCallGraphFunction> MLCallGraphFunctionPtr;
34 
36 {
37 public:
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  {
48  function = function_;
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 
59 inline 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 
67 typedef double MLProfilingTimeType;
68 
71 {
72  MLTimeStatistics() { reset(); }
73  void reset();
75 
77  {
78  Values() { reset(); }
79  Values& operator+=(const Values& other);
81  void reset();
85  };
86 
89  unsigned int count;
90 };
91 
92 
94 {
95 public:
96  MLCallGraphNode(MLTimeProfile* timeProfile = nullptr,
97  MLCallGraphNode* parent = nullptr,
98  void* userData = nullptr,
99  const MLCallGraphFunctionPtr& callGraphFunction = MLCallGraphFunctionPtr(),
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 
142 private:
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 {
158 public:
159  typedef boost::unordered_map<MLCallGraphFunction*, MLTimeStatistics> CallGraphFunctionMap;
160  typedef boost::unordered_map<const MLTimeProfile*, MLTimeStatistics> TimeStatisticsMap;
161 
162 public:
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 
223 private:
224  bool removeFunctionFromMap(CallGraphFunctionMap& map, MLCallGraphFunction* function);
225 
226 private:
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 
244 inline bool MLCallGraphNode::isRecursive() const
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 
258 inline bool MLCallGraphNode::isRecursive(const MLTimeProfile* timeProfile_) const
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_
const TimeStatisticsMap & statisticsByTimeProfile() const
Definition: mlCallGraph.h:220
void addCallGraphNode(MLCallGraphNode *node)
Adds a call graph node that represents a function call to this function.
const std::string & filename() const
Returns the filename of the function.
Definition: mlCallGraph.h:172
int type() const
Returns the user id.
Definition: mlCallGraph.h:168
void addCaller(MLCallGraphFunction *function, MLProfilingTimeType elapsed, MLProfilingTimeType consumed)
Adds a caller function. If it is already added, then only the statistics are updated.
MLTimeStatistics & statistics()
Returns the time statistics.
Definition: mlCallGraph.h:208
void removeCallGraphNode(MLCallGraphNode *node)
Removes a call graph node that represents a function call to this function.
MLCallGraphFunction * getCallee(MLCallGraphFunction *callee)
Returns the callee object for the given function.
std::vector< MLCallGraphNode * > & callGraphNodes()
Returns all call graph nodes.
Definition: mlCallGraph.h:183
const MLTimeStatistics & statistics() const
Definition: mlCallGraph.h:209
MLTimeStatistics & statistics(const MLTimeProfile *timeProfile)
@
Definition: mlCallGraph.h:214
const CallGraphFunctionMap & callers() const
Returns the ids of the functions that called this function.
Definition: mlCallGraph.h:189
int linenumber() const
Returns the line number of the function.
Definition: mlCallGraph.h:174
const MLTimeStatistics & statistics(const MLTimeProfile *timeProfile) const
Definition: mlCallGraph.h:215
MLCallGraphFunction(const std::string &name, const std::string &filename, int linenumber, int type)
const std::string & name() const
Returns the name of the function.
Definition: mlCallGraph.h:170
const MLTimeStatistics * statisticsByCallee(MLCallGraphFunction *callee)
Returns the statistics of the given callee.
const std::vector< MLCallGraphNode * > & callGraphNodes() const
Returns all call graph nodes.
Definition: mlCallGraph.h:181
const CallGraphFunctionMap & callees() const
Returns the ids of the functions that are called by this function.
Definition: mlCallGraph.h:192
MLCallGraphFunction * getCaller(MLCallGraphFunction *caller)
Returns the caller object for the given function.
boost::unordered_map< const MLTimeProfile *, MLTimeStatistics > TimeStatisticsMap
Definition: mlCallGraph.h:160
bool hasStatisticsForTimeProfile(const MLTimeProfile *timeProfile) const
@
const MLTimeStatistics * statisticsByCaller(MLCallGraphFunction *caller)
Returns the statistics of the given caller.
boost::unordered_map< MLCallGraphFunction *, MLTimeStatistics > CallGraphFunctionMap
Definition: mlCallGraph.h:159
MLCallGraphNode * createChild(MLTimeProfile *timeProfile, const MLCallGraphFunctionPtr &function, void *userData, const std::string &nodeInfo)
const MLTimeStatistics & statistics() const
Returns the time statistics of the call graph node.
Definition: mlCallGraph.h:131
void printTrace(std::ostream &out) const
Prints the current call graph node for tracing.
const std::string & nodeInfo() const
Returns the node info.
Definition: mlCallGraph.h:137
bool isRecursive() const
Returns true if the node is a recursive call of its function.
Definition: mlCallGraph.h:244
const std::vector< MLCallGraphNode * > & children() const
Definition: mlCallGraph.h:110
MLCallGraphNode(MLTimeProfile *timeProfile=nullptr, MLCallGraphNode *parent=nullptr, void *userData=nullptr, const MLCallGraphFunctionPtr &callGraphFunction=MLCallGraphFunctionPtr(), const std::string &nodeInfo="")
const MLTimer & timer() const
Definition: mlCallGraph.h:106
void removeFromParent()
Removes the node from its parent.
void print(std::ostream &out, const std::string &indent="") const
void clearFunctionRecursive()
const MLTimeProfile * timeProfile() const
Definition: mlCallGraph.h:107
MLCallGraphNode * parent() const
Definition: mlCallGraph.h:111
void clearUserData()
Clears the user data.
Definition: mlCallGraph.h:140
void * userData() const
Returns the user data.
Definition: mlCallGraph.h:134
std::string function
Definition: mlCallGraph.h:38
MLGlobalFunctionKey()
Initializes line to 0.
Definition: mlCallGraph.h:43
std::string file
Definition: mlCallGraph.h:39
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:31
#define MLPROFILINGMANAGER_EXPORT
MLProfilingTimeType minimum
Definition: mlCallGraph.h:82
Values & operator+=(const Values &other)
MLProfilingTimeType maximum
Definition: mlCallGraph.h:83
MLProfilingTimeType total
Definition: mlCallGraph.h:84
void add(MLProfilingTimeType time)
Time is stored in seconds.
Definition: mlCallGraph.h:71
unsigned int count
Definition: mlCallGraph.h:89
MLTimeStatistics & operator+=(const MLTimeStatistics &other)