Skip to content

Commit 12fe122

Browse files
palmtenorborkmann
authored andcommitted
samples/bpf: add example to test reading address
This commit adds additional test in the trace_event example, by attaching the bpf program to MEM_UOPS_RETIRED.LOCK_LOADS event with PERF_SAMPLE_ADDR requested, and print the lock address value read from the bpf program to trace_pipe. Signed-off-by: Teng Qin <[email protected]> Signed-off-by: Daniel Borkmann <[email protected]>
1 parent 95da0cd commit 12fe122

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

samples/bpf/trace_event_kern.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ int bpf_prog1(struct bpf_perf_event_data *ctx)
3939
{
4040
char time_fmt1[] = "Time Enabled: %llu, Time Running: %llu";
4141
char time_fmt2[] = "Get Time Failed, ErrCode: %d";
42+
char addr_fmt[] = "Address recorded on event: %llx";
4243
char fmt[] = "CPU-%d period %lld ip %llx";
4344
u32 cpu = bpf_get_smp_processor_id();
4445
struct bpf_perf_event_value value_buf;
@@ -64,6 +65,9 @@ int bpf_prog1(struct bpf_perf_event_data *ctx)
6465
else
6566
bpf_trace_printk(time_fmt2, sizeof(time_fmt2), ret);
6667

68+
if (ctx->addr != 0)
69+
bpf_trace_printk(addr_fmt, sizeof(addr_fmt), ctx->addr);
70+
6771
val = bpf_map_lookup_elem(&counts, &key);
6872
if (val)
6973
(*val)++;

samples/bpf/trace_event_user.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,17 @@ static void test_bpf_perf_event(void)
215215
/* Intel Instruction Retired */
216216
.config = 0xc0,
217217
};
218+
struct perf_event_attr attr_type_raw_lock_load = {
219+
.sample_freq = SAMPLE_FREQ,
220+
.freq = 1,
221+
.type = PERF_TYPE_RAW,
222+
/* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
223+
.config = 0x21d0,
224+
/* Request to record lock address from PEBS */
225+
.sample_type = PERF_SAMPLE_ADDR,
226+
/* Record address value requires precise event */
227+
.precise_ip = 2,
228+
};
218229

219230
printf("Test HW_CPU_CYCLES\n");
220231
test_perf_event_all_cpu(&attr_type_hw);
@@ -236,6 +247,10 @@ static void test_bpf_perf_event(void)
236247
test_perf_event_all_cpu(&attr_type_raw);
237248
test_perf_event_task(&attr_type_raw);
238249

250+
printf("Test Lock Load\n");
251+
test_perf_event_all_cpu(&attr_type_raw_lock_load);
252+
test_perf_event_task(&attr_type_raw_lock_load);
253+
239254
printf("*** PASS ***\n");
240255
}
241256

0 commit comments

Comments
 (0)