Commit 3bbe0869 authored by Alexei Starovoitov's avatar Alexei Starovoitov Committed by Daniel Borkmann

selftests/bpf: test for bpf_get_stackid() from raw tracepoints

similar to traditional traceopint test add bpf_get_stackid() test
from raw tracepoints
and reduce verbosity of existing stackmap test
Signed-off-by: default avatarAlexei Starovoitov <ast@kernel.org>
Signed-off-by: default avatarDaniel Borkmann <daniel@iogearbox.net>
parent 4662a4e5
...@@ -877,7 +877,7 @@ static void test_stacktrace_map() ...@@ -877,7 +877,7 @@ static void test_stacktrace_map()
err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd); err = bpf_prog_load(file, BPF_PROG_TYPE_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno)) if (CHECK(err, "prog_load", "err %d errno %d\n", err, errno))
goto out; return;
/* Get the ID for the sched/sched_switch tracepoint */ /* Get the ID for the sched/sched_switch tracepoint */
snprintf(buf, sizeof(buf), snprintf(buf, sizeof(buf),
...@@ -888,8 +888,7 @@ static void test_stacktrace_map() ...@@ -888,8 +888,7 @@ static void test_stacktrace_map()
bytes = read(efd, buf, sizeof(buf)); bytes = read(efd, buf, sizeof(buf));
close(efd); close(efd);
if (CHECK(bytes <= 0 || bytes >= sizeof(buf), if (bytes <= 0 || bytes >= sizeof(buf))
"read", "bytes %d errno %d\n", bytes, errno))
goto close_prog; goto close_prog;
/* Open the perf event and attach bpf progrram */ /* Open the perf event and attach bpf progrram */
...@@ -906,29 +905,24 @@ static void test_stacktrace_map() ...@@ -906,29 +905,24 @@ static void test_stacktrace_map()
goto close_prog; goto close_prog;
err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0); err = ioctl(pmu_fd, PERF_EVENT_IOC_ENABLE, 0);
if (CHECK(err, "perf_event_ioc_enable", "err %d errno %d\n", if (err)
err, errno)) goto disable_pmu;
goto close_pmu;
err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd); err = ioctl(pmu_fd, PERF_EVENT_IOC_SET_BPF, prog_fd);
if (CHECK(err, "perf_event_ioc_set_bpf", "err %d errno %d\n", if (err)
err, errno))
goto disable_pmu; goto disable_pmu;
/* find map fds */ /* find map fds */
control_map_fd = bpf_find_map(__func__, obj, "control_map"); control_map_fd = bpf_find_map(__func__, obj, "control_map");
if (CHECK(control_map_fd < 0, "bpf_find_map control_map", if (control_map_fd < 0)
"err %d errno %d\n", err, errno))
goto disable_pmu; goto disable_pmu;
stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap"); stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
if (CHECK(stackid_hmap_fd < 0, "bpf_find_map stackid_hmap", if (stackid_hmap_fd < 0)
"err %d errno %d\n", err, errno))
goto disable_pmu; goto disable_pmu;
stackmap_fd = bpf_find_map(__func__, obj, "stackmap"); stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
if (CHECK(stackmap_fd < 0, "bpf_find_map stackmap", "err %d errno %d\n", if (stackmap_fd < 0)
err, errno))
goto disable_pmu; goto disable_pmu;
/* give some time for bpf program run */ /* give some time for bpf program run */
...@@ -945,24 +939,78 @@ static void test_stacktrace_map() ...@@ -945,24 +939,78 @@ static void test_stacktrace_map()
err = compare_map_keys(stackid_hmap_fd, stackmap_fd); err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap", if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
"err %d errno %d\n", err, errno)) "err %d errno %d\n", err, errno))
goto disable_pmu; goto disable_pmu_noerr;
err = compare_map_keys(stackmap_fd, stackid_hmap_fd); err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap", if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
"err %d errno %d\n", err, errno)) "err %d errno %d\n", err, errno))
; /* fall through */ goto disable_pmu_noerr;
goto disable_pmu_noerr;
disable_pmu: disable_pmu:
error_cnt++;
disable_pmu_noerr:
ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE); ioctl(pmu_fd, PERF_EVENT_IOC_DISABLE);
close_pmu:
close(pmu_fd); close(pmu_fd);
close_prog: close_prog:
bpf_object__close(obj); bpf_object__close(obj);
}
out: static void test_stacktrace_map_raw_tp()
return; {
int control_map_fd, stackid_hmap_fd, stackmap_fd;
const char *file = "./test_stacktrace_map.o";
int efd, err, prog_fd;
__u32 key, val, duration = 0;
struct bpf_object *obj;
err = bpf_prog_load(file, BPF_PROG_TYPE_RAW_TRACEPOINT, &obj, &prog_fd);
if (CHECK(err, "prog_load raw tp", "err %d errno %d\n", err, errno))
return;
efd = bpf_raw_tracepoint_open("sched_switch", prog_fd);
if (CHECK(efd < 0, "raw_tp_open", "err %d errno %d\n", efd, errno))
goto close_prog;
/* find map fds */
control_map_fd = bpf_find_map(__func__, obj, "control_map");
if (control_map_fd < 0)
goto close_prog;
stackid_hmap_fd = bpf_find_map(__func__, obj, "stackid_hmap");
if (stackid_hmap_fd < 0)
goto close_prog;
stackmap_fd = bpf_find_map(__func__, obj, "stackmap");
if (stackmap_fd < 0)
goto close_prog;
/* give some time for bpf program run */
sleep(1);
/* disable stack trace collection */
key = 0;
val = 1;
bpf_map_update_elem(control_map_fd, &key, &val, 0);
/* for every element in stackid_hmap, we can find a corresponding one
* in stackmap, and vise versa.
*/
err = compare_map_keys(stackid_hmap_fd, stackmap_fd);
if (CHECK(err, "compare_map_keys stackid_hmap vs. stackmap",
"err %d errno %d\n", err, errno))
goto close_prog;
err = compare_map_keys(stackmap_fd, stackid_hmap_fd);
if (CHECK(err, "compare_map_keys stackmap vs. stackid_hmap",
"err %d errno %d\n", err, errno))
goto close_prog;
goto close_prog_noerr;
close_prog:
error_cnt++;
close_prog_noerr:
bpf_object__close(obj);
} }
static int extract_build_id(char *build_id, size_t size) static int extract_build_id(char *build_id, size_t size)
...@@ -1138,6 +1186,7 @@ int main(void) ...@@ -1138,6 +1186,7 @@ int main(void)
test_tp_attach_query(); test_tp_attach_query();
test_stacktrace_map(); test_stacktrace_map();
test_stacktrace_build_id(); test_stacktrace_build_id();
test_stacktrace_map_raw_tp();
printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt); printf("Summary: %d PASSED, %d FAILED\n", pass_cnt, error_cnt);
return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS; return error_cnt ? EXIT_FAILURE : EXIT_SUCCESS;
......
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