Lesson 9. bitehist.py

下面这个例子和之前的例子相比少了执行了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      |**************************************|

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值