Skip to content

Commit 31a9f7f

Browse files
liu-song-6Alexei Starovoitov
authored andcommitted
bpf: Add selftest for BPF_ENABLE_STATS
Add test for BPF_ENABLE_STATS, which should enable run_time_ns stats. ~/selftests/bpf# ./test_progs -t enable_stats -v test_enable_stats:PASS:skel_open_and_load 0 nsec test_enable_stats:PASS:get_stats_fd 0 nsec test_enable_stats:PASS:attach_raw_tp 0 nsec test_enable_stats:PASS:get_prog_info 0 nsec test_enable_stats:PASS:check_stats_enabled 0 nsec test_enable_stats:PASS:check_run_cnt_valid 0 nsec Summary: 1/0 PASSED, 0 SKIPPED, 0 FAILED Signed-off-by: Song Liu <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Acked-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent 0bee106 commit 31a9f7f

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
#include <test_progs.h>
3+
#include "test_enable_stats.skel.h"
4+
5+
void test_enable_stats(void)
6+
{
7+
struct test_enable_stats *skel;
8+
int stats_fd, err, prog_fd;
9+
struct bpf_prog_info info;
10+
__u32 info_len = sizeof(info);
11+
int duration = 0;
12+
13+
skel = test_enable_stats__open_and_load();
14+
if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
15+
return;
16+
17+
stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
18+
if (CHECK(stats_fd < 0, "get_stats_fd", "failed %d\n", errno)) {
19+
test_enable_stats__destroy(skel);
20+
return;
21+
}
22+
23+
err = test_enable_stats__attach(skel);
24+
if (CHECK(err, "attach_raw_tp", "err %d\n", err))
25+
goto cleanup;
26+
27+
test_enable_stats__detach(skel);
28+
29+
prog_fd = bpf_program__fd(skel->progs.test_enable_stats);
30+
memset(&info, 0, info_len);
31+
err = bpf_obj_get_info_by_fd(prog_fd, &info, &info_len);
32+
if (CHECK(err, "get_prog_info",
33+
"failed to get bpf_prog_info for fd %d\n", prog_fd))
34+
goto cleanup;
35+
if (CHECK(info.run_time_ns == 0, "check_stats_enabled",
36+
"failed to enable run_time_ns stats\n"))
37+
goto cleanup;
38+
39+
CHECK(info.run_cnt != skel->bss->count, "check_run_cnt_valid",
40+
"invalid run_cnt stats\n");
41+
42+
cleanup:
43+
test_enable_stats__destroy(skel);
44+
close(stats_fd);
45+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// SPDX-License-Identifier: GPL-2.0
2+
// Copyright (c) 2020 Facebook
3+
4+
#include <linux/bpf.h>
5+
#include <stdint.h>
6+
#include <linux/types.h>
7+
#include <bpf/bpf_helpers.h>
8+
9+
char _license[] SEC("license") = "GPL";
10+
11+
__u64 count = 0;
12+
13+
SEC("raw_tracepoint/sys_enter")
14+
int test_enable_stats(void *ctx)
15+
{
16+
count += 1;
17+
return 0;
18+
}

0 commit comments

Comments
 (0)