BPF:BCC工具 funccount 统计内核函数调用(内核函数、跟踪点USDT探针)认知

不必太纠结于当下,也不必太忧虑未来,当你经历过一些事情的时候,眼前的风景已经和从前不一样了。——村上春树

写在前面


  • 博文内容涉及BCC工具 funccount 认知
  • 理解不足小伙伴帮忙指正 :),生活加油

不必太纠结于当下,也不必太忧虑未来,当你经历过一些事情的时候,眼前的风景已经和从前不一样了。——村上春树


funccount 是什么?

funccount(8) 是BCC对事件,特别是函数调用进行计数的一个工具,可以使用它回答以下问题:

  • 某个内核态或用户态函数是否被调用过?
1
2
3
4
5
6
7
8
9
┌──[root@liruilongs.github.io]-[~] 
└─$funccount tcp_send_fin
Tracing 1 functions for "b'tcp_send_fin'"... Hit Ctrl-C to end.
^C
FUNC COUNT
tcp_send_fin 8
Detaching...
┌──[root@liruilongs.github.io]-[~]
└─$

这将统计 tcp_send_fin 函数的调用次数。如果输出显示调用次数大于 0,那就说明这个函数确实被调用过。

  • 该函数每秒被调用了多少次?
1
2
3
4
5
6
7
8
9
┌──[root@liruilongs.github.io]-[~] 
└─$funccount -d 10 tcp_send_fin
Tracing 1 functions for "b'tcp_send_fin'"... Hit Ctrl-C to end.

FUNC COUNT
tcp_send_fin 6
Detaching...
┌──[root@liruilongs.github.io]-[~]
└─$

这将每 10 秒钟输出一次 tcp_send_fin 函数的调用统计信息。从输出中我们可以计算出每秒被调用的次数,这就是该函数的调用频率。

funccount 用于自动执行 ftrace 的简单脚本。它只做一件事:内核函数计数

需要说明的是,并不是所有的函数都可以统计,判断内核函数是否可以被计数,需要检查是否在下面两个文件中。

1
2
3
4
# 内核函数
$cat /proc/kallsyms
# ftrace 可以跟踪的内容
$cat /sys/kernel/debug/tracing/available_filter_functions

常见的应用场景

跟踪 TCP 发送函数的调用次数

观察 TCP 发送相关函数的调用频率,可以通过下面的方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$funccount -i 2 -d 10 "tcp_send_*"
Tracing 13 functions for "b'tcp_send_*'"... Hit Ctrl-C to end.

FUNC COUNT
tcp_send_fin 1
tcp_send_delayed_ack 1
tcp_send_ack 2
tcp_send_mss 3

FUNC COUNT
tcp_send_mss 3

FUNC COUNT
tcp_send_fin 1
tcp_send_delayed_ack 1
tcp_send_ack 2
tcp_send_mss 4

FUNC COUNT
tcp_send_ack 1
tcp_send_mss 3

FUNC COUNT
tcp_send_fin 2
tcp_send_delayed_ack 2
tcp_send_ack 4
tcp_send_mss 6
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

tcp_send_fin: 此函数用于发送 TCP 连接终止请求(FIN 包)。当一方完成数据传输并想要关闭连接时,会发送 FIN 包。收到 FIN 包的一方也会发送一个 ACK 确认包,然后可能继续发送剩余的数据,最后发送自己的 FIN 包来关闭连接。

tcp_send_delayed_ack: 此函数用于发送 TCP 延迟确认(ACK)包。在某些情况下,TCP 协议允许在接收到数据包后不立即发送 ACK 确认包,而是等待一段时间(称为“延迟 ACK”)。此函数负责在适当的时机发送这些延迟 ACK。

tcp_send_ack: 此函数用于发送 TCP 确认(ACK)包。当接收到数据包时,TCP 协议要求发送方发送一个 ACK 确认包以通知接收方已成功接收数据。此函数负责发送这些 ACK 确认包。

tcp_send_mss: 此函数用于发送 TCP 最大分段大小(MSS)选项。MSS 是 TCP 分段中可携带的最大有效载荷。在建立连接时,双方会交换 MSS 值,以便在传输过程中选择合适的分段大小。此函数负责在 SYN 包中发送本地系统的 MSS 值。

跟踪文件读写函数的调用次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
┌──[root@liruilongs.github.io]-[~] 
└─$funccount -i 2 -d 5 'vfs_read|vfs_write'
Tracing 2 functions for "b'vfs_read|vfs_write'"... Hit Ctrl-C to end.

FUNC COUNT
vfs_write 197
vfs_read 451

FUNC COUNT
vfs_write 320
vfs_read 3553

FUNC COUNT
vfs_read 28
vfs_write 85
Detaching...
┌──[root@liruilongs.github.io]-[~]
└─$
  • vfs_read: 此函数负责在内核空间执行文件读取操作。
  • vfs_write: 此函数负责在内核空间执行文件写入操作

跟踪网络套接字创建和销毁函数的调用次数

1
2
3
4
5
6
┌──[root@liruilongs.github.io]-[~] 
└─$funccount -i 25 -d 25 "sock_create|sock_release"
Tracing 2 functions for "b'sock_create|sock_release'"... Hit Ctrl-C to end.

FUNC COUNT
Detaching...

跟踪进程创建和退出的函数调用次数

1
2
3
4
5
6
7
┌──[root@liruilongs.github.io]-[~] 
└─$funccount -i 25 -d 25 'do_fork|do_exit'
Tracing 1 functions for "b'do_fork|do_exit'"... Hit Ctrl-C to end.

FUNC COUNT
do_exit 141
Detaching...

funccount 的示例

1
liruilonger@cloudshell:~/bcc/tools$ cat funccount_example.txt

funccount_example.txt 这个文档描述了 funccount 这个 eBPF/bcc 工具的使用方法和功能。

指定一个模式(正则表达式或*通配符),追踪匹配的 函数/tracepoints 调用

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# ./funccount 'vfs_*'
Tracing... Ctrl-C to end.
^C
FUNC COUNT
vfs_create 1
vfs_rename 1
vfs_fsync_range 2
vfs_lock_file 30
vfs_fstatat 152
vfs_fstat 154
vfs_write 166
vfs_getattr_nosec 262
vfs_getattr 262
vfs_open 264
vfs_read 470
Detaching...

上面的输出显示,在跟踪vfsread()函数时调用了 470 次,vfs_open() 264 次等等。

这对于探索内核代码非常有用,可以找出哪些函数正在使用,哪些没有使用。这可以将调查范围缩小到几个功能,这些功能的计数与所调查的工作负载类似。

统计 vfs 函数调用次数

统计所有 tcp 相关函数调用次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$./funccount 'tcp_*'
Tracing 397 functions for "b'tcp_*'"... Hit Ctrl-C to end.
^C
FUNC COUNT
tcp_delack_timer_handler 1
tcp_leave_memory_pressure 1
tcp_delack_timer 2
tcp_write_timer 3
。。。。。。。。。。。。。。。。
tcp_release_cb 972
tcp_poll 1079
tcp_mstamp_refresh 1414
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─

设置统计间隔和最大时间限制,每一秒钟采样,持续时间为5s

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$./funccount -i 1 -d 5 vfs_read
Tracing 1 functions for "b'vfs_read'"... Hit Ctrl-C to end.

FUNC COUNT
vfs_read 356

FUNC COUNT
vfs_read 90

FUNC COUNT
vfs_read 82

FUNC COUNT
vfs_read 3478
^C
FUNC COUNT
vfs_read 13
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

过滤指定进程 ID 下的函数调用

1
./funccount -p 1442 contentions:*

使用正则表达式匹配名称

1
2
3
4
5
6
7
8
9
10
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$./funccount -r 'c:(write|read)$' -d 5
Tracing 2 functions for "b'c:(write|read)$'"... Hit Ctrl-C to end.

FUNC COUNT
read 233
write 388
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

统计指定内核态 tracepoint 静态跟踪事件调用次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$./funccount t:block:* -d 3
Tracing 21 functions for "b't:block:*'"... Hit Ctrl-C to end.

