Skip to content

Commit

Permalink
Merge pull request iovisor#1620 from palmtenor/perf_buffer_ptr
Browse files Browse the repository at this point in the history
C++ API: Allow obtaining BPFPerfBuffer pointer for polling
  • Loading branch information
yonghong-song committed Mar 6, 2018
2 parents f34acfc + 570fd5e commit 66db879
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 8 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;
}
9 changes: 7 additions & 2 deletions src/cc/api/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -443,11 +443,16 @@ StatusTuple BPF::close_perf_buffer(const std::string& name) {
return StatusTuple(0);
}

void BPF::poll_perf_buffer(const std::string& name, int timeout) {
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())
return;
it->second->poll(timeout);
it->second->poll(timeout_ms);
}

StatusTuple BPF::load_func(const std::string& func_name, bpf_prog_type type,
Expand Down
11 changes: 10 additions & 1 deletion src/cc/api/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,21 @@ 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);
void poll_perf_buffer(const std::string& name, int timeout = -1);
// 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,
int& fd);
Expand Down
4 changes: 2 additions & 2 deletions src/cc/api/BPFTable.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,10 +295,10 @@ StatusTuple BPFPerfBuffer::close_all_cpu() {
return StatusTuple(0);
}

void BPFPerfBuffer::poll(int timeout) {
void BPFPerfBuffer::poll(int timeout_ms) {
if (epfd_ < 0)
return;
int cnt = epoll_wait(epfd_, ep_events_.get(), cpu_readers_.size(), timeout);
int cnt = epoll_wait(epfd_, ep_events_.get(), cpu_readers_.size(), timeout_ms);
if (cnt <= 0)
return;
for (int i = 0; i < cnt; i++)
Expand Down
2 changes: 1 addition & 1 deletion src/cc/api/BPFTable.h
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ class BPFPerfBuffer : public BPFTableBase<int, int> {
StatusTuple open_all_cpu(perf_reader_raw_cb cb, perf_reader_lost_cb lost_cb,
void* cb_cookie, int page_cnt);
StatusTuple close_all_cpu();
void poll(int timeout);
void poll(int timeout_ms);

private:
StatusTuple open_on_cpu(perf_reader_raw_cb cb, perf_reader_lost_cb lost_cb,
Expand Down

0 comments on commit 66db879

Please sign in to comment.