-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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 <xiakaixu@huawei.com> Signed-off-by: David S. Miller <davem@davemloft.net>
- Loading branch information
Showing
4 changed files
with
100 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#include <linux/version.h> | ||
#include <uapi/linux/bpf.h> | ||
#include "bpf_helpers.h" | ||
|
||
struct bpf_map_def SEC("maps") my_map = { | ||
.type = BPF_MAP_TYPE_PERF_EVENT_ARRAY, | ||
.key_size = sizeof(int), | ||
.value_size = sizeof(u32), | ||
.max_entries = 32, | ||
}; | ||
|
||
SEC("kprobe/sys_write") | ||
int bpf_prog1(struct pt_regs *ctx) | ||
{ | ||
u64 count; | ||
u32 key = bpf_get_smp_processor_id(); | ||
char fmt[] = "CPU-%d %llu\n"; | ||
|
||
count = bpf_perf_event_read(&my_map, key); | ||
bpf_trace_printk(fmt, sizeof(fmt), key, count); | ||
|
||
return 0; | ||
} | ||
|
||
char _license[] SEC("license") = "GPL"; | ||
u32 _version SEC("version") = LINUX_VERSION_CODE; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <stdlib.h> | ||
#include <stdbool.h> | ||
#include <string.h> | ||
#include <fcntl.h> | ||
#include <poll.h> | ||
#include <sys/ioctl.h> | ||
#include <linux/perf_event.h> | ||
#include <linux/bpf.h> | ||
#include "libbpf.h" | ||
#include "bpf_load.h" | ||
|
||
#define SAMPLE_PERIOD 0x7fffffffffffffffULL | ||
|
||
static void test_bpf_perf_event(void) | ||
{ | ||
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF); | ||
int *pmu_fd = malloc(nr_cpus * sizeof(int)); | ||
unsigned long value; | ||
int i; | ||
|
||
struct perf_event_attr attr_insn_pmu = { | ||
.freq = 0, | ||
.sample_period = SAMPLE_PERIOD, | ||
.inherit = 0, | ||
.type = PERF_TYPE_HARDWARE, | ||
.read_format = 0, | ||
.sample_type = 0, | ||
.config = 0,/* PMU: cycles */ | ||
}; | ||
|
||
for (i = 0; i < nr_cpus; i++) { | ||
pmu_fd[i] = perf_event_open(&attr_insn_pmu, -1/*pid*/, i/*cpu*/, -1/*group_fd*/, 0); | ||
if (pmu_fd[i] < 0) | ||
printf("event syscall failed\n"); | ||
|
||
bpf_update_elem(map_fd[0], &i, &pmu_fd[i], BPF_ANY); | ||
ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0); | ||
} | ||
|
||
system("ls"); | ||
system("pwd"); | ||
system("sleep 2"); | ||
|
||
for (i = 0; i < nr_cpus; i++) | ||
close(pmu_fd[i]); | ||
|
||
close(map_fd); | ||
|
||
free(pmu_fd); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
char filename[256]; | ||
|
||
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); | ||
|
||
if (load_bpf_file(filename)) { | ||
printf("%s", bpf_log_buf); | ||
return 1; | ||
} | ||
|
||
test_bpf_perf_event(); | ||
|
||
return 0; | ||
} |