2019-05-28 11:10:09 -06:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-09-01 19:37:25 -06:00
|
|
|
/* Copyright (c) 2016 Facebook
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdbool.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <linux/perf_event.h>
|
|
|
|
#include <linux/bpf.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/resource.h>
|
2020-03-21 04:04:24 -06:00
|
|
|
#include <bpf/bpf.h>
|
2020-01-20 06:06:49 -07:00
|
|
|
#include <bpf/libbpf.h>
|
2016-12-08 19:46:19 -07:00
|
|
|
#include "perf-sys.h"
|
2018-04-28 23:28:13 -06:00
|
|
|
#include "trace_helpers.h"
|
2016-09-01 19:37:25 -06:00
|
|
|
|
|
|
|
#define SAMPLE_FREQ 50
|
|
|
|
|
2020-03-21 04:04:24 -06:00
|
|
|
static int pid;
|
|
|
|
/* counts, stackmap */
|
|
|
|
static int map_fd[2];
|
|
|
|
struct bpf_program *prog;
|
2016-09-01 19:37:25 -06:00
|
|
|
static bool sys_read_seen, sys_write_seen;
|
|
|
|
|
|
|
|
static void print_ksym(__u64 addr)
|
|
|
|
{
|
|
|
|
struct ksym *sym;
|
|
|
|
|
|
|
|
if (!addr)
|
|
|
|
return;
|
|
|
|
sym = ksym_search(addr);
|
2019-04-03 16:17:56 -06:00
|
|
|
if (!sym) {
|
|
|
|
printf("ksym not found. Is kallsyms loaded?\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
printf("%s;", sym->name);
|
2019-12-05 01:01:13 -07:00
|
|
|
if (!strstr(sym->name, "sys_read"))
|
2016-09-01 19:37:25 -06:00
|
|
|
sys_read_seen = true;
|
2019-12-05 01:01:13 -07:00
|
|
|
else if (!strstr(sym->name, "sys_write"))
|
2016-09-01 19:37:25 -06:00
|
|
|
sys_write_seen = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_addr(__u64 addr)
|
|
|
|
{
|
|
|
|
if (!addr)
|
|
|
|
return;
|
|
|
|
printf("%llx;", addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
#define TASK_COMM_LEN 16
|
|
|
|
|
|
|
|
struct key_t {
|
|
|
|
char comm[TASK_COMM_LEN];
|
|
|
|
__u32 kernstack;
|
|
|
|
__u32 userstack;
|
|
|
|
};
|
|
|
|
|
|
|
|
static void print_stack(struct key_t *key, __u64 count)
|
|
|
|
{
|
|
|
|
__u64 ip[PERF_MAX_STACK_DEPTH] = {};
|
|
|
|
static bool warned;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
printf("%3lld %s;", count, key->comm);
|
samples/bpf: Make samples more libbpf-centric
Switch all of the sample code to use the function names from
tools/lib/bpf so that they're consistent with that, and to declare their
own log buffers. This allow the next commit to be purely devoted to
getting rid of the duplicate library in samples/bpf.
Committer notes:
Testing it:
On a fedora rawhide container, with clang/llvm 3.9, sharing the host
linux kernel git tree:
# make O=/tmp/build/linux/ headers_install
# make O=/tmp/build/linux -C samples/bpf/
Since I forgot to make it privileged, just tested it outside the
container, using what it generated:
# uname -a
Linux jouet 4.9.0-rc8+ #1 SMP Mon Dec 12 11:20:49 BRT 2016 x86_64 x86_64 x86_64 GNU/Linux
# cd /var/lib/docker/devicemapper/mnt/c43e09a53ff56c86a07baf79847f00e2cc2a17a1e2220e1adbf8cbc62734feda/rootfs/tmp/build/linux/samples/bpf/
# ls -la offwaketime
-rwxr-xr-x. 1 root root 24200 Dec 15 12:19 offwaketime
# file offwaketime
offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c940d3f127d5e66cdd680e42d885cb0b64f8a0e4, not stripped
# readelf -SW offwaketime_kern.o | grep PROGBITS
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8
[ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8
[ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4
[ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1
[ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4
# ./offwaketime | head -5
swapper/1;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 106
CPU 0/KVM;entry_SYSCALL_64_fastpath;sys_ioctl;do_vfs_ioctl;kvm_vcpu_ioctl;kvm_arch_vcpu_ioctl_run;kvm_vcpu_block;schedule;__schedule;-;try_to_wake_up;swake_up_locked;swake_up;apic_timer_expired;apic_timer_fn;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter;call_cpuidle;cpu_startup_entry;start_secondary;;swapper/3 2
Compositor;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;futex_requeue;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;SoftwareVsyncTh 5
firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 13
JS Helper;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;firefox 2
#
Signed-off-by: Joe Stringer <joe@ovn.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: netdev@vger.kernel.org
Link: http://lkml.kernel.org/r/20161214224342.12858-2-joe@ovn.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-12-14 15:43:38 -07:00
|
|
|
if (bpf_map_lookup_elem(map_fd[1], &key->kernstack, ip) != 0) {
|
2016-09-01 19:37:25 -06:00
|
|
|
printf("---;");
|
|
|
|
} else {
|
|
|
|
for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
|
|
|
|
print_ksym(ip[i]);
|
|
|
|
}
|
|
|
|
printf("-;");
|
samples/bpf: Make samples more libbpf-centric
Switch all of the sample code to use the function names from
tools/lib/bpf so that they're consistent with that, and to declare their
own log buffers. This allow the next commit to be purely devoted to
getting rid of the duplicate library in samples/bpf.
Committer notes:
Testing it:
On a fedora rawhide container, with clang/llvm 3.9, sharing the host
linux kernel git tree:
# make O=/tmp/build/linux/ headers_install
# make O=/tmp/build/linux -C samples/bpf/
Since I forgot to make it privileged, just tested it outside the
container, using what it generated:
# uname -a
Linux jouet 4.9.0-rc8+ #1 SMP Mon Dec 12 11:20:49 BRT 2016 x86_64 x86_64 x86_64 GNU/Linux
# cd /var/lib/docker/devicemapper/mnt/c43e09a53ff56c86a07baf79847f00e2cc2a17a1e2220e1adbf8cbc62734feda/rootfs/tmp/build/linux/samples/bpf/
# ls -la offwaketime
-rwxr-xr-x. 1 root root 24200 Dec 15 12:19 offwaketime
# file offwaketime
offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c940d3f127d5e66cdd680e42d885cb0b64f8a0e4, not stripped
# readelf -SW offwaketime_kern.o | grep PROGBITS
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8
[ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8
[ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4
[ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1
[ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4
# ./offwaketime | head -5
swapper/1;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 106
CPU 0/KVM;entry_SYSCALL_64_fastpath;sys_ioctl;do_vfs_ioctl;kvm_vcpu_ioctl;kvm_arch_vcpu_ioctl_run;kvm_vcpu_block;schedule;__schedule;-;try_to_wake_up;swake_up_locked;swake_up;apic_timer_expired;apic_timer_fn;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter;call_cpuidle;cpu_startup_entry;start_secondary;;swapper/3 2
Compositor;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;futex_requeue;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;SoftwareVsyncTh 5
firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 13
JS Helper;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;firefox 2
#
Signed-off-by: Joe Stringer <joe@ovn.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: netdev@vger.kernel.org
Link: http://lkml.kernel.org/r/20161214224342.12858-2-joe@ovn.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-12-14 15:43:38 -07:00
|
|
|
if (bpf_map_lookup_elem(map_fd[1], &key->userstack, ip) != 0) {
|
2016-09-01 19:37:25 -06:00
|
|
|
printf("---;");
|
|
|
|
} else {
|
|
|
|
for (i = PERF_MAX_STACK_DEPTH - 1; i >= 0; i--)
|
|
|
|
print_addr(ip[i]);
|
|
|
|
}
|
2017-06-02 22:03:53 -06:00
|
|
|
if (count < 6)
|
|
|
|
printf("\r");
|
|
|
|
else
|
|
|
|
printf("\n");
|
2016-09-01 19:37:25 -06:00
|
|
|
|
|
|
|
if (key->kernstack == -EEXIST && !warned) {
|
|
|
|
printf("stackmap collisions seen. Consider increasing size\n");
|
|
|
|
warned = true;
|
|
|
|
} else if ((int)key->kernstack < 0 && (int)key->userstack < 0) {
|
|
|
|
printf("err stackid %d %d\n", key->kernstack, key->userstack);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-21 04:04:24 -06:00
|
|
|
static void err_exit(int err)
|
2016-09-01 19:37:25 -06:00
|
|
|
{
|
2020-03-21 04:04:24 -06:00
|
|
|
kill(pid, SIGKILL);
|
|
|
|
exit(err);
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void print_stacks(void)
|
|
|
|
{
|
|
|
|
struct key_t key = {}, next_key;
|
|
|
|
__u64 value;
|
|
|
|
__u32 stackid = 0, next_id;
|
2020-03-21 04:04:24 -06:00
|
|
|
int error = 1, fd = map_fd[0], stack_map = map_fd[1];
|
2016-09-01 19:37:25 -06:00
|
|
|
|
|
|
|
sys_read_seen = sys_write_seen = false;
|
samples/bpf: Make samples more libbpf-centric
Switch all of the sample code to use the function names from
tools/lib/bpf so that they're consistent with that, and to declare their
own log buffers. This allow the next commit to be purely devoted to
getting rid of the duplicate library in samples/bpf.
Committer notes:
Testing it:
On a fedora rawhide container, with clang/llvm 3.9, sharing the host
linux kernel git tree:
# make O=/tmp/build/linux/ headers_install
# make O=/tmp/build/linux -C samples/bpf/
Since I forgot to make it privileged, just tested it outside the
container, using what it generated:
# uname -a
Linux jouet 4.9.0-rc8+ #1 SMP Mon Dec 12 11:20:49 BRT 2016 x86_64 x86_64 x86_64 GNU/Linux
# cd /var/lib/docker/devicemapper/mnt/c43e09a53ff56c86a07baf79847f00e2cc2a17a1e2220e1adbf8cbc62734feda/rootfs/tmp/build/linux/samples/bpf/
# ls -la offwaketime
-rwxr-xr-x. 1 root root 24200 Dec 15 12:19 offwaketime
# file offwaketime
offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c940d3f127d5e66cdd680e42d885cb0b64f8a0e4, not stripped
# readelf -SW offwaketime_kern.o | grep PROGBITS
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8
[ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8
[ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4
[ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1
[ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4
# ./offwaketime | head -5
swapper/1;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 106
CPU 0/KVM;entry_SYSCALL_64_fastpath;sys_ioctl;do_vfs_ioctl;kvm_vcpu_ioctl;kvm_arch_vcpu_ioctl_run;kvm_vcpu_block;schedule;__schedule;-;try_to_wake_up;swake_up_locked;swake_up;apic_timer_expired;apic_timer_fn;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter;call_cpuidle;cpu_startup_entry;start_secondary;;swapper/3 2
Compositor;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;futex_requeue;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;SoftwareVsyncTh 5
firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 13
JS Helper;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;firefox 2
#
Signed-off-by: Joe Stringer <joe@ovn.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: netdev@vger.kernel.org
Link: http://lkml.kernel.org/r/20161214224342.12858-2-joe@ovn.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-12-14 15:43:38 -07:00
|
|
|
while (bpf_map_get_next_key(fd, &key, &next_key) == 0) {
|
|
|
|
bpf_map_lookup_elem(fd, &next_key, &value);
|
2016-09-01 19:37:25 -06:00
|
|
|
print_stack(&next_key, value);
|
samples/bpf: Make samples more libbpf-centric
Switch all of the sample code to use the function names from
tools/lib/bpf so that they're consistent with that, and to declare their
own log buffers. This allow the next commit to be purely devoted to
getting rid of the duplicate library in samples/bpf.
Committer notes:
Testing it:
On a fedora rawhide container, with clang/llvm 3.9, sharing the host
linux kernel git tree:
# make O=/tmp/build/linux/ headers_install
# make O=/tmp/build/linux -C samples/bpf/
Since I forgot to make it privileged, just tested it outside the
container, using what it generated:
# uname -a
Linux jouet 4.9.0-rc8+ #1 SMP Mon Dec 12 11:20:49 BRT 2016 x86_64 x86_64 x86_64 GNU/Linux
# cd /var/lib/docker/devicemapper/mnt/c43e09a53ff56c86a07baf79847f00e2cc2a17a1e2220e1adbf8cbc62734feda/rootfs/tmp/build/linux/samples/bpf/
# ls -la offwaketime
-rwxr-xr-x. 1 root root 24200 Dec 15 12:19 offwaketime
# file offwaketime
offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c940d3f127d5e66cdd680e42d885cb0b64f8a0e4, not stripped
# readelf -SW offwaketime_kern.o | grep PROGBITS
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8
[ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8
[ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4
[ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1
[ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4
# ./offwaketime | head -5
swapper/1;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 106
CPU 0/KVM;entry_SYSCALL_64_fastpath;sys_ioctl;do_vfs_ioctl;kvm_vcpu_ioctl;kvm_arch_vcpu_ioctl_run;kvm_vcpu_block;schedule;__schedule;-;try_to_wake_up;swake_up_locked;swake_up;apic_timer_expired;apic_timer_fn;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter;call_cpuidle;cpu_startup_entry;start_secondary;;swapper/3 2
Compositor;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;futex_requeue;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;SoftwareVsyncTh 5
firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 13
JS Helper;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;firefox 2
#
Signed-off-by: Joe Stringer <joe@ovn.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: netdev@vger.kernel.org
Link: http://lkml.kernel.org/r/20161214224342.12858-2-joe@ovn.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-12-14 15:43:38 -07:00
|
|
|
bpf_map_delete_elem(fd, &next_key);
|
2016-09-01 19:37:25 -06:00
|
|
|
key = next_key;
|
|
|
|
}
|
2017-06-02 22:03:53 -06:00
|
|
|
printf("\n");
|
2016-09-01 19:37:25 -06:00
|
|
|
if (!sys_read_seen || !sys_write_seen) {
|
|
|
|
printf("BUG kernel stack doesn't contain sys_read() and sys_write()\n");
|
2020-03-21 04:04:24 -06:00
|
|
|
err_exit(error);
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
/* clear stack map */
|
samples/bpf: Make samples more libbpf-centric
Switch all of the sample code to use the function names from
tools/lib/bpf so that they're consistent with that, and to declare their
own log buffers. This allow the next commit to be purely devoted to
getting rid of the duplicate library in samples/bpf.
Committer notes:
Testing it:
On a fedora rawhide container, with clang/llvm 3.9, sharing the host
linux kernel git tree:
# make O=/tmp/build/linux/ headers_install
# make O=/tmp/build/linux -C samples/bpf/
Since I forgot to make it privileged, just tested it outside the
container, using what it generated:
# uname -a
Linux jouet 4.9.0-rc8+ #1 SMP Mon Dec 12 11:20:49 BRT 2016 x86_64 x86_64 x86_64 GNU/Linux
# cd /var/lib/docker/devicemapper/mnt/c43e09a53ff56c86a07baf79847f00e2cc2a17a1e2220e1adbf8cbc62734feda/rootfs/tmp/build/linux/samples/bpf/
# ls -la offwaketime
-rwxr-xr-x. 1 root root 24200 Dec 15 12:19 offwaketime
# file offwaketime
offwaketime: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamically linked, interpreter /lib64/ld-linux-x86-64.so.2, for GNU/Linux 2.6.32, BuildID[sha1]=c940d3f127d5e66cdd680e42d885cb0b64f8a0e4, not stripped
# readelf -SW offwaketime_kern.o | grep PROGBITS
[ 2] .text PROGBITS 0000000000000000 000040 000000 00 AX 0 0 4
[ 3] kprobe/try_to_wake_up PROGBITS 0000000000000000 000040 0000d8 00 AX 0 0 8
[ 5] tracepoint/sched/sched_switch PROGBITS 0000000000000000 000118 000318 00 AX 0 0 8
[ 7] maps PROGBITS 0000000000000000 000430 000050 00 WA 0 0 4
[ 8] license PROGBITS 0000000000000000 000480 000004 00 WA 0 0 1
[ 9] version PROGBITS 0000000000000000 000484 000004 00 WA 0 0 4
# ./offwaketime | head -5
swapper/1;start_secondary;cpu_startup_entry;schedule_preempt_disabled;schedule;__schedule;-;---;; 106
CPU 0/KVM;entry_SYSCALL_64_fastpath;sys_ioctl;do_vfs_ioctl;kvm_vcpu_ioctl;kvm_arch_vcpu_ioctl_run;kvm_vcpu_block;schedule;__schedule;-;try_to_wake_up;swake_up_locked;swake_up;apic_timer_expired;apic_timer_fn;__hrtimer_run_queues;hrtimer_interrupt;local_apic_timer_interrupt;smp_apic_timer_interrupt;__irqentry_text_start;cpuidle_enter;call_cpuidle;cpu_startup_entry;start_secondary;;swapper/3 2
Compositor;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;futex_requeue;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;SoftwareVsyncTh 5
firefox;entry_SYSCALL_64_fastpath;sys_poll;do_sys_poll;poll_schedule_timeout;schedule_hrtimeout_range;schedule_hrtimeout_range_clock;schedule;__schedule;-;try_to_wake_up;pollwake;__wake_up_common;__wake_up_sync_key;pipe_write;__vfs_write;vfs_write;sys_write;entry_SYSCALL_64_fastpath;;Timer 13
JS Helper;entry_SYSCALL_64_fastpath;sys_futex;do_futex;futex_wait;futex_wait_queue_me;schedule;__schedule;-;try_to_wake_up;do_futex;sys_futex;entry_SYSCALL_64_fastpath;;firefox 2
#
Signed-off-by: Joe Stringer <joe@ovn.org>
Tested-by: Arnaldo Carvalho de Melo <acme@redhat.com>
Cc: Alexei Starovoitov <ast@fb.com>
Cc: Daniel Borkmann <daniel@iogearbox.net>
Cc: Wang Nan <wangnan0@huawei.com>
Cc: netdev@vger.kernel.org
Link: http://lkml.kernel.org/r/20161214224342.12858-2-joe@ovn.org
Signed-off-by: Arnaldo Carvalho de Melo <acme@redhat.com>
2016-12-14 15:43:38 -07:00
|
|
|
while (bpf_map_get_next_key(stack_map, &stackid, &next_id) == 0) {
|
|
|
|
bpf_map_delete_elem(stack_map, &next_id);
|
2016-09-01 19:37:25 -06:00
|
|
|
stackid = next_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-04 07:36:37 -06:00
|
|
|
static inline int generate_load(void)
|
|
|
|
{
|
|
|
|
if (system("dd if=/dev/zero of=/dev/null count=5000k status=none") < 0) {
|
|
|
|
printf("failed to generate some load with dd: %s\n", strerror(errno));
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
static void test_perf_event_all_cpu(struct perf_event_attr *attr)
|
|
|
|
{
|
2020-03-21 04:04:24 -06:00
|
|
|
int nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
struct bpf_link **links = calloc(nr_cpus, sizeof(struct bpf_link *));
|
|
|
|
int i, pmu_fd, error = 1;
|
|
|
|
|
|
|
|
if (!links) {
|
|
|
|
printf("malloc of links failed\n");
|
|
|
|
goto err;
|
|
|
|
}
|
2016-09-01 19:37:25 -06:00
|
|
|
|
2017-10-05 10:19:23 -06:00
|
|
|
/* system wide perf event, no need to inherit */
|
|
|
|
attr->inherit = 0;
|
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
/* open perf_event on all cpus */
|
|
|
|
for (i = 0; i < nr_cpus; i++) {
|
2020-03-21 04:04:24 -06:00
|
|
|
pmu_fd = sys_perf_event_open(attr, -1, i, -1, 0);
|
|
|
|
if (pmu_fd < 0) {
|
2016-12-08 19:46:19 -07:00
|
|
|
printf("sys_perf_event_open failed\n");
|
2016-09-01 19:37:25 -06:00
|
|
|
goto all_cpu_err;
|
|
|
|
}
|
2020-03-21 04:04:24 -06:00
|
|
|
links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
|
2020-05-15 22:06:04 -06:00
|
|
|
if (libbpf_get_error(links[i])) {
|
2020-03-21 04:04:24 -06:00
|
|
|
printf("bpf_program__attach_perf_event failed\n");
|
|
|
|
links[i] = NULL;
|
|
|
|
close(pmu_fd);
|
|
|
|
goto all_cpu_err;
|
|
|
|
}
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
2018-07-04 07:36:37 -06:00
|
|
|
|
2020-03-21 04:04:24 -06:00
|
|
|
if (generate_load() < 0)
|
2018-07-04 07:36:37 -06:00
|
|
|
goto all_cpu_err;
|
2020-03-21 04:04:24 -06:00
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
print_stacks();
|
2020-03-21 04:04:24 -06:00
|
|
|
error = 0;
|
2016-09-01 19:37:25 -06:00
|
|
|
all_cpu_err:
|
2020-03-21 04:04:24 -06:00
|
|
|
for (i--; i >= 0; i--)
|
|
|
|
bpf_link__destroy(links[i]);
|
|
|
|
err:
|
|
|
|
free(links);
|
2017-06-02 22:03:53 -06:00
|
|
|
if (error)
|
2020-03-21 04:04:24 -06:00
|
|
|
err_exit(error);
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_perf_event_task(struct perf_event_attr *attr)
|
|
|
|
{
|
2020-03-21 04:04:24 -06:00
|
|
|
struct bpf_link *link = NULL;
|
|
|
|
int pmu_fd, error = 1;
|
2016-09-01 19:37:25 -06:00
|
|
|
|
2017-10-05 10:19:23 -06:00
|
|
|
/* per task perf event, enable inherit so the "dd ..." command can be traced properly.
|
|
|
|
* Enabling inherit will cause bpf_perf_prog_read_time helper failure.
|
|
|
|
*/
|
|
|
|
attr->inherit = 1;
|
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
/* open task bound event */
|
2016-12-08 19:46:19 -07:00
|
|
|
pmu_fd = sys_perf_event_open(attr, 0, -1, -1, 0);
|
2016-09-01 19:37:25 -06:00
|
|
|
if (pmu_fd < 0) {
|
2016-12-08 19:46:19 -07:00
|
|
|
printf("sys_perf_event_open failed\n");
|
2020-03-21 04:04:24 -06:00
|
|
|
goto err;
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
2020-03-21 04:04:24 -06:00
|
|
|
link = bpf_program__attach_perf_event(prog, pmu_fd);
|
2020-05-15 22:06:04 -06:00
|
|
|
if (libbpf_get_error(link)) {
|
2020-03-21 04:04:24 -06:00
|
|
|
printf("bpf_program__attach_perf_event failed\n");
|
|
|
|
link = NULL;
|
|
|
|
close(pmu_fd);
|
2018-07-04 07:36:37 -06:00
|
|
|
goto err;
|
|
|
|
}
|
2020-03-21 04:04:24 -06:00
|
|
|
|
|
|
|
if (generate_load() < 0)
|
|
|
|
goto err;
|
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
print_stacks();
|
2020-03-21 04:04:24 -06:00
|
|
|
error = 0;
|
2018-07-04 07:36:37 -06:00
|
|
|
err:
|
2020-03-21 04:04:24 -06:00
|
|
|
bpf_link__destroy(link);
|
2018-07-04 07:36:37 -06:00
|
|
|
if (error)
|
2020-03-21 04:04:24 -06:00
|
|
|
err_exit(error);
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
static void test_bpf_perf_event(void)
|
|
|
|
{
|
|
|
|
struct perf_event_attr attr_type_hw = {
|
|
|
|
.sample_freq = SAMPLE_FREQ,
|
|
|
|
.freq = 1,
|
|
|
|
.type = PERF_TYPE_HARDWARE,
|
|
|
|
.config = PERF_COUNT_HW_CPU_CYCLES,
|
|
|
|
};
|
|
|
|
struct perf_event_attr attr_type_sw = {
|
|
|
|
.sample_freq = SAMPLE_FREQ,
|
|
|
|
.freq = 1,
|
|
|
|
.type = PERF_TYPE_SOFTWARE,
|
|
|
|
.config = PERF_COUNT_SW_CPU_CLOCK,
|
|
|
|
};
|
2017-06-02 22:03:53 -06:00
|
|
|
struct perf_event_attr attr_hw_cache_l1d = {
|
|
|
|
.sample_freq = SAMPLE_FREQ,
|
|
|
|
.freq = 1,
|
|
|
|
.type = PERF_TYPE_HW_CACHE,
|
|
|
|
.config =
|
|
|
|
PERF_COUNT_HW_CACHE_L1D |
|
|
|
|
(PERF_COUNT_HW_CACHE_OP_READ << 8) |
|
|
|
|
(PERF_COUNT_HW_CACHE_RESULT_ACCESS << 16),
|
|
|
|
};
|
|
|
|
struct perf_event_attr attr_hw_cache_branch_miss = {
|
|
|
|
.sample_freq = SAMPLE_FREQ,
|
|
|
|
.freq = 1,
|
|
|
|
.type = PERF_TYPE_HW_CACHE,
|
|
|
|
.config =
|
|
|
|
PERF_COUNT_HW_CACHE_BPU |
|
|
|
|
(PERF_COUNT_HW_CACHE_OP_READ << 8) |
|
|
|
|
(PERF_COUNT_HW_CACHE_RESULT_MISS << 16),
|
|
|
|
};
|
|
|
|
struct perf_event_attr attr_type_raw = {
|
|
|
|
.sample_freq = SAMPLE_FREQ,
|
|
|
|
.freq = 1,
|
|
|
|
.type = PERF_TYPE_RAW,
|
|
|
|
/* Intel Instruction Retired */
|
|
|
|
.config = 0xc0,
|
|
|
|
};
|
2018-03-06 11:55:02 -07:00
|
|
|
struct perf_event_attr attr_type_raw_lock_load = {
|
|
|
|
.sample_freq = SAMPLE_FREQ,
|
|
|
|
.freq = 1,
|
|
|
|
.type = PERF_TYPE_RAW,
|
|
|
|
/* Intel MEM_UOPS_RETIRED.LOCK_LOADS */
|
|
|
|
.config = 0x21d0,
|
|
|
|
/* Request to record lock address from PEBS */
|
|
|
|
.sample_type = PERF_SAMPLE_ADDR,
|
|
|
|
/* Record address value requires precise event */
|
|
|
|
.precise_ip = 2,
|
|
|
|
};
|
2016-09-01 19:37:25 -06:00
|
|
|
|
2017-06-02 22:03:53 -06:00
|
|
|
printf("Test HW_CPU_CYCLES\n");
|
2016-09-01 19:37:25 -06:00
|
|
|
test_perf_event_all_cpu(&attr_type_hw);
|
|
|
|
test_perf_event_task(&attr_type_hw);
|
2017-06-02 22:03:53 -06:00
|
|
|
|
|
|
|
printf("Test SW_CPU_CLOCK\n");
|
2016-09-01 19:37:25 -06:00
|
|
|
test_perf_event_all_cpu(&attr_type_sw);
|
|
|
|
test_perf_event_task(&attr_type_sw);
|
2017-06-02 22:03:53 -06:00
|
|
|
|
|
|
|
printf("Test HW_CACHE_L1D\n");
|
|
|
|
test_perf_event_all_cpu(&attr_hw_cache_l1d);
|
|
|
|
test_perf_event_task(&attr_hw_cache_l1d);
|
|
|
|
|
|
|
|
printf("Test HW_CACHE_BPU\n");
|
|
|
|
test_perf_event_all_cpu(&attr_hw_cache_branch_miss);
|
|
|
|
test_perf_event_task(&attr_hw_cache_branch_miss);
|
|
|
|
|
|
|
|
printf("Test Instruction Retired\n");
|
|
|
|
test_perf_event_all_cpu(&attr_type_raw);
|
|
|
|
test_perf_event_task(&attr_type_raw);
|
|
|
|
|
2018-03-06 11:55:02 -07:00
|
|
|
printf("Test Lock Load\n");
|
|
|
|
test_perf_event_all_cpu(&attr_type_raw_lock_load);
|
|
|
|
test_perf_event_task(&attr_type_raw_lock_load);
|
|
|
|
|
2017-06-02 22:03:53 -06:00
|
|
|
printf("*** PASS ***\n");
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
|
|
|
struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY};
|
2020-03-21 04:04:24 -06:00
|
|
|
struct bpf_object *obj = NULL;
|
2016-09-01 19:37:25 -06:00
|
|
|
char filename[256];
|
2020-03-21 04:04:24 -06:00
|
|
|
int error = 1;
|
2016-09-01 19:37:25 -06:00
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
|
|
|
setrlimit(RLIMIT_MEMLOCK, &r);
|
|
|
|
|
2020-03-21 04:04:24 -06:00
|
|
|
signal(SIGINT, err_exit);
|
|
|
|
signal(SIGTERM, err_exit);
|
2016-09-01 19:37:25 -06:00
|
|
|
|
|
|
|
if (load_kallsyms()) {
|
|
|
|
printf("failed to process /proc/kallsyms\n");
|
2020-03-21 04:04:24 -06:00
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
obj = bpf_object__open_file(filename, NULL);
|
2020-05-15 22:06:04 -06:00
|
|
|
if (libbpf_get_error(obj)) {
|
2020-03-21 04:04:24 -06:00
|
|
|
printf("opening BPF object file failed\n");
|
|
|
|
obj = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
prog = bpf_object__find_program_by_name(obj, "bpf_prog1");
|
|
|
|
if (!prog) {
|
|
|
|
printf("finding a prog in obj file failed\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* load BPF program */
|
|
|
|
if (bpf_object__load(obj)) {
|
|
|
|
printf("loading BPF object file failed\n");
|
|
|
|
goto cleanup;
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-03-21 04:04:24 -06:00
|
|
|
map_fd[0] = bpf_object__find_map_fd_by_name(obj, "counts");
|
|
|
|
map_fd[1] = bpf_object__find_map_fd_by_name(obj, "stackmap");
|
|
|
|
if (map_fd[0] < 0 || map_fd[1] < 0) {
|
|
|
|
printf("finding a counts/stackmap map in obj file failed\n");
|
|
|
|
goto cleanup;
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
|
|
|
|
2020-03-21 04:04:24 -06:00
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
2016-09-01 19:37:25 -06:00
|
|
|
read_trace_pipe();
|
|
|
|
return 0;
|
2020-03-21 04:04:24 -06:00
|
|
|
} else if (pid == -1) {
|
|
|
|
printf("couldn't spawn process\n");
|
|
|
|
goto cleanup;
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|
2020-03-21 04:04:24 -06:00
|
|
|
|
2016-09-01 19:37:25 -06:00
|
|
|
test_bpf_perf_event();
|
2020-03-21 04:04:24 -06:00
|
|
|
error = 0;
|
|
|
|
|
|
|
|
cleanup:
|
|
|
|
bpf_object__close(obj);
|
|
|
|
err_exit(error);
|
2016-09-01 19:37:25 -06:00
|
|
|
}
|