HPCToolkit
PCProfile.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 // PCProfile.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 PCProfile_H
61 #define PCProfile_H
62 
63 //************************* System Include Files ****************************
64 
65 #include <string>
66 #include <vector>
67 #include <list>
68 
69 //*************************** User Include Files ****************************
70 
71 #include <include/uint.h>
72 
73 #include "PCProfileMetric.hpp"
74 #include "PCProfileFilter.hpp"
75 
76 #include <lib/isa/ISA.hpp>
77 
78 //*************************** Forward Declarations ***************************
79 
80 // Some useful containers
81 typedef std::list<PCProfileMetric*> PCProfileMetricList;
82 typedef PCProfileMetricList::iterator PCProfileMetricListIt;
83 typedef PCProfileMetricList::const_iterator PCProfileMetricListCIt;
84 
85 typedef std::vector<PCProfileMetric*> PCProfileMetricVec;
86 typedef PCProfileMetricVec::iterator PCProfileMetricVecIt;
87 typedef PCProfileMetricVec::const_iterator PCProfileMetricVecCIt;
88 
89 typedef std::vector<VMA> PCVec;
90 typedef PCVec::iterator PCVecIt;
91 typedef PCVec::const_iterator PCVecCIt;
92 
93 //****************************************************************************
94 // PCProfileMetricSet
95 //****************************************************************************
96 
97 // 'PCProfileMetricSet' is a set of PCProfileMetrics. It can be used
98 // to represent all metrics in raw profile data or some collection of
99 // raw metrics forming a derived metric. Filters can be applied to
100 // yield a new set. A set contains an ISA, which its metrics point
101 // to, primarily for conversion between 'pcs' and 'operation pcs'.
102 // Note that the ISA is reference counted.
104 {
105 private:
106 
107 public:
108  // Constructor: reserves at least 'sz' slots; 'isa_' is *reference counted*
109  PCProfileMetricSet(ISA* isa_, unsigned int sz = 16);
110  virtual ~PCProfileMetricSet();
111 
112  // Access to metrics (0-based). When a set is created, space for
113  // slots is only reserved. To add slots, one must use 'Add' or
114  // 'SetSz'. Once slots exist, they can be randomly accessed via
115  // 'Assign' and 'operator[]'.
116  const PCProfileMetric* Index(unsigned int i) const { return metricVec[i]; }
117  const PCProfileMetric* operator[](unsigned int i) const { return metricVec[i]; }
118 
119  void Assign(unsigned int i, const PCProfileMetric* m) {
120  metricVec[i] = const_cast<PCProfileMetric*>(m); // assume ownership of m
121  }
122  PCProfileMetric*& operator[](unsigned int i) { return metricVec[i]; }
123 
124  void Add(const PCProfileMetric* m) {
125  metricVec.push_back(const_cast<PCProfileMetric*>(m));
126  }
127 
128  unsigned int GetSz() const { return metricVec.size(); }
129  void SetSz(unsigned int sz) { metricVec.resize(sz); }
130 
131  void Clear() { metricVec.clear(); }
132 
133 
134  // 'GetISA': Note: A user must call ISA::attach() if this is more
135  // than a momentary reference!
136  ISA* GetISA() const { return isa; }
137 
138  // DataExists(): Does non-NIL data exist for some metric at the
139  // operation designated by 'pc' and 'opIndex'? If yes, returns the
140  // index of the first metric with non-nil data (forward iteration
141  // from 0 to size); otherwise, returns negative.
142  int DataExists(VMA pc, ushort opIndex) const;
143 
144  // Filter(): Returns a new, non-null but possibly empty, set of
145  // metrics that pass the filter (i.e., every metric metric 'm' for
146  // which 'filter' returns true.) User becomes responsible for
147  // freeing memory.
148  PCProfileMetricSet* Filter(MetricFilter* filter) const;
149 
150  void dump(std::ostream& o = std::cerr);
151  void ddump();
152 
153 private:
154  // Should not be used
156  PCProfileMetricSet& operator=(const PCProfileMetricSet& p) { return *this; }
157 
159 
160 protected:
161 private:
162 
163  // We lie a little about this being a 'set' and use a vector for
164  // implementation. Set indexing (random access) may be convenient;
165  // and any possible resizing should be cheap b/c these sets should
166  // be relatively small.
168  ISA* isa;
169 };
170 
171 
172 // 'PCProfileMetricSetIterator' iterates over all 'PCProfileMetric'
173 // within a 'PCProfile'.
175 {
176 public:
178  Reset();
179  }
181 
182  PCProfileMetric* Current() const { return (*it); }
183 
184  void operator++() { it++; } // prefix
185  void operator++(int) { ++it; } // postfix
186 
187  bool IsValid() const { return it != s.metricVec.end(); }
188  bool IsEmpty() const { return it == s.metricVec.end(); }
189 
190  // Reset and prepare for iteration again
191  void Reset() { it = s.metricVec.begin(); }
192 
193 private:
194  // Should not be used
198  { return *this; }
199 
200 protected:
201 private:
204 };
205 
206 //****************************************************************************
207 // PCProfile
208 //****************************************************************************
209 
210 // 'PCProfile' represents data resulting from one or more different
211 // profile runs. A 'PCProfile' is a 'PCProfileMetricSet' and
212 // consequently contains a set of metrics, each with their own
213 // [pc->count] map. 'PCProfile' is abstract enough to represent
214 // profile data from event-sampling systems such as SGI's ssrun or
215 // instruction-sampling systems such as DEC/Compaq/HP's DCPI
216 // ProfileMe.
217 //
218 // Because each metric contains its own sparse [pc->count] map (0
219 // counts are not recorded) it is difficult to know in advance for
220 // which PCs there is a non-zero count for at least one metric.
221 // Because of this, a 'PCProfile' also contains a list of PCs at which
222 // at least one metric contains non-zero counts.
223 //
225 {
226 public:
227  PCProfile(ISA* isa_, unsigned int sz = 16);
228  virtual ~PCProfile();
229 
230  // ProfiledFile: the name of the profiled program image
231  // HdrInfo: a copy of the unparsed header info.
232  const std::string& GetProfiledFile() const { return profiledFile; }
233  const std::string& GetHdrInfo() const { return fHdrInfo; }
234 
235  void SetProfiledFile(const char* s) { profiledFile = s; }
236  void SetProfiledFile(const std::string& s) { profiledFile = s; }
237 
238  void SetHdrInfo(const char* s) { fHdrInfo = s; }
239  void SetHdrInfo(const std::string& s) { fHdrInfo = s; }
240 
241  // Text start and size (redundant). Note: all metrics for one
242  // profile should have identical values for
243  // PCProfileMetric::GetTxtStart() and PCProfileMetric::GetTxtSz()
244  VMA GetTxtStart() const { return (GetSz()) ? Index(0)->GetTxtStart() : 0; }
245  VMA GetTxtSz() const { return (GetSz()) ? Index(0)->GetTxtSz() : 0; }
246 
247  // Access to metrics (redundant)
248  const PCProfileMetric* GetMetric(unsigned int i) const { return Index(i); }
249  void SetMetric(unsigned int i, const PCProfileMetric* m) { Assign(i, m); }
250  void AddMetric(const PCProfileMetric* m) { Add(m); }
251  unsigned int GetNumMetrics() const { return GetSz(); }
252  void SetNumMetrics(unsigned int sz) { SetSz(sz); }
253 
254  // Access to PCs containing non-zero profiling info
255  unsigned int GetNumPCs() { return pcVec.size(); }
256  void AddPC(VMA pc, ushort opIndex); // be careful: should be no duplicates
257 
258  void dump(std::ostream& o = std::cerr);
259  void ddump();
260 
261 private:
262  // Should not be used
263  PCProfile(const PCProfile& p);
264  PCProfile& operator=(const PCProfile& p) { return *this; }
265 
266  friend class PCProfile_PCIterator;
267 
268 protected:
269 private:
270  std::string profiledFile; // name of profiled file
271  std::string fHdrInfo; // unparsed file header info
272 
273  PCVec pcVec; // PCs for which some metric has non-zero data
274 };
275 
276 
277 // PCProfile_PCIterator
279 {
280 public:
281  PCProfile_PCIterator(const PCProfile& x) : p(x) {
282  Reset();
283  }
284  virtual ~PCProfile_PCIterator() { }
285 
286  // Note: This is the 'operation PC' and may not actually be the true
287  // PC! cf. ISA::ConvertOpPCToPC(...).
288  VMA Current() const { return (*it); }
289 
290  void operator++() { it++; } // prefix
291  void operator++(int) { ++it; } // postfix
292 
293  bool IsValid() const { return it != p.pcVec.end(); }
294  bool IsEmpty() const { return it == p.pcVec.end(); }
295 
296  // Reset and prepare for iteration again
297  void Reset() { it = p.pcVec.begin(); }
298 
299 private:
300  // Should not be used
304  { return *this; }
305 
306 protected:
307 private:
308  const PCProfile& p;
310 };
311 
312 //****************************************************************************
313 // PCProfileVec
314 //****************************************************************************
315 
316 // 'PCProfileVec' is a vector of 'PCProfileDatum.' A value can be
317 // associated with the vector. A 'PCProfileVec' can be used to record
318 // a cross-section of several 'PCProfileMetric'. E.g. it could
319 // contain all counts at a particlar PC (or line number) or represent
320 // statistics accross all metrics.
321 
323 {
324 public:
325  PCProfileVec(unsigned int sz);
326  virtual ~PCProfileVec() { }
327 
328  uint64_t GetDatum() const { return datum; }
329  unsigned int GetSz() const { return vec.size(); }
330 
331  void SetDatum(uint64_t d) { datum = d; }
332 
333  bool IsZeroed();
334 
335  PCProfileDatum& operator[](unsigned int i) { return vec[i]; }
336 
337  void dump(std::ostream& o = std::cerr);
338  void ddump();
339 
340 private:
341  // Should not be used
344  PCProfileVec& operator=(const PCProfileVec& v) { return *this; }
345 
346 protected:
347 private:
348  uint64_t datum; // datum to associate with vec (e.g. pc value, line no, ptr)
349  std::vector<PCProfileDatum> vec;
350 };
351 
352 
353 #endif
std::vector< PCProfileDatum > vec
Definition: PCProfile.hpp:349
void Assign(unsigned int i, const PCProfileMetric *m)
Definition: PCProfile.hpp:119
unsigned int GetSz() const
Definition: PCProfile.hpp:329
PCProfileMetricSet(const PCProfileMetricSet &p)
Definition: PCProfile.hpp:155
const PCProfileMetric * Index(unsigned int i) const
Definition: PCProfile.hpp:116
PCProfileMetricSet * Filter(MetricFilter *filter) const
Definition: PCProfile.cpp:107
bfd_vma VMA
Definition: ISATypes.hpp:79
std::list< PCProfileMetric * > PCProfileMetricList
Definition: PCProfile.hpp:81
PCProfileMetricSet & operator=(const PCProfileMetricSet &p)
Definition: PCProfile.hpp:156
friend class PCProfileMetricSetIterator
Definition: PCProfile.hpp:158
void Add(const PCProfileMetric *m)
Definition: PCProfile.hpp:124
PCProfile_PCIterator(const PCProfile &x)
Definition: PCProfile.hpp:281
VMA GetTxtSz() const
PCProfileMetricVec::const_iterator PCProfileMetricVecCIt
Definition: PCProfile.hpp:87
virtual ~PCProfile_PCIterator()
Definition: PCProfile.hpp:284
unsigned int GetSz() const
Definition: PCProfile.hpp:128
PCProfile & operator=(const PCProfile &p)
Definition: PCProfile.hpp:264
PCProfileMetricSet(ISA *isa_, unsigned int sz=16)
unsigned int GetNumMetrics() const
Definition: PCProfile.hpp:251
VMA GetTxtStart() const
void SetHdrInfo(const std::string &s)
Definition: PCProfile.hpp:239
void dump(std::ostream &o=std::cerr)
Definition: PCProfile.cpp:121
VMA GetTxtSz() const
Definition: PCProfile.hpp:245
PCVec::const_iterator PCVecCIt
Definition: PCProfile.hpp:91
PCProfileMetricSetIterator & operator=(const PCProfileMetricSetIterator &x)
Definition: PCProfile.hpp:197
PCProfileMetricVec::iterator PCProfileMetricVecIt
Definition: PCProfile.hpp:86
bool IsValid() const
Definition: PCProfile.hpp:293
const std::string & GetHdrInfo() const
Definition: PCProfile.hpp:233
PCProfileVec & operator=(const PCProfileVec &v)
Definition: PCProfile.hpp:344
unsigned int GetNumPCs()
Definition: PCProfile.hpp:255
void SetHdrInfo(const char *s)
Definition: PCProfile.hpp:238
PCProfileMetricSetIterator(const PCProfileMetricSet &x)
Definition: PCProfile.hpp:177
const PCProfile & p
Definition: PCProfile.hpp:308
PCProfileVec(const PCProfileVec &v)
Definition: PCProfile.hpp:343
void SetNumMetrics(unsigned int sz)
Definition: PCProfile.hpp:252
PCProfileMetricVecCIt it
Definition: PCProfile.hpp:203
bool IsEmpty() const
Definition: PCProfile.hpp:294
const PCProfileMetricSet & s
Definition: PCProfile.hpp:202
unsigned short int ushort
Definition: uint.h:120
std::string fHdrInfo
Definition: PCProfile.hpp:271
uint64_t GetDatum() const
Definition: PCProfile.hpp:328
PCProfileMetricList::const_iterator PCProfileMetricListCIt
Definition: PCProfile.hpp:83
void AddMetric(const PCProfileMetric *m)
Definition: PCProfile.hpp:250
Definition: ISA.hpp:106
PCProfileMetric * Current() const
Definition: PCProfile.hpp:182
std::vector< PCProfileMetric * > PCProfileMetricVec
Definition: PCProfile.hpp:85
void SetSz(unsigned int sz)
Definition: PCProfile.hpp:129
int DataExists(VMA pc, ushort opIndex) const
Definition: PCProfile.cpp:95
ulong PCProfileDatum
PCProfileDatum & operator[](unsigned int i)
Definition: PCProfile.hpp:335
PCProfileMetricList::iterator PCProfileMetricListIt
Definition: PCProfile.hpp:82
PCVec pcVec
Definition: PCProfile.hpp:273
void SetProfiledFile(const std::string &s)
Definition: PCProfile.hpp:236
ISA * GetISA() const
Definition: PCProfile.hpp:136
virtual ~PCProfileMetricSet()
Definition: PCProfile.cpp:85
VMA Current() const
Definition: PCProfile.hpp:288
PCProfile_PCIterator & operator=(const PCProfile_PCIterator &x)
Definition: PCProfile.hpp:303
PCProfileMetricVec metricVec
Definition: PCProfile.hpp:167
std::string profiledFile
Definition: PCProfile.hpp:270
VMA GetTxtStart() const
Definition: PCProfile.hpp:244
const PCProfileMetric * operator[](unsigned int i) const
Definition: PCProfile.hpp:117
void SetDatum(uint64_t d)
Definition: PCProfile.hpp:331
PCVec::iterator PCVecIt
Definition: PCProfile.hpp:90
PCProfileMetric *& operator[](unsigned int i)
Definition: PCProfile.hpp:122
virtual ~PCProfileVec()
Definition: PCProfile.hpp:326
std::vector< VMA > PCVec
Definition: PCProfile.hpp:89
const std::string & GetProfiledFile() const
Definition: PCProfile.hpp:232
const PCProfileMetric * GetMetric(unsigned int i) const
Definition: PCProfile.hpp:248
void SetMetric(unsigned int i, const PCProfileMetric *m)
Definition: PCProfile.hpp:249
uint64_t datum
Definition: PCProfile.hpp:348
virtual ~PCProfileMetricSetIterator()
Definition: PCProfile.hpp:180
void SetProfiledFile(const char *s)
Definition: PCProfile.hpp:235