2019-05-29 00:10:09 +07:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-only
|
2016-09-02 08:37:26 +07:00
|
|
|
/*
|
|
|
|
* sampleip: sample instruction pointer and frequency count in a BPF map.
|
|
|
|
*
|
|
|
|
* Copyright 2016 Netflix, Inc.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <signal.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <linux/perf_event.h>
|
|
|
|
#include <linux/ptrace.h>
|
|
|
|
#include <linux/bpf.h>
|
2020-03-21 17:04:24 +07:00
|
|
|
#include <bpf/bpf.h>
|
2020-01-20 20:06:49 +07:00
|
|
|
#include <bpf/libbpf.h>
|
2016-12-09 09:46:19 +07:00
|
|
|
#include "perf-sys.h"
|
2018-04-29 12:28:13 +07:00
|
|
|
#include "trace_helpers.h"
|
2016-09-02 08:37:26 +07:00
|
|
|
|
|
|
|
#define DEFAULT_FREQ 99
|
|
|
|
#define DEFAULT_SECS 5
|
|
|
|
#define MAX_IPS 8192
|
|
|
|
#define PAGE_OFFSET 0xffff880000000000
|
|
|
|
|
2020-03-21 17:04:24 +07:00
|
|
|
static int map_fd;
|
2016-09-02 08:37:26 +07:00
|
|
|
static int nr_cpus;
|
|
|
|
|
|
|
|
static void usage(void)
|
|
|
|
{
|
|
|
|
printf("USAGE: sampleip [-F freq] [duration]\n");
|
|
|
|
printf(" -F freq # sample frequency (Hertz), default 99\n");
|
|
|
|
printf(" duration # sampling duration (seconds), default 5\n");
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:04:24 +07:00
|
|
|
static int sampling_start(int freq, struct bpf_program *prog,
|
|
|
|
struct bpf_link *links[])
|
2016-09-02 08:37:26 +07:00
|
|
|
{
|
2020-03-21 17:04:24 +07:00
|
|
|
int i, pmu_fd;
|
2016-09-02 08:37:26 +07:00
|
|
|
|
|
|
|
struct perf_event_attr pe_sample_attr = {
|
|
|
|
.type = PERF_TYPE_SOFTWARE,
|
|
|
|
.freq = 1,
|
|
|
|
.sample_period = freq,
|
|
|
|
.config = PERF_COUNT_SW_CPU_CLOCK,
|
|
|
|
.inherit = 1,
|
|
|
|
};
|
|
|
|
|
|
|
|
for (i = 0; i < nr_cpus; i++) {
|
2020-03-21 17:04:24 +07:00
|
|
|
pmu_fd = sys_perf_event_open(&pe_sample_attr, -1 /* pid */, i,
|
2016-09-02 08:37:26 +07:00
|
|
|
-1 /* group_fd */, 0 /* flags */);
|
2020-03-21 17:04:24 +07:00
|
|
|
if (pmu_fd < 0) {
|
2016-09-02 08:37:26 +07:00
|
|
|
fprintf(stderr, "ERROR: Initializing perf sampling\n");
|
|
|
|
return 1;
|
|
|
|
}
|
2020-03-21 17:04:24 +07:00
|
|
|
links[i] = bpf_program__attach_perf_event(prog, pmu_fd);
|
2020-05-16 11:06:04 +07:00
|
|
|
if (libbpf_get_error(links[i])) {
|
2020-03-21 17:04:24 +07:00
|
|
|
fprintf(stderr, "ERROR: Attach perf event\n");
|
|
|
|
links[i] = NULL;
|
|
|
|
close(pmu_fd);
|
|
|
|
return 1;
|
|
|
|
}
|
2016-09-02 08:37:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-03-21 17:04:24 +07:00
|
|
|
static void sampling_end(struct bpf_link *links[])
|
2016-09-02 08:37:26 +07:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < nr_cpus; i++)
|
2020-03-21 17:04:24 +07:00
|
|
|
bpf_link__destroy(links[i]);
|
2016-09-02 08:37:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
struct ipcount {
|
|
|
|
__u64 ip;
|
|
|
|
__u32 count;
|
|
|
|
};
|
|
|
|
|
|
|
|
/* used for sorting */
|
|
|
|
struct ipcount counts[MAX_IPS];
|
|
|
|
|
|
|
|
static int count_cmp(const void *p1, const void *p2)
|
|
|
|
{
|
|
|
|
return ((struct ipcount *)p1)->count - ((struct ipcount *)p2)->count;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void print_ip_map(int fd)
|
|
|
|
{
|
|
|
|
struct ksym *sym;
|
|
|
|
__u64 key, next_key;
|
|
|
|
__u32 value;
|
|
|
|
int i, max;
|
|
|
|
|
|
|
|
printf("%-19s %-32s %s\n", "ADDR", "KSYM", "COUNT");
|
|
|
|
|
|
|
|
/* fetch IPs and counts */
|
|
|
|
key = 0, i = 0;
|
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-15 05: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-02 08:37:26 +07:00
|
|
|
counts[i].ip = next_key;
|
|
|
|
counts[i++].count = value;
|
|
|
|
key = next_key;
|
|
|
|
}
|
|
|
|
max = i;
|
|
|
|
|
|
|
|
/* sort and print */
|
|
|
|
qsort(counts, max, sizeof(struct ipcount), count_cmp);
|
|
|
|
for (i = 0; i < max; i++) {
|
|
|
|
if (counts[i].ip > PAGE_OFFSET) {
|
|
|
|
sym = ksym_search(counts[i].ip);
|
2019-04-04 05:17:56 +07:00
|
|
|
if (!sym) {
|
|
|
|
printf("ksym not found. Is kallsyms loaded?\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-09-02 08:37:26 +07:00
|
|
|
printf("0x%-17llx %-32s %u\n", counts[i].ip, sym->name,
|
|
|
|
counts[i].count);
|
|
|
|
} else {
|
|
|
|
printf("0x%-17llx %-32s %u\n", counts[i].ip, "(user)",
|
|
|
|
counts[i].count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max == MAX_IPS) {
|
|
|
|
printf("WARNING: IP hash was full (max %d entries); ", max);
|
|
|
|
printf("may have dropped samples\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void int_exit(int sig)
|
|
|
|
{
|
|
|
|
printf("\n");
|
2020-03-21 17:04:24 +07:00
|
|
|
print_ip_map(map_fd);
|
2016-09-02 08:37:26 +07:00
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv)
|
|
|
|
{
|
2020-03-21 17:04:24 +07:00
|
|
|
int opt, freq = DEFAULT_FREQ, secs = DEFAULT_SECS, error = 1;
|
|
|
|
struct bpf_object *obj = NULL;
|
|
|
|
struct bpf_program *prog;
|
|
|
|
struct bpf_link **links;
|
2016-09-02 08:37:26 +07:00
|
|
|
char filename[256];
|
|
|
|
|
|
|
|
/* process arguments */
|
|
|
|
while ((opt = getopt(argc, argv, "F:h")) != -1) {
|
|
|
|
switch (opt) {
|
|
|
|
case 'F':
|
|
|
|
freq = atoi(optarg);
|
|
|
|
break;
|
|
|
|
case 'h':
|
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (argc - optind == 1)
|
|
|
|
secs = atoi(argv[optind]);
|
|
|
|
if (freq == 0 || secs == 0) {
|
|
|
|
usage();
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* initialize kernel symbol translation */
|
|
|
|
if (load_kallsyms()) {
|
|
|
|
fprintf(stderr, "ERROR: loading /proc/kallsyms\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* create perf FDs for each CPU */
|
2020-03-21 17:04:24 +07:00
|
|
|
nr_cpus = sysconf(_SC_NPROCESSORS_ONLN);
|
|
|
|
links = calloc(nr_cpus, sizeof(struct bpf_link *));
|
|
|
|
if (!links) {
|
|
|
|
fprintf(stderr, "ERROR: malloc of links\n");
|
|
|
|
goto cleanup;
|
2016-09-02 08:37:26 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]);
|
2020-03-21 17:04:24 +07:00
|
|
|
obj = bpf_object__open_file(filename, NULL);
|
2020-05-16 11:06:04 +07:00
|
|
|
if (libbpf_get_error(obj)) {
|
2020-03-21 17:04:24 +07:00
|
|
|
fprintf(stderr, "ERROR: opening BPF object file failed\n");
|
|
|
|
obj = NULL;
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
prog = bpf_object__find_program_by_name(obj, "do_sample");
|
|
|
|
if (!prog) {
|
|
|
|
fprintf(stderr, "ERROR: finding a prog in obj file failed\n");
|
|
|
|
goto cleanup;
|
2016-09-02 08:37:26 +07:00
|
|
|
}
|
2020-03-21 17:04:24 +07:00
|
|
|
|
|
|
|
/* load BPF program */
|
|
|
|
if (bpf_object__load(obj)) {
|
|
|
|
fprintf(stderr, "ERROR: loading BPF object file failed\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
map_fd = bpf_object__find_map_fd_by_name(obj, "ip_map");
|
|
|
|
if (map_fd < 0) {
|
|
|
|
fprintf(stderr, "ERROR: finding a map in obj file failed\n");
|
|
|
|
goto cleanup;
|
|
|
|
}
|
|
|
|
|
2016-09-02 08:37:26 +07:00
|
|
|
signal(SIGINT, int_exit);
|
2017-05-12 02:52:30 +07:00
|
|
|
signal(SIGTERM, int_exit);
|
2016-09-02 08:37:26 +07:00
|
|
|
|
|
|
|
/* do sampling */
|
|
|
|
printf("Sampling at %d Hertz for %d seconds. Ctrl-C also ends.\n",
|
|
|
|
freq, secs);
|
2020-03-21 17:04:24 +07:00
|
|
|
if (sampling_start(freq, prog, links) != 0)
|
|
|
|
goto cleanup;
|
|
|
|
|
2016-09-02 08:37:26 +07:00
|
|
|
sleep(secs);
|
2020-03-21 17:04:24 +07:00
|
|
|
error = 0;
|
2016-09-02 08:37:26 +07:00
|
|
|
|
2020-03-21 17:04:24 +07:00
|
|
|
cleanup:
|
|
|
|
sampling_end(links);
|
2016-09-02 08:37:26 +07:00
|
|
|
/* output sample counts */
|
2020-03-21 17:04:24 +07:00
|
|
|
if (!error)
|
|
|
|
print_ip_map(map_fd);
|
2016-09-02 08:37:26 +07:00
|
|
|
|
2020-03-21 17:04:24 +07:00
|
|
|
free(links);
|
|
|
|
bpf_object__close(obj);
|
|
|
|
return error;
|
2016-09-02 08:37:26 +07:00
|
|
|
}
|