diff --git a/src/cc/BPF.cc b/src/cc/BPF.cc index 6628df4b33a3..dda6fd7a1cd6 100644 --- a/src/cc/BPF.cc +++ b/src/cc/BPF.cc @@ -53,7 +53,8 @@ std::string sanitize_str(std::string str, bool (*validator)(char), } StatusTuple BPF::init(const std::string& bpf_program, - std::vector cflags, std::vector usdt) { + const std::vector& cflags, + const std::vector& usdt) { std::string all_bpf_program; for (auto u : usdt) { @@ -86,7 +87,7 @@ StatusTuple BPF::detach_all() { bool has_error = false; std::string error_msg; - for (auto it : kprobes_) { + for (auto& it : kprobes_) { auto res = detach_kprobe_event(it.first, it.second); if (res.code() != 0) { error_msg += "Failed to detach kprobe event " + it.first + ": "; @@ -95,7 +96,7 @@ StatusTuple BPF::detach_all() { } } - for (auto it : uprobes_) { + for (auto& it : uprobes_) { auto res = detach_uprobe_event(it.first, it.second); if (res.code() != 0) { error_msg += "Failed to detach uprobe event " + it.first + ": "; @@ -104,7 +105,7 @@ StatusTuple BPF::detach_all() { } } - for (auto it : tracepoints_) { + for (auto& it : tracepoints_) { auto res = detach_tracepoint_event(it.first, it.second); if (res.code() != 0) { error_msg += "Failed to detach Tracepoint " + it.first + ": "; @@ -113,7 +114,7 @@ StatusTuple BPF::detach_all() { } } - for (auto it : perf_buffers_) { + for (auto& it : perf_buffers_) { auto res = it.second->close_all_cpu(); if (res.code() != 0) { error_msg += "Failed to close perf buffer " + it.first + ": "; @@ -123,7 +124,7 @@ StatusTuple BPF::detach_all() { delete it.second; } - for (auto it : perf_events_) { + for (auto& it : perf_events_) { auto res = detach_perf_event_all_cpu(it.second); if (res.code() != 0) { error_msg += res.msg() + "\n"; @@ -131,7 +132,7 @@ StatusTuple BPF::detach_all() { } } - for (auto it : funcs_) { + for (auto& it : funcs_) { int res = close(it.second); if (res != 0) { error_msg += "Failed to unload BPF program for " + it.first + ": "; @@ -216,7 +217,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path, StatusTuple BPF::attach_usdt(const USDT& usdt, pid_t pid, int cpu, int group_fd) { - for (auto& u : usdt_) + for (const auto& u : usdt_) if (u == usdt) { bool failed = false; std::string err_msg; @@ -301,7 +302,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config, int fd = bpf_attach_perf_event(probe_fd, ev_type, ev_config, sample_period, sample_freq, pid, i, group_fd); if (fd < 0) { - for (auto it : *fds) + for (const auto& it : *fds) close(it.second); delete fds; TRY2(unload_func(probe_func)); @@ -352,7 +353,7 @@ StatusTuple BPF::detach_uprobe(const std::string& binary_path, } StatusTuple BPF::detach_usdt(const USDT& usdt) { - for (auto& u : usdt_) + for (const auto& u : usdt_) if (u == usdt) { bool failed = false; std::string err_msg; @@ -491,6 +492,13 @@ BPFProgTable BPF::get_prog_table(const std::string& name) { return BPFProgTable({}); } +BPFStackTable BPF::get_stack_table(const std::string& name) { + TableStorage::iterator it; + if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it)) + return BPFStackTable(it->second); + return BPFStackTable({}); +} + std::string BPF::get_uprobe_event(const std::string& binary_path, uint64_t offset, bpf_probe_attach_type type) { std::string res = attach_type_prefix(type) + "_"; @@ -538,7 +546,7 @@ StatusTuple BPF::detach_tracepoint_event(const std::string& tracepoint, StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) { bool has_error = false; std::string err_msg; - for (auto it : *attr.per_cpu_fd) { + for (const auto& it : *attr.per_cpu_fd) { int res = close(it.second); if (res < 0) { has_error = true; @@ -556,11 +564,10 @@ StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) { } StatusTuple USDT::init() { - auto ctx = - std::unique_ptr<::USDT::Context>(new ::USDT::Context(binary_path_)); - if (!ctx->loaded()) + ::USDT::Context ctx(binary_path_); + if (!ctx.loaded()) return StatusTuple(-1, "Unable to load USDT " + print_name()); - auto probe = ctx->get(name_); + auto probe = ctx.get(name_); if (probe == nullptr) return StatusTuple(-1, "Unable to find USDT " + print_name()); @@ -572,8 +579,9 @@ StatusTuple USDT::init() { -1, "Unable to generate program text for USDT " + print_name()); program_text_ = ::USDT::USDT_PROGRAM_HEADER + stream.str(); + addresses_.reserve(probe->num_locations()); for (size_t i = 0; i < probe->num_locations(); i++) - addresses_.push_back(probe->address(i)); + addresses_.emplace_back(probe->address(i)); initialized_ = true; return StatusTuple(0); diff --git a/src/cc/BPF.h b/src/cc/BPF.h index 21c1ab274794..2fe35d55d8db 100644 --- a/src/cc/BPF.h +++ b/src/cc/BPF.h @@ -47,8 +47,8 @@ class BPF { explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr) : bpf_module_(new BPFModule(flag, ts)) {} StatusTuple init(const std::string& bpf_program, - std::vector cflags = {}, - std::vector usdt = {}); + const std::vector& cflags = {}, + const std::vector& usdt = {}); ~BPF(); StatusTuple detach_all(); @@ -108,12 +108,7 @@ class BPF { BPFProgTable get_prog_table(const std::string& name); - BPFStackTable get_stack_table(const std::string& name) { - TableStorage::iterator it; - if (bpf_module_->table_storage().Find(Path({bpf_module_->id(), name}), it)) - return BPFStackTable(it->second); - return BPFStackTable({}); - } + BPFStackTable get_stack_table(const std::string& name); StatusTuple open_perf_buffer(const std::string& name, perf_reader_raw_cb cb, diff --git a/src/cc/perf_reader.c b/src/cc/perf_reader.c index 9d40e3d883bf..2f6ddcf212ad 100644 --- a/src/cc/perf_reader.c +++ b/src/cc/perf_reader.c @@ -14,6 +14,7 @@ * limitations under the License. */ +#include #include #include #include @@ -243,7 +244,7 @@ void perf_reader_event_read(struct perf_reader *reader) { if (reader->lost_cb) { reader->lost_cb(lost); } else { - fprintf(stderr, "Possibly lost %llu samples\n", lost); + fprintf(stderr, "Possibly lost " PRIu64 " samples\n", lost); } } else if (e->type == PERF_RECORD_SAMPLE) { if (reader->type == PERF_TYPE_TRACEPOINT) diff --git a/tools/offwaketime_example.txt b/tools/offwaketime_example.txt index 16fa45f82427..72bec06babdb 100644 --- a/tools/offwaketime_example.txt +++ b/tools/offwaketime_example.txt @@ -4,7 +4,7 @@ Demonstrations of offwaketime, the Linux eBPF/bcc version. This program shows kernel stack traces and task names that were blocked and "off-CPU", along with the stack traces and task names for the threads that woke them, and the total elapsed time from when they blocked to when they were woken -up. This combines the summaries from both the offcputime and wakeuptime tools. +up. This combines the summaries from both the offwaketime and wakeuptime tools. The time measurement will be very similar to off-CPU time, however, off-CPU time may include a little extra time spent waiting on a run queue to be scheduled. The combined stacks, task names, and total time is summarized in kernel context @@ -343,13 +343,13 @@ optional arguments: examples: ./offwaketime # trace off-CPU + waker stack time until Ctrl-C - ./offcputime 5 # trace for 5 seconds only - ./offcputime -f 5 # 5 seconds, and output in folded format - ./offcputime -m 1000 # trace only events that last more than 1000 usec - ./offcputime -M 10000 # trace only events that last less than 10000 usec - ./offcputime -p 185 # only trace threads for PID 185 - ./offcputime -t 188 # only trace thread 188 - ./offcputime -u # only trace user threads (no kernel) - ./offcputime -k # only trace kernel threads (no user) - ./offcputime -U # only show user space stacks (no kernel) - ./offcputime -K # only show kernel space stacks (no user) + ./offwaketime 5 # trace for 5 seconds only + ./offwaketime -f 5 # 5 seconds, and output in folded format + ./offwaketime -m 1000 # trace only events that last more than 1000 usec + ./offwaketime -M 10000 # trace only events that last less than 10000 usec + ./offwaketime -p 185 # only trace threads for PID 185 + ./offwaketime -t 188 # only trace thread 188 + ./offwaketime -u # only trace user threads (no kernel) + ./offwaketime -k # only trace kernel threads (no user) + ./offwaketime -U # only show user space stacks (no kernel) + ./offwaketime -K # only show kernel space stacks (no user)