Skip to content

Commit f21b273

Browse files
author
241200050
committed
docs(linux): 更新 ftrace 和 perf 文档并添加命名空间相关内容
- 在 ftrace.adoc 中添加了 ftrace 功能的简要说明 - 在 linux.adoc 中新增了命名空间相关内容,包括命名空间的类型和使用示例 - 在 perf.adoc 中添加了 perf 工具的功能概述和更多使用示例 - 优化了文档结构,增加了更多小节标题和说明,提高了可读性
1 parent afd4117 commit f21b273

File tree

4 files changed

+122
-7
lines changed

4 files changed

+122
-7
lines changed
758 KB
Loading

linux/ftrace.adoc

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,34 @@ ifdef::rootpath[]
1010
endif::rootpath[]
1111

1212
== ftrace
13+
14+
perf使用子命令来实现不同的功能,而ftrace有剖析器和跟踪器,剖析器提供系统摘要,如计数和直方图,而跟踪器提供每个事件的实现细节。
15+
16+
17+
18+
19+
20+
21+
22+
23+
24+
25+
26+
27+
28+
29+
30+
31+
32+
33+
34+
35+
36+
37+
38+
39+
40+
41+
42+
43+
image::linux/image-2025-03-25-14-35-46-266.png[]

linux/linux.adoc

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,66 @@ net.ipv4.tcp_tw_recycle = 1
105105
----
106106

107107

108+
=== 命名空间
109+
110+
命名空间对系统的视图进行过滤,使容器只能看到和管理自己的进程、挂载点以及其他资源
111+
112+
[cols="~,~", options="header"]
113+
|===
114+
|命名空间 |描述
115+
116+
|cgroup |用于cgroup可见性
117+
|ipc |用于进程间通讯的可见性
118+
|mnt |用于文件系统挂载点
119+
|net |用于网络隔离,过滤接口、套接字、路由等
120+
|pid |用于进程可见性,过滤/proc
121+
|user |用于用户ID
122+
|uts |用于主机名、域名和uname系统调用
123+
|time |用于不同容器单独的系统时钟
124+
|===
125+
126+
==== lsns
127+
128+
使用lsns可以查看系统中当前命名空间
129+
130+
[source,bash]
131+
----
132+
# 列出所有命名空间
133+
lsns
134+
----
135+
136+
137+
138+
139+
140+
141+
142+
143+
144+
145+
146+
147+
148+
149+
150+
151+
152+
153+
154+
155+
156+
157+
158+
159+
160+
161+
162+
163+
164+
165+
参见: 网络命名空间
166+
167+
108168

109169

110170

linux/perf.adoc

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ endif::rootpath[]
1111

1212
== perf
1313

14+
perf能回答以下问题:
15+
16+
- 哪些代码路径在消耗CPU资源
17+
- CPU是否被滞留在内存的负载/存储上
18+
- 线程因为什么原因离开CPU? 是主动让出还是被动调度
19+
- 磁盘I/O的模式是什么?
20+
1421
- trace system calls faster than trace
1522
- profile your C, Go, C++, node.js, Rust, and Java/JVM programs really easily
1623
- trace or count almost `any` kernel event
@@ -284,13 +291,6 @@ sudo perf record -e 'compaction:*' -a -I 1000
284291

285292

286293

287-
288-
289-
290-
291-
292-
293-
294294
==== Counting Events
295295

296296
[source,bash]
@@ -418,6 +418,7 @@ perf top -F 49 -ns comm,dso
418418
[source,bash]
419419
----
420420
# Trace new processes, until Ctrl-C:
421+
# 跟踪新的进程,知道按下Ctrl-C组合键
421422
perf record -e sched:sched_process_exec -a
422423
423424
# Sample (take a subset of) context-switches, until Ctrl-C:
@@ -495,6 +496,7 @@ perf record -e sdt_node:http__server__request -a
495496
[source,bash]
496497
----
497498
# Add a tracepoint for the kernel tcp_sendmsg() function entry ("--add" is optional):
499+
# 为内核函数 tcp_sendmsg() 函数入口添加一个tracepoint,可以忽略 --add
498500
perf probe --add tcp_sendmsg
499501
500502
# Remove the tcp_sendmsg() tracepoint (or use "--del"):
@@ -550,6 +552,8 @@ perf probe 'myfunc%return +0($retval):string'
550552
551553
# Add a tracepoint for the user-level malloc() function from libc:
552554
perf probe -x /lib64/libc.so.6 malloc
555+
perf probe -x /lib64/libc.so.6 --add fopen
556+
perf probe --del probe_libc:fopen --del probe_libc:malloc
553557
554558
# Add a tracepoint for this user-level static probe (USDT, aka SDT event):
555559
perf probe -x /usr/lib64/libpthread-2.24.so %sdt_libpthread:mutex_entry
@@ -559,6 +563,16 @@ perf probe -l
559563
----
560564

561565

566+
.eg 内核函数 do_nanosleep 跟踪
567+
[source, bash]
568+
----
569+
perf probe --add do_nanosleep
570+
perf record -e probe:do_nanosleep -a sleep 5
571+
perf script
572+
perf probe --del do_nanosleep
573+
----
574+
575+
562576
==== Mixed
563577

564578
[source,bash]
@@ -752,6 +766,16 @@ perf record和perf top收集的信息一样,但是perf record会将收集的
752766
- perf record [COMMAND] : 运行命令,知道命令退出
753767
- perf record PID : 监控指定进程,知道CTRL+C退出
754768
- perf record -a : 监控所有进程,知道CTRL+C退出
769+
- -e event记录指定的事件
770+
- --filter 对一个事件设置一个布尔过滤器表达式
771+
- -p PID 仅仅记录这个PID的进程
772+
- -t TID仅仅记录这个TID的线程
773+
- -G cgroup 仅记录这个cgroup
774+
- -g 记录栈踪迹
775+
- --call-graph mode:使用指定的方法记录栈踪迹 (fp、dwarf或者lbr)
776+
- -o file设置输出文件
777+
- -v 显示详细信息,使用-vv可以输出更多信息
778+
755779

756780
当然perf也能够支持定时任务,比如:
757781

0 commit comments

Comments
 (0)