vt 1.6.0
(Virtual Transport)
Loading...
Searching...
No Matches
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
63namespace vt::metrics {
64
66
76{
77public:
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
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_
196 }
197
198private:
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
265namespace vt {
266
268
269} // end namespace vt
270
271#endif /*INCLUDED_VT_METRICS_PERF_DATA_H*/
Definition example_events.h:52
std::unordered_map< std::string, PerfEventDescriptor > PerfEventDescriptorMap
Definition perf_event_groups.h:56
Definition activefn.h:51
metrics::PerfData * thePerfData()
Definition perf_data.h:199
std::unordered_map< uint64_t, std::string > event_ids_
Definition perf_data.h:202
PerfEventGroupInfo info_
Definition perf_data.h:200
int leader_fd_
Definition perf_data.h:201
double getScalingRatio() const
Definition perf_data.h:84
std::unordered_map< std::string, uint64_t > measurements_
Definition perf_data.h:82
uint64_t time_enabled_
Definition perf_data.h:80
double getRunningFraction() const
Definition perf_data.h:93
uint64_t time_running_
Definition perf_data.h:81
PerfEventGroupInfo group_
Definition perf_data.h:79
Tracks performance metrics per task.
Definition perf_data.h:76
void startup() override
Component startup method.
Definition perf_data.cc:329
PerfEventDescriptorMap event_map_
Map of event names to event type and configuration.
Definition perf_data.h:208
std::string name() override
Get the component name.
Definition perf_data.cc:334
std::vector< std::string > event_names_
List of event names being tracked.
Definition perf_data.h:213
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
std::vector< PerfEventGroupInfo > event_groups_
Resolved event groups after explicit and automatic grouping.
Definition perf_data.h:218
std::unordered_map< std::string, uint64_t > getTaskMeasurements()
Get the measurements collected during the task execution.
Definition perf_data.cc:300
std::vector< TaskGroupMeasurements > readTaskGroupMeasurements() const
Common implementation for grouped perf reads.
Definition perf_data.cc:232
void serialize(SerializerT &s)
Serialize the PerfData object.
Definition perf_data.h:192
void maybePrintEventGroups() const
Print resolved perf event groups when requested by the environment.
Definition perf_data.cc:121
virtual ~PerfData()
Destructor for PerfData.
Definition perf_data.cc:207
void cleanupBeforeAbort()
Cleanup resources before aborting.
Definition perf_data.cc:336
void startTaskMeasurement()
Start performance measurement for a task.
Definition perf_data.cc:215
std::vector< GroupState > group_states_
Runtime state for each opened event group.
Definition perf_data.h:223
std::unordered_map< std::string, PerfEventDescriptor > getEventMap() const
Retrieve the current event map.
Definition perf_data.cc:321
PerfData()
Constructor for PerfData.
Definition perf_data.cc:142
std::vector< TaskGroupMeasurements > getTaskGroupMeasurements()
Get grouped task measurements and raw perf multiplexing metadata.
Definition perf_data.cc:317
void stopTaskMeasurement()
Stop performance measurement for a task.
Definition perf_data.cc:224
std::vector< PerfEventGroupInfo > getEventGroups() const
Retrieve resolved event grouping metadata.
Definition perf_data.cc:325
std::vector< int > open_fds_
Flat list of open file descriptors to simplify cleanup.
Definition perf_data.h:228
Definition perf_event_groups.h:59
Component class for a generic VT runtime module, CRTP'ed over the component's actual type
Definition component.h:95