Commit e2a03777 authored by 4ast's avatar 4ast Committed by GitHub

Merge pull request #1099 from palmtenor/stylefix

Miscellaneous style and code layout improvements
parents 0a24ba41 37c9f74a
...@@ -53,7 +53,8 @@ std::string sanitize_str(std::string str, bool (*validator)(char), ...@@ -53,7 +53,8 @@ std::string sanitize_str(std::string str, bool (*validator)(char),
} }
StatusTuple BPF::init(const std::string& bpf_program, StatusTuple BPF::init(const std::string& bpf_program,
std::vector<std::string> cflags, std::vector<USDT> usdt) { const std::vector<std::string>& cflags,
const std::vector<USDT>& usdt) {
std::string all_bpf_program; std::string all_bpf_program;
for (auto u : usdt) { for (auto u : usdt) {
...@@ -86,7 +87,7 @@ StatusTuple BPF::detach_all() { ...@@ -86,7 +87,7 @@ StatusTuple BPF::detach_all() {
bool has_error = false; bool has_error = false;
std::string error_msg; std::string error_msg;
for (auto it : kprobes_) { for (auto& it : kprobes_) {
auto res = detach_kprobe_event(it.first, it.second); auto res = detach_kprobe_event(it.first, it.second);
if (res.code() != 0) { if (res.code() != 0) {
error_msg += "Failed to detach kprobe event " + it.first + ": "; error_msg += "Failed to detach kprobe event " + it.first + ": ";
...@@ -95,7 +96,7 @@ StatusTuple BPF::detach_all() { ...@@ -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); auto res = detach_uprobe_event(it.first, it.second);
if (res.code() != 0) { if (res.code() != 0) {
error_msg += "Failed to detach uprobe event " + it.first + ": "; error_msg += "Failed to detach uprobe event " + it.first + ": ";
...@@ -104,7 +105,7 @@ StatusTuple BPF::detach_all() { ...@@ -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); auto res = detach_tracepoint_event(it.first, it.second);
if (res.code() != 0) { if (res.code() != 0) {
error_msg += "Failed to detach Tracepoint " + it.first + ": "; error_msg += "Failed to detach Tracepoint " + it.first + ": ";
...@@ -113,7 +114,7 @@ StatusTuple BPF::detach_all() { ...@@ -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(); auto res = it.second->close_all_cpu();
if (res.code() != 0) { if (res.code() != 0) {
error_msg += "Failed to close perf buffer " + it.first + ": "; error_msg += "Failed to close perf buffer " + it.first + ": ";
...@@ -123,7 +124,7 @@ StatusTuple BPF::detach_all() { ...@@ -123,7 +124,7 @@ StatusTuple BPF::detach_all() {
delete it.second; delete it.second;
} }
for (auto it : perf_events_) { for (auto& it : perf_events_) {
auto res = detach_perf_event_all_cpu(it.second); auto res = detach_perf_event_all_cpu(it.second);
if (res.code() != 0) { if (res.code() != 0) {
error_msg += res.msg() + "\n"; error_msg += res.msg() + "\n";
...@@ -131,7 +132,7 @@ StatusTuple BPF::detach_all() { ...@@ -131,7 +132,7 @@ StatusTuple BPF::detach_all() {
} }
} }
for (auto it : funcs_) { for (auto& it : funcs_) {
int res = close(it.second); int res = close(it.second);
if (res != 0) { if (res != 0) {
error_msg += "Failed to unload BPF program for " + it.first + ": "; error_msg += "Failed to unload BPF program for " + it.first + ": ";
...@@ -216,7 +217,7 @@ StatusTuple BPF::attach_uprobe(const std::string& binary_path, ...@@ -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, StatusTuple BPF::attach_usdt(const USDT& usdt, pid_t pid, int cpu,
int group_fd) { int group_fd) {
for (auto& u : usdt_) for (const auto& u : usdt_)
if (u == usdt) { if (u == usdt) {
bool failed = false; bool failed = false;
std::string err_msg; std::string err_msg;
...@@ -301,7 +302,7 @@ StatusTuple BPF::attach_perf_event(uint32_t ev_type, uint32_t ev_config, ...@@ -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, int fd = bpf_attach_perf_event(probe_fd, ev_type, ev_config, sample_period,
sample_freq, pid, i, group_fd); sample_freq, pid, i, group_fd);
if (fd < 0) { if (fd < 0) {
for (auto it : *fds) for (const auto& it : *fds)
close(it.second); close(it.second);
delete fds; delete fds;
TRY2(unload_func(probe_func)); TRY2(unload_func(probe_func));
...@@ -352,7 +353,7 @@ StatusTuple BPF::detach_uprobe(const std::string& binary_path, ...@@ -352,7 +353,7 @@ StatusTuple BPF::detach_uprobe(const std::string& binary_path,
} }
StatusTuple BPF::detach_usdt(const USDT& usdt) { StatusTuple BPF::detach_usdt(const USDT& usdt) {
for (auto& u : usdt_) for (const auto& u : usdt_)
if (u == usdt) { if (u == usdt) {
bool failed = false; bool failed = false;
std::string err_msg; std::string err_msg;
...@@ -491,6 +492,13 @@ BPFProgTable BPF::get_prog_table(const std::string& name) { ...@@ -491,6 +492,13 @@ BPFProgTable BPF::get_prog_table(const std::string& name) {
return BPFProgTable({}); 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, std::string BPF::get_uprobe_event(const std::string& binary_path,
uint64_t offset, bpf_probe_attach_type type) { uint64_t offset, bpf_probe_attach_type type) {
std::string res = attach_type_prefix(type) + "_"; std::string res = attach_type_prefix(type) + "_";
...@@ -538,7 +546,7 @@ StatusTuple BPF::detach_tracepoint_event(const std::string& tracepoint, ...@@ -538,7 +546,7 @@ StatusTuple BPF::detach_tracepoint_event(const std::string& tracepoint,
StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) { StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) {
bool has_error = false; bool has_error = false;
std::string err_msg; std::string err_msg;
for (auto it : *attr.per_cpu_fd) { for (const auto& it : *attr.per_cpu_fd) {
int res = close(it.second); int res = close(it.second);
if (res < 0) { if (res < 0) {
has_error = true; has_error = true;
...@@ -556,11 +564,10 @@ StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) { ...@@ -556,11 +564,10 @@ StatusTuple BPF::detach_perf_event_all_cpu(open_probe_t& attr) {
} }
StatusTuple USDT::init() { StatusTuple USDT::init() {
auto ctx = ::USDT::Context ctx(binary_path_);
std::unique_ptr<::USDT::Context>(new ::USDT::Context(binary_path_)); if (!ctx.loaded())
if (!ctx->loaded())
return StatusTuple(-1, "Unable to load USDT " + print_name()); return StatusTuple(-1, "Unable to load USDT " + print_name());
auto probe = ctx->get(name_); auto probe = ctx.get(name_);
if (probe == nullptr) if (probe == nullptr)
return StatusTuple(-1, "Unable to find USDT " + print_name()); return StatusTuple(-1, "Unable to find USDT " + print_name());
...@@ -572,8 +579,9 @@ StatusTuple USDT::init() { ...@@ -572,8 +579,9 @@ StatusTuple USDT::init() {
-1, "Unable to generate program text for USDT " + print_name()); -1, "Unable to generate program text for USDT " + print_name());
program_text_ = ::USDT::USDT_PROGRAM_HEADER + stream.str(); program_text_ = ::USDT::USDT_PROGRAM_HEADER + stream.str();
addresses_.reserve(probe->num_locations());
for (size_t i = 0; i < probe->num_locations(); i++) for (size_t i = 0; i < probe->num_locations(); i++)
addresses_.push_back(probe->address(i)); addresses_.emplace_back(probe->address(i));
initialized_ = true; initialized_ = true;
return StatusTuple(0); return StatusTuple(0);
......
...@@ -47,8 +47,8 @@ public: ...@@ -47,8 +47,8 @@ public:
explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr) explicit BPF(unsigned int flag = 0, TableStorage* ts = nullptr)
: bpf_module_(new BPFModule(flag, ts)) {} : bpf_module_(new BPFModule(flag, ts)) {}
StatusTuple init(const std::string& bpf_program, StatusTuple init(const std::string& bpf_program,
std::vector<std::string> cflags = {}, const std::vector<std::string>& cflags = {},
std::vector<USDT> usdt = {}); const std::vector<USDT>& usdt = {});
~BPF(); ~BPF();
StatusTuple detach_all(); StatusTuple detach_all();
...@@ -108,12 +108,7 @@ public: ...@@ -108,12 +108,7 @@ public:
BPFProgTable get_prog_table(const std::string& name); BPFProgTable get_prog_table(const std::string& name);
BPFStackTable get_stack_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({});
}
StatusTuple open_perf_buffer(const std::string& name, StatusTuple open_perf_buffer(const std::string& name,
perf_reader_raw_cb cb, perf_reader_raw_cb cb,
......
...@@ -14,6 +14,7 @@ ...@@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include <inttypes.h>
#include <poll.h> #include <poll.h>
#include <stdio.h> #include <stdio.h>
#include <stdint.h> #include <stdint.h>
...@@ -243,7 +244,7 @@ void perf_reader_event_read(struct perf_reader *reader) { ...@@ -243,7 +244,7 @@ void perf_reader_event_read(struct perf_reader *reader) {
if (reader->lost_cb) { if (reader->lost_cb) {
reader->lost_cb(lost); reader->lost_cb(lost);
} else { } else {
fprintf(stderr, "Possibly lost %llu samples\n", lost); fprintf(stderr, "Possibly lost " PRIu64 " samples\n", lost);
} }
} else if (e->type == PERF_RECORD_SAMPLE) { } else if (e->type == PERF_RECORD_SAMPLE) {
if (reader->type == PERF_TYPE_TRACEPOINT) if (reader->type == PERF_TYPE_TRACEPOINT)
......
...@@ -4,7 +4,7 @@ Demonstrations of offwaketime, the Linux eBPF/bcc version. ...@@ -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 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 "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 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 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. 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 The combined stacks, task names, and total time is summarized in kernel context
...@@ -343,13 +343,13 @@ optional arguments: ...@@ -343,13 +343,13 @@ optional arguments:
examples: examples:
./offwaketime # trace off-CPU + waker stack time until Ctrl-C ./offwaketime # trace off-CPU + waker stack time until Ctrl-C
./offcputime 5 # trace for 5 seconds only ./offwaketime 5 # trace for 5 seconds only
./offcputime -f 5 # 5 seconds, and output in folded format ./offwaketime -f 5 # 5 seconds, and output in folded format
./offcputime -m 1000 # trace only events that last more than 1000 usec ./offwaketime -m 1000 # trace only events that last more than 1000 usec
./offcputime -M 10000 # trace only events that last less than 10000 usec ./offwaketime -M 10000 # trace only events that last less than 10000 usec
./offcputime -p 185 # only trace threads for PID 185 ./offwaketime -p 185 # only trace threads for PID 185
./offcputime -t 188 # only trace thread 188 ./offwaketime -t 188 # only trace thread 188
./offcputime -u # only trace user threads (no kernel) ./offwaketime -u # only trace user threads (no kernel)
./offcputime -k # only trace kernel threads (no user) ./offwaketime -k # only trace kernel threads (no user)
./offcputime -U # only show user space stacks (no kernel) ./offwaketime -U # only show user space stacks (no kernel)
./offcputime -K # only show kernel space stacks (no user) ./offwaketime -K # only show kernel space stacks (no user)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment