当然,可以使用 eBPF 来做到这一点。
实际上,在密件抄送工具中有一个工具可以做类似的事情。funclatency跟踪一组函数并显示其延迟的直方图(每次调用在函数中花费的时间):
# ./funclatency do_sys_open
Tracing do_sys_open... Hit Ctrl-C to end.
^C
nsecs : count distribution
0 -> 1 : 0 | |
2 -> 3 : 0 | |
4 -> 7 : 0 | |
8 -> 15 : 0 | |
16 -> 31 : 0 | |
32 -> 63 : 0 | |
64 -> 127 : 0 | |
128 -> 255 : 0 | |
256 -> 511 : 0 | |
512 -> 1023 : 0 | |
1024 -> 2047 : 0 | |
2048 -> 4095 : 124 |**************** |
4096 -> 8191 : 291 |**************************************|
8192 -> 16383 : 36 |**** |
16384 -> 32767 : 16 |** |
32768 -> 65535 : 8 |* |
65536 -> 131071 : 0 | |
131072 -> 262143 : 0 | |
262144 -> 524287 : 0 | |
524288 -> 1048575 : 0 | |
1048576 -> 2097151 : 0 | |
2097152 -> 4194303 : 1 | |
Detaching...
从funclatency开始,很容易派生出一个可以满足您需求的工具:
from __future__ import print_function
from bcc import BPF
from time import sleep, strftime
import argparse
parser = argparse.ArgumentParser(
description="Time functions and print time spent in each function",
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument("-p", "--pid", type=int, help="trace this PID only")
parser.add_argument("pattern", help="search expression for functions")
args = parser.parse_args()
# define BPF program
bpf_text = """
#include <uapi/linux/ptrace.h>
struct stats_t {
u64 time;
u64 freq;
};
BPF_HASH(start, u32);
BPF_HASH(ipaddr, u32);
BPF_HASH(stats, u64, struct stats_t);
int trace_func_entry(struct pt_regs *ctx)
{
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 pid = pid_tgid;
u64 ts = bpf_ktime_get_ns();
u64 ip = PT_REGS_IP(ctx);
ipaddr.update(&pid, &ip);
start.update(&pid, &ts);
return 0;
}
int trace_func_return(struct pt_regs *ctx)
{
u64 *tsp, delta;
u64 pid_tgid = bpf_get_current_pid_tgid();
u32 pid = pid_tgid;
// calculate delta time
tsp = start.lookup(&pid);
if (tsp == 0) {
return 0; // missed start
}
delta = bpf_ktime_get_ns() - *tsp;
start.delete(&pid);
// store as histogram
u64 ip, *ipp = ipaddr.lookup(&pid);
if (ipp) {
ip = *ipp;
struct stats_t *stat = stats.lookup(&ip);
if (stat) {
stat->time += delta;
stat->freq++;
} else {
struct stats_t s = {};
s.time = delta;
s.freq = 1;
stats.update(&ip, &s);
}
ipaddr.delete(&pid);
}
return 0;
}
"""
# load BPF program
b = BPF(text=bpf_text)
pattern = args.pattern.replace('*', '.*')
pattern = '^' + pattern + '$'
b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry")
b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return")
matched = b.num_open_kprobes()
if matched == 0:
print("0 functions matched by \"%s\". Exiting." % args.pattern)
exit()
# header
print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
(matched / 2, args.pattern))
while (1):
try:
sleep(99999999)
except KeyboardInterrupt:
break
print()
print("%-36s %8s %16s" % ("FUNC", "COUNT", "TIME (nsecs)"))
stats = b.get_table("stats")
for k, v in stats.items():
print("%-36s %8s %16s" % (BPF.sym(k.value, -1), v.freq, v.time))
该工具使用 BPF 直接在内核中计算每个函数调用的频率和总持续时间。在显示它们之前,聚合值仅在最后复制到用户空间。输出:
# ./time-spent do_sys_*
Tracing 6 functions for "do_sys_*"... Hit Ctrl-C to end.
^C
FUNC COUNT TIME (nsecs)
do_sys_poll 6783 213928440379
do_sys_open 3127 43172656
do_sys_ftruncate.constprop.15 39 189623
此解决方案依赖 bcc 在内核中加载 BPF 程序,但也可以在 C 中执行所有操作。您可以在存储库中找到开始使用 bcc的教程。