Skip to content

Commit

Permalink
Merge pull request iovisor#1099 from palmtenor/stylefix
Browse files Browse the repository at this point in the history
Miscellaneous style and code layout improvements
  • Loading branch information
4ast committed Apr 6, 2017
2 parents 0a24ba4 + 37c9f74 commit e2a0377
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 36 deletions.
40 changes: 24 additions & 16 deletions src/cc/BPF.cc
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ std::string sanitize_str(std::string str, bool (*validator)(char),
}

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;

for (auto u : usdt) {
Expand Down Expand Up @@ -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 + ": ";
Expand 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);
if (res.code() != 0) {
error_msg += "Failed to detach uprobe event " + it.first + ": ";
Expand 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);
if (res.code() != 0) {
error_msg += "Failed to detach Tracepoint " + it.first + ": ";
Expand 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();
if (res.code() != 0) {
error_msg += "Failed to close perf buffer " + it.first + ": ";
Expand All @@ -123,15 +124,15 @@ 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";
has_error = true;
}
}

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 + ": ";
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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));
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) + "_";
Expand Down Expand Up @@ -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;
Expand All @@ -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());

Expand All @@ -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);
Expand Down
11 changes: 3 additions & 8 deletions src/cc/BPF.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string> cflags = {},
std::vector<USDT> usdt = {});
const std::vector<std::string>& cflags = {},
const std::vector<USDT>& usdt = {});

~BPF();
StatusTuple detach_all();
Expand Down Expand Up @@ -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,
Expand Down
3 changes: 2 additions & 1 deletion src/cc/perf_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
* limitations under the License.
*/

#include <inttypes.h>
#include <poll.h>
#include <stdio.h>
#include <stdint.h>
Expand Down Expand Up @@ -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)
Expand Down
22 changes: 11 additions & 11 deletions tools/offwaketime_example.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)

0 comments on commit e2a0377

Please sign in to comment.