mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-04-10 14:18:37 +08:00
Deploying to gh-pages from @ eunomia-bpf/bpf-developer-tutorial@f91e119b81 🚀
This commit is contained in:
42
12-profile/profile.bpf.c
Normal file
42
12-profile/profile.bpf.c
Normal file
@@ -0,0 +1,42 @@
|
|||||||
|
// SPDX-License-Identifier: GPL-2.0 OR BSD-3-Clause
|
||||||
|
/* Copyright (c) 2022 Meta Platforms, Inc. */
|
||||||
|
#include "vmlinux.h"
|
||||||
|
#include <bpf/bpf_helpers.h>
|
||||||
|
#include <bpf/bpf_tracing.h>
|
||||||
|
#include <bpf/bpf_core_read.h>
|
||||||
|
|
||||||
|
#include "profile.h"
|
||||||
|
|
||||||
|
char LICENSE[] SEC("license") = "Dual BSD/GPL";
|
||||||
|
|
||||||
|
struct {
|
||||||
|
__uint(type, BPF_MAP_TYPE_RINGBUF);
|
||||||
|
__uint(max_entries, 256 * 1024);
|
||||||
|
} events SEC(".maps");
|
||||||
|
|
||||||
|
SEC("perf_event")
|
||||||
|
int profile(void *ctx)
|
||||||
|
{
|
||||||
|
int pid = bpf_get_current_pid_tgid() >> 32;
|
||||||
|
int cpu_id = bpf_get_smp_processor_id();
|
||||||
|
struct stacktrace_event *event;
|
||||||
|
int cp;
|
||||||
|
|
||||||
|
event = bpf_ringbuf_reserve(&events, sizeof(*event), 0);
|
||||||
|
if (!event)
|
||||||
|
return 1;
|
||||||
|
|
||||||
|
event->pid = pid;
|
||||||
|
event->cpu_id = cpu_id;
|
||||||
|
|
||||||
|
if (bpf_get_current_comm(event->comm, sizeof(event->comm)))
|
||||||
|
event->comm[0] = 0;
|
||||||
|
|
||||||
|
event->kstack_sz = bpf_get_stack(ctx, event->kstack, sizeof(event->kstack), 0);
|
||||||
|
|
||||||
|
event->ustack_sz = bpf_get_stack(ctx, event->ustack, sizeof(event->ustack), BPF_F_USER_STACK);
|
||||||
|
|
||||||
|
bpf_ringbuf_submit(event, 0);
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
26
12-profile/profile.h
Normal file
26
12-profile/profile.h
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
|
||||||
|
/* Copyright (c) 2022 Meta Platforms, Inc. */
|
||||||
|
#ifndef __PROFILE_H_
|
||||||
|
#define __PROFILE_H_
|
||||||
|
|
||||||
|
#ifndef TASK_COMM_LEN
|
||||||
|
#define TASK_COMM_LEN 16
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef MAX_STACK_DEPTH
|
||||||
|
#define MAX_STACK_DEPTH 128
|
||||||
|
#endif
|
||||||
|
|
||||||
|
typedef __u64 stack_trace_t[MAX_STACK_DEPTH];
|
||||||
|
|
||||||
|
struct stacktrace_event {
|
||||||
|
__u32 pid;
|
||||||
|
__u32 cpu_id;
|
||||||
|
char comm[TASK_COMM_LEN];
|
||||||
|
__s32 kstack_sz;
|
||||||
|
__s32 ustack_sz;
|
||||||
|
stack_trace_t kstack;
|
||||||
|
stack_trace_t ustack;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif /* __PROFILE_H_ */
|
||||||
Reference in New Issue
Block a user