forked from linuxkerneltravel/lmp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b8ab113
commit b043362
Showing
3 changed files
with
300 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
#include <stdio.h> | ||
#include <unistd.h> | ||
#include <sys/resource.h> | ||
#include <bpf/libbpf.h> | ||
#include <signal.h> | ||
#include "open.skel.h" //包含了 BPF 字节码和相关的管理函数 | ||
#include "open.h" | ||
|
||
static volatile bool exiting = false; | ||
|
||
static void sig_handler(int sig) | ||
{ | ||
exiting = true; | ||
} | ||
|
||
static int handle_event(void *ctx, void *data,unsigned long data_sz) | ||
{ | ||
const struct fs_t *e = data; | ||
printf("pid:%d uid:%llu time:%llu fd:%d comm:%s\n",e->pid,e->uid,e->ts,e->fd,e->comm); | ||
|
||
return 0; | ||
} | ||
|
||
|
||
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | ||
{ | ||
return vfprintf(stderr, format, args); | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct ring_buffer *rb = NULL; | ||
struct open_bpf *skel; | ||
int err; | ||
|
||
libbpf_set_strict_mode(LIBBPF_STRICT_ALL); | ||
/* 设置libbpf错误和调试信息回调 */ | ||
libbpf_set_print(libbpf_print_fn); | ||
|
||
/* 更干净地处理Ctrl-C | ||
SIGINT:由Interrupt Key产生,通常是CTRL+C或者DELETE。发送给所有ForeGround Group的进程 | ||
SIGTERM:请求中止进程,kill命令发送 | ||
*/ | ||
signal(SIGINT, sig_handler); //signal设置某一信号的对应动作 | ||
signal(SIGTERM, sig_handler); | ||
|
||
/* 打开BPF应用程序 */ | ||
skel = open_bpf__open(); | ||
if (!skel) { | ||
fprintf(stderr, "Failed to open BPF skeleton\n"); | ||
return 1; | ||
} | ||
|
||
/* 加载并验证BPF程序 */ | ||
err = open_bpf__load(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to load and verify BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* 附加跟踪点处理程序 */ | ||
err = open_bpf__attach(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to attach BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* 设置环形缓冲区轮询 */ | ||
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), handle_event, NULL, NULL); //ring_buffer__new() API,允许在不使用额外选项数据结构下指定回调 | ||
if (!rb) { | ||
err = -1; | ||
fprintf(stderr, "Failed to create ring buffer\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* 处理事件 */ | ||
while (!exiting) { | ||
err = ring_buffer__poll(rb, 100 /* timeout, ms */); //ring_buffer__poll(),轮询打开ringbuf缓冲区。如果有事件,handle_event函数会执行 | ||
/* Ctrl-C will cause -EINTR */ | ||
if (err == -EINTR) { | ||
err = 0; | ||
break; | ||
} | ||
if (err < 0) { | ||
printf("Error polling perf buffer: %d\n", err); | ||
break; | ||
} | ||
|
||
//exiting = true; //使用该程序时,将该行代码注释掉 | ||
|
||
} | ||
|
||
/* 卸载BPF程序 */ | ||
cleanup: | ||
ring_buffer__free(rb); | ||
open_bpf__destroy(skel); | ||
|
||
return err < 0 ? -err : 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
#include <stdio.h> | ||
#include <signal.h> | ||
#include <time.h> | ||
#include <bpf/libbpf.h> | ||
#include "read.h" | ||
#include "read.skel.h" | ||
|
||
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | ||
{ | ||
return vfprintf(stderr, format, args); | ||
} | ||
|
||
static volatile bool exiting = false; | ||
|
||
static void sig_handler(int sig) | ||
{ | ||
exiting = true; | ||
} | ||
|
||
static int read_event(void *ctx, void *data, size_t data_sz) | ||
{ | ||
const struct event *e = data; | ||
struct tm *tm; | ||
char ts[32]; | ||
time_t t; | ||
|
||
time(&t); | ||
tm = localtime(&t); | ||
strftime(ts, sizeof(ts), "%H:%M:%S", tm); | ||
|
||
printf("%-8s %-7d %-7llu\n", ts, e->pid,e->duration_ns); | ||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct ring_buffer *rb = NULL; | ||
struct read_bpf *skel; | ||
int err; | ||
|
||
/* Set up libbpf errors and debug info callback */ | ||
libbpf_set_print(libbpf_print_fn); | ||
|
||
|
||
/* Cleaner handling of Ctrl-C */ | ||
signal(SIGINT, sig_handler); | ||
signal(SIGTERM, sig_handler); | ||
|
||
/* Open BPF application */ | ||
skel = read_bpf__open(); | ||
if (!skel) { | ||
fprintf(stderr, "Failed to open BPF skeleton\n"); | ||
return 1; | ||
} | ||
|
||
/* Load & verify BPF programs */ | ||
err = read_bpf__load(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to load and verify BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* Attach tracepoints */ | ||
err = read_bpf__attach(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to attach BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* Set up ring buffer polling */ | ||
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), read_event, NULL, NULL); | ||
if (!rb) { | ||
err = -1; | ||
fprintf(stderr, "Failed to create ring buffer\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* Process events */ | ||
printf("%-8s %-7s %-7s\n", "TIME", "PID", "durations"); | ||
while (!exiting) { | ||
err = ring_buffer__poll(rb, 100 /* timeout, ms */); | ||
/* Ctrl-C will cause -EINTR */ | ||
if (err == -EINTR) { | ||
err = 0; | ||
break; | ||
} | ||
|
||
if (err < 0) { | ||
printf("Error polling perf buffer: %d\n", err); | ||
break; | ||
} | ||
} | ||
|
||
cleanup: | ||
/* Clean up */ | ||
ring_buffer__free(rb); | ||
read_bpf__destroy(skel); | ||
|
||
return err < 0 ? -err : 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,101 @@ | ||
#include <stdio.h> | ||
#include <signal.h> | ||
#include <unistd.h> | ||
#include <sys/resource.h> | ||
#include <time.h> | ||
#include <bpf/libbpf.h> | ||
#include "write.h" | ||
#include "write.skel.h" | ||
|
||
static int libbpf_print_fn(enum libbpf_print_level level, const char *format, va_list args) | ||
{ | ||
return vfprintf(stderr, format, args); | ||
} | ||
|
||
static volatile bool exiting = false; | ||
|
||
static void sig_handler(int sig) | ||
{ | ||
exiting = true; | ||
} | ||
|
||
static int write_event(void *ctx, void *data, size_t data_sz) | ||
{ | ||
const struct fs_t *e = data; | ||
struct tm *tm; | ||
char ts[32]; | ||
time_t t; | ||
|
||
time(&t); | ||
tm = localtime(&t); | ||
strftime(ts, sizeof(ts), "%H:%M:%S", tm); | ||
printf("%-8s %-7d %-7llu\n", ts, e->pid,e->duration_ns); | ||
return 0; | ||
} | ||
|
||
int main(int argc, char **argv) | ||
{ | ||
struct ring_buffer *rb = NULL; | ||
struct write_bpf *skel; | ||
int err; | ||
|
||
/* Set up libbpf errors and debug info callback */ | ||
libbpf_set_print(libbpf_print_fn); | ||
|
||
|
||
/* Cleaner handling of Ctrl-C */ | ||
signal(SIGINT, sig_handler); | ||
signal(SIGTERM, sig_handler); | ||
|
||
/* Open BPF application */ | ||
skel = write_bpf__open(); | ||
if (!skel) { | ||
fprintf(stderr, "Failed to open BPF skeleton\n"); | ||
return 1; | ||
} | ||
|
||
/* Load & verify BPF programs */ | ||
err = write_bpf__load(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to load and verify BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* Attach tracepoints */ | ||
err = write_bpf__attach(skel); | ||
if (err) { | ||
fprintf(stderr, "Failed to attach BPF skeleton\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* Set up ring buffer polling */ | ||
rb = ring_buffer__new(bpf_map__fd(skel->maps.rb), write_event, NULL, NULL); | ||
if (!rb) { | ||
err = -1; | ||
fprintf(stderr, "Failed to create ring buffer\n"); | ||
goto cleanup; | ||
} | ||
|
||
/* Process events */ | ||
printf("%-8s %-7s %-7s\n", "TIME", "PID", "durations"); | ||
while (!exiting) { | ||
err = ring_buffer__poll(rb, 100 /* timeout, ms */); | ||
/* Ctrl-C will cause -EINTR */ | ||
if (err == -EINTR) { | ||
err = 0; | ||
break; | ||
} | ||
|
||
if (err < 0) { | ||
printf("Error polling perf buffer: %d\n", err); | ||
break; | ||
} | ||
} | ||
|
||
cleanup: | ||
/* Clean up */ | ||
ring_buffer__free(rb); | ||
write_bpf__destroy(skel); | ||
|
||
return err < 0 ? -err : 0; | ||
} |