blob: 21438fb8b8d3840050b9b61d560fa14931bed227 [file] [log] [blame]
Brendan Gregg74016c32015-09-21 15:49:21 -07001#!/usr/bin/python
2#
3# funclatency Time kernel funcitons and print latency as a histogram.
4# For Linux, uses BCC, eBPF.
5#
6# USAGE: funclatency [-h] [-p PID] [-i INTERVAL] [-T] [-u] [-m] [-r] pattern
7#
8# Run "funclatency -h" for full usage.
9#
10# The pattern is a string with optional '*' wildcards, similar to file globbing.
11# If you'd prefer to use regular expressions, use the -r option. Matching
12# multiple functions is of limited use, since the output has one histogram for
13# everything. Future versions should split the output histogram by the function.
14#
Brendan Gregg50bbca42015-09-25 12:47:53 -070015# Currently nested or recursive functions are not supported properly, and
16# timestamps will be overwritten, creating dubious output. Try to match single
17# functions, or groups of functions that run at the same stack layer, and
18# don't ultimately call each other.
19#
Brendan Gregg74016c32015-09-21 15:49:21 -070020# Copyright (c) 2015 Brendan Gregg.
21# Licensed under the Apache License, Version 2.0 (the "License")
22#
23# 20-Sep-2015 Brendan Gregg Created this.
24
25from __future__ import print_function
26from bcc import BPF
27from time import sleep, strftime
28import argparse
29import signal
30
31# arguments
32examples = """examples:
33 ./funclatency do_sys_open # time the do_sys_open() kenel function
34 ./funclatency -u vfs_read # time vfs_read(), in microseconds
35 ./funclatency -m do_nanosleep # time do_nanosleep(), in milliseconds
36 ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps
37 ./funclatency -p 181 vfs_read # time process 181 only
38 ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat()
Brendan Gregg50bbca42015-09-25 12:47:53 -070039 ./funclatency -F 'vfs_r*' # show one histogram per matched function
Brendan Gregg74016c32015-09-21 15:49:21 -070040"""
41parser = argparse.ArgumentParser(
42 description="Time kernel funcitons and print latency as a histogram",
43 formatter_class=argparse.RawDescriptionHelpFormatter,
44 epilog=examples)
45parser.add_argument("-p", "--pid",
46 help="trace this PID only")
47parser.add_argument("-i", "--interval", default=99999999,
48 help="summary interval, seconds")
49parser.add_argument("-T", "--timestamp", action="store_true",
50 help="include timestamp on output")
51parser.add_argument("-u", "--microseconds", action="store_true",
52 help="microsecond histogram")
53parser.add_argument("-m", "--milliseconds", action="store_true",
54 help="millisecond histogram")
Brendan Gregg50bbca42015-09-25 12:47:53 -070055parser.add_argument("-F", "--function", action="store_true",
56 help="show a separate histogram per function")
Brendan Gregg74016c32015-09-21 15:49:21 -070057parser.add_argument("-r", "--regexp", action="store_true",
58 help="use regular expressions. Default is \"*\" wildcards only.")
59parser.add_argument("pattern",
60 help="search expression for kernel functions")
61args = parser.parse_args()
62pattern = args.pattern
63if not args.regexp:
64 pattern = pattern.replace('*', '.*')
65 pattern = '^' + pattern + '$'
66debug = 0
67
68# define BPF program
69bpf_text = """
70#include <uapi/linux/ptrace.h>
71#include <linux/blkdev.h>
72
Brendan Gregg50bbca42015-09-25 12:47:53 -070073typedef struct ip_key {
74 u64 ip;
75 u64 slot;
76} ip_key_t;
77
Brendan Gregg74016c32015-09-21 15:49:21 -070078BPF_HASH(start, u32);
Brendan Gregg50bbca42015-09-25 12:47:53 -070079STORAGE
Brendan Gregg74016c32015-09-21 15:49:21 -070080
81int trace_func_entry(struct pt_regs *ctx)
82{
83 u32 pid = bpf_get_current_pid_tgid();
84 u64 ts = bpf_ktime_get_ns();
85
86 FILTER
Brendan Gregg50bbca42015-09-25 12:47:53 -070087 ENTRYSTORE
Brendan Gregg74016c32015-09-21 15:49:21 -070088 start.update(&pid, &ts);
89
90 return 0;
91}
92
93int trace_func_return(struct pt_regs *ctx)
94{
95 u64 *tsp, delta;
96 u32 pid = bpf_get_current_pid_tgid();
97
98 // calculate delta time
99 tsp = start.lookup(&pid);
100 if (tsp == 0) {
101 return 0; // missed start
102 }
Brendan Gregg74016c32015-09-21 15:49:21 -0700103 delta = bpf_ktime_get_ns() - *tsp;
Brendan Gregg37368852015-09-25 11:17:00 -0700104 start.delete(&pid);
Brendan Gregg74016c32015-09-21 15:49:21 -0700105 FACTOR
106
107 // store as histogram
Brendan Gregg50bbca42015-09-25 12:47:53 -0700108 STORE
Brendan Gregg74016c32015-09-21 15:49:21 -0700109
110 return 0;
111}
112"""
Brendan Gregg50bbca42015-09-25 12:47:53 -0700113
114# code substitutions
Brendan Gregg74016c32015-09-21 15:49:21 -0700115if args.pid:
116 bpf_text = bpf_text.replace('FILTER',
117 'if (pid != %s) { return 0; }' % args.pid)
118else:
119 bpf_text = bpf_text.replace('FILTER', '')
120if args.milliseconds:
121 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
122 label = "msecs"
123elif args.microseconds:
124 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
125 label = "usecs"
126else:
127 bpf_text = bpf_text.replace('FACTOR', '')
128 label = "nsecs"
Brendan Gregg50bbca42015-09-25 12:47:53 -0700129if args.function:
130 bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n' +
131 'BPF_HISTOGRAM(dist, ip_key_t);')
132 # stash the IP on entry, as on return it's kretprobe_trampoline:
133 bpf_text = bpf_text.replace('ENTRYSTORE',
134 'u64 ip = ctx->ip; ipaddr.update(&pid, &ip);')
135 bpf_text = bpf_text.replace('STORE',
136 'u64 ip, *ipp = ipaddr.lookup(&pid); if (ipp) { ip = *ipp; ' +
137 'dist.increment((ip_key_t){ip, bpf_log2l(delta)}); ' +
138 'ipaddr.delete(&pid); }')
139else:
140 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
141 bpf_text = bpf_text.replace('ENTRYSTORE', '')
142 bpf_text = bpf_text.replace('STORE',
143 'dist.increment(bpf_log2l(delta));')
Brendan Gregg74016c32015-09-21 15:49:21 -0700144if debug:
145 print(bpf_text)
146
147# signal handler
148def signal_ignore(signal, frame):
149 print()
150
151# load BPF program
152b = BPF(text=bpf_text)
153b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry")
154b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return")
Brendan Gregg6b8add02015-09-25 11:16:33 -0700155matched = b.num_open_kprobes()
156if matched == 0:
157 print("0 functions matched by \"%s\". Exiting." % args.pattern)
158 exit()
Brendan Gregg74016c32015-09-21 15:49:21 -0700159
160# header
Brendan Gregg6b8add02015-09-25 11:16:33 -0700161print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
162 (matched / 2, args.pattern))
Brendan Gregg74016c32015-09-21 15:49:21 -0700163
Brendan Gregg50bbca42015-09-25 12:47:53 -0700164# custom output (from __init__.py)
165def print_log2_hist_byfunc(self, val_type="value"):
166 tmp = {}
167 f1 = self.Key._fields_[0][0]
168 f2 = self.Key._fields_[1][0]
169 for k, v in self.items():
170 bucket = getattr(k, f1)
171 vals = tmp[bucket] = tmp.get(bucket, [0] * 65)
172 slot = getattr(k, f2)
173 vals[slot] = v.value
174 for bucket, vals in tmp.items():
175 print("\nFunction = %s" % BPF.ksym(bucket))
176 self._print_log2_hist(vals, val_type, 0)
177
Brendan Gregg74016c32015-09-21 15:49:21 -0700178# output
179exiting = 0 if args.interval else 1
180dist = b.get_table("dist")
181while (1):
182 try:
183 sleep(int(args.interval))
184 except KeyboardInterrupt:
185 exiting=1
186 # as cleanup can take many seconds, trap Ctrl-C:
187 signal.signal(signal.SIGINT, signal_ignore)
188
189 print()
190 if args.timestamp:
191 print("%-8s\n" % strftime("%H:%M:%S"), end="")
192
Brendan Gregg50bbca42015-09-25 12:47:53 -0700193 if args.function:
194 print_log2_hist_byfunc(dist, label)
195 else:
196 dist.print_log2_hist(label)
Brendan Gregg74016c32015-09-21 15:49:21 -0700197 dist.clear()
198
199 if exiting:
200 print("Detaching...")
201 exit()