HPCToolkit
DerivedProfile.cpp
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.C
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 //************************* System Include Files ****************************
61 
62 #include <iostream>
63 using std::endl;
64 using std::hex;
65 using std::dec;
66 
67 #include <string>
68 using std::string;
69 
70 //*************************** User Include Files ****************************
71 
72 #include "DerivedProfile.hpp"
73 
75 
76 //*************************** Forward Declarations ***************************
77 
78 static bool
80 
81 static ulong
83 
84 string
86 
87 //****************************************************************************
88 // DerivedProfile
89 //****************************************************************************
90 
92  : metricVec(0)
93 {
94 }
95 
97  const PCProfileFilterList* filtlist)
98  : metricVec(0)
99 {
100  Create(pcprof_, filtlist);
101 }
102 
104 {
105  for (DerivedProfile_MetricIterator it(*this); it.IsValid(); ++it) {
106  DerivedProfileMetric* dm = it.Current();
107  delete dm;
108  }
109  metricVec.clear();
110 }
111 
112 void
114  const PCProfileFilterList* filtlist)
115 {
116  pcprof = pcprof_;
117 
118  // -------------------------------------------------------
119  // 1. Create metrics. As a special case, if filtlist is null we
120  // have a one to one mapping between the raw and derived profile
121  // metrics (identity filter).
122  // -------------------------------------------------------
123  if (filtlist == NULL) {
125  for (unsigned int i = 0; i < pcprof->GetNumMetrics(); ++i) {
126  const PCProfileMetric* m = pcprof->GetMetric(i);
128  SetMetric(i, dm);
129  }
130  } else {
131  // For each metric filter, create a derived metric
132  SetNumMetrics(filtlist->size());
133  PCProfileFilterList::const_iterator it = filtlist->begin();
134  for (unsigned int i = 0; it != filtlist->end(); ++it, ++i) {
135  PCProfileFilter* filt = *it;
136  MetricFilter* mfilt = filt->GetMetricFilter();
137  const PCProfileMetricSet* s = pcprof->Filter(mfilt);
138  DIAG_Assert(VerifyPeriod(s), ""); // we should all have the same period
139 
141  dm->SetName(filt->GetName());
143  dm->SetDescription(filt->GetDescription());
144  dm->SetPeriod(GetPeriod(s));
145  SetMetric(i, dm);
146  }
147  }
148 
149  // -------------------------------------------------------
150  // 2. Determine set of interesting PCs. As a special case, if
151  // filtlist is null, we have a one to one mapping between raw PCs
152  // and derived PCs.
153  // -------------------------------------------------------
154 
155  if (filtlist == NULL) {
156  for (DerivedProfile_MetricIterator dmIt(*this); dmIt.IsValid(); ++dmIt) {
157  DerivedProfileMetric* dm = dmIt.Current();
159  }
160  } else {
161  // FIXME: Save memory by using MakeDerivedPCSetCoterminousWithPCSet()
162  // when the insn filter is the identity filter.
163 
164  // Iterate over VMA values in the profile
165  for (PCProfile_PCIterator it(*pcprof); it.IsValid(); ++it) {
166 
167  VMA opvma = it.Current(); // an 'operation vma'
168  ushort opIndex;
169  VMA vma = binutils::LM::isa->convertOpVMAToVMA(opvma, opIndex);
170 
171  // For each derived metric and its insn filter
172  PCProfileFilterList::const_iterator fIt = filtlist->begin();
173  for (unsigned int i = 0; fIt != filtlist->end(); ++fIt, ++i) {
174  PCProfileFilter* filt = *fIt;
175  PCFilter* pcfilt = filt->GetPCFilter();
176 
178  = const_cast<DerivedProfileMetric*>(GetMetric(i));
179  const PCProfileMetricSet* rawMSet = dm->GetMetricSet();
180 
181  // if vma has data && insn filter is satisfied for vma, record vma
182  if (rawMSet->DataExists(vma, opIndex) >= 0 && (*pcfilt)(vma, opIndex)) {
183  dm->InsertPC(vma, opIndex);
184  }
185  }
186  }
187  }
188 
189 }
190 
191 void
192 DerivedProfile::Dump(std::ostream& o)
193 {
194  o << "'DerivedProfile' --\n";
195  if (pcprof) {
196  const_cast<PCProfile*>(pcprof)->dump(o);
197  }
198 
199  o << "'Metrics' --\n";
200  for (DerivedProfile_MetricIterator it(*this); it.IsValid(); ++it) {
201  DerivedProfileMetric* dm = it.Current();
202  dm->Dump(o);
203  }
204 }
205 
206 void
208 {
209  Dump(std::cerr);
210 }
211 
212 //****************************************************************************
213 // DerivedProfileMetric
214 //****************************************************************************
215 
217 {
218  Ctor(s);
219 }
220 
222 {
223  PCProfileMetricSet* s = new PCProfileMetricSet(m->GetISA(), 1);
224  s->Add(m);
225 
226  Ctor(s);
227  SetName(m->GetName());
228  SetNativeName(m->GetName());
229  SetDescription(m->GetDescription());
230  SetPeriod(m->GetPeriod());
231 }
232 
233 void
235 {
236  SetMetricSet(s);
237  pcset = new PCSet;
238 }
239 
241 {
242  mset->Clear(); // we own the set container, but not the contents!
243  delete mset;
244  delete pcset;
245 }
246 
247 void
249 {
250  o << "'DerivedProfileMetric' --\n";
251  o << " name: " << name << "\n";
252  o << " description: " << description << "\n";
253  o << " period: " << period << "\n";
254 }
255 
256 void
258 {
259  Dump(std::cerr);
260 }
261 
262 //****************************************************************************
263 
264 // Verify that all metrics within the set 's' have the same period.
265 // Otherwise, things get complicated...
266 static bool
268 {
269  if (s->GetSz() == 0) { return true; }
270 
271  ulong period = (*s)[0]->GetPeriod();
272  for (PCProfileMetricSetIterator it(*s); it.IsValid(); ++it) {
273  PCProfileMetric* m = it.Current();
274  if (period != m->GetPeriod()) {
275  return false;
276  }
277  }
278  return true;
279 }
280 
281 static ulong
283 {
284  // We assume that if s is non-empty, all the metrics periods are the same!
285  if (s->GetSz() == 0) {
286  return 0;
287  } else {
288  return (*s)[0]->GetPeriod();
289  }
290 }
291 
292 string
294 {
295  string nm;
297  for (unsigned int i = 0; it.IsValid(); ++it, ++i) {
298  PCProfileMetric* m = it.Current();
299  if (i != 0) { nm += "+"; }
300  nm += "[" + m->GetName() + "]";
301  }
302 
303  if (nm.empty()) { nm = "[no-matching-metrics]"; }
304  return nm;
305 }
PCFilter * GetPCFilter() const
void Dump(std::ostream &o=std::cerr)
DerivedProfileMetric(const PCProfileMetricSet *s=NULL)
PCProfileMetricSet * Filter(MetricFilter *filter) const
Definition: PCProfile.cpp:107
void Dump(std::ostream &o=std::cerr)
bfd_vma VMA
Definition: ISATypes.hpp:79
void SetNumMetrics(unsigned int sz)
void SetMetric(unsigned int i, const DerivedProfileMetric *m)
void Add(const PCProfileMetric *m)
Definition: PCProfile.hpp:124
unsigned int GetSz() const
Definition: PCProfile.hpp:128
unsigned int GetNumMetrics() const
Definition: PCProfile.hpp:251
const PCProfileMetricSet * GetMetricSet() const
void Create(const PCProfile *pcprof_, const PCProfileFilterList *filtlist)
bool IsValid() const
Definition: PCProfile.hpp:293
DerivedProfileMetricVec metricVec
unsigned long int ulong
Definition: uint.h:128
void MakeDerivedPCSetCoterminousWithPCSet()
void SetDescription(const char *s)
MetricFilter * GetMetricFilter() const
virtual ~DerivedProfile()
ISA * GetISA() const
unsigned short int ushort
Definition: uint.h:120
ulong GetPeriod() const
const DerivedProfileMetric * GetMetric(unsigned int i) const
void InsertPC(VMA pc, ushort opIndex)
const std::string & GetDescription() const
const std::string & GetDescription() const
void Ctor(const PCProfileMetricSet *s)
PCProfileMetric * Current() const
Definition: PCProfile.hpp:182
string GetNativeName(const PCProfileMetricSet *s)
int DataExists(VMA pc, ushort opIndex) const
Definition: PCProfile.cpp:95
static ulong GetPeriod(const PCProfileMetricSet *s)
const std::string & GetName() const
void SetName(const char *s)
#define NULL
Definition: ElfHelper.cpp:85
void SetNativeName(const char *s)
static bool VerifyPeriod(const PCProfileMetricSet *s)
const PCProfileMetric * GetMetric(unsigned int i) const
Definition: PCProfile.hpp:248
std::set< VMA > PCSet
static long period
Definition: itimer.c:194
const PCProfile * pcprof
const std::string & GetName() const