bpf: fix build warnings and add function read_trace_pipe()
There are two improvements in this patch: 1. Fix the build warnings; 2. Add function read_trace_pipe() to print the result on the screen; Before this patch, we can get the result through /sys/kernel/de bug/tracing/trace_pipe and get nothing on the screen. By applying this patch, the result can be printed on the screen. $ ./tracex6 ... tracex6-705 [003] d..1 131.428593: : CPU-3 19981414 sshd-683 [000] d..1 131.428727: : CPU-0 221682321 sshd-683 [000] d..1 131.428821: : CPU-0 221808766 sshd-683 [000] d..1 131.428950: : CPU-0 221982984 sshd-683 [000] d..1 131.429045: : CPU-0 222111851 tracex6-705 [003] d..1 131.429168: : CPU-3 20757551 sshd-683 [000] d..1 131.429170: : CPU-0 222281240 sshd-683 [000] d..1 131.429261: : CPU-0 222403340 sshd-683 [000] d..1 131.429378: : CPU-0 222561024 ... Signed-off-by: Kaixu Xia <xiakaixu@huawei.com> Acked-by: Alexei Starovoitov <ast@plumgrid.com> Signed-off-by: David S. Miller <davem@davemloft.net>
This commit is contained in:
committed by
David S. Miller
parent
47b344b27a
commit
5ed3ccbd5a
@@ -1,3 +1,4 @@
|
|||||||
|
#include <linux/ptrace.h>
|
||||||
#include <linux/version.h>
|
#include <linux/version.h>
|
||||||
#include <uapi/linux/bpf.h>
|
#include <uapi/linux/bpf.h>
|
||||||
#include "bpf_helpers.h"
|
#include "bpf_helpers.h"
|
||||||
|
@@ -17,8 +17,7 @@ static void test_bpf_perf_event(void)
|
|||||||
{
|
{
|
||||||
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
int nr_cpus = sysconf(_SC_NPROCESSORS_CONF);
|
||||||
int *pmu_fd = malloc(nr_cpus * sizeof(int));
|
int *pmu_fd = malloc(nr_cpus * sizeof(int));
|
||||||
unsigned long value;
|
int status, i;
|
||||||
int i;
|
|
||||||
|
|
||||||
struct perf_event_attr attr_insn_pmu = {
|
struct perf_event_attr attr_insn_pmu = {
|
||||||
.freq = 0,
|
.freq = 0,
|
||||||
@@ -32,22 +31,26 @@ static void test_bpf_perf_event(void)
|
|||||||
|
|
||||||
for (i = 0; i < nr_cpus; i++) {
|
for (i = 0; i < nr_cpus; i++) {
|
||||||
pmu_fd[i] = perf_event_open(&attr_insn_pmu, -1/*pid*/, i/*cpu*/, -1/*group_fd*/, 0);
|
pmu_fd[i] = perf_event_open(&attr_insn_pmu, -1/*pid*/, i/*cpu*/, -1/*group_fd*/, 0);
|
||||||
if (pmu_fd[i] < 0)
|
if (pmu_fd[i] < 0) {
|
||||||
printf("event syscall failed\n");
|
printf("event syscall failed\n");
|
||||||
|
goto exit;
|
||||||
|
}
|
||||||
|
|
||||||
bpf_update_elem(map_fd[0], &i, &pmu_fd[i], BPF_ANY);
|
bpf_update_elem(map_fd[0], &i, &pmu_fd[i], BPF_ANY);
|
||||||
ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
|
ioctl(pmu_fd[i], PERF_EVENT_IOC_ENABLE, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
system("ls");
|
status = system("ls > /dev/null");
|
||||||
system("pwd");
|
if (status)
|
||||||
system("sleep 2");
|
goto exit;
|
||||||
|
status = system("sleep 2");
|
||||||
|
if (status)
|
||||||
|
goto exit;
|
||||||
|
|
||||||
|
exit:
|
||||||
for (i = 0; i < nr_cpus; i++)
|
for (i = 0; i < nr_cpus; i++)
|
||||||
close(pmu_fd[i]);
|
close(pmu_fd[i]);
|
||||||
|
close(map_fd[0]);
|
||||||
close(map_fd);
|
|
||||||
|
|
||||||
free(pmu_fd);
|
free(pmu_fd);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -63,6 +66,7 @@ int main(int argc, char **argv)
|
|||||||
}
|
}
|
||||||
|
|
||||||
test_bpf_perf_event();
|
test_bpf_perf_event();
|
||||||
|
read_trace_pipe();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user