Skip to content

Commit

Permalink
C++ interface for attaching to perf event with raw perf_event_attr ar…
Browse files Browse the repository at this point in the history
…gument
  • Loading branch information
palmtenor committed Mar 10, 2018
1 parent 5b7c678 commit 6c33a52
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 3 deletions.
51 changes: 49 additions & 2 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include <linux/bpf.h>
#include <linux/perf_event.h>
#include <unistd.h>
#include <cstdio>
#include <cstring>
Expand Down Expand Up @@ -292,12 +293,13 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
int probe_fd;
TRY2(load_func(probe_func, BPF_PROG_TYPE_PERF_EVENT, probe_fd));

auto fds = new std::map<int, int>();
std::vector<int> cpus;
if (cpu >= 0)
cpus.push_back(cpu);
else
cpus = get_online_cpus();
auto fds = new std::vector<std::pair<int, int>>();
fds->reserve(cpus.size());
for (int i : cpus) {
int fd = bpf_attach_perf_event(probe_fd, ev_type, ev_config, sample_period,
sample_freq, pid, i, group_fd);
Expand All @@ -309,7 +311,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
return StatusTuple(-1, "Failed to attach perf event type %d config %d",
ev_type, ev_config);
}
fds->emplace(i, fd);
fds->emplace_back(i, fd);
}

open_probe_t p = {};
Expand All @@ -319,6 +321,46 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config,
return StatusTuple(0);
}

StatusTuple BPF::attach_perf_event_raw(void* perf_event_attr,
const std::string& probe_func,
pid_t pid, int cpu, int group_fd) {
auto attr = static_cast<struct perf_event_attr*>(perf_event_attr);
auto ev_pair = std::make_pair(attr->type, attr->config);
if (perf_events_.find(ev_pair) != perf_events_.end())
return StatusTuple(-1, "Perf event type %d config %d already attached",
attr->type, attr->config);

int probe_fd;
TRY2(load_func(probe_func, BPF_PROG_TYPE_PERF_EVENT, probe_fd));

std::vector<int> cpus;
if (cpu >= 0)
cpus.push_back(cpu);
else
cpus = get_online_cpus();
auto fds = new std::vector<std::pair<int, int>>();
fds->reserve(cpus.size());
for (int i : cpus) {
int fd = bpf_attach_perf_event_raw(probe_fd, attr, pid, i, group_fd);
if (fd < 0) {
for (const auto& it : *fds)
close(it.second);
delete fds;
TRY2(unload_func(probe_func));
return StatusTuple(-1, "Failed to attach perf event type %d config %d",
attr->type, attr->config);
}
fds->emplace_back(i, fd);
}

open_probe_t p = {};
p.func = probe_func;
p.per_cpu_fd = fds;
perf_events_[ev_pair] = std::move(p);
return StatusTuple(0);

}

StatusTuple BPF::detach_kprobe(const std::string& kernel_func,
bpf_probe_attach_type attach_type) {
std::string event = get_kprobe_event(kernel_func, attach_type);
Expand Down Expand Up @@ -394,6 +436,11 @@ StatusTuple BPF::detach_perf_event(uint32_t ev_type, uint32_t ev_config) {
return StatusTuple(0);
}

StatusTuple BPF::detach_perf_event_raw(void* perf_event_attr) {
auto attr = static_cast<struct perf_event_attr*>(perf_event_attr);
return detach_perf_event(attr->type, attr->config);
}

StatusTuple BPF::open_perf_event(const std::string& name, uint32_t type,
uint64_t config) {
if (perf_event_arrays_.find(name) == perf_event_arrays_.end()) {
Expand Down
7 changes: 6 additions & 1 deletion src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace ebpf {
struct open_probe_t {
int perf_event_fd;
std::string func;
std::map<int, int>* per_cpu_fd;
std::vector<std::pair<int, int>>* per_cpu_fd;
};

class USDT;
Expand Down Expand Up @@ -84,7 +84,12 @@ class BPF {
uint64_t sample_period, uint64_t sample_freq,
pid_t pid = -1, int cpu = -1,
int group_fd = -1);
StatusTuple attach_perf_event_raw(void* perf_event_attr,
const std::string& probe_func,
pid_t pid = -1, int cpu = -1,
int group_fd = -1);
StatusTuple detach_perf_event(uint32_t ev_type, uint32_t ev_config);
StatusTuple detach_perf_event_raw(void* perf_event_attr);

BPFTable get_table(const std::string& name) {
TableStorage::iterator it;
Expand Down

0 comments on commit 6c33a52

Please sign in to comment.