Skip to content

Commit 47efb30

Browse files
Kaixu Xiadavem330
authored andcommitted
samples/bpf: example of get selected PMU counter value
This is a simple example and shows how to use the new ability to get the selected Hardware PMU counter value. Signed-off-by: Kaixu Xia <[email protected]> Signed-off-by: David S. Miller <[email protected]>
1 parent 35578d7 commit 47efb30

File tree

4 files changed

+100
-0
lines changed

4 files changed

+100
-0
lines changed

samples/bpf/Makefile

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ hostprogs-y += tracex2
1212
hostprogs-y += tracex3
1313
hostprogs-y += tracex4
1414
hostprogs-y += tracex5
15+
hostprogs-y += tracex6
1516
hostprogs-y += lathist
1617

1718
test_verifier-objs := test_verifier.o libbpf.o
@@ -25,6 +26,7 @@ tracex2-objs := bpf_load.o libbpf.o tracex2_user.o
2526
tracex3-objs := bpf_load.o libbpf.o tracex3_user.o
2627
tracex4-objs := bpf_load.o libbpf.o tracex4_user.o
2728
tracex5-objs := bpf_load.o libbpf.o tracex5_user.o
29+
tracex6-objs := bpf_load.o libbpf.o tracex6_user.o
2830
lathist-objs := bpf_load.o libbpf.o lathist_user.o
2931

3032
# Tell kbuild to always build the programs
@@ -37,6 +39,7 @@ always += tracex2_kern.o
3739
always += tracex3_kern.o
3840
always += tracex4_kern.o
3941
always += tracex5_kern.o
42+
always += tracex6_kern.o
4043
always += tcbpf1_kern.o
4144
always += lathist_kern.o
4245

@@ -51,6 +54,7 @@ HOSTLOADLIBES_tracex2 += -lelf
5154
HOSTLOADLIBES_tracex3 += -lelf
5255
HOSTLOADLIBES_tracex4 += -lelf -lrt
5356
HOSTLOADLIBES_tracex5 += -lelf
57+
HOSTLOADLIBES_tracex6 += -lelf
5458
HOSTLOADLIBES_lathist += -lelf
5559

5660
# point this to your LLVM backend with bpf support

samples/bpf/bpf_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ static unsigned long long (*bpf_get_current_uid_gid)(void) =
3131
(void *) BPF_FUNC_get_current_uid_gid;
3232
static int (*bpf_get_current_comm)(void *buf, int buf_size) =
3333
(void *) BPF_FUNC_get_current_comm;
34+
static int (*bpf_perf_event_read)(void *map, int index) =
35+
(void *) BPF_FUNC_perf_event_read;
3436

3537
/* llvm builtin functions that eBPF C program may use to
3638
* emit BPF_LD_ABS and BPF_LD_IND instructions

samples/bpf/tracex6_kern.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#include <linux/version.h>
2+
#include <uapi/linux/bpf.h>
3+
#include "bpf_helpers.h"
4+
5+
struct bpf_map_def SEC("maps") my_map = {
6+
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY,
7+
.key_size = sizeof(int),
8+
.value_size = sizeof(u32),
9+
.max_entries = 32,
10+
};
11+
12+
SEC("kprobe/sys_write")
13+
int bpf_prog1(struct pt_regs *ctx)
14+
{
15+
u64 count;
16+
u32 key = bpf_get_smp_processor_id();
17+
char fmt[] = "CPU-%d %llu\n";
18+
19+
count = bpf_perf_event_read(&my_map, key);
20+
bpf_trace_printk(fmt, sizeof(fmt), key, count);
21+
22+
return 0;
23+
}
24+
25+
char _license[] SEC("license") = "GPL";
26+
u32 _version SEC("version") = LINUX_VERSION_CODE;

samples/bpf/tracex6_user.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#include <stdio.h>
2+
#include <unistd.h>
3+
#include <stdlib.h>
4+
#include <stdbool.h>
5+
#include <string.h>
6+
#include <fcntl.h>
7+
#include <poll.h>
8+
#include <sys/ioctl.h>
9+
#include <linux/perf_event.h>
10+
#include <linux/bpf.h>
11+
#include "libbpf.h"
12+
#include "bpf_load.h"
13+
14+
#define SAMPLE_PERIOD 0x7fffffffffffffffULL
15+
16+
static void test_bpf_perf_event(void)
17+
{
18+
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
19+
int *pmu_fd = malloc(nr_cpus * sizeof(int));
20+
unsigned long value;
21+
int i;
22+
23+
struct perf_event_attr attr_insn_pmu = {
24+
.freq = 0,
25+
.sample_period = SAMPLE_PERIOD,
26+
.inherit = 0,
27+
.type = PERF_TYPE_HARDWARE,
28+
.read_format = 0,
29+
.sample_type = 0,
30+
.config = 0,/* PMU: cycles */
31+
};
32+
33+
for (i = 0; i < nr_cpus; i++) {
34+
pmu_fd[i] = perf_event_open(&attr_insn_pmu, -1/*pid*/, i/*cpu*/, -1/*group_fd*/, 0);
35+
if (pmu_fd[i] < 0)
36+
printf("event syscall failed\n");
37+
38+
bpf_update_elem(map_fd[0], &i, &pmu_fd[i], BPF_ANY);
39+
ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
40+
}
41+
42+
system("ls");
43+
system("pwd");
44+
system("sleep 2");
45+
46+
for (i = 0; i < nr_cpus; i++)
47+
close(pmu_fd[i]);
48+
49+
close(map_fd);
50+
51+
free(pmu_fd);
52+
}
53+
54+
int main(int argc, char **argv)
55+
{
56+
char filename[256];
57+
58+
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
59+
60+
if (load_bpf_file(filename)) {
61+
printf("%s", bpf_log_buf);
62+
return 1;
63+
}
64+
65+
test_bpf_perf_event();
66+
67+
return 0;
68+
}

0 commit comments

Comments
 (0)