mirror of
https://github.com/eunomia-bpf/bpf-developer-tutorial.git
synced 2026-02-03 18:24:27 +08:00
add github issue template
This commit is contained in:
33
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
33
.github/ISSUE_TEMPLATE/bug_report.md
vendored
Normal file
@@ -0,0 +1,33 @@
|
||||
---
|
||||
name: Bug report
|
||||
about: Create a report to help me improve
|
||||
title: "[BUG]"
|
||||
labels: bug
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Describe the bug**
|
||||
A clear and concise description of what the bug is.
|
||||
|
||||
**To Reproduce**
|
||||
Steps to reproduce the behavior:
|
||||
|
||||
1. Go to '...'
|
||||
2. Click on '....'
|
||||
3. Scroll down to '....'
|
||||
4. See error
|
||||
|
||||
**Expected behavior**
|
||||
A clear and concise description of what you expected to happen.
|
||||
|
||||
**Screenshots**
|
||||
If applicable, add screenshots to help explain your problem.
|
||||
|
||||
**Desktop (please complete the following information):**
|
||||
|
||||
* OS: [e.g. Windows]
|
||||
* Version [e.g. 10]
|
||||
|
||||
**Additional context**
|
||||
Add any other context about the problem here.
|
||||
10
.github/ISSUE_TEMPLATE/custom.md
vendored
Normal file
10
.github/ISSUE_TEMPLATE/custom.md
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
name: Custom issue template
|
||||
about: Describe this issue template's purpose here.
|
||||
title: ''
|
||||
labels: ''
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
|
||||
25
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
25
.github/ISSUE_TEMPLATE/feature_request.md
vendored
Normal file
@@ -0,0 +1,25 @@
|
||||
---
|
||||
name: Feature request
|
||||
about: Suggest an idea for this project
|
||||
title: "[FEATURE]"
|
||||
labels: enhancement
|
||||
assignees: ''
|
||||
|
||||
---
|
||||
|
||||
**Is your feature request related to a problem? Please describe.**
|
||||
A clear and concise description of what the problem is. Ex. I'm always frustrated
|
||||
when [...]
|
||||
|
||||
**Describe the solution you'd like**
|
||||
A clear and concise description of what you want to happen.
|
||||
|
||||
**Describe alternatives you've considered**
|
||||
A clear and concise description of any alternative solutions or features you've considered.
|
||||
|
||||
**Provide usage examples**
|
||||
A few examples of how the feature should be used. Please make sure they are clear
|
||||
and concise.
|
||||
|
||||
**Additional context**
|
||||
Add any other context or screenshots about the feature request here.
|
||||
5
.vscode/settings.json
vendored
5
.vscode/settings.json
vendored
@@ -1,5 +0,0 @@
|
||||
{
|
||||
"files.associations": {
|
||||
"numbers": "c"
|
||||
}
|
||||
}
|
||||
@@ -87,6 +87,10 @@ int handle_tp(void *ctx)
|
||||
|
||||
这段程序通过定义一个 handle_tp 函数并使用 SEC 宏把它附加到 sys_enter_write tracepoint(即在进入 write 系统调用时执行)。该函数通过使用 bpf_get_current_pid_tgid 和 bpf_printk 函数获取调用 write 系统调用的进程 ID,并在内核日志中打印出来。
|
||||
|
||||
- `bpf_trace_printk()`: 一种将信息输出到trace_pipe(/sys/kernel/debug/tracing/trace_pipe)简单机制。 在一些简单用例中这样使用没有问题, but它也有一些限制:最多3 参数; 第一个参数必须是%s(即字符串);同时trace_pipe在内核中全局共享,其他并行使用trace_pipe的程序有可能会将 trace_pipe 的输出扰乱。 一个更好的方式是通过 BPF_PERF_OUTPUT(), 稍后将会讲到。
|
||||
- `void *ctx`:ctx本来是具体类型的参数, 但是由于我们这里没有使用这个参数,因此就将其写成void *类型。
|
||||
- `return 0`;:必须这样,返回0 (如果要知道why, 参考 #139 https://github.com/iovisor/bcc/issues/139)。
|
||||
|
||||
要编译和运行这段程序,可以使用 ecc 工具和 ecli 命令。首先使用 ecc 编译程序:
|
||||
|
||||
```console
|
||||
|
||||
@@ -1,75 +1 @@
|
||||
## eBPF 入门实践教程:编写 eBPF 程序 llcstat 监控 cache miss 和 cache reference
|
||||
|
||||
### 背景
|
||||
|
||||
为了能更好地优化程序性能,开发者有时需要考虑如何更好地减少cache miss的发生。
|
||||
但是程序到底可能发生多少次cache miss这是一个难以回答的问题。`llcstat` 通过
|
||||
ebpf技术,实现了对 cache miss 和 cache reference 的准确追踪,可以极大方便开发者
|
||||
调试程序,优化性能。
|
||||
|
||||
### 实现原理
|
||||
|
||||
`llcstat` 引入了linux中的 `perf_event` 机制,程序在用户态载入的时候,
|
||||
会将现有的c `perf_event` attach到指定的位置。
|
||||
```c
|
||||
if (open_and_attach_perf_event(PERF_COUNT_HW_CACHE_MISSES,
|
||||
env.sample_period,
|
||||
obj->progs.on_cache_miss, mlinks))
|
||||
goto cleanup;
|
||||
if (open_and_attach_perf_event(PERF_COUNT_HW_CACHE_REFERENCES,
|
||||
env.sample_period,
|
||||
obj->progs.on_cache_ref, rlinks))
|
||||
```
|
||||
|
||||
同时,`llcstat` 在内核态中会在`perf_event`下挂载执行函数,当程序运行到了
|
||||
挂载点,执行函数会启动并开始计数,将结果写入对应的map中。
|
||||
|
||||
```c
|
||||
static __always_inline
|
||||
int trace_event(__u64 sample_period, bool miss)
|
||||
{
|
||||
struct key_info key = {};
|
||||
struct value_info *infop, zero = {};
|
||||
|
||||
u64 pid_tgid = bpf_get_current_pid_tgid();
|
||||
key.cpu = bpf_get_smp_processor_id();
|
||||
key.pid = pid_tgid >> 32;
|
||||
if (targ_per_thread)
|
||||
key.tid = (u32)pid_tgid;
|
||||
else
|
||||
key.tid = key.pid;
|
||||
|
||||
infop = bpf_map_lookup_or_try_init(&infos, &key, &zero);
|
||||
if (!infop)
|
||||
return 0;
|
||||
if (miss)
|
||||
infop->miss += sample_period;
|
||||
else
|
||||
infop->ref += sample_period;
|
||||
bpf_get_current_comm(infop->comm, sizeof(infop->comm));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
SEC("perf_event")
|
||||
int on_cache_miss(struct bpf_perf_event_data *ctx)
|
||||
{
|
||||
return trace_event(ctx->sample_period, true);
|
||||
}
|
||||
|
||||
SEC("perf_event")
|
||||
int on_cache_ref(struct bpf_perf_event_data *ctx)
|
||||
{
|
||||
return trace_event(ctx->sample_period, false);
|
||||
}
|
||||
```
|
||||
|
||||
用户态程序会读取map存入的 cache miss 和 cache reference 的计数信息,并
|
||||
逐进程的进行展示。
|
||||
|
||||
### Eunomia中使用方式
|
||||
|
||||
|
||||
### 总结
|
||||
`llcstat` 运用了ebpf计数,高效简洁地展示了某个线程发生cache miss和cache
|
||||
reference的次数,这使得开发者们在优化程序的过程中有了更明确的量化指标。
|
||||
## eBPF 入门实践教程:libbpf-bootstrap with user space
|
||||
@@ -2,12 +2,14 @@
|
||||
|
||||
这是一个基于 `CO-RE`(一次编译,到处运行)的 `libbpf` 的 eBPF 的开发教程,提供了从入门到进阶的 eBPF 开发实践指南,包括基本概念、代码实例、实际应用等内容。我们主要提供了一些 eBPF 工具的案例,帮助开发者学习 eBPF 的开发方法和技巧。教程内容可以在目录中找到,每个目录都是一个独立的 eBPF 工具案例。
|
||||
|
||||
在学习 eBPF 的过程中,我们受到了 [tutorial_bcc_python_developer](https://github.com/iovisor/bcc/blob/master/docs/tutorial_bcc_python_developer.md) 的许多启发和帮助,但从 2022 年的角度出发,使用 libbpf 开发 eBPF 的应用是目前相对更好的选择。但目前似乎很少有基于 libbpf 和 BPF CO-RE 出发的、通过案例和工具介绍 eBPF 开发的教程,因此我们发起了这个项目。
|
||||
在学习 eBPF 的过程中,我们受到了 [tutorial_bcc_python_developer](https://github.com/iovisor/bcc/blob/master/docs/tutorial_bcc_python_developer.md) 的许多启发和帮助,但从 2022 年的角度出发,使用 libbpf 开发 eBPF 的应用是目前相对更好的选择。但目前似乎很少有基于 libbpf 和 BPF CO-RE 出发的、通过案例和工具介绍 eBPF 开发的教程,因此我们发起了这个项目,采用类似 tutorial_bcc_python_developer 的组织方式,但使用 CO-RE 的 libbpf 进行开发。
|
||||
|
||||
本项目主要基于 [libbpf-boostrap](https://github.com/libbpf/libbpf-bootstrap) 和 [eunomia-bpf](https://github.com/eunomia-bpf/eunomia-bpf) 两个框架完成,并使用 eunomia-bpf 帮助简化一部分 libbpf eBPF 用户态代码的编写。
|
||||
本项目主要基于 [libbpf-boostrap](https://github.com/libbpf/libbpf-bootstrap) 和 [eunomia-bpf](https://github.com/eunomia-bpf/eunomia-bpf) 两个框架完成,并使用 eunomia-bpf 帮助简化一部分 libbpf eBPF 用户态代码的编写,让开发者专注于内核态的 eBPF 代码的开发。
|
||||
|
||||
教程主要关注于可观察性,并简要介绍了 eBPF 的其他应用,例如网络、安全等等。
|
||||
|
||||
Gitee 镜像: <https://gitee.com/yunwei37/bpf-developer-tutorial>
|
||||
|
||||
## 让 chatGPT 来帮助我们
|
||||
|
||||
本教程大部分内容由 chatGPT 生成,我们尝试教会 chatGPT 编写 eBPF 程序:
|
||||
@@ -34,7 +36,7 @@
|
||||
- [lesson 7-execsnoop](7-execsnoop/README.md) 捕获进程执行时间,通过 perf event array 向用户态打印输出
|
||||
- [lesson 8-execsnoop](8-exitsnoop/README.md) 捕获进程退出事件,使用 ring buffer 向用户态打印输出
|
||||
- [lesson 9-runqlat](9-runqlat/README.md) 捕获进程调度延迟,以直方图方式记录
|
||||
- [lesson 10-hardirqs](20-hardirqs/README.md) 使用 hardirqs 或 softirqs 捕获中断事件
|
||||
- [lesson 10-hardirqs](10-hardirqs/README.md) 使用 hardirqs 或 softirqs 捕获中断事件
|
||||
- [lesson 11-bootstrap](11-bootstrap/README.md) 使用 libbpf-boostrap 为 eBPF 编写原生的的用户态代码
|
||||
- [lesson 12-profile](12-profile/README.md) 使用 eBPF 进行性能分析
|
||||
- [lesson 13-tcpconnlat](13-tcpconnlat/README.md) 记录 TCP 连接延迟
|
||||
|
||||
Reference in New Issue
Block a user