FUNC COUNT
block:block_plug 6
block:block_unplug 6
block:block_getrq 10
block:block_rq_insert 12
block:block_io_done 12
block:block_rq_complete 13
block:block_rq_issue 13
block:block_io_start 13
block:block_bio_remap 20
block:block_bio_backmerge 23
block:block_bio_queue 34
block:block_dirty_buffer 125
block:block_touch_buffer 145
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

统计用户态 USDT 探针静态跟踪调用次数

1
./funccount u:pthread:mutex -p 1442

动态查看指定函数调用变化,每秒统计一次数据信息

1
./funccount -i 1 'vfs_*'

统计单个函数指定时间内调用次数

1
./funccount -d 5 vfs_read

过滤指定 CPU 下的函数调用

1
funccount.py -i 1 -c 1 lapic_next_deadline

funccount 的语法

funccount(8)的命令行参数包括可以用来改变行为的选项,以及一个描述被插桩事件的字符串:

1
funccount [opention] eventname

eventname 的语法是:

  • name 或者 p:name:对内核函数 name() 进行插桩。
  • lib:name 或者 p:1ib:name:对用户态 lib 库中的函数 name() 进行插桩。
  • Path:name:对位于 path 路径下文件中的用户态函数 name() 进行插桩。
  • t:system:name:对名为 system:name 的内核跟踪点进行插桩。
  • u:lib:name:对 lib 库中名为 name 的 USDT 探针进行插桩。
  • *:用来匹配任意字符的通配符。-r 选项允许使用正则表达式。
1
2
3
4
5
6
7
func            -- probe a kernel function
lib:func -- probe a user-space function in the library 'lib'
/path:func -- probe a user-space function in binary '/path'
p::func -- same thing as 'func'
p:lib:func -- same thing as 'lib:func'
t:cat:event -- probe a kernel tracepoint
u:lib:probe -- probe a USDT tracepoint

funccount 的单行程序

对虚拟文件系统 VFS 内核函数进行计数

1
funccount 'vfs_*'

对 TCP 内核函数进行计数

1
funccount "tcp_*"

统计每秒 TCP 发送函数的调用次数

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$funccount -i 2 -d 4 "tcp_send_*"
Tracing 13 functions for "b'tcp_send_*'"... Hit Ctrl-C to end.

FUNC COUNT
tcp_send_mss 1
tcp_send_ack 3
tcp_send_delayed_ack 6

FUNC COUNT
tcp_send_fin 1
tcp_send_delayed_ack 1
tcp_send_mss 4
tcp_send_ack 5
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

展示每秒块 IO 事件的数量

1
funccount -i 1 't:block;*

展示每秒新创建的进程数量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$funccount -i 1 t:sched:sched_process_fork -d 5
Tracing 1 functions for "b't:sched:sched_process_fork'"... Hit Ctrl-C to end.

FUNC COUNT
sched:sched_process_fork 9

FUNC COUNT
sched:sched_process_fork 15

FUNC COUNT
sched:sched_process_fork 2

FUNC COUNT
sched:sched_process_fork 1

FUNC COUNT
sched:sched_process_fork 1
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

展示每秒 libcgetaddrinfo()(域名解析)函数的调用次数

1
2
3
4
5
6
7
8
9
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools] 
└─$funccount -i 25 -d 25 "c:getaddrinfo"
Tracing 1 functions for "b'c:getaddrinfo'"... Hit Ctrl-C to end.

FUNC COUNT
getaddrinfo 14
Detaching...
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$

libcmalloc() 调用进行计数:

1
2
3
4
5
6
7
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools/doc] 
└─$funccount c:malloc
Tracing 1 functions for "b'c:malloc'"... Hit Ctrl-C to end.
^C
FUNC COUNT
malloc 18168
Detaching...

funccount 的帮助信息

1
2
liruilonger@cloudshell:~$ funccount  -h
liruilonger@cloudshell:~$ cat funccount_example.txt

funcccount 常见报错

1
2
3
4
5
6
7
8
9
10
11
12
┌──[root@liruilongs.github.io]-[~] 
└─$funccount -i 2 -d 10 'vfs_read,vfs_write'
No functions matched by pattern b'^vfs_read,vfs_write$'
┌──[root@liruilongs.github.io]-[~]
└─$funccount u:pthread:mutex
USDT failed to instrument path b'/lib64/libpthread.so.0'
┌──[root@liruilongs.github.io]-[/usr/share/bcc/tools]
└─$funccount -i 1 't:block;*'
Can't mix strings and bytes in path components
┌──[root@liruilongs.github.io]-[~]
└─$funccount go:os.*
Can't mix strings and bytes in path components

