1025bd7c7SSong Liu // SPDX-License-Identifier: GPL-2.0
2025bd7c7SSong Liu /* Copyright (c) 2021 Facebook */
3025bd7c7SSong Liu #include "vmlinux.h"
4025bd7c7SSong Liu #include <bpf/bpf_helpers.h>
5025bd7c7SSong Liu #include <bpf/bpf_tracing.h>
6025bd7c7SSong Liu 
7025bd7c7SSong Liu char _license[] SEC("license") = "GPL";
8025bd7c7SSong Liu 
9025bd7c7SSong Liu __u64 test1_hits = 0;
10025bd7c7SSong Liu __u64 address_low = 0;
11025bd7c7SSong Liu __u64 address_high = 0;
12025bd7c7SSong Liu int wasted_entries = 0;
13025bd7c7SSong Liu long total_entries = 0;
14025bd7c7SSong Liu 
15025bd7c7SSong Liu #define ENTRY_CNT 32
16025bd7c7SSong Liu struct perf_branch_entry entries[ENTRY_CNT] = {};
17025bd7c7SSong Liu 
gbs_in_range(__u64 val)18*f9bff0e3SMatthew Wilcox (Oracle) static inline bool gbs_in_range(__u64 val)
19025bd7c7SSong Liu {
20025bd7c7SSong Liu 	return (val >= address_low) && (val < address_high);
21025bd7c7SSong Liu }
22025bd7c7SSong Liu 
23025bd7c7SSong Liu SEC("fexit/bpf_testmod_loop_test")
BPF_PROG(test1,int n,int ret)24025bd7c7SSong Liu int BPF_PROG(test1, int n, int ret)
25025bd7c7SSong Liu {
26025bd7c7SSong Liu 	long i;
27025bd7c7SSong Liu 
28025bd7c7SSong Liu 	total_entries = bpf_get_branch_snapshot(entries, sizeof(entries), 0);
29025bd7c7SSong Liu 	total_entries /= sizeof(struct perf_branch_entry);
30025bd7c7SSong Liu 
31025bd7c7SSong Liu 	for (i = 0; i < ENTRY_CNT; i++) {
32025bd7c7SSong Liu 		if (i >= total_entries)
33025bd7c7SSong Liu 			break;
34*f9bff0e3SMatthew Wilcox (Oracle) 		if (gbs_in_range(entries[i].from) && gbs_in_range(entries[i].to))
35025bd7c7SSong Liu 			test1_hits++;
36025bd7c7SSong Liu 		else if (!test1_hits)
37025bd7c7SSong Liu 			wasted_entries++;
38025bd7c7SSong Liu 	}
39025bd7c7SSong Liu 	return 0;
40025bd7c7SSong Liu }
41