• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1#!/usr/bin/env 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 ctypes import c_ushort, c_int, c_ulonglong
21from time import sleep
22from sys import argv
23
24def usage():
25	print("USAGE: %s [interval [count]]" % argv[0])
26	exit()
27
28# arguments
29interval = 5
30count = -1
31if len(argv) > 1:
32	try:
33		interval = int(argv[1])
34		if interval == 0:
35			raise
36		if len(argv) > 2:
37			count = int(argv[2])
38	except:	# also catches -h, --help
39		usage()
40
41# load BPF program
42b = BPF(src_file = "vfsreadlat.c")
43b.attach_kprobe(event="vfs_read", fn_name="do_entry")
44b.attach_kretprobe(event="vfs_read", fn_name="do_return")
45
46# header
47print("Tracing... Hit Ctrl-C to end.")
48
49# output
50loop = 0
51do_exit = 0
52while (1):
53	if count > 0:
54		loop += 1
55		if loop > count:
56			exit()
57	try:
58		sleep(interval)
59	except KeyboardInterrupt:
60		pass; do_exit = 1
61
62	print()
63	b["dist"].print_log2_hist("usecs")
64	b["dist"].clear()
65	if do_exit:
66		exit()
67