Skip to content

Commit

Permalink
libbpf-tools: add CO-RE profile (iovisor#3782)
Browse files Browse the repository at this point in the history
It is based on Brendan Gregg's BCC profile and much of the code for it is borrowed from offcputime.c and ruqlen.c.
This is an example of usage.

# ./profile -iK
Sampling at 49 Hertz of all threads by kernel stack... Hit Ctrl-C to end.
^C    
    _raw_spin_unlock_irq
    finish_task_switch
    __schedule
    schedule
    schedule_timeout
    msleep
    HDMI21_Tx_EARC_MainThread
    kthread
    ret_from_fork
    -                hdmi21_earc_eng (1427)
        1

    __pi___clean_dcache_area_poc
    dma_direct_map_page
    dma_direct_map_sg
    dma_buf_unified_map
    dma_buf_map_attachment
    kbase_mem_umm_map_attachment
    kbase_mem_import
    kbase_ioctl
    __arm64_compat_sys_ioctl
    el0_svc_common.constprop.0
    el0_svc_compat_handler
    el0_svc_compat
    -                surface-manager (1911)
        4

    __pi___clean_dcache_area_poc
    dma_direct_map_page
    dma_direct_map_sg
    dma_buf_unified_map
    dma_buf_map_attachment
    kbase_mem_umm_map_attachment
    kbase_mem_import
    kbase_ioctl
    __arm64_compat_sys_ioctl
    el0_svc_common.constprop.0
    el0_svc_compat_handler
    el0_svc_compat
    -                tAiToneService (2141)
        12

    [Missed Kernel Stack]
    -                WebAppMgr (3624)
        1

...

This is the result of using the -f (folded) option.

# ./profile -fdK
...
tCMState;low_mem_notify_threshold;__alloc_pages_nodemask;__get_free_pages;__pollwait;unix_poll;sock_poll;do_sys_poll;__arm64_sys_poll;el0_svc_common.constprop.0;el0_svc_compat_handler;el0_svc_compat 2
surface-manager;sock_poll;do_sys_poll;__arm64_sys_poll;el0_svc_common.constprop.0;el0_svc_compat_handler;el0_svc_compat 1
qml-runner;sock_poll;do_sys_poll;__arm64_sys_poll;el0_svc_common.constprop.0;el0_svc_compat_handler;el0_svc_compat 96
mali-cmar-backe;_raw_spin_unlock_irqrestore;kbase_pm_update_cores_state;kbase_pm_do_poweron;kbase_pm_update_active;kbase_hwaccess_pm_gpu_active;kbase_pm_context_active_handle_suspend;kbase_js_sched;kbase_jd_submit;kbase_ioctl;__arm64_compat_sys_ioctl;el0_svc_common.constprop.0;el0_svc_compat_handler;el0_svc_compat 2
surface-manager;__fget;__fget_light;__fdget;sockfd_lookup_light;__sys_sendmsg;__arm64_compat_sys_sendmsg;el0_svc_common.constprop.0;el0_svc_compat_handler;el0_svc_compat 1
...
WARNING: 4 stack traces could not be displayed. Consider increasing --stack-storage-size.

Signed-off-by: Eunseon Lee [email protected]
  • Loading branch information
ekyooo committed Feb 7, 2024
1 parent a5eb4cb commit b14c463
Show file tree
Hide file tree
Showing 5 changed files with 625 additions and 0 deletions.
1 change: 1 addition & 0 deletions libbpf-tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
/offcputime
/oomkill
/opensnoop
/profile
/readahead
/runqlat
/runqlen
Expand Down
1 change: 1 addition & 0 deletions libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ APPS = \
numamove \
offcputime \
oomkill \
profile \
readahead \
runqlat \
runqlen \
Expand Down
73 changes: 73 additions & 0 deletions libbpf-tools/profile.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause)
/*
* Copyright (c) 2022 LG Electronics
*
* Based on profile from BCC by Brendan Gregg and others.
* 28-Dec-2021 Eunseon Lee Created this.
*/
#include <vmlinux.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_tracing.h>
#include "profile.h"
#include "maps.bpf.h"

const volatile bool kernel_stacks_only = false;
const volatile bool user_stacks_only = false;
const volatile bool include_idle = false;
const volatile pid_t targ_pid = -1;
const volatile pid_t targ_tid = -1;

struct {
__uint(type, BPF_MAP_TYPE_STACK_TRACE);
__type(key, u32);
} stackmap SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__type(key, struct key_t);
__type(value, u64);
__uint(max_entries, MAX_ENTRIES);
} counts SEC(".maps");

SEC("perf_event")
int do_perf_event(struct bpf_perf_event_data *ctx)
{
u64 id = bpf_get_current_pid_tgid();
u32 pid = id >> 32;
u32 tid = id;
u64 *valp;
static const u64 zero;
struct key_t key = {};

if (!include_idle && tid == 0)
return 0;

if (targ_pid != -1 && targ_pid != pid)
return 0;

if (targ_tid != -1 && targ_tid != tid)
return 0;

key.pid = pid;
bpf_get_current_comm(&key.name, sizeof(key.name));

if (user_stacks_only)
key.kern_stack_id = -1;
else
key.kern_stack_id = bpf_get_stackid(&ctx->regs, &stackmap, 0);

if (kernel_stacks_only)
key.user_stack_id = -1;
else
key.user_stack_id = bpf_get_stackid(&ctx->regs, &stackmap,
BPF_F_USER_STACK);

valp = bpf_map_lookup_or_try_init(&counts, &key, &zero);
if (valp)
__sync_fetch_and_add(valp, 1);

return 0;
}

char LICENSE[] SEC("license") = "GPL";
Loading

0 comments on commit b14c463

Please sign in to comment.