funccount 源码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
#!/usr/bin/env python
# @lint-avoid-python-3-compatibility-imports
#
# funccount Count functions, tracepoints, and USDT probes.
# For Linux, uses BCC, eBPF.
#
# USAGE: funccount [-h] [-p PID] [-i INTERVAL] [-d DURATION] [-T] [-r]
# [-c CPU] pattern
#
# The pattern is a string with optional '*' wildcards, similar to file
# globbing. If you'd prefer to use regular expressions, use the -r option.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 09-Sep-2015 Brendan Gregg Created this.
# 18-Oct-2016 Sasha Goldshtein Generalized for uprobes, tracepoints, USDT.

from __future__ import print_function
from bcc import ArgString, BPF, USDT
from time import sleep, strftime
import argparse
import re
import signal
import sys
import traceback

debug = False

def verify_limit(num):
probe_limit = BPF.get_probe_limit()
if num > probe_limit:
raise Exception("maximum of %d probes allowed, attempted %d" %
(probe_limit, num))

# 实际的观测类
class Probe(object):
# 初始化跟踪对象
def __init__(self, pattern, use_regex=False, pid=None, cpu=None):
"""Init a new probe.

Init the probe from the pattern provided by the user. The supported
patterns mimic the 'trace' and 'argdist' tools, but are simpler because
we don't have to distinguish between probes and retprobes.

func -- probe a kernel function
lib:func -- probe a user-space function in the library 'lib'
/path:func -- probe a user-space function in binary '/path'
p::func -- same thing as 'func'
p:lib:func -- same thing as 'lib:func'
t:cat:event -- probe a kernel tracepoint
u:lib:probe -- probe a USDT tracepoint
"""
# 跟踪的表达式进行解析,可以看到这里都是字节操作
# 基于`分隔符b':'拆分字节串`,得到一个部分列表。
parts = bytes(pattern).split(b':')
# 如果只有一个部分,意味着缺少类型,因此将类型设置为b"p"(表示“模式”),库设置为空字节串,
if len(parts) == 1:
# 也就是上面 func ===》 `p::func` 部分
parts = [b"p", b"", parts[0]]
# 如果有两个切片,将类型设置为b"p",并将第一个部分作为库,第二个部分作为模式
elif len(parts) == 2:
# lib:func ==》 `p:lib:func``
parts = [b"p", parts[0], parts[1]]
# 如果有三个
elif len(parts) == 3:
# 第一个切片为 `t` 为内核跟踪点
if parts[0] == b"t":
parts = [b"t", b"", b"%s:%s" % tuple(parts[1:])]
# 如果不为 t p u 那么抛异常
if parts[0] not in [b"p", b"t", b"u"]:
raise Exception("Type must be 'p', 't', or 'u', but got %s" %
parts[0])
else:
# 不在上面的范围,抛异常
raise Exception("Too many ':'-separated components in pattern %s" %
pattern)
# 从 parts 中解构 三个变量
(self.type, self.library, self.pattern) = parts
# use_regex 为初始化方法参数,是否使用正则,如果不使用,use_regex为False,需要转化为正则
if not use_regex:
self.pattern = self.pattern.replace(b'*', b'.*')
self.pattern = b'^' + self.pattern + b'$'

# 如果类型是b"p"且指定了库,或者类型是b"u",
# 尝试使用BPF.find_library()方法找到库路径。如果找不到,
# 尝试使用BPF.find_exe()方法找到可执行文件
if (self.type == b"p" and self.library) or self.type == b"u":
libpath = BPF.find_library(self.library)
if libpath is None:
# This might be an executable (e.g. 'bash')
libpath = BPF.find_exe(str(self.library))
if libpath is None or len(libpath) == 0:
raise Exception("unable to find library %s" % self.library)
self.library = libpath

