mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-05-06 13:21:52 +08:00
add pid filter to profile
This commit is contained in:
@@ -14,6 +14,8 @@ struct {
|
|||||||
__uint(max_entries, 256 * 1024);
|
__uint(max_entries, 256 * 1024);
|
||||||
} events SEC(".maps");
|
} events SEC(".maps");
|
||||||
|
|
||||||
|
__u32 target_pid = 0;
|
||||||
|
|
||||||
SEC("perf_event")
|
SEC("perf_event")
|
||||||
int profile(void *ctx)
|
int profile(void *ctx)
|
||||||
{
|
{
|
||||||
@@ -22,6 +24,10 @@ int profile(void *ctx)
|
|||||||
struct stacktrace_event *event;
|
struct stacktrace_event *event;
|
||||||
int cp;
|
int cp;
|
||||||
|
|
||||||
|
/* Check if we need to filter by PID */
|
||||||
|
if (target_pid != 0 && target_pid != pid)
|
||||||
|
return 0;
|
||||||
|
|
||||||
event = bpf_ringbuf_reserve(&events, sizeof(*event), 0);
|
event = bpf_ringbuf_reserve(&events, sizeof(*event), 0);
|
||||||
if (!event)
|
if (!event)
|
||||||
return 1;
|
return 1;
|
||||||
@@ -33,7 +39,6 @@ int profile(void *ctx)
|
|||||||
event->comm[0] = 0;
|
event->comm[0] = 0;
|
||||||
|
|
||||||
event->kstack_sz = bpf_get_stack(ctx, event->kstack, sizeof(event->kstack), 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);
|
event->ustack_sz = bpf_get_stack(ctx, event->ustack, sizeof(event->ustack), BPF_F_USER_STACK);
|
||||||
|
|
||||||
bpf_ringbuf_submit(event, 0);
|
bpf_ringbuf_submit(event, 0);
|
||||||
|
|||||||
@@ -118,7 +118,10 @@ static int event_handler(void *_ctx, void *data, size_t size)
|
|||||||
|
|
||||||
static void show_help(const char *progname)
|
static void show_help(const char *progname)
|
||||||
{
|
{
|
||||||
printf("Usage: %s [-f <frequency>] [-h]\n", progname);
|
printf("Usage: %s [-f <frequency>] [-p <pid>] [-h]\n", progname);
|
||||||
|
printf(" -f <frequency> sampling frequency (default: 1)\n");
|
||||||
|
printf(" -p <pid> target PID to profile (default: all processes)\n");
|
||||||
|
printf(" -h show this help\n");
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char * const argv[])
|
int main(int argc, char * const argv[])
|
||||||
@@ -133,8 +136,10 @@ int main(int argc, char * const argv[])
|
|||||||
int *pefds = NULL, pefd;
|
int *pefds = NULL, pefd;
|
||||||
int argp, i, err = 0;
|
int argp, i, err = 0;
|
||||||
bool *online_mask = NULL;
|
bool *online_mask = NULL;
|
||||||
|
__u32 target_pid = 0; /* 0 means profile all processes */
|
||||||
|
__u32 key = 0;
|
||||||
|
|
||||||
while ((argp = getopt(argc, argv, "hf:")) != -1) {
|
while ((argp = getopt(argc, argv, "hf:p:")) != -1) {
|
||||||
switch (argp) {
|
switch (argp) {
|
||||||
case 'f':
|
case 'f':
|
||||||
freq = atoi(optarg);
|
freq = atoi(optarg);
|
||||||
@@ -142,6 +147,10 @@ int main(int argc, char * const argv[])
|
|||||||
freq = 1;
|
freq = 1;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'p':
|
||||||
|
target_pid = atoi(optarg);
|
||||||
|
break;
|
||||||
|
|
||||||
case 'h':
|
case 'h':
|
||||||
default:
|
default:
|
||||||
show_help(argv[0]);
|
show_help(argv[0]);
|
||||||
@@ -169,6 +178,14 @@ int main(int argc, char * const argv[])
|
|||||||
goto cleanup;
|
goto cleanup;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Set the target PID in the BPF map */
|
||||||
|
if (target_pid) {
|
||||||
|
skel->bss->target_pid = target_pid;
|
||||||
|
printf("Profiling process with PID %u only\n", target_pid);
|
||||||
|
} else {
|
||||||
|
printf("Profiling all processes\n");
|
||||||
|
}
|
||||||
|
|
||||||
symbolizer = blazesym_new();
|
symbolizer = blazesym_new();
|
||||||
if (!symbolizer) {
|
if (!symbolizer) {
|
||||||
fprintf(stderr, "Fail to create a symbolizer\n");
|
fprintf(stderr, "Fail to create a symbolizer\n");
|
||||||
|
|||||||
Reference in New Issue
Block a user