xref: /aosp_15_r20/external/bcc/examples/tracing/vfsreadlat.py (revision 387f9dfdfa2baef462e92476d413c7bc2470293e)
1#!/usr/bin/python
2#
3# vfsreadlat.py		VFS read latency distribution.
4#			For Linux, uses BCC, eBPF. See .c file.
5#
6# Written as a basic example of a function latency distribution histogram.
7#
8# USAGE: vfsreadlat.py [interval [count]]
9#
10# The default interval is 5 seconds. A Ctrl-C will print the partially
11# gathered histogram then exit.
12#
13# Copyright (c) 2015 Brendan Gregg.
14# Licensed under the Apache License, Version 2.0 (the "License")
15#
16# 15-Aug-2015	Brendan Gregg	Created this.
17
18from __future__ import print_function
19from bcc import BPF
20from time import sleep
21from sys import argv
22
23def usage():
24	print("USAGE: %s [interval [count]]" % argv[0])
25	exit()
26
27# arguments
28interval = 5
29count = -1
30if len(argv) > 1:
31	try:
32		interval = int(argv[1])
33		if interval == 0:
34			raise
35		if len(argv) > 2:
36			count = int(argv[2])
37	except:	# also catches -h, --help
38		usage()
39
40# load BPF program
41b = BPF(src_file = "vfsreadlat.c")
42b.attach_kprobe(event="vfs_read", fn_name="do_entry")
43b.attach_kretprobe(event="vfs_read", fn_name="do_return")
44
45# header
46print("Tracing... Hit Ctrl-C to end.")
47
48# output
49loop = 0
50do_exit = 0
51while (1):
52	if count > 0:
53		loop += 1
54		if loop > count:
55			exit()
56	try:
57		sleep(interval)
58	except KeyboardInterrupt:
59		pass; do_exit = 1
60
61	print()
62	b["dist"].print_log2_hist("usecs")
63	b["dist"].clear()
64	if do_exit:
65		exit()
66