vt  1.6.0
(Virtual Transport)
perf_data.h
Go to the documentation of this file.
1 /*
2 //@HEADER
3 // *****************************************************************************
4 //
5 // perf_data.h
6 // DARMA/vt => Virtual Transport
7 //
8 // Copyright 2019-2024 National Technology & Engineering Solutions of Sandia, LLC
9 // (NTESS). Under the terms of Contract DE-NA0003525 with NTESS, the U.S.
10 // Government retains certain rights in this software.
11 //
12 // Redistribution and use in source and binary forms, with or without
13 // modification, are permitted provided that the following conditions are met:
14 //
15 // * Redistributions of source code must retain the above copyright notice,
16 // this list of conditions and the following disclaimer.
17 //
18 // * Redistributions in binary form must reproduce the above copyright notice,
19 // this list of conditions and the following disclaimer in the documentation
20 // and/or other materials provided with the distribution.
21 //
22 // * Neither the name of the copyright holder nor the names of its
23 // contributors may be used to endorse or promote products derived from this
24 // software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
27 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
30 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 // POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact darma@sandia.gov
39 //
40 // *****************************************************************************
41 //@HEADER
42 */
43 
44 #if !defined INCLUDED_VT_METRICS_PERF_DATA_H
45 #define INCLUDED_VT_METRICS_PERF_DATA_H
46 
47 #include "vt/config.h"
49 #include "vt/context/context.h"
52 
53 #include <linux/perf_event.h>
54 #include <sys/ioctl.h>
55 #include <sys/syscall.h>
56 #include <unistd.h>
57 #include <cstdint>
58 #include <cstring>
59 #include <unordered_map>
60 #include <vector>
61 #include <string>
62 
63 namespace vt::metrics {
64 
76 {
77 public:
80  uint64_t time_enabled_ = 0;
81  uint64_t time_running_ = 0;
82  std::unordered_map<std::string, uint64_t> measurements_;
83 
84  double getScalingRatio() const {
85  if (time_running_ == 0) {
86  return 0.0;
87  }
88 
89  return static_cast<double>(time_enabled_) /
90  static_cast<double>(time_running_);
91  }
92 
93  double getRunningFraction() const {
94  if (time_enabled_ == 0) {
95  return 0.0;
96  }
97 
98  return static_cast<double>(time_running_) /
99  static_cast<double>(time_enabled_);
100  }
101  };
102 
116  PerfData();
117 
124  virtual ~PerfData();
125 
131  void startTaskMeasurement();
132 
138  void stopTaskMeasurement();
139 
149  std::unordered_map<std::string, uint64_t> getTaskMeasurements();
150 
157  std::vector<TaskGroupMeasurements> getTaskGroupMeasurements();
158 
166  std::unordered_map<std::string, PerfEventDescriptor> getEventMap() const;
167 
174  std::vector<PerfEventGroupInfo> getEventGroups() const;
175 
179  void startup() override;
180 
186  std::string name() override;
187 
191  template <typename SerializerT>
192  void serialize(SerializerT& s) {
193  s | event_map_
194  | event_names_
195  | event_groups_;
196  }
197 
198 private:
199  struct GroupState {
201  int leader_fd_ = -1;
202  std::unordered_map<uint64_t, std::string> event_ids_;
203  };
204 
209 
213  std::vector<std::string> event_names_;
214 
218  std::vector<PerfEventGroupInfo> event_groups_;
219 
223  std::vector<GroupState> group_states_;
224 
228  std::vector<int> open_fds_;
229 
233  std::vector<TaskGroupMeasurements> readTaskGroupMeasurements() const;
234 
238  void maybePrintEventGroups() const;
239 
245  void cleanupBeforeAbort();
246 
260  static long perfEventOpen(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags);
261 };
262 
263 } // end namespace vt::metrics
264 
265 namespace vt {
266 
268 
269 } // end namespace vt
270 
271 #endif /*INCLUDED_VT_METRICS_PERF_DATA_H*/
vt::metrics::PerfData::getEventMap
std::unordered_map< std::string, PerfEventDescriptor > getEventMap() const
Retrieve the current event map.
Definition: perf_data.cc:321
perf_event_groups.h
vt::metrics::PerfData::getTaskMeasurements
std::unordered_map< std::string, uint64_t > getTaskMeasurements()
Get the measurements collected during the task execution.
Definition: perf_data.cc:300
vt::metrics::PerfData::GroupState::event_ids_
std::unordered_map< uint64_t, std::string > event_ids_
Definition: perf_data.h:202
vt::metrics::PerfData::TaskGroupMeasurements::time_enabled_
uint64_t time_enabled_
Definition: perf_data.h:80
vt::metrics::PerfData::perfEventOpen
static long perfEventOpen(struct perf_event_attr *hw_event, pid_t pid, int cpu, int group_fd, unsigned long flags)
Open a performance counter event.
Definition: perf_data.cc:346
vt::metrics::PerfData::event_names_
std::vector< std::string > event_names_
List of event names being tracked.
Definition: perf_data.h:213
vt::metrics::PerfData::PerfData
PerfData()
Constructor for PerfData.
Definition: perf_data.cc:142
vt::metrics::PerfData::group_states_
std::vector< GroupState > group_states_
Runtime state for each opened event group.
Definition: perf_data.h:223
vt::metrics::PerfData::stopTaskMeasurement
void stopTaskMeasurement()
Stop performance measurement for a task.
Definition: perf_data.cc:224
vt::metrics::PerfEventGroupInfo
Definition: perf_event_groups.h:59
vt::metrics::PerfData::name
std::string name() override
Get the component name.
Definition: perf_data.cc:334
vt::thePerfData
metrics::PerfData * thePerfData()
vt::metrics::PerfData::TaskGroupMeasurements::getScalingRatio
double getScalingRatio() const
Definition: perf_data.h:84
example_events.h
vt::metrics::PerfData::event_map_
PerfEventDescriptorMap event_map_
Map of event names to event type and configuration.
Definition: perf_data.h:208
vt::runtime::component::Component
Component class for a generic VT runtime module, CRTP'ed over the component's actual type
Definition: component.h:95
vt::metrics::PerfEventDescriptorMap
std::unordered_map< std::string, PerfEventDescriptor > PerfEventDescriptorMap
Definition: perf_event_groups.h:57
vt::metrics::PerfData::TaskGroupMeasurements::measurements_
std::unordered_map< std::string, uint64_t > measurements_
Definition: perf_data.h:82
vt::metrics::PerfData::getTaskGroupMeasurements
std::vector< TaskGroupMeasurements > getTaskGroupMeasurements()
Get grouped task measurements and raw perf multiplexing metadata.
Definition: perf_data.cc:317
vt::metrics::PerfData::TaskGroupMeasurements
Definition: perf_data.h:78
vt::metrics::PerfData::readTaskGroupMeasurements
std::vector< TaskGroupMeasurements > readTaskGroupMeasurements() const
Common implementation for grouped perf reads.
Definition: perf_data.cc:232
vt::metrics::PerfData::serialize
void serialize(SerializerT &s)
Serialize the PerfData object.
Definition: perf_data.h:192
component_pack.h
vt::metrics::PerfData::TaskGroupMeasurements::time_running_
uint64_t time_running_
Definition: perf_data.h:81
vt::metrics::PerfData::GroupState
Definition: perf_data.h:199
vt::metrics::PerfData::startup
void startup() override
Component startup method.
Definition: perf_data.cc:329
vt
Definition: activefn.h:51
vt::metrics::PerfData::open_fds_
std::vector< int > open_fds_
Flat list of open file descriptors to simplify cleanup.
Definition: perf_data.h:228
vt::metrics::PerfData::~PerfData
virtual ~PerfData()
Destructor for PerfData.
Definition: perf_data.cc:207
vt::metrics::PerfData::event_groups_
std::vector< PerfEventGroupInfo > event_groups_
Resolved event groups after explicit and automatic grouping.
Definition: perf_data.h:218
vt::metrics::PerfData::TaskGroupMeasurements::getRunningFraction
double getRunningFraction() const
Definition: perf_data.h:93
vt::metrics::PerfData::cleanupBeforeAbort
void cleanupBeforeAbort()
Cleanup resources before aborting.
Definition: perf_data.cc:336
vt::metrics::PerfData::GroupState::info_
PerfEventGroupInfo info_
Definition: perf_data.h:200
vt::metrics::PerfData::GroupState::leader_fd_
int leader_fd_
Definition: perf_data.h:201
vt::metrics::PerfData::TaskGroupMeasurements::group_
PerfEventGroupInfo group_
Definition: perf_data.h:79
vt::metrics::PerfData
Tracks performance metrics per task.
Definition: perf_data.h:75
context.h
config.h
vt::metrics::PerfData::getEventGroups
std::vector< PerfEventGroupInfo > getEventGroups() const
Retrieve resolved event grouping metadata.
Definition: perf_data.cc:325
vt::metrics::PerfData::maybePrintEventGroups
void maybePrintEventGroups() const
Print resolved perf event groups when requested by the environment.
Definition: perf_data.cc:121
vt::metrics
Definition: example_events.h:52
vt::metrics::PerfData::startTaskMeasurement
void startTaskMeasurement()
Start performance measurement for a task.
Definition: perf_data.cc:215