下面这个例子和之前的例子相比少了执行了attach_kprobe。具体看看是怎么实现的呢?
#!/usr/bin/python
#
# bitehist.py Block I/O size histogram.
# For Linux, uses BCC, eBPF. Embedded C.
#
# Written as a basic example of using a histogram to show a distribution.
#
# The default interval is 5 seconds. A Ctrl-C will print the partially
# gathered histogram then exit.
#
# Copyright (c) 2015 Brendan Gregg.
# Licensed under the Apache License, Version 2.0 (the "License")
#
# 15-Aug-2015 Brendan Gregg Created this.
from bcc import BPF
#看起来这个程序中会用到sleep来睡眠
from time import sleep
# load BPF program
b = BPF(text="""
#include <uapi/linux/ptrace.h>
#include <linux/blkdev.h>
#定义一个变量,这边变量会一直记录保存在这个变量中值变化的历史
BPF_HISTOGRAM(dist);
#原来在在要监控的c函数前面加上kprobe__ 就可以省去attach_kprobe
int kprobe__blk_account_io_completion(struct pt_regs *ctx, struct request *req)
{
#继续保存,注意这里不会覆盖上一次的值
dist.increment(bpf_log2l(req->__data_len / 1024));
return 0;
}
""")
# header
#python中打印一行header,后面会显示这次测试的结果
print("Tracing... Hit Ctrl-C to end.")
# trace until Ctrl-C
#一直睡眠,从而让dist中存储的值增加,方便后面显示这个值的history
try:
sleep(99999999)
except KeyboardInterrupt:
print
# output
#打印我们前面定义dist。以kbyte为head
b["dist"].print_log2_hist("kbytes")
Tracing... Hit Ctrl-C to end.
^C
kbytes : count distribution
0 -> 1 : 3 | |
2 -> 3 : 0 | |
4 -> 7 : 211 |********** |
8 -> 15 : 0 | |
16 -> 31 : 0 | |
32 -> 63 : 0 | |
64 -> 127 : 1 | |
128 -> 255 : 800 |**************************************|
Lesson 9. bitehist.py
最新推荐文章于 2024-04-25 14:19:26 发布