self.pid = pid
self.cpu = cpu
self.matched = 0
self.trace_functions = {} # map location number to function name
# 是否为内核探针
def is_kernel_probe(self):
return self.type == b"t" or (self.type == b"p" and self.library == b"")

def attach(self):
if self.type == b"p" and not self.library:
for index, function in self.trace_functions.items():
self.bpf.attach_kprobe(
event=function,
fn_name="trace_count_%d" % index)
elif self.type == b"p" and self.library:
for index, function in self.trace_functions.items():
self.bpf.attach_uprobe(
name=self.library,
sym=function,
fn_name="trace_count_%d" % index,
pid=self.pid or -1)
elif self.type == b"t":
for index, function in self.trace_functions.items():
self.bpf.attach_tracepoint(
tp=function,
fn_name="trace_count_%d" % index)
elif self.type == b"u":
pass # Nothing to do -- attach already happened in `load`

def _add_function(self, template, probe_name):
new_func = b"trace_count_%d" % self.matched
text = template.replace(b"PROBE_FUNCTION", new_func)
text = text.replace(b"LOCATION", b"%d" % self.matched)
self.trace_functions[self.matched] = probe_name
self.matched += 1
return text

def _generate_functions(self, template):
self.usdt = None
text = b""
if self.type == b"p" and not self.library:
functions = BPF.get_kprobe_functions(self.pattern)
verify_limit(len(functions))
for function in functions:
text += self._add_function(template, function)
elif self.type == b"p" and self.library:
# uprobes are tricky because the same function may have multiple
# addresses, and the same address may be mapped to multiple
# functions. We aren't allowed to create more than one uprobe
# per address, so track unique addresses and ignore functions that
# map to an address that we've already seen. Also ignore functions
# that may repeat multiple times with different addresses.
addresses, functions = (set(), set())
functions_and_addresses = BPF.get_user_functions_and_addresses(
self.library, self.pattern)
verify_limit(len(functions_and_addresses))
for function, address in functions_and_addresses:
if address in addresses or function in functions:
continue
addresses.add(address)
functions.add(function)
text += self._add_function(template, function)
elif self.type == b"t":
tracepoints = BPF.get_tracepoints(self.pattern)
verify_limit(len(tracepoints))
for tracepoint in tracepoints:
text += self._add_function(template, tracepoint)
elif self.type == b"u":
self.usdt = USDT(path=str(self.library), pid=self.pid)
matches = []
for probe in self.usdt.enumerate_probes():
if not self.pid and (probe.bin_path != self.library):
continue
if re.match(self.pattern, probe.name):
matches.append(probe.name)
verify_limit(len(matches))
for match in matches:
new_func = b"trace_count_%d" % self.matched
text += self._add_function(template, match)
self.usdt.enable_probe(match, new_func)
if debug:
print(self.usdt.get_text())
return text

def load(self):
"""
@Desc : 更具给定的探针配置加载 eBPF 程序
"""

trace_count_text = b"""
int PROBE_FUNCTION(void *ctx) {
FILTERPID
FILTERCPU
int loc = LOCATION;
counts.atomic_increment(loc);
return 0;
}
"""
bpf_text = b"""#include <uapi/linux/ptrace.h>

BPF_ARRAY(counts, u64, NUMLOCATIONS);
"""

# We really mean the tgid from the kernel's perspective, which is in
# the top 32 bits of bpf_get_current_pid_tgid().
if self.pid:
trace_count_text = trace_count_text.replace(b'FILTERPID',
b"""u32 pid = bpf_get_current_pid_tgid() >> 32;
if (pid != %d) { return 0; }""" % self.pid)
else:
trace_count_text = trace_count_text.replace(b'FILTERPID', b'')

if self.cpu:
trace_count_text = trace_count_text.replace(b'FILTERCPU',
b"""u32 cpu = bpf_get_smp_processor_id();
if (cpu != %d) { return 0; }""" % int(self.cpu))
else:
trace_count_text = trace_count_text.replace(b'FILTERCPU', b'')

bpf_text += self._generate_functions(trace_count_text)
bpf_text = bpf_text.replace(b"NUMLOCATIONS",
b"%d" % len(self.trace_functions))
if debug:
print(bpf_text)

