blob: 1968c94faff34c88972e1b659a57dfd833a021e0 [file] [log] [blame]
Brendan Gregg74016c32015-09-21 15:49:21 -07001#!/usr/bin/python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg74016c32015-09-21 15:49:21 -07003#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08004# funclatency Time kernel funcitons and print latency as a histogram.
5# For Linux, uses BCC, eBPF.
Brendan Gregg74016c32015-09-21 15:49:21 -07006#
7# USAGE: funclatency [-h] [-p PID] [-i INTERVAL] [-T] [-u] [-m] [-r] pattern
8#
9# Run "funclatency -h" for full usage.
10#
11# The pattern is a string with optional '*' wildcards, similar to file globbing.
12# If you'd prefer to use regular expressions, use the -r option. Matching
13# multiple functions is of limited use, since the output has one histogram for
14# everything. Future versions should split the output histogram by the function.
15#
Brendan Gregg50bbca42015-09-25 12:47:53 -070016# Currently nested or recursive functions are not supported properly, and
17# timestamps will be overwritten, creating dubious output. Try to match single
18# functions, or groups of functions that run at the same stack layer, and
19# don't ultimately call each other.
20#
Brendan Gregg74016c32015-09-21 15:49:21 -070021# Copyright (c) 2015 Brendan Gregg.
22# Licensed under the Apache License, Version 2.0 (the "License")
23#
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080024# 20-Sep-2015 Brendan Gregg Created this.
Brendan Gregg74016c32015-09-21 15:49:21 -070025
26from __future__ import print_function
27from bcc import BPF
28from time import sleep, strftime
29import argparse
30import signal
31
32# arguments
33examples = """examples:
34 ./funclatency do_sys_open # time the do_sys_open() kenel function
35 ./funclatency -u vfs_read # time vfs_read(), in microseconds
36 ./funclatency -m do_nanosleep # time do_nanosleep(), in milliseconds
37 ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps
38 ./funclatency -p 181 vfs_read # time process 181 only
39 ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat()
Brendan Gregg50bbca42015-09-25 12:47:53 -070040 ./funclatency -F 'vfs_r*' # show one histogram per matched function
Brendan Gregg74016c32015-09-21 15:49:21 -070041"""
42parser = argparse.ArgumentParser(
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080043 description="Time kernel funcitons and print latency as a histogram",
44 formatter_class=argparse.RawDescriptionHelpFormatter,
45 epilog=examples)
Brendan Gregg74016c32015-09-21 15:49:21 -070046parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080047 help="trace this PID only")
Brendan Gregg74016c32015-09-21 15:49:21 -070048parser.add_argument("-i", "--interval", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 help="summary interval, seconds")
Brendan Gregg74016c32015-09-21 15:49:21 -070050parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080051 help="include timestamp on output")
Brendan Gregg74016c32015-09-21 15:49:21 -070052parser.add_argument("-u", "--microseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080053 help="microsecond histogram")
Brendan Gregg74016c32015-09-21 15:49:21 -070054parser.add_argument("-m", "--milliseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080055 help="millisecond histogram")
Brendan Gregg50bbca42015-09-25 12:47:53 -070056parser.add_argument("-F", "--function", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080057 help="show a separate histogram per function")
Brendan Gregg74016c32015-09-21 15:49:21 -070058parser.add_argument("-r", "--regexp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080059 help="use regular expressions. Default is \"*\" wildcards only.")
Brendan Gregg74016c32015-09-21 15:49:21 -070060parser.add_argument("pattern",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080061 help="search expression for kernel functions")
Brendan Gregg74016c32015-09-21 15:49:21 -070062args = parser.parse_args()
63pattern = args.pattern
64if not args.regexp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080065 pattern = pattern.replace('*', '.*')
66 pattern = '^' + pattern + '$'
Brendan Gregg74016c32015-09-21 15:49:21 -070067debug = 0
68
69# define BPF program
70bpf_text = """
71#include <uapi/linux/ptrace.h>
72#include <linux/blkdev.h>
73
Brendan Gregg50bbca42015-09-25 12:47:53 -070074typedef struct ip_key {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080075 u64 ip;
76 u64 slot;
Brendan Gregg50bbca42015-09-25 12:47:53 -070077} ip_key_t;
78
Brendan Gregg74016c32015-09-21 15:49:21 -070079BPF_HASH(start, u32);
Brendan Gregg50bbca42015-09-25 12:47:53 -070080STORAGE
Brendan Gregg74016c32015-09-21 15:49:21 -070081
82int trace_func_entry(struct pt_regs *ctx)
83{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080084 u32 pid = bpf_get_current_pid_tgid();
85 u64 ts = bpf_ktime_get_ns();
Brendan Gregg74016c32015-09-21 15:49:21 -070086
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080087 FILTER
88 ENTRYSTORE
89 start.update(&pid, &ts);
Brendan Gregg74016c32015-09-21 15:49:21 -070090
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080091 return 0;
Brendan Gregg74016c32015-09-21 15:49:21 -070092}
93
94int trace_func_return(struct pt_regs *ctx)
95{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080096 u64 *tsp, delta;
97 u32 pid = bpf_get_current_pid_tgid();
Brendan Gregg74016c32015-09-21 15:49:21 -070098
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080099 // calculate delta time
100 tsp = start.lookup(&pid);
101 if (tsp == 0) {
102 return 0; // missed start
103 }
104 delta = bpf_ktime_get_ns() - *tsp;
105 start.delete(&pid);
106 FACTOR
Brendan Gregg74016c32015-09-21 15:49:21 -0700107
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800108 // store as histogram
109 STORE
Brendan Gregg74016c32015-09-21 15:49:21 -0700110
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800111 return 0;
Brendan Gregg74016c32015-09-21 15:49:21 -0700112}
113"""
Brendan Gregg50bbca42015-09-25 12:47:53 -0700114
115# code substitutions
Brendan Gregg74016c32015-09-21 15:49:21 -0700116if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800117 bpf_text = bpf_text.replace('FILTER',
118 'if (pid != %s) { return 0; }' % args.pid)
Brendan Gregg74016c32015-09-21 15:49:21 -0700119else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800120 bpf_text = bpf_text.replace('FILTER', '')
Brendan Gregg74016c32015-09-21 15:49:21 -0700121if args.milliseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800122 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
123 label = "msecs"
Brendan Gregg74016c32015-09-21 15:49:21 -0700124elif args.microseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800125 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
126 label = "usecs"
Brendan Gregg74016c32015-09-21 15:49:21 -0700127else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800128 bpf_text = bpf_text.replace('FACTOR', '')
129 label = "nsecs"
Brendan Gregg50bbca42015-09-25 12:47:53 -0700130if args.function:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800131 bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n' +
132 'BPF_HISTOGRAM(dist, ip_key_t);')
133 # stash the IP on entry, as on return it's kretprobe_trampoline:
134 bpf_text = bpf_text.replace('ENTRYSTORE',
135 'u64 ip = ctx->ip; ipaddr.update(&pid, &ip);')
136 bpf_text = bpf_text.replace('STORE',
137 'u64 ip, *ipp = ipaddr.lookup(&pid); if (ipp) { ip = *ipp; ' +
138 'dist.increment((ip_key_t){ip, bpf_log2l(delta)}); ' +
139 'ipaddr.delete(&pid); }')
Brendan Gregg50bbca42015-09-25 12:47:53 -0700140else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800141 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
142 bpf_text = bpf_text.replace('ENTRYSTORE', '')
143 bpf_text = bpf_text.replace('STORE',
144 'dist.increment(bpf_log2l(delta));')
Brendan Gregg74016c32015-09-21 15:49:21 -0700145if debug:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800146 print(bpf_text)
Brendan Gregg74016c32015-09-21 15:49:21 -0700147
148# signal handler
149def signal_ignore(signal, frame):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800150 print()
Brendan Gregg74016c32015-09-21 15:49:21 -0700151
152# load BPF program
153b = BPF(text=bpf_text)
154b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry")
155b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return")
Brendan Gregg6b8add02015-09-25 11:16:33 -0700156matched = b.num_open_kprobes()
157if matched == 0:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800158 print("0 functions matched by \"%s\". Exiting." % args.pattern)
159 exit()
Brendan Gregg74016c32015-09-21 15:49:21 -0700160
161# header
Brendan Gregg6b8add02015-09-25 11:16:33 -0700162print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
163 (matched / 2, args.pattern))
Brendan Gregg74016c32015-09-21 15:49:21 -0700164
165# output
166exiting = 0 if args.interval else 1
167dist = b.get_table("dist")
168while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800169 try:
170 sleep(int(args.interval))
171 except KeyboardInterrupt:
172 exiting = 1
173 # as cleanup can take many seconds, trap Ctrl-C:
174 signal.signal(signal.SIGINT, signal_ignore)
Brendan Gregg74016c32015-09-21 15:49:21 -0700175
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800176 print()
177 if args.timestamp:
178 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg74016c32015-09-21 15:49:21 -0700179
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800180 if args.function:
181 dist.print_log2_hist(label, "Function", BPF.ksym)
182 else:
183 dist.print_log2_hist(label)
184 dist.clear()
Brendan Gregg74016c32015-09-21 15:49:21 -0700185
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800186 if exiting:
187 print("Detaching...")
188 exit()