From 0bd29aab56b153951596a59bced914f1c7ee8f7d Mon Sep 17 00:00:00 2001 From: Teng Qin Date: Mon, 5 Mar 2018 12:46:41 -0800 Subject: [PATCH 1/2] BPFPerfBuffer: Make it more clear that timeout is milliseconds --- src/cc/api/BPF.cc | 4 ++-- src/cc/api/BPF.h | 2 +- src/cc/api/BPFTable.cc | 4 ++-- src/cc/api/BPFTable.h | 2 +- 4 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/cc/api/BPF.cc b/src/cc/api/BPF.cc index a8861b9b0f23..7e0796b8eb01 100644 --- a/src/cc/api/BPF.cc +++ b/src/cc/api/BPF.cc @@ -443,11 +443,11 @@ StatusTuple BPF::close_perf_buffer(const std::string& name) { return StatusTuple(0); } -void BPF::poll_perf_buffer(const std::string& name, int timeout) { +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, diff --git a/src/cc/api/BPF.h b/src/cc/api/BPF.h index 0864ba3e01d7..20786d604c87 100644 --- a/src/cc/api/BPF.h +++ b/src/cc/api/BPF.h @@ -141,7 +141,7 @@ class BPF { void* cb_cookie = nullptr, int page_cnt = DEFAULT_PERF_BUFFER_PAGE_CNT); StatusTuple close_perf_buffer(const std::string& name); - void poll_perf_buffer(const std::string& name, int timeout = -1); + 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); diff --git a/src/cc/api/BPFTable.cc b/src/cc/api/BPFTable.cc index 14be59aaf944..8f709cfb23b1 100644 --- a/src/cc/api/BPFTable.cc +++ b/src/cc/api/BPFTable.cc @@ -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++) diff --git a/src/cc/api/BPFTable.h b/src/cc/api/BPFTable.h index db16ba0904e7..dee75371c14d 100644 --- a/src/cc/api/BPFTable.h +++ b/src/cc/api/BPFTable.h @@ -309,7 +309,7 @@ class BPFPerfBuffer : public BPFTableBase { 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, From 570fd5e33baca2cb06cf2b9ca064a6468b3b293b Mon Sep 17 00:00:00 2001 From: Teng Qin Date: Mon, 5 Mar 2018 12:23:34 -0800 Subject: [PATCH 2/2] Allow obtaining BPFPerfBuffer pointer for polling --- examples/cpp/FollyRequestContextSwitch.cc | 7 +++++-- src/cc/api/BPF.cc | 5 +++++ src/cc/api/BPF.h | 9 +++++++++ 3 files changed, 19 insertions(+), 2 deletions(-) diff --git a/examples/cpp/FollyRequestContextSwitch.cc b/examples/cpp/FollyRequestContextSwitch.cc index fe81e3a3a7f7..0031821d86e6 100644 --- a/examples/cpp/FollyRequestContextSwitch.cc +++ b/examples/cpp/FollyRequestContextSwitch.cc @@ -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; } diff --git a/src/cc/api/BPF.cc b/src/cc/api/BPF.cc index 7e0796b8eb01..b3c8e242c46d 100644 --- a/src/cc/api/BPF.cc +++ b/src/cc/api/BPF.cc @@ -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()) diff --git a/src/cc/api/BPF.h b/src/cc/api/BPF.h index 20786d604c87..3f2556f13262 100644 --- a/src/cc/api/BPF.h +++ b/src/cc/api/BPF.h @@ -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,