Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

libbpf-tools: add funcinterval #5100

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions libbpf-tools/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
/filetop
/fsdist
/fsslower
/funcinterval
/funclatency
/futexctn
/gethostlatency
Expand Down
1 change: 1 addition & 0 deletions libbpf-tools/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ APPS = \
filetop \
fsdist \
fsslower \
funcinterval \
funclatency \
gethostlatency \
hardirqs \
Expand Down
86 changes: 86 additions & 0 deletions libbpf-tools/funcinterval.bpf.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (c) 2024 Feng Yang
// Based on funcinterval.py from BCC by Edward Wu

#include "vmlinux.h"
#include <bpf/bpf_core_read.h>
#include <bpf/bpf_helpers.h>
#include <bpf/bpf_tracing.h>
#include "funcinterval.h"
#include "maps.bpf.h"
#include "bits.bpf.h"

const volatile pid_t targ_pid = 0;
const volatile bool targ_ms = false;

static struct hist zero = {0};

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, u64);
} start SEC(".maps");

struct {
__uint(type, BPF_MAP_TYPE_HASH);
__uint(max_entries, 1);
__type(key, __u32);
__type(value, struct hist);
} hists SEC(".maps");

static int trace_func_entry(void *ctx)
{
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 index = 0, tgid = pid_tgid >> 32;
u64 *tsp, ts = bpf_ktime_get_ns(), delta, slot;
struct hist *histp;

if (targ_pid && tgid != targ_pid)
return 0;

tsp = bpf_map_lookup_elem(&start, &index);
if (tsp == 0)
goto out;

histp = bpf_map_lookup_or_try_init(&hists, &index, &zero);
if (!histp)
return 0;

delta = ts - *tsp;
if (targ_ms)
delta /= 1000000U;
else
delta /= 1000U;

// store as histogram
slot = log2l(delta);
if (slot >= MAX_SLOTS)
slot = MAX_SLOTS - 1;
__sync_fetch_and_add(&histp->slots[slot], 1);

out:
bpf_map_update_elem(&start, &index, &ts, BPF_ANY);

return 0;
}

SEC("kprobe")
int BPF_KPROBE(function_entry)
{
return trace_func_entry(ctx);
}

SEC("tracepoint")
int tracepoint_entry(void *ctx)
{
return trace_func_entry(ctx);
}

SEC("uprobe")
int BPF_KPROBE(function_uprobe_entry)
{
return trace_func_entry(ctx);
}

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