if self.matched == 0:
raise Exception("No functions matched by pattern %s" %
self.pattern)

self.bpf = BPF(text=bpf_text,
usdt_contexts=[self.usdt] if self.usdt else [])
self.clear() # Initialize all array items to zero

def counts(self):
return self.bpf["counts"]

def clear(self):
counts = self.bpf["counts"]
for location, _ in list(self.trace_functions.items()):
counts[counts.Key(location)] = counts.Leaf()

class Tool(object):
def __init__(self) -> None:
# 工具的使用 Demo 说明
examples = """examples:
./funccount 'vfs_*' # count kernel fns starting with "vfs"
./funccount -r '^vfs.*' # same as above, using regular expressions
./funccount -Ti 5 'vfs_*' # output every 5 seconds, with timestamps
./funccount -d 10 'vfs_*' # trace for 10 seconds only
./funccount -p 185 'vfs_*' # count vfs calls for PID 181 only
./funccount t:sched:sched_fork # count calls to the sched_fork tracepoint
./funccount -p 185 u:node:gc* # count all GC USDT probes in node, PID 185
./funccount c:malloc # count all malloc() calls in libc
./funccount go:os.* # count all "os.*" calls in libgo
./funccount -p 185 go:os.* # count all "os.*" calls in libgo, PID 185
./funccount ./test:read* # count "read*" calls in the ./test binary
./funccount -c 1 'vfs_*' # count vfs calls on CPU 1 only
"""
#argparse 用于帮助输出,以及参数解析
parser = argparse.ArgumentParser(
description="Count functions, tracepoints, and USDT probes",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog=examples)
parser.add_argument("-p", "--pid", type=int,
help="trace this PID only")
parser.add_argument("-i", "--interval",
help="summary interval, seconds")
parser.add_argument("-d", "--duration",
help="total duration of trace, seconds")
parser.add_argument("-T", "--timestamp", action="store_true",
help="include timestamp on output")
parser.add_argument("-r", "--regexp", action="store_true",
help="use regular expressions. Default is \"*\" wildcards only.")
parser.add_argument("-D", "--debug", action="store_true",
help="print BPF program before starting (for debugging purposes)")
parser.add_argument("-c", "--cpu",
help="trace this CPU only")
parser.add_argument("pattern",
type=ArgString,
help="search expression for events")
# 获取到解析的参数
self.args = parser.parse_args()
global debug
debug = self.args.debug
# 初始化跟踪对象,跟踪对象用于执行实际的 BPF 操作
self.probe = Probe(self.args.pattern, self.args.regexp, self.args.pid,
self.args.cpu)
# 这里可以看到获得到的命令行参数包括 跟踪的表达式,正则匹配式,进程ID 已经 CPU 编号
# 如果 有 -d 没有 -i 。那么 -i 配置成 -d 的纸
if self.args.duration and not self.args.interval:
self.args.interval = self.args.duration
# 如果 -i 没有设置,默认是 99999999
if not self.args.interval:
self.args.interval = 99999999

@staticmethod
def _signal_ignore(signal, frame):
print()

def run(self):
# 调用self.probe.load()方法加载eBPF程序
self.probe.load()
# 调用self.probe.attach()方法将eBPF程序附加到相应的目标上。
self.probe.attach()
print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
(self.probe.matched, bytes(self.args.pattern)))
# 初始化exiting变量以控制程序的退出
exiting = 0 if self.args.interval else 1
# 初始化seconds变量为0,用于跟踪程序的运行时间
seconds = 0
# 进入无限循环,直到exiting变量变为1:
while True:
try:
sleep(int(self.args.interval))
seconds += int(self.args.interval)
except KeyboardInterrupt:
exiting = 1
# as cleanup can take many seconds, trap Ctrl-C:
signal.signal(signal.SIGINT, Tool._signal_ignore) # type: ignore
if self.args.duration and seconds >= int(self.args.duration):
exiting = 1

print()
if self.args.timestamp:
print("%-8s\n" % strftime("%H:%M:%S"), end="")

print("%-36s %8s" % ("FUNC", "COUNT"))
counts = self.probe.counts()
for k, v in sorted(counts.items(),
key=lambda counts: counts[1].value):
if v.value == 0:
continue
print("%-36s %8d" %
(self.probe.trace_functions[k.value].decode('utf-8', 'replace'), v.value))
if exiting:
print("Detaching...")
exit()
else:
self.probe.clear()

