xref: /openbmc/linux/tools/testing/selftests/bpf/prog_tests/enable_stats.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
131a9f7feSSong Liu // SPDX-License-Identifier: GPL-2.0
231a9f7feSSong Liu #include <test_progs.h>
331a9f7feSSong Liu #include "test_enable_stats.skel.h"
431a9f7feSSong Liu 
test_enable_stats(void)531a9f7feSSong Liu void test_enable_stats(void)
631a9f7feSSong Liu {
731a9f7feSSong Liu 	struct test_enable_stats *skel;
831a9f7feSSong Liu 	int stats_fd, err, prog_fd;
931a9f7feSSong Liu 	struct bpf_prog_info info;
1031a9f7feSSong Liu 	__u32 info_len = sizeof(info);
1131a9f7feSSong Liu 	int duration = 0;
1231a9f7feSSong Liu 
1331a9f7feSSong Liu 	skel = test_enable_stats__open_and_load();
1431a9f7feSSong Liu 	if (CHECK(!skel, "skel_open_and_load", "skeleton open/load failed\n"))
1531a9f7feSSong Liu 		return;
1631a9f7feSSong Liu 
1731a9f7feSSong Liu 	stats_fd = bpf_enable_stats(BPF_STATS_RUN_TIME);
1831a9f7feSSong Liu 	if (CHECK(stats_fd < 0, "get_stats_fd", "failed %d\n", errno)) {
1931a9f7feSSong Liu 		test_enable_stats__destroy(skel);
2031a9f7feSSong Liu 		return;
2131a9f7feSSong Liu 	}
2231a9f7feSSong Liu 
2331a9f7feSSong Liu 	err = test_enable_stats__attach(skel);
2431a9f7feSSong Liu 	if (CHECK(err, "attach_raw_tp", "err %d\n", err))
2531a9f7feSSong Liu 		goto cleanup;
2631a9f7feSSong Liu 
2731a9f7feSSong Liu 	test_enable_stats__detach(skel);
2831a9f7feSSong Liu 
2931a9f7feSSong Liu 	prog_fd = bpf_program__fd(skel->progs.test_enable_stats);
3031a9f7feSSong Liu 	memset(&info, 0, info_len);
31*c5a237a4SIlya Leoshkevich 	err = bpf_prog_get_info_by_fd(prog_fd, &info, &info_len);
3231a9f7feSSong Liu 	if (CHECK(err, "get_prog_info",
3331a9f7feSSong Liu 		  "failed to get bpf_prog_info for fd %d\n", prog_fd))
3431a9f7feSSong Liu 		goto cleanup;
3531a9f7feSSong Liu 	if (CHECK(info.run_time_ns == 0, "check_stats_enabled",
3631a9f7feSSong Liu 		  "failed to enable run_time_ns stats\n"))
3731a9f7feSSong Liu 		goto cleanup;
3831a9f7feSSong Liu 
3931a9f7feSSong Liu 	CHECK(info.run_cnt != skel->bss->count, "check_run_cnt_valid",
4031a9f7feSSong Liu 	      "invalid run_cnt stats\n");
4131a9f7feSSong Liu 
4231a9f7feSSong Liu cleanup:
4331a9f7feSSong Liu 	test_enable_stats__destroy(skel);
4431a9f7feSSong Liu 	close(stats_fd);
4531a9f7feSSong Liu }
46