HPCToolkit
CCT-TreeIterator.hpp
Go to the documentation of this file.
1 // -*-Mode: C++;-*-
2 
3 // * BeginRiceCopyright *****************************************************
4 //
5 // $HeadURL$
6 // $Id$
7 //
8 // --------------------------------------------------------------------------
9 // Part of HPCToolkit (hpctoolkit.org)
10 //
11 // Information about sources of support for research and development of
12 // HPCToolkit is at 'hpctoolkit.org' and in 'README.Acknowledgments'.
13 // --------------------------------------------------------------------------
14 //
15 // Copyright ((c)) 2002-2019, Rice University
16 // All rights reserved.
17 //
18 // Redistribution and use in source and binary forms, with or without
19 // modification, are permitted provided that the following conditions are
20 // met:
21 //
22 // * Redistributions of source code must retain the above copyright
23 // notice, this list of conditions and the following disclaimer.
24 //
25 // * Redistributions in binary form must reproduce the above copyright
26 // notice, this list of conditions and the following disclaimer in the
27 // documentation and/or other materials provided with the distribution.
28 //
29 // * Neither the name of Rice University (RICE) nor the names of its
30 // contributors may be used to endorse or promote products derived from
31 // this software without specific prior written permission.
32 //
33 // This software is provided by RICE and contributors "as is" and any
34 // express or implied warranties, including, but not limited to, the
35 // implied warranties of merchantability and fitness for a particular
36 // purpose are disclaimed. In no event shall RICE or contributors be
37 // liable for any direct, indirect, incidental, special, exemplary, or
38 // consequential damages (including, but not limited to, procurement of
39 // substitute goods or services; loss of use, data, or profits; or
40 // business interruption) however caused and on any theory of liability,
41 // whether in contract, strict liability, or tort (including negligence
42 // or otherwise) arising in any way out of the use of this software, even
43 // if advised of the possibility of such damage.
44 //
45 // ******************************************************* EndRiceCopyright *
46 
47 //***************************************************************************
48 //
49 // File:
50 // $HeadURL$
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // [The set of functions, macros, etc. defined in the file]
57 //
58 //***************************************************************************
59 
60 #ifndef prof_Prof_CCT_TreeIterator_hpp
61 #define prof_Prof_CCT_TreeIterator_hpp
62 
63 //************************* System Include Files ****************************
64 
65 #include <iostream>
66 
67 //*************************** User Include Files ****************************
68 
69 #include <include/uint.h>
70 
71 // NOTE: included by CCT-Tree.hpp
72 //#include "CCT-Tree.hpp"
73 
75 #include <lib/support/WordSet.hpp>
76 
77 //*************************** Forward Declarations **************************
78 
79 //***************************************************************************
80 
81 namespace Prof {
82 
83 namespace CCT {
84 
85 //***************************************************************************
86 // ANodeFilter
87 //***************************************************************************
88 
89 typedef bool (*ANodeFilterFct)(const ANode& x, long addArg);
90 
91 class ANodeFilter {
92 public:
93  ANodeFilter(ANodeFilterFct f, const char* n, long a)
94  : fct(f), m_name(n), arg(a)
95  { }
96 
97  virtual ~ANodeFilter()
98  { }
99 
100  bool
101  apply(const ANode& s) const
102  { return fct(s, arg); }
103 
104  const char*
105  name() const
106  { return m_name; }
107 
108 private:
110  const char* m_name;
111  long arg;
112 };
113 
114 // HasANodeTy(s,tp) == ((tp == ANY) || (s.Type() == tp));
115 extern bool HasANodeTy(const ANode &sinfo, long type);
116 
117 // ANodeTyFilter[tp].Apply(s) == HasANodeTy(s,tp)
119 
120 
121 //*****************************************************************************
122 // ANodeChildIterator: Iterates over all children of a ANode that pass
123 // the given filter; a NULL filter matches all children. No
124 // particular order is guaranteed.
125 //*****************************************************************************
126 
127 // NOTE: To support insertion and deletion of nodes during the
128 // lifetime of a ANodeChildIterator using link() and unlink(), use
129 // *reverse* iteration.
130 // link() : links newNode after the iteration start point:
131 // newNode->linkAfter(newParent->LastChild())
132 // unlink() : won't disturb the iteration end point:
133 // parent->FirstChild()
134 // until the last node is removed
135 
137 public:
138 
139  // If filter == NULL enumerate all entries; otherwise, only entries
140  // with filter->fct(e) == true
141  ANodeChildIterator(const ANode* root, const ANodeFilter* filter = NULL)
142  : NonUniformDegreeTreeNodeChildIterator(root, /*forward*/false),
143  m_filter(filter)
144  { }
145 
146 
147  ANode*
148  current() const
149  { return static_cast<ANode*>(Current()); }
150 
151 
152  virtual NonUniformDegreeTreeNode*
153  Current() const
154  {
155  NonUniformDegreeTreeNode* x_base = NULL;
156  while ( (x_base = NonUniformDegreeTreeNodeChildIterator::Current()) ) {
157  ANode* x = static_cast<ANode*>(x_base);
158  if ((m_filter == NULL) || m_filter->apply(*x)) {
159  break;
160  }
161  const_cast<ANodeChildIterator*>(this)->operator++();
162  }
163  return x_base;
164  }
165 
166 private:
168 };
169 
170 
171 //***************************************************************************
172 // ANodeIterator: Iterates over all ANodes in the tree
173 // rooted at a given ANode that pass the given filter; a NULL
174 // filter matches all nodes. No particular order is guaranteed,
175 // unless stated explicitly, i.e. the default value for 'torder' is
176 // changed.
177 //***************************************************************************
178 
180 public:
181  // If filter == NULL enumerate all entries; otherwise, only entries
182  // with filter->fct(e) == true
183  ANodeIterator(const ANode* root,
184  const ANodeFilter* filter = NULL,
185  bool leavesOnly = false,
186  TraversalOrder torder = PreOrder)
187  : NonUniformDegreeTreeIterator(root, torder,
188  ((leavesOnly)
191  m_filter(filter)
192  { }
193 
194 
195  ANode*
196  current() const
197  { return static_cast<ANode*>(Current()); }
198 
199 
200  virtual NonUniformDegreeTreeNode*
201  Current() const
202  {
203  NonUniformDegreeTreeNode* x_base = NULL;
204  while ( (x_base = NonUniformDegreeTreeIterator::Current()) ) {
205  ANode* x = static_cast<ANode*>(x_base);
206  if ((m_filter == NULL) || m_filter->apply(*x)) {
207  break;
208  }
209  const_cast<ANodeIterator*>(this)->operator++();
210  }
211  return x_base;
212  }
213 
214 private:
216 };
217 
218 
219 //*****************************************************************************
220 // ANodeSortedIterator
221 //
222 // ANodeSortedChildIterator
223 //*****************************************************************************
224 
226 public:
227  // return -1, 0, or 1 for x < y, x = y, or x > y, respectively
228  typedef int (*cmp_fptr_t) (const void* x, const void* y);
229 
230  static int cmpByName(const void* x, const void* y);
231  static int cmpByLine(const void* x, const void* y);
232 
233  // cmpByStructureInfo: Designed to produce deterministic sorts for a
234  // "structured CCT" across multiple runs and multiple processes.
235  static int cmpByStructureInfo(const void* x, const void* y);
236 
237  // cmpByDynInfo: Designed to produce deterministic sorts for a
238  // "non-structured CCT" or "partially structured CCT" across
239  // multiple runs and multiple processes.
240  static int cmpByDynInfo(const void* x, const void* y);
241 
242 public:
244  cmp_fptr_t compare_fn,
245  const ANodeFilter* filterFunc = NULL,
246  bool leavesOnly = true);
247 
249  {
250  delete ptrSetIt;
251  }
252 
253  ANode*
254  current() const
255  {
256  ANode* x = NULL;
257  if (ptrSetIt->Current()) {
258  x = reinterpret_cast<ANode*>(*ptrSetIt->Current());
259  }
260  return x;
261  }
262 
263  void
265  {
266  (*ptrSetIt)++;
267  }
268 
269  void
271  {
272  ptrSetIt->Reset();
273  }
274 
275  void
276  dumpAndReset(std::ostream &os = std::cerr);
277 
278 private:
281 };
282 
283 
285 public:
286  ANodeSortedChildIterator(const ANode* root,
288  const ANodeFilter* filterFunc = NULL);
289 
291  {
292  delete ptrSetIt;
293  }
294 
295  ANode*
296  current() const
297  {
298  ANode* x = NULL;
299  if (ptrSetIt->Current()) {
300  x = reinterpret_cast<ANode*>(*ptrSetIt->Current());
301  }
302  return x;
303  }
304 
305  void
307  { (*ptrSetIt)++; }
308 
309  void
311  {
312  ptrSetIt->Reset();
313  }
314 
315  void
316  dumpAndReset(std::ostream &os = std::cerr);
317 
318 private:
321 };
322 
323 
324 } // namespace CCT
325 
326 } // namespace Prof
327 
328 //***************************************************************************
329 
330 #endif /* prof_Prof_CCT_TreeIterator_hpp */
const ANodeFilterFct fct
const ANodeFilter * m_filter
virtual NonUniformDegreeTreeNode * Current() const
virtual NonUniformDegreeTreeNode * Current() const
ANodeIterator(const ANode *root, const ANodeFilter *filter=NULL, bool leavesOnly=false, TraversalOrder torder=PreOrder)
cct_node_t * node
Definition: cct.c:128
int(* cmp_fptr_t)(const void *x, const void *y)
WordSetSortedIterator * ptrSetIt
bool(* ANodeFilterFct)(const ANode &x, long addArg)
#define NULL
Definition: ElfHelper.cpp:85
ANodeFilter(ANodeFilterFct f, const char *n, long a)
bool apply(const ANode &s) const
bool HasANodeTy(const ANode &node, long type)
ANodeChildIterator(const ANode *root, const ANodeFilter *filter=NULL)
<!-- ********************************************************************--> n<!-- HPCToolkit Experiment DTD --> n<!-- Version 2.1 --> n<!-- ********************************************************************--> n<!ELEMENT HPCToolkitExperiment(Header,(SecCallPathProfile|SecFlatProfile) *)> n<!ATTLIST HPCToolkitExperiment\n version CDATA #REQUIRED > n n<!-- ******************************************************************--> n n<!-- Info/NV:flexible name-value pairs:(n) ame;(t) ype;(v) alue --> n<!ELEMENT Info(NV *)> n<!ATTLIST Info\n n CDATA #IMPLIED > n<!ELEMENT NV EMPTY > n<!ATTLIST NV\n n CDATA #REQUIRED\n t CDATA #IMPLIED\n v CDATA #REQUIRED > n n<!-- ******************************************************************--> n<!-- Header --> n<!-- ******************************************************************--> n<!ELEMENT Header(Info *)> n<!ATTLIST Header\n n CDATA #REQUIRED > n n<!-- ******************************************************************--> n<!-- Section Header --> n<!-- ******************************************************************--> n<!ELEMENT SecHeader(MetricTable?, MetricDBTable?, TraceDBTable?, LoadModuleTable?, FileTable?, ProcedureTable?, Info *)> n n<!-- MetricTable:--> n<!ELEMENT MetricTable(Metric) * > n n<!-- Metric:(i) d;(n) ame --> n<!--(v) alue-type:transient type of values --> n<!--(t) ype:persistent type of metric --> n<!-- fmt:format;show;--> n<!ELEMENT Metric(MetricFormula *, Info?)> n<!ATTLIST Metric\n i CDATA #REQUIRED\n n CDATA #REQUIRED\n es CDATA #IMPLIED\n em CDATA #IMPLIED\n ep CDATA #IMPLIED\n v(raw|final|derived-incr|derived) \"raw\\ t (inclusive|exclusive|nil) \nil\\ partner CDATA #IMPLIED\ fmt CDATA #IMPLIED\ show (1|0) \1\\ show-percent (1|0) \1> n n<!-- MetricFormula represents derived metrics: (t)ype; (frm): formula --> n<!ELEMENT MetricFormula (Info?)> n<!ATTLIST MetricFormula\ t (combine|finalize) \finalize\\ i CDATA #IMPLIED\ frm CDATA #REQUIRED> n n<!-- Metric data, used in sections: (n)ame [from Metric]; (v)alue --> n<!ELEMENT M EMPTY> n<!ATTLIST M\ n CDATA #REQUIRED\ v CDATA #REQUIRED> n n<!-- MetricDBTable: --> n<!ELEMENT MetricDBTable (MetricDB)*> n n<!-- MetricDB: (i)d; (n)ame --> n<!-- (t)ype: persistent type of metric --> n<!-- db-glob: file glob describing files in metric db --> n<!-- db-id: id within metric db --> n<!-- db-num-metrics: number of metrics in db --> n<!-- db-header-sz: size (in bytes) of a db file header --> n<!ELEMENT MetricDB EMPTY> n<!ATTLIST MetricDB\ i CDATA #REQUIRED\ n CDATA #REQUIRED\ t (inclusive|exclusive|nil) \nil\\ partner CDATA #IMPLIED\ db-glob CDATA #IMPLIED\ db-id CDATA #IMPLIED\ db-num-metrics CDATA #IMPLIED\ db-header-sz CDATA #IMPLIED> n n<!-- TraceDBTable: --> n<!ELEMENT TraceDBTable (TraceDB)> n n<!-- TraceDB: (i)d --> n<!-- db-min-time: min beginning time stamp (global) --> n<!-- db-max-time: max ending time stamp (global) --> n<!ELEMENT TraceDB EMPTY> n<!ATTLIST TraceDB\ i CDATA #REQUIRED\ db-glob CDATA #IMPLIED\ db-min-time CDATA #IMPLIED\ db-max-time CDATA #IMPLIED\ db-header-sz CDATA #IMPLIED> n n<!-- LoadModuleTable assigns a short name to a load module --> n<!ELEMENT LoadModuleTable (LoadModule)*> n n<!ELEMENT LoadModule (Info?)> n<!ATTLIST LoadModule\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!-- FileTable assigns a short name to a file --> n<!ELEMENT FileTable (File)*> n n<!ELEMENT File (Info?)> n<!ATTLIST File\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!-- ProcedureTable assigns a short name to a procedure --> n<!ELEMENT ProcedureTable (Procedure)*> n n<!-- Info/NV: flexible name-value pairs: (n)ame; (t)ype; (v)alue --> n<!-- f: family of the procedure (fake, root, ...)--> n<!ELEMENT Procedure (Info?)> n<!ATTLIST Procedure\ i CDATA #REQUIRED\ n CDATA #REQUIRED\ f CDATA #IMPLIED> n n<!-- ****************************************************************** --> n<!-- Section: Call path profile --> n<!-- ****************************************************************** --> n<!ELEMENT SecCallPathProfile (SecHeader, SecCallPathProfileData)> n<!ATTLIST SecCallPathProfile\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!ELEMENT SecCallPathProfileData (PF|M)*> n<!-- Procedure frame --> n<!-- (i)d: unique identifier for cross referencing --> n<!-- (s)tatic scope id --> n<!-- (n)ame: a string or an id in ProcedureTable --> n<!-- (lm) load module: a string or an id in LoadModuleTable --> n<!-- (f)ile name: a string or an id in LoadModuleTable --> n<!-- (l)ine range: \beg-end\ (inclusive range) --> n<!-- (a)lien: whether frame is alien to enclosing P --> n<!-- (str)uct: hpcstruct node id --> n<!-- (t)ype: hpcrun node type: memory access, variable declaration, ... --> n<!-- (v)ma-range-set: \{[beg-end), [beg-end)...}\ --> n<!ELEMENT PF (PF|Pr|L|C|S|M)*> n<!ATTLIST PF\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ n CDATA #REQUIRED\ lm CDATA #IMPLIED\ f CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Procedure (static): GOAL: replace with 'P' --> n<!ELEMENT Pr (Pr|L|C|S|M)*> n<!ATTLIST Pr\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ n CDATA #REQUIRED\ lm CDATA #IMPLIED\ f CDATA #IMPLIED\ l CDATA #IMPLIED\ a (1|0) \0\\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Callsite (a special StatementRange) --> n<!ELEMENT C (PF|M)*> n<!ATTLIST C\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n n<!-- ****************************************************************** --> n<!-- Section: Flat profile --> n<!-- ****************************************************************** --> n<!ELEMENT SecFlatProfile (SecHeader, SecFlatProfileData)> n<!ATTLIST SecFlatProfile\ i CDATA #REQUIRED\ n CDATA #REQUIRED> n n<!ELEMENT SecFlatProfileData (LM|M)*> n<!-- Load module: (i)d; (n)ame; (v)ma-range-set --> n<!ELEMENT LM (F|P|M)*> n<!ATTLIST LM\ i CDATA #IMPLIED\ n CDATA #REQUIRED\ v CDATA #IMPLIED> n<!-- File --> n<!ELEMENT F (P|L|S|M)*> n<!ATTLIST F\ i CDATA #IMPLIED\ n CDATA #REQUIRED> n<!-- Procedure (Note 1) --> n<!ELEMENT P (P|A|L|S|C|M)*> n<!ATTLIST P\ i CDATA #IMPLIED\ n CDATA #REQUIRED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Alien (Note 1) --> n<!ELEMENT A (A|L|S|C|M)*> n<!ATTLIST A\ i CDATA #IMPLIED\ f CDATA #IMPLIED\ n CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Loop (Note 1,2) --> n<!ELEMENT L (A|Pr|L|S|C|M)*> n<!ATTLIST L\ i CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ f CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Statement (Note 2) --> n<!-- (it): trace record identifier --> n<!ELEMENT S (S|M)*> n<!ATTLIST S\ i CDATA #IMPLIED\ it CDATA #IMPLIED\ s CDATA #IMPLIED\ l CDATA #IMPLIED\ str CDATA #IMPLIED\ v CDATA #IMPLIED> n<!-- Note 1: Contained Cs may not contain PFs --> n<!-- Note 2: The 's' attribute is not used for flat profiles --> n
const ANodeFilter ANodeTyFilter[ANode::TyNUMBER]
const char * name() const
virtual NonUniformDegreeTreeNode * Current() const
virtual NonUniformDegreeTreeNode * Current() const