if __name__ == "__main__":
try:
Tool().run()
except Exception:
if debug:
traceback.print_exc()
elif sys.exc_info()[0] is not SystemExit:
print(sys.exc_info()[1])

对源码进行简单分析

Probe 类提供了用于创建、配置和附加 eBPF 探测对象的方法。

  1. **__init__**:初始化一个新的探测对象。解析用户提供的模式、PID 和 CPU 参数。根据模式类型(内核函数、用户空间函数、跟踪点或 USDT 探针),设置探测对象的属性。

  2. **is_kernel_probe**:检查探测对象是否为内核探测。如果探测类型为 t(跟踪点)或者是类型为 p(用户空间或内核函数)且库名称为空(表示内核函数),则返回 True

  3. **attach**:将探测对象附加到目标上。根据探测类型(内核函数、用户空间函数、跟踪点或 USDT 探针),使用 BCC 库将 BPF 程序附加到相应的目标上。

  4. **_add_function**:向 BPF 程序模板中添加新的探测函数。这个方法根据给定的模板和探测名称生成一个新的探测函数,并将其添加到 BPF 程序文本中。同时,将新函数的索引和名称添加到 trace_functions 字典中。

  5. **_generate_functions**:根据探测类型和模式生成 BPF 程序文本。这个方法根据探测类型(内核函数、用户空间函数、跟踪点或 USDT 探针)和模式,生成相应的 BPF 程序文本。对于用户空间函数和 USDT 探针,还需要处理多个地址和重复函数的问题。

  6. **load**:加载 BPF 程序。这个方法首先定义了一个基本的 BPF 程序模板,然后根据探测类型和模式生成具体的 BPF 程序文本。接着,使用 BCC 库将 BPF 程序加载到内核中。最后,初始化所有计数器数组项为零。

  7. **counts**:返回 BPF 程序的计数器数组。这个方法返回一个字典,其中键是探测位置的索引,值是对应的计数值。

  8. **clear**:清除所有计数器数组项。这个方法遍历 trace_functions 字典,将所有计数器数组项的值重置为零。

Tool 类是 funccount 脚本的主体部分,它负责解析命令行参数、创建探测对象、加载 BPF 程序、附加探测并定期输出计数结果

  1. **__init__**:初始化 Tool 对象。设置命令行参数和示例,使用 argparse 解析命令行参数,如目标进程的 PID、采样间隔、持续时间、是否使用正则表达式匹配函数名等。然后,根据解析得到的参数创建一个 Probe 对象。

  2. **_signal_ignore**:静态方法,用于忽略 Ctrl+C 信号。这在脚本运行期间捕获 Ctrl+C 时很有用,因为它允许脚本在退出前完成清理工作。

  3. **run**:运行 funccount 工具。首先,调用 probe.load() 加载 BPF 程序,然后调用 probe.attach() 将探测附加到目标上。接着,进入一个循环,定期输出当前的计数结果。循环将持续到达到指定的持续时间或用户按下 Ctrl+C。在循环中,根据参数设置,输出计数结果,包括时间戳、函数名和调用次数。如果达到持续时间或用户按下 Ctrl+C,脚本将停止统计,卸载 BPF 程序,并输出最后一次的计数结果。

Tool 类提供了运行 funccount 工具所需的主要功能。

博文部分内容参考

© 文中涉及参考链接内容版权归原作者所有,如有侵权请告知 :)


《BPF Performance Tools》


© 2018-2024 liruilonger@gmail.com, 保持署名-非商用-相同方式共享(CC BY-NC-SA 4.0)

BPF:BCC工具 funccount 统计内核函数调用(内核函数、跟踪点USDT探针)认知

https://liruilongs.github.io/2024/06/09/待发布/BPF:BCC工具 funccount统计事件(内核函数、跟踪点USDT探针)认知/

发布于

2024-06-09

更新于

2024-07-05

许可协议

评论
Your browser is out-of-date!

Update your browser to view this website correctly.&npsb;Update my browser now

×