HPCToolkit
Metric-Mgr.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 #ifndef prof_Prof_Metric_Mgr_hpp
48 #define prof_Prof_Metric_Mgr_hpp
49 
50 //************************ System Include Files ******************************
51 
52 #include <iostream>
53 #include <string>
54 #include <list>
55 #include <vector>
56 #include <map>
57 
58 #include <climits>
59 
60 //************************* User Include Files *******************************
61 
62 #include <include/uint.h>
63 
64 #include "Metric-ADesc.hpp"
65 
66 #include <lib/support/Unique.hpp>
67 
68 //************************ Forward Declarations ******************************
69 
70 namespace Prof {
71 
72 namespace Metric {
73 
74 //****************************************************************************
75 
76 class Mgr
77  : public Unique // non copyable
78 {
79 public:
80  typedef std::map<std::string, Metric::ADesc*> StringToADescMap;
81  typedef std::map<std::string, Metric::ADescVec> StringToADescVecMap;
82 
83 public:
84  Mgr();
85  ~Mgr();
86 
87  // ------------------------------------------------------------
88  // make raw metrics (N.B.: currently only for flat profiles)
89  // ------------------------------------------------------------
90 
91  void
92  makeRawMetrics(const std::vector<std::string>& profileFiles,
93  bool isUnitsEvents = true,
94  bool doDispPercent = true);
95 
96  void
97  makeRawMetrics(const std::string& profileFile,
98  bool isUnitsEvents = true,
99  bool doDispPercent = true)
100  {
101  std::vector<std::string> vec(1, profileFile);
102  makeRawMetrics(vec, isUnitsEvents, doDispPercent);
103  }
104 
105 
106  // ------------------------------------------------------------
107  // make summary metrics: [srcBegId, srcEndId)
108  // ------------------------------------------------------------
109 
110  uint
111  makeSummaryMetrics(bool needAllStats, bool needMultiOccurance,
112  uint srcBegId = Mgr::npos, uint srcEndId = Mgr::npos);
113 
114  uint
115  makeSummaryMetricsIncr(bool needAllStats,
116  uint srcBegId = Mgr::npos,
117  uint srcEndId = Mgr::npos);
118 
119 
120  // ------------------------------------------------------------
121  // The metric table
122  //
123  // INVARIANTS:
124  // - All 'raw' metrics are independent of each other
125  // - A metric's id is always within [0 size())
126  // - A derived metric is dependent only upon 'prior' metrics,
127  // i.e. metrics with ids strictly less than its own.
128  //
129  // ------------------------------------------------------------
132  { return m_metrics[i]; }
133 
134  const Metric::ADesc*
135  metric(uint i) const
136  { return m_metrics[i]; }
137 
139  metric(const std::string& uniqNm)
140  {
141  StringToADescMap::const_iterator it = m_uniqnmToMetricMap.find(uniqNm);
142  return (it != m_uniqnmToMetricMap.end()) ? it->second : NULL;
143  }
144 
145  const Metric::ADesc*
146  metric(const std::string& uniqNm) const
147  {
148  StringToADescMap::const_iterator it = m_uniqnmToMetricMap.find(uniqNm);
149  return (it != m_uniqnmToMetricMap.end()) ? it->second : NULL;
150  }
151 
152  uint
153  size() const
154  { return m_metrics.size(); }
155 
156  bool
157  empty() const
158  { return m_metrics.empty(); }
159 
160 
161  // ------------------------------------------------------------
162  // insert
163  // ------------------------------------------------------------
164 
165  // Given m, insert m into the tables, ensuring it has a unique name
166  // by qualifying it if necessary. Returns true if the name was
167  // modified, false otherwise.
168  // NOTE: Assumes ownership of 'm'
169  bool
170  insert(Metric::ADesc* m);
171 
172  // Given m, insert m into the tables if the metric name does not
173  // exist. Returns true if inserted (assuming ownership) and false
174  // otherwise.
175  bool
177 
178  // ------------------------------------------------------------
179  //
180  // ------------------------------------------------------------
181 
182  // Return the (first) metric this has the sort-by attribute set
184  findSortKey() const;
185 
186  // Return the last metric that is visible
188  findFirstVisible() const;
189 
190  // Return the last metric that is visible
192  findLastVisible() const;
193 
194  bool
195  hasDerived() const;
196 
197 
198  // ------------------------------------------------------------
199  //
200  // ------------------------------------------------------------
201 
202  static const uint npos = UINT_MAX;
203 
204  // findGroup: finds the group of metrics in 'x' = 'this' that
205  // correspond to those in 'y' (i.e., a mapping between 'y' and
206  // 'x'). Returns the first index of the group if found; otherwise
207  // Mgr::npos.
208  uint
209  findGroup(const Mgr& y_mMgr) const;
210 
211 
212  // ------------------------------------------------------------
213  // helper maps/tables
214  // ------------------------------------------------------------
215 
216  const StringToADescVecMap&
218  { return m_fnameToFMetricMap; }
219 
220  void
221  recomputeMaps();
222 
223  void
224  computePartners();
225 
226  // specifically for merging the statistics of perf event metrics
227  // it sums the statistics of the "source" into the correspondent metrics
228  // this doesn't check if the metric is exactly the same or not
229  // we assume the position of metrics in the source and target
230  // are the same.
231  void
232  mergePerfEventStatistics(Mgr *source);
233 
234  void
235  mergePerfEventStatistics_finalize(int num_profiles);
236 
237  // ------------------------------------------------------------
238  //
239  // ------------------------------------------------------------
240 
241  void
242  zeroDBInfo() const;
243 
244  // ------------------------------------------------------------
245  //
246  // ------------------------------------------------------------
247 
248  std::string
249  toString(const char* pfx = "") const;
250 
251  std::ostream&
252  dump(std::ostream& os = std::cerr, const char* pfx = "") const;
253 
254  void
255  ddump() const;
256 
257 public:
258 
259 private:
260  bool
262 
264  makeSummaryMetric(const std::string mDrvdTy, const Metric::ADesc* mSrc,
265  const Metric::ADescVec& mOpands);
266 
268  makeSummaryMetricIncr(const std::string mDrvdTy, const Metric::ADesc* mSrc);
269 
270 
271 private:
272  // the metric table
274 
275  // non-unique-metric name to Metric::ADescVec table (i.e., name excludes
276  // qualifications added by insert())
277  StringToADescVecMap m_nuniqnmToMetricMap;
278 
279  // unique-metric name to Metric::ADescVec table
280  StringToADescMap m_uniqnmToMetricMap;
281 
282  // profile file name to Metric::SampledDesc table
283  StringToADescVecMap m_fnameToFMetricMap;
284 };
285 
286 //****************************************************************************
287 
288 } // namespace Metric
289 
290 } // namespace Prof
291 
292 
293 //****************************************************************************
294 
295 #endif // prof_Prof_Metric_Mgr_hpp
void makeRawMetrics(const std::vector< std::string > &profileFiles, bool isUnitsEvents=true, bool doDispPercent=true)
Definition: Metric-Mgr.cpp:98
bool insertInMapsAndMakeUniqueName(Metric::ADesc *m)
Definition: Metric-Mgr.cpp:779
bool insertIf(Metric::ADesc *m)
Definition: Metric-Mgr.cpp:474
bool insert(Metric::ADesc *m)
Definition: Metric-Mgr.cpp:460
StringToADescVecMap m_nuniqnmToMetricMap
Definition: Metric-Mgr.hpp:277
bool hasDerived() const
Definition: Metric-Mgr.cpp:532
void mergePerfEventStatistics(Mgr *source)
Definition: Metric-Mgr.cpp:203
void ddump() const
Definition: Metric-Mgr.cpp:767
std::vector< ADesc * > ADescVec
Metric::ADesc * findSortKey() const
Definition: Metric-Mgr.cpp:487
std::ostream & dump(std::ostream &os=std::cerr, const char *pfx="") const
Definition: Metric-Mgr.cpp:744
void zeroDBInfo() const
Definition: Metric-Mgr.cpp:721
StringToADescVecMap m_fnameToFMetricMap
Definition: Metric-Mgr.hpp:283
bool empty() const
Definition: Metric-Mgr.hpp:157
uint makeSummaryMetricsIncr(bool needAllStats, uint srcBegId=Mgr::npos, uint srcEndId=Mgr::npos)
Definition: Metric-Mgr.cpp:235
void mergePerfEventStatistics_finalize(int num_profiles)
Definition: Metric-Mgr.cpp:222
void makeRawMetrics(const std::string &profileFile, bool isUnitsEvents=true, bool doDispPercent=true)
Definition: Metric-Mgr.hpp:97
uint size() const
Definition: Metric-Mgr.hpp:153
uint makeSummaryMetrics(bool needAllStats, bool needMultiOccurance, uint srcBegId=Mgr::npos, uint srcEndId=Mgr::npos)
Definition: Metric-Mgr.cpp:135
const StringToADescVecMap & fnameToFMetricMap() const
Definition: Metric-Mgr.hpp:217
std::string toString(const char *pfx="") const
Definition: Metric-Mgr.cpp:735
unsigned int uint
Definition: uint.h:124
const Metric::ADesc * metric(const std::string &uniqNm) const
Definition: Metric-Mgr.hpp:146
static const uint npos
Definition: Metric-Mgr.hpp:202
Metric::DerivedIncrDesc * makeSummaryMetricIncr(const std::string mDrvdTy, const Metric::ADesc *mSrc)
Definition: Metric-Mgr.cpp:365
std::map< std::string, Metric::ADescVec > StringToADescVecMap
Definition: Metric-Mgr.hpp:81
Metric::DerivedDesc * makeSummaryMetric(const std::string mDrvdTy, const Metric::ADesc *mSrc, const Metric::ADescVec &mOpands)
Definition: Metric-Mgr.cpp:272
Metric::ADesc * findFirstVisible() const
Definition: Metric-Mgr.cpp:502
Metric::ADesc * metric(const std::string &uniqNm)
Definition: Metric-Mgr.hpp:139
Metric::ADescVec m_metrics
Definition: Metric-Mgr.hpp:273
const Metric::ADesc * metric(uint i) const
Definition: Metric-Mgr.hpp:135
#define NULL
Definition: ElfHelper.cpp:85
std::map< std::string, Metric::ADesc * > StringToADescMap
Definition: Metric-Mgr.hpp:80
uint findGroup(const Mgr &y_mMgr) const
Definition: Metric-Mgr.cpp:586
Metric::ADesc * metric(uint i)
Definition: Metric-Mgr.hpp:131
Metric::ADesc * findLastVisible() const
Definition: Metric-Mgr.cpp:517
StringToADescMap m_uniqnmToMetricMap
Definition: Metric-Mgr.hpp:280