HPCToolkit
DerivedProfile.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 // DerivedProfile.h
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // See, in particular, the comments associated with 'DerivedProfile'.
57 //
58 //***************************************************************************
59 
60 #ifndef DerivedProfile_H
61 #define DerivedProfile_H
62 
63 //************************* System Include Files ****************************
64 
65 #include <vector>
66 #include <list>
67 #include <string>
68 
69 //*************************** User Include Files ****************************
70 
71 #include <include/uint.h>
72 
73 #include "PCProfile.hpp"
74 
75 #include <lib/isa/ISA.hpp>
76 
78 
79 //*************************** Forward Declarations ***************************
80 
82 
83 // Some useful containers
84 typedef std::list<DerivedProfileMetric*> DerivedProfileMetricList;
85 typedef DerivedProfileMetricList::iterator DerivedProfileMetricListIt;
86 typedef DerivedProfileMetricList::const_iterator DerivedProfileMetricListCIt;
87 
88 typedef std::vector<DerivedProfileMetric*> DerivedProfileMetricVec;
89 typedef DerivedProfileMetricVec::iterator DerivedProfileMetricVecIt;
90 typedef DerivedProfileMetricVec::const_iterator DerivedProfileMetricVecCIt;
91 
92 //****************************************************************************
93 // DerivedProfile
94 //****************************************************************************
95 
96 // 'DerivedProfile' represents metrics derived in some way from the
97 // raw profiling information in *one* 'PCProfile'. It contains a set
98 // of 'DerivedProfileMetrics' (which themselves contain a
99 // 'PCProfileMetricSet'). The derived metric information is specified
100 // and obtained by applying certain metric and pc filters to the raw
101 // profiling information. In particular, there will be one
102 // 'DerivedProfileMetric' for each 'PCProfileFilter' in the filter
103 // list. Note that a 'DerivedProfileMetric' may be an emtpy set,
104 // i.e. the corresponding filter was never satisfied.
106 {
107 public:
108  DerivedProfile();
109  DerivedProfile(const PCProfile* pcprof_,
110  const PCProfileFilterList* filtlist);
111  virtual ~DerivedProfile();
112 
113  // Create: Does not assume ownership of 'pcprof_' or 'filtlist' but
114  // does save a pointer to the former. If filtlist is NULL, an
115  // 'identity' filter is assumed.
116  void Create(const PCProfile* pcprof_, const PCProfileFilterList* filtlist);
117 
118  // GetPCProfile: Returns 'PCProfile', the underlying raw data
119  const PCProfile* GetPCProfile() const { return pcprof; }
120 
121  // Access to metrics (0 based): When a metric set is created, space
122  // for slots is only reserved. To add slots, one must use
123  // 'AddMetric' or 'SetNumMetrics'. Once slots exist, they can be
124  // randomly accessed via 'SetMetric', etc.
125  const DerivedProfileMetric* GetMetric(unsigned int i) const {
126  return metricVec[i];
127  }
128  void SetMetric(unsigned int i, const DerivedProfileMetric* m) {
129  metricVec[i] = const_cast<DerivedProfileMetric*>(m); // assume ownership
130  }
131  void AddMetric(const DerivedProfileMetric* m) {
132  metricVec.push_back(const_cast<DerivedProfileMetric*>(m));
133  }
134  unsigned int GetNumMetrics() const { return metricVec.size(); }
135  void SetNumMetrics(unsigned int sz) { metricVec.resize(sz); }
136 
137  void Dump(std::ostream& o = std::cerr);
138  void DDump();
139 
140 private:
141  // Should not be used
143  DerivedProfile& operator=(const DerivedProfile& p) { return *this; }
144 
146 
147 protected:
148 private:
150 
151  // We lie a little about this being a 'set' and use a vector for
152  // implementation. Set indexing (random access) may be convenient;
153  // and any possible resizing should be cheap b/c these sets should
154  // be relatively small.
156 };
157 
158 
159 // 'DerivedProfile_MetricIterator' iterates over all 'DerivedProfileMetric'
160 // within a 'DerivedProfile'.
162 {
163 public:
165  Reset();
166  }
168 
169  DerivedProfileMetric* Current() const { return (*it); }
170 
171  void operator++() { it++; } // prefix
172  void operator++(int) { ++it; } // postfix
173 
174  bool IsValid() const { return it != p.metricVec.end(); }
175  bool IsEmpty() const { return it == p.metricVec.end(); }
176 
177  // Reset and prepare for iteration again
178  void Reset() { it = p.metricVec.begin(); }
179 
180 private:
181  // Should not be used
185  { return *this; }
186 
187 protected:
188 private:
191 };
192 
193 
194 //****************************************************************************
195 // DerivedProfileMetric
196 //****************************************************************************
197 
198 // 'DerivedProfileMetric' represents a metric derived in some way from
199 // the raw profiling information in *one* 'PCProfile'. It contains a
200 // 'PCProfileMetricSet' pointing to one or more raw metrics within the
201 // 'PCProfile' that *additively* represent this derived metric.
203 {
204 public:
205  // One can create an empty object or quickly create an object
206  // initialized with an existing set. As a special case, one can
207  // quickly create a fully initialized object with a one-element set;
208  // the derived name and description will come directly from the
209  // 'PCProfileMetric'. We assume ownership of the metric set
210  // container (but not the contents -- the 'PCProfileMetric's).
213  virtual ~DerivedProfileMetric();
214 
215  // Name, Description: The metric name (high level name) and a description
216  // NativeName: A name that is a combination of the raw metrics
217  // Period: The sampling period (whether event or instruction based)
218  const std::string& GetName() const { return name; }
219  const std::string& GetNativeName() const { return nativeName; }
220  const std::string& GetDescription() const { return description; }
221  ulong GetPeriod() const { return period; }
222 
223  void SetName(const char* s) { name = s; }
224  void SetName(const std::string& s) { name = s; }
225 
226  void SetNativeName(const char* s) { nativeName = s; }
227  void SetNativeName(const std::string& s) { nativeName = s; }
228 
229  void SetDescription(const char* s) { description = s; }
230  void SetDescription(const std::string& s) { description = s; }
231 
232  void SetPeriod(ulong p) { period = p; }
233 
234  // MetricSet: The set of 'PCProfileMetric's
235  const PCProfileMetricSet* GetMetricSet() const { return mset; }
237  mset = const_cast<PCProfileMetricSet*>(s);
238  }
239 
240  // PCSet: The set of relevant PCs (may be NULL or empty)
241  const PCSet* GetPCSet() const { return pcset; }
242  // A special function for saving some memory; use with caution
243  void MakeDerivedPCSetCoterminousWithPCSet() { delete pcset; pcset = NULL; }
244 
245  bool FindPC(VMA pc, ushort opIndex) {
246  if (pcset) {
247  VMA oppc = mset->GetISA()->convertVMAToOpVMA(pc, opIndex);
248  PCSetIt it = pcset->find(oppc);
249  return (it != pcset->end());
250  } else {
251  return (mset->DataExists(pc, opIndex) >= 0);
252  }
253  }
254  void InsertPC(VMA pc, ushort opIndex) {
255  DIAG_Assert(pcset, "");
256  VMA oppc = mset->GetISA()->convertVMAToOpVMA(pc, opIndex);
257  pcset->insert(oppc); // do not add duplicates!
258  }
259 
260  void Dump(std::ostream& o = std::cerr);
261  void DDump();
262 
263 private:
264  // Should not be used
267 
268  void Ctor(const PCProfileMetricSet* s);
269 
270 
271 protected:
272 private:
273  std::string name;
274  std::string nativeName;
275  std::string description;
276  ulong period; // sampling period
277 
278  PCProfileMetricSet* mset; // we own the set container, but not the contents!
279  PCSet* pcset; // the set of PCs relevant for this metric. If NULL,
280  // the set is coterminous with the set of PCs within 'mset'
281 };
282 
283 #endif
DerivedProfileMetric & operator=(const DerivedProfileMetric &m)
unsigned int GetNumMetrics() const
DerivedProfile & operator=(const DerivedProfile &p)
void Dump(std::ostream &o=std::cerr)
bfd_vma VMA
Definition: ISATypes.hpp:79
const std::string & GetNativeName() const
void SetNumMetrics(unsigned int sz)
const std::string & GetDescription() const
void SetMetric(unsigned int i, const DerivedProfileMetric *m)
DerivedProfileMetricList::const_iterator DerivedProfileMetricListCIt
PCProfileMetricSet * mset
const std::string & GetName() const
const PCProfileMetricSet * GetMetricSet() const
void Create(const PCProfile *pcprof_, const PCProfileFilterList *filtlist)
DerivedProfileMetricVec metricVec
unsigned long int ulong
Definition: uint.h:128
void MakeDerivedPCSetCoterminousWithPCSet()
DerivedProfileMetric * Current() const
void SetDescription(const char *s)
const PCProfile * GetPCProfile() const
virtual ~DerivedProfile()
std::vector< DerivedProfileMetric * > DerivedProfileMetricVec
unsigned short int ushort
Definition: uint.h:120
DerivedProfileMetricVec::iterator DerivedProfileMetricVecIt
const DerivedProfileMetric * GetMetric(unsigned int i) const
void InsertPC(VMA pc, ushort opIndex)
void SetName(const std::string &s)
DerivedProfile_MetricIterator & operator=(const DerivedProfile_MetricIterator &x)
DerivedProfileMetricList::iterator DerivedProfileMetricListIt
DerivedProfile(const DerivedProfile &p)
DerivedProfileMetricVecCIt it
friend class DerivedProfile_MetricIterator
bool FindPC(VMA pc, ushort opIndex)
const DerivedProfile & p
void SetName(const char *s)
void AddMetric(const DerivedProfileMetric *m)
#define NULL
Definition: ElfHelper.cpp:85
void SetNativeName(const std::string &s)
void SetNativeName(const char *s)
DerivedProfileMetricVec::const_iterator DerivedProfileMetricVecCIt
DerivedProfileMetric(const DerivedProfileMetric &m)
std::list< DerivedProfileMetric * > DerivedProfileMetricList
const PCSet * GetPCSet() const
std::set< VMA > PCSet
DerivedProfile_MetricIterator(const DerivedProfile &x)
void SetDescription(const std::string &s)
static long period
Definition: itimer.c:194
PCSet::iterator PCSetIt
const PCProfile * pcprof
void SetMetricSet(const PCProfileMetricSet *s)