Skip to content

Commit

Permalink
Allow obtaining BPFPerfBuffer pointer for polling
Browse files Browse the repository at this point in the history
  • Loading branch information
palmtenor committed Mar 5, 2018
1 parent 0bd29aa commit 570fd5e
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
7 changes: 5 additions & 2 deletions examples/cpp/FollyRequestContextSwitch.cc
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,11 @@ int main(int argc, char** argv) {

signal(SIGINT, signal_handler);
std::cout << "Started tracing, hit Ctrl-C to terminate." << std::endl;
while (true)
bpf->poll_perf_buffer("events");
auto perf_buffer = bpf->get_perf_buffer("events");
if (perf_buffer)
while (true)
// 100ms timeout
perf_buffer->poll(100);

return 0;
}
5 changes: 5 additions & 0 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,6 +443,11 @@ StatusTuple BPF::close_perf_buffer(const std::string& name) {
return StatusTuple(0);
}

BPFPerfBuffer* BPF::get_perf_buffer(const std::string& name) {
auto it = perf_buffers_.find(name);
return (it == perf_buffers_.end()) ? nullptr : it->second;
}

void BPF::poll_perf_buffer(const std::string& name, int timeout_ms) {
auto it = perf_buffers_.find(name);
if (it == perf_buffers_.end())
Expand Down
9 changes: 9 additions & 0 deletions src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,20 @@ class BPF {

StatusTuple close_perf_event(const std::string& name);

// Open a Perf Buffer of given name, providing callback and callback cookie
// to use when polling. BPF class owns the opened Perf Buffer and will free
// it on-demand or on destruction.
StatusTuple open_perf_buffer(const std::string& name, perf_reader_raw_cb cb,
perf_reader_lost_cb lost_cb = nullptr,
void* cb_cookie = nullptr,
int page_cnt = DEFAULT_PERF_BUFFER_PAGE_CNT);
// Close and free the Perf Buffer of given name.
StatusTuple close_perf_buffer(const std::string& name);
// Obtain an pointer to the opened BPFPerfBuffer instance of given name.
// Will return nullptr if such open Perf Buffer doesn't exist.
BPFPerfBuffer* get_perf_buffer(const std::string& name);
// Poll an opened Perf Buffer of given name with given timeout, using callback
// provided when opening. Do nothing if such open Perf Buffer doesn't exist.
void poll_perf_buffer(const std::string& name, int timeout_ms = -1);

StatusTuple load_func(const std::string& func_name, enum bpf_prog_type type,
Expand Down

0 comments on commit 570fd5e

Please sign in to comment.