-
Notifications
You must be signed in to change notification settings - Fork 7
/
perf_event.zig
82 lines (69 loc) · 2.75 KB
/
perf_event.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
const std = @import("std");
const root = @import("root.zig");
const print = std.debug.print;
const testing = std.testing;
const allocator = root.allocator;
const libbpf = root.libbpf;
test "perf_event" {
const bytes = @embedFile("@perf_event");
_ = libbpf.libbpf_set_print(root.dbg_printf);
const obj = libbpf.bpf_object__open_mem(bytes.ptr, bytes.len, null);
if (obj == null) {
print("failed to open bpf object: {}\n", .{std.posix.errno(-1)});
return error.OPEN;
}
defer libbpf.bpf_object__close(obj);
var ret = libbpf.bpf_object__load(obj);
if (ret != 0) {
print("failed to load bpf object: {}\n", .{std.posix.errno(-1)});
return error.LOAD;
}
if (libbpf.bpf_object__next_program(obj, null)) |prog| {
const my_pid = libbpf.bpf_object__find_map_by_name(obj, "my_pid").?;
// map[0] = current pid
const k: u32 = 0;
const v: u32 = std.Thread.getCurrentId();
ret = libbpf.bpf_map__update_elem(my_pid, &k, @sizeOf(@TypeOf(k)), &v, @sizeOf(@TypeOf(v)), 0);
if (ret != 0) {
print("failed update map element: {}\n", .{std.posix.errno(-1)});
return error.MAP_UPDATE;
}
const link = libbpf.bpf_program__attach(prog) orelse {
print("failed to attach prog {s}: {}\n", .{ libbpf.bpf_program__name(prog), std.posix.errno(-1) });
return error.ATTACH;
};
defer _ = libbpf.bpf_link__destroy(link);
// setup events perf buffer
const events = libbpf.bpf_object__find_map_by_name(obj, "events").?;
var got = std.ArrayList(u8).init(allocator);
defer got.deinit();
var ctx = Ctx{
.seen = 0,
.got = &got,
};
const perf_buf = libbpf.perf_buffer__new(libbpf.bpf_map__fd(events), 2, on_sample, null, &ctx, null).?;
defer libbpf.perf_buffer__free(perf_buf);
const expected_count = 3;
const expected_str = "hello" ** expected_count;
for (0..expected_count) |_| {
std.time.sleep(11);
}
ret = libbpf.perf_buffer__consume(perf_buf);
if (ret != 0) {
print("failed consume perf buffer: {}\n", .{std.posix.errno(-1)});
return error.PERF_BUF;
}
try testing.expectEqual(@as(@TypeOf(ctx.seen), @intCast(expected_count)), ctx.seen);
try testing.expectEqualStrings(expected_str, got.items);
}
}
const Ctx = extern struct {
seen: u32,
got: *std.ArrayList(u8),
};
fn on_sample(_ctx: ?*anyopaque, _: c_int, data: ?*anyopaque, _: u32) callconv(.C) void {
var ctx: *Ctx = @ptrCast(@alignCast(_ctx.?));
const s = std.mem.sliceTo(@as([*c]const u8, @ptrCast(data)), 0);
ctx.seen += 1;
ctx.got.appendSlice(s) catch unreachable;
}