HPCToolkit
PCProfileMetric.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 // PCProfileMetric.h
51 //
52 // Purpose:
53 // [The purpose of this file]
54 //
55 // Description:
56 // See, in particular, the comments associated with 'PCProfile'.
57 //
58 //***************************************************************************
59 
60 #ifndef PCProfileMetric_H
61 #define PCProfileMetric_H
62 
63 //************************* System Include Files ****************************
64 
65 #include <map>
66 #include <set>
67 
68 #include <string>
69 
70 #ifdef NO_STD_CHEADERS
71 # include <limits.h>
72 #else
73 # include <climits>
74 #endif
75 
76 //*************************** User Include Files ****************************
77 
78 #include <include/uint.h>
79 
80 #include "PCProfileFilter.hpp"
81 
82 #include <lib/isa/ISA.hpp>
83 
84 //*************************** Forward Declarations ***************************
85 
86 // 'PCProfileDatum' holds a single profile count or statistic.
88 #define PCProfileDatum_NIL 0 /* no data is present */
89 
90 typedef std::set<VMA> PCSet;
91 typedef PCSet::iterator PCSetIt;
92 typedef PCSet::const_iterator PCSetCIt;
93 
94 //****************************************************************************
95 // PCProfileMetric
96 //****************************************************************************
97 
98 // 'PCProfileMetric' defines a profiling metric and all raw data
99 // resulting from one profiling run in a [pc->datum] map. Since
100 // profiling data for mutiple metrics often results in a 'sparse
101 // matrix', data values of 0 (PCProfileDatum_NIL) are not explicitly
102 // stored. Note that in the case of VLIW machines, PCs are really
103 // 'operation PCs', that is, a single value that combines the PC and
104 // an offset identifying the operation within the VLIW packet. The
105 // 'operation PC' should follow ISA class conventions. Because of
106 // this, a metric contains a pointer to an appropriate ISA. (Creaters
107 // of a 'PCProfileMetric' should therefore use ISA::convertVMAToOpVMA to
108 // generate the 'operation VMAs'.) see: 'PCProfileMetric_MapIterator'
110 {
111 private:
112  typedef std::map<VMA, PCProfileDatum> PCToPCProfileDatumMap;
113  typedef PCToPCProfileDatumMap::value_type PCToPCProfileDatumMapVal;
114  typedef PCToPCProfileDatumMap::iterator PCToPCProfileDatumMapIt;
115  typedef PCToPCProfileDatumMap::const_iterator PCToPCProfileDatumMapCIt;
116 
117 public:
118  // Constructor: the ISA is *reference counted*
119  PCProfileMetric(ISA* isa_);
120  virtual ~PCProfileMetric();
121 
122  // Name, Description: The metric name and a description
123  // TotalCount: The sum of all raw data for this metric
124  // Period: The sampling period (whether event or instruction based)
125  // TxtStart, TxtSz: Beginning of the text segment and the text segment size
126  const std::string& GetName() const { return name; }
127  const std::string& GetDescription() const { return description; }
128 
129  PCProfileDatum GetTotalCount() const { return total; }
130  ulong GetPeriod() const { return period; }
131  VMA GetTxtStart() const { return txtStart; }
132  VMA GetTxtSz() const { return txtSz; }
133 
134  void SetName(const char* s) { name = s; }
135  void SetName(const std::string& s) { name = s; }
136 
137  void SetDescription(const char* s) { description = s; }
138  void SetDescription(const std::string& s) { description = s; }
139 
141  void SetPeriod(ulong p) { period = p; }
142  void SetTxtStart(VMA a) { txtStart = a; }
143  void SetTxtSz(VMA a) { txtSz = a; }
144 
145  // 'GetSz': The number of entries (note: this is not necessarily the
146  // number of instructions or PC values in the text segment).
147  unsigned int GetSz() const { return map.size(); }
148 
149  // 'GetISA': Note: A user must call ISA::attach() if this is more
150  // than a momentary reference!
151  ISA* GetISA() const { return isa; }
152 
153  // find/insert by PC value (GetTxtStart() <= PC <= GetTxtStart()+GetTxtSz())
154  // 'Find': return datum for 'pc'; PCProfileDatum_NIL if not found.
155  // (Note that this means one cannot distinguish between a dataset
156  // resulting from the insertion of <pc, 0> and a dataset in which no
157  // insertion was performed for the same pc. However, this should
158  // not be a problem.)
159  PCProfileDatum Find(VMA pc, ushort opIndex) const {
160  VMA oppc = isa->convertVMAToOpVMA(pc, opIndex);
161  PCToPCProfileDatumMapCIt it = map.find(oppc);
162  if (it == map.end()) { return PCProfileDatum_NIL; }
163  else { return ((*it).second); }
164  }
165  void Insert(VMA pc, ushort opIndex, PCProfileDatum& d) {
166  VMA oppc = isa->convertVMAToOpVMA(pc, opIndex);
167  if (d != PCProfileDatum_NIL) {
168  map.insert(PCToPCProfileDatumMapVal(oppc, d)); // do not add duplicates!
169  }
170  }
171 
172  // Filter(): Returns a set of PCs metrics that pass the filter
173  // (i.e., every PC 'pc' for which 'filter' returns true.) User
174  // becomes responsible for freeing memory.
175  PCSet* Filter(PCFilter* filter) const;
176 
177  void dump(std::ostream& o = std::cerr);
178  void ddump();
179 
180 private:
181  // Should not be used
183  PCProfileMetric& operator=(const PCProfileMetric& m) { return *this; }
184 
186 
187 protected:
188 private:
189  std::string name;
190  std::string description;
191 
192  PCProfileDatum total; // sum across all pc values recorded for this event
193  ulong period; // sampling period
194  VMA txtStart; // beginning of text segment
195  VMA txtSz; // size of text segment
196 
197  ISA* isa; // we do not own; points to containing set
198  PCToPCProfileDatumMap map; // map of sampling data
199 };
200 
201 
202 // 'PCProfileMetric_MapIterator' iterates over the [pc->datum] map of a
203 // 'PCProfileMetric'. Because data values of 0 (PCProfileDatum_NIL)
204 // are not explicitly stored, they will not appear in the iteration.
206 {
207 public:
209  Reset();
210  }
212 
213  // Note: This is the 'operation PC' and may not actually be the true
214  // PC! cf. ISA::ConvertOpPCToPC(...).
215  VMA CurrentSrc() { return (*it).first; }
216  PCProfileDatum CurrentTarget() { return (*it).second; }
217 
218  void operator++() { it++; } // prefix
219  void operator++(int) { ++it; } // postfix
220 
221  bool IsValid() const { return it != m.map.end(); }
222  bool IsEmpty() const { return it == m.map.end(); }
223 
224  // Reset and prepare for iteration again
225  void Reset() { it = m.map.begin(); }
226 
227 private:
228  // Should not be used
232  { return *this; }
233 
234 protected:
235 private:
238 };
239 
240 
241 //****************************************************************************
242 
243 #endif
244 
virtual VMA convertVMAToOpVMA(VMA vma, ushort GCC_ATTR_UNUSED opIndex) const
Definition: ISA.hpp:471
PCProfileDatum Find(VMA pc, ushort opIndex) const
void SetDescription(const std::string &s)
bfd_vma VMA
Definition: ISATypes.hpp:79
void SetPeriod(ulong p)
void SetTxtSz(VMA a)
void Insert(VMA pc, ushort opIndex, PCProfileDatum &d)
VMA GetTxtSz() const
void dump(std::ostream &o=std::cerr)
VMA GetTxtStart() const
PCProfileMetric & operator=(const PCProfileMetric &m)
unsigned long int ulong
Definition: uint.h:128
ISA * GetISA() const
PCProfileMetric(ISA *isa_)
PCToPCProfileDatumMap::iterator PCToPCProfileDatumMapIt
PCToPCProfileDatumMap map
unsigned int GetSz() const
PCProfileMetric(const PCProfileMetric &m)
std::string description
PCSet::const_iterator PCSetCIt
const PCProfileMetric & m
unsigned short int ushort
Definition: uint.h:120
ulong GetPeriod() const
PCToPCProfileDatumMap::value_type PCToPCProfileDatumMapVal
PCSet * Filter(PCFilter *filter) const
void SetTotalCount(PCProfileDatum d)
const std::string & GetDescription() const
PCProfileMetric_MapIterator & operator=(const PCProfileMetric_MapIterator &x)
void SetName(const char *s)
void SetDescription(const char *s)
Definition: ISA.hpp:106
ulong PCProfileDatum
PCToPCProfileDatumMap::const_iterator PCToPCProfileDatumMapCIt
void SetName(const std::string &s)
PCProfileDatum GetTotalCount() const
std::map< VMA, PCProfileDatum > PCToPCProfileDatumMap
virtual ~PCProfileMetric()
PCProfileMetric_MapIterator(const PCProfileMetric &x)
PCProfileDatum total
std::set< VMA > PCSet
PCProfileMetric::PCToPCProfileDatumMapCIt it
void SetTxtStart(VMA a)
PCSet::iterator PCSetIt
friend class PCProfileMetric_MapIterator
#define PCProfileDatum_NIL
const std::string & GetName() const