Skip to content

Commit

Permalink
tools/trace: add max count support
Browse files Browse the repository at this point in the history
  • Loading branch information
tw4452852 committed Jan 22, 2025
1 parent eb70ab9 commit 700041e
Showing 1 changed file with 19 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/tools/trace/trace.zig
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ fn usage() void {
\\ --testing
\\ --lbr
\\ --vmlinux [/path/to/vmlinux]
\\ --count [n]
++ "\n", .{});
}

Expand Down Expand Up @@ -71,6 +72,7 @@ pub fn main() !void {

var seconds: ?usize = null;
var vmlinux_path: ?[]const u8 = null;
var max_count: ?usize = null;
while (nextArg(args, &arg_idx)) |arg| {
if (std.mem.eql(u8, arg, "--timeout")) {
const s = nextArg(args, &arg_idx) orelse {
Expand All @@ -90,6 +92,12 @@ pub fn main() !void {
usage();
return error.ARGS;
};
} else if (std.mem.eql(u8, arg, "--count")) {
const s = nextArg(args, &arg_idx) orelse {
usage();
return error.ARGS;
};
max_count = try std.fmt.parseUnsigned(usize, s, 0);
} else {
print("unknown parameter\n", .{});
usage();
Expand Down Expand Up @@ -157,12 +165,22 @@ pub fn main() !void {
_ = testing_call(1, 2);
}
const begin_ts = std.time.timestamp();
var consumed: usize = 0;
while (!exiting) {
ret = libbpf.ring_buffer__poll(ring_buf, 100);
if (max_count) |max| {
ret = libbpf.ring_buffer__consume_n(ring_buf, max - consumed);
} else {
ret = libbpf.ring_buffer__poll(ring_buf, 100);
}

if (ret < 0) {
return error.POLL;
}
consumed += @intCast(ret);

if (max_count) |max| {
if (consumed >= max) break;
}

if (seconds) |timeout| {
const cur_ts = std.time.timestamp();
Expand Down

0 comments on commit 700041e

Please sign in to comment.