Skip to content

Commit

Permalink
define BPerfEventsManager for managing shared global perf events
Browse files Browse the repository at this point in the history
Summary: ^

Differential Revision: D62403504
  • Loading branch information
Alston Tang authored and facebook-github-bot committed Sep 11, 2024
1 parent 5d9352d commit 42b7ee2
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 22 deletions.
23 changes: 11 additions & 12 deletions hbt/src/perf_event/BPerfEventsGroup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ int BPerfEventsGroup::syncCpu_(__u32 cpu, int leader_fd) {
return ::bpf_prog_test_run_opts(leader_fd, &opts);
}

void BPerfEventsGroup::syncGlobal_() {
void BPerfEventsGroup::syncGlobal_() const {
int err;

for (int cpu = 0; cpu < cpu_cnt_; cpu++) {
Expand Down Expand Up @@ -497,7 +497,7 @@ int BPerfEventsGroup::preparePerThreadBPerf_(bperf_leader_cgroup* skel) {

std::vector<struct bpf_perf_event_value> BPerfEventsGroup::readFromBpf_(
int fd,
__u64 id) {
__u64 id) const {
std::vector<struct bpf_perf_event_value> values(
(size_t)cpu_cnt_ * BPERF_MAX_GROUP_SIZE);
if (!enabled_) {
Expand All @@ -518,7 +518,7 @@ int BPerfEventsGroup::read(
struct bpf_perf_event_value* output,
int fd,
__u64 id,
bool skip_offset) {
bool skip_offset) const {
auto event_cnt = confs_.size();

auto values = readFromBpf_(fd, id);
Expand Down Expand Up @@ -550,7 +550,7 @@ int BPerfEventsGroup::readPerCpu(
int,
std::array<struct bpf_perf_event_value, BPERF_MAX_GROUP_SIZE>>& output,
int fd,
__u64 id) {
__u64 id) const {
auto event_cnt = confs_.size();

auto values = readFromBpf_(fd, id);
Expand All @@ -569,11 +569,11 @@ int BPerfEventsGroup::readPerCpu(

int BPerfEventsGroup::readGlobal(
struct bpf_perf_event_value* output,
bool skip_offset) {
bool skip_offset) const {
return read(output, global_output_fd_, 0, skip_offset);
}

bool BPerfEventsGroup::readGlobal(ReadValues& rv, bool skip_offset) {
bool BPerfEventsGroup::readGlobal(ReadValues& rv, bool skip_offset) const {
const auto num_events = rv.getNumEvents();
HBT_ARG_CHECK_EQ(confs_.size(), rv.getNumEvents());

Expand All @@ -589,11 +589,11 @@ bool BPerfEventsGroup::readGlobal(ReadValues& rv, bool skip_offset) {
int BPerfEventsGroup::readGlobalPerCpu(
std::
map<int, std::array<struct bpf_perf_event_value, BPERF_MAX_GROUP_SIZE>>&
output) {
output) const {
return readPerCpu(output, global_output_fd_, 0);
}

bool BPerfEventsGroup::readGlobalPerCpu(std::map<int, ReadValues>& rv) {
bool BPerfEventsGroup::readGlobalPerCpu(std::map<int, ReadValues>& rv) const {
std::map<int, std::array<struct bpf_perf_event_value, BPERF_MAX_GROUP_SIZE>>
output;
int numEvents = readGlobalPerCpu(output);
Expand All @@ -604,13 +604,12 @@ bool BPerfEventsGroup::readGlobalPerCpu(std::map<int, ReadValues>& rv) {
return numEvents > 0;
}

int BPerfEventsGroup::readCgroup(
struct bpf_perf_event_value* output,
__u64 id) {
int BPerfEventsGroup::readCgroup(struct bpf_perf_event_value* output, __u64 id)
const {
return read(output, cgroup_output_fd_, id, true /* skip_offset */);
}

bool BPerfEventsGroup::readCgroup(ReadValues& rv, __u64 id) {
bool BPerfEventsGroup::readCgroup(ReadValues& rv, __u64 id) const {
const auto num_events = rv.getNumEvents();
HBT_ARG_CHECK_EQ(confs_.size(), rv.getNumEvents());

Expand Down
21 changes: 11 additions & 10 deletions hbt/src/perf_event/BPerfEventsGroup.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,17 +70,18 @@ class BPerfEventsGroup {
bool removeCgroup(__u64 id);

// eBPF like interface to read counters from all CPUs and accumulate them.
int readGlobal(struct bpf_perf_event_value* output, bool skip_offset = false);
bool readGlobal(ReadValues& rv, bool skip_offset = false);
int readGlobal(struct bpf_perf_event_value* output, bool skip_offset = false)
const;
bool readGlobal(ReadValues& rv, bool skip_offset = false) const;
int readGlobalPerCpu(
std::map<
int,
std::array<struct bpf_perf_event_value, BPERF_MAX_GROUP_SIZE>>&
output);
bool readGlobalPerCpu(std::map<int, ReadValues>& rv);
output) const;
bool readGlobalPerCpu(std::map<int, ReadValues>& rv) const;

int readCgroup(struct bpf_perf_event_value* output, __u64 id);
bool readCgroup(ReadValues& rv, __u64 id);
int readCgroup(struct bpf_perf_event_value* output, __u64 id) const;
bool readCgroup(ReadValues& rv, __u64 id) const;

protected:
const EventConfs confs_;
Expand Down Expand Up @@ -128,28 +129,28 @@ class BPerfEventsGroup {
// read(&offset_);
struct bpf_perf_event_value offsets_[BPERF_MAX_GROUP_SIZE];

std::vector<struct bpf_perf_event_value> readFromBpf_(int fd, __u64 id);
std::vector<struct bpf_perf_event_value> readFromBpf_(int fd, __u64 id) const;

int read(
struct bpf_perf_event_value* output,
int fd,
__u64 id,
bool skip_offset = false);
bool skip_offset = false) const;

int readPerCpu(
std::map<
int,
std::array<struct bpf_perf_event_value, BPERF_MAX_GROUP_SIZE>>&
output,
int fd,
__u64 id);
__u64 id) const;

int reloadSkel_(struct bperf_attr_map_elem* entry);
int loadPerfEvent_(struct bperf_leader_cgroup* skel);

static int syncCpu_(__u32 cpu, int leader_pd);
static void toReadValues(ReadValues& rv, struct bpf_perf_event_value*);
void syncGlobal_();
void syncGlobal_() const;

// For per thread monitoring
bool per_thread_;
Expand Down

0 comments on commit 42b7ee2

Please sign in to comment.