tools/perf/util/bpf_skel/sample_filter.bpf.c

Source file repositories/reference/linux-study-clean/tools/perf/util/bpf_skel/sample_filter.bpf.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/bpf_skel/sample_filter.bpf.c
Extension
.c
Size
7541 bytes
Lines
299
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

struct filters {
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, int);
	__type(value, struct perf_bpf_filter_entry[MAX_FILTERS]);
	__uint(max_entries, 1);
} filters SEC(".maps");

/*
 * An evsel has multiple instances for each CPU or task but we need a single
 * id to be used as a key for the idx_hash.  This hashmap would translate the
 * instance's ID to a representative ID.
 */
struct event_hash {
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, __u64);
	__type(value, __u64);
	__uint(max_entries, 1);
} event_hash SEC(".maps");

/* tgid/evtid to filter index */
struct idx_hash {
	__uint(type, BPF_MAP_TYPE_HASH);
	__type(key, struct idx_hash_key);
	__type(value, int);
	__uint(max_entries, 1);
} idx_hash SEC(".maps");

/* tgid to filter index */
struct lost_count {
	__uint(type, BPF_MAP_TYPE_ARRAY);
	__type(key, int);
	__type(value, int);
	__uint(max_entries, 1);
} dropped SEC(".maps");

volatile const int use_idx_hash;

void *bpf_cast_to_kern_ctx(void *) __ksym;

/* new kernel perf_sample_data definition */
struct perf_sample_data___new {
	__u64 sample_flags;
} __attribute__((preserve_access_index));

/* new kernel perf_mem_data_src definition */
union perf_mem_data_src___new {
	__u64 val;
	struct {
		__u64   mem_op:5,	/* type of opcode */
			mem_lvl:14,	/* memory hierarchy level */
			mem_snoop:5,	/* snoop mode */
			mem_lock:2,	/* lock instr */
			mem_dtlb:7,	/* tlb access */
			mem_lvl_num:4,	/* memory hierarchy level number */
			mem_remote:1,   /* remote */
			mem_snoopx:2,	/* snoop mode, ext */
			mem_blk:3,	/* access blocked */
			mem_hops:3,	/* hop level */
			mem_rsvd:18;
	};
};

/* helper function to return the given perf sample data */
static inline __u64 perf_get_sample(struct bpf_perf_event_data_kern *kctx,
				    struct perf_bpf_filter_entry *entry)
{
	struct perf_sample_data___new *data = (void *)kctx->data;

	if (!bpf_core_field_exists(data->sample_flags))
		return 0;

#define BUILD_CHECK_SAMPLE(x)					\
	_Static_assert((1 << (PBF_TERM_##x - PBF_TERM_SAMPLE_START)) == PERF_SAMPLE_##x, \
		"Mismatched PBF term to sample bit " #x)
	BUILD_CHECK_SAMPLE(IP);
	BUILD_CHECK_SAMPLE(TID);
	BUILD_CHECK_SAMPLE(TIME);
	BUILD_CHECK_SAMPLE(ADDR);
	BUILD_CHECK_SAMPLE(ID);
	BUILD_CHECK_SAMPLE(CPU);
	BUILD_CHECK_SAMPLE(PERIOD);
	BUILD_CHECK_SAMPLE(WEIGHT);
	BUILD_CHECK_SAMPLE(DATA_SRC);
	BUILD_CHECK_SAMPLE(TRANSACTION);
	BUILD_CHECK_SAMPLE(PHYS_ADDR);
	BUILD_CHECK_SAMPLE(CGROUP);
	BUILD_CHECK_SAMPLE(DATA_PAGE_SIZE);
	BUILD_CHECK_SAMPLE(CODE_PAGE_SIZE);
	BUILD_CHECK_SAMPLE(WEIGHT_STRUCT);
#undef BUILD_CHECK_SAMPLE

Annotation

Implementation Notes