blob: f23d8f06bc2cb5507da9daf6d58a9965675b1c7d [file] [log] [blame]
Alexey Ivanov777e8022019-01-03 13:46:38 -08001#!/usr/bin/env python
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08002# @lint-avoid-python-3-compatibility-imports
Brendan Gregg74016c32015-09-21 15:49:21 -07003#
Sasha Goldshteina466c462016-10-06 21:17:59 +03004# funclatency Time functions and print latency as a histogram.
Alexei Starovoitovbdf07732016-01-14 10:09:20 -08005# For Linux, uses BCC, eBPF.
Brendan Gregg74016c32015-09-21 15:49:21 -07006#
Sasha Goldshteina466c462016-10-06 21:17:59 +03007# USAGE: funclatency [-h] [-p PID] [-i INTERVAL] [-T] [-u] [-m] [-F] [-r] [-v]
8# pattern
Brendan Gregg74016c32015-09-21 15:49:21 -07009#
10# Run "funclatency -h" for full usage.
11#
Sasha Goldshteinf41ae862016-10-19 01:14:30 +030012# The pattern is a string with optional '*' wildcards, similar to file
13# globbing. If you'd prefer to use regular expressions, use the -r option.
Brendan Gregg74016c32015-09-21 15:49:21 -070014#
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#
Sasha Goldshteina466c462016-10-06 21:17:59 +030023# 20-Sep-2015 Brendan Gregg Created this.
24# 06-Oct-2016 Sasha Goldshtein Added user function support.
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:
Sasha Goldshteina466c462016-10-06 21:17:59 +030034 ./funclatency do_sys_open # time the do_sys_open() kernel function
35 ./funclatency c:read # time the read() C library function
Brendan Gregg74016c32015-09-21 15:49:21 -070036 ./funclatency -u vfs_read # time vfs_read(), in microseconds
37 ./funclatency -m do_nanosleep # time do_nanosleep(), in milliseconds
Akilesh Kailash89967192018-05-18 13:36:54 -070038 ./funclatency -i 2 -d 10 c:open # output every 2 seconds, for duration 10s
Brendan Gregg74016c32015-09-21 15:49:21 -070039 ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps
40 ./funclatency -p 181 vfs_read # time process 181 only
41 ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat()
Sasha Goldshteina466c462016-10-06 21:17:59 +030042 ./funclatency 'c:*printf' # time the *printf family of functions
Brendan Gregg50bbca42015-09-25 12:47:53 -070043 ./funclatency -F 'vfs_r*' # show one histogram per matched function
Brendan Gregg74016c32015-09-21 15:49:21 -070044"""
45parser = argparse.ArgumentParser(
Sasha Goldshteina466c462016-10-06 21:17:59 +030046 description="Time functions and print latency as a histogram",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080047 formatter_class=argparse.RawDescriptionHelpFormatter,
48 epilog=examples)
htbegin5ac5d6e2017-05-24 22:53:17 +080049parser.add_argument("-p", "--pid", type=int,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080050 help="trace this PID only")
Akilesh Kailash89967192018-05-18 13:36:54 -070051parser.add_argument("-i", "--interval", type=int,
52 help="summary interval, in seconds")
53parser.add_argument("-d", "--duration", type=int,
54 help="total duration of trace, in seconds")
Brendan Gregg74016c32015-09-21 15:49:21 -070055parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080056 help="include timestamp on output")
Brendan Gregg74016c32015-09-21 15:49:21 -070057parser.add_argument("-u", "--microseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080058 help="microsecond histogram")
Brendan Gregg74016c32015-09-21 15:49:21 -070059parser.add_argument("-m", "--milliseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080060 help="millisecond histogram")
Brendan Gregg50bbca42015-09-25 12:47:53 -070061parser.add_argument("-F", "--function", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080062 help="show a separate histogram per function")
Brendan Gregg74016c32015-09-21 15:49:21 -070063parser.add_argument("-r", "--regexp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080064 help="use regular expressions. Default is \"*\" wildcards only.")
Sasha Goldshteina466c462016-10-06 21:17:59 +030065parser.add_argument("-v", "--verbose", action="store_true",
66 help="print the BPF program (for debugging purposes)")
Brendan Gregg74016c32015-09-21 15:49:21 -070067parser.add_argument("pattern",
Sasha Goldshteina466c462016-10-06 21:17:59 +030068 help="search expression for functions")
Nathan Scottcf0792f2018-02-02 16:56:50 +110069parser.add_argument("--ebpf", action="store_true",
70 help=argparse.SUPPRESS)
Brendan Gregg74016c32015-09-21 15:49:21 -070071args = parser.parse_args()
Akilesh Kailash89967192018-05-18 13:36:54 -070072if args.duration and not args.interval:
73 args.interval = args.duration
74if not args.interval:
75 args.interval = 99999999
Sasha Goldshteina466c462016-10-06 21:17:59 +030076
77def bail(error):
78 print("Error: " + error)
79 exit(1)
80
81parts = args.pattern.split(':')
82if len(parts) == 1:
83 library = None
84 pattern = args.pattern
85elif len(parts) == 2:
86 library = parts[0]
87 libpath = BPF.find_library(library) or BPF.find_exe(library)
88 if not libpath:
89 bail("can't resolve library %s" % library)
90 library = libpath
91 pattern = parts[1]
92else:
93 bail("unrecognized pattern format '%s'" % pattern)
94
Brendan Gregg74016c32015-09-21 15:49:21 -070095if not args.regexp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080096 pattern = pattern.replace('*', '.*')
97 pattern = '^' + pattern + '$'
Brendan Gregg74016c32015-09-21 15:49:21 -070098
99# define BPF program
100bpf_text = """
101#include <uapi/linux/ptrace.h>
Brendan Gregg74016c32015-09-21 15:49:21 -0700102
Sasha Goldshteina466c462016-10-06 21:17:59 +0300103typedef struct ip_pid {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800104 u64 ip;
Sasha Goldshteina466c462016-10-06 21:17:59 +0300105 u64 pid;
106} ip_pid_t;
107
108typedef struct hist_key {
109 ip_pid_t key;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800110 u64 slot;
Sasha Goldshteina466c462016-10-06 21:17:59 +0300111} hist_key_t;
Brendan Gregg50bbca42015-09-25 12:47:53 -0700112
Brendan Gregg74016c32015-09-21 15:49:21 -0700113BPF_HASH(start, u32);
Brendan Gregg50bbca42015-09-25 12:47:53 -0700114STORAGE
Brendan Gregg74016c32015-09-21 15:49:21 -0700115
116int trace_func_entry(struct pt_regs *ctx)
117{
Sasha Goldshteina466c462016-10-06 21:17:59 +0300118 u64 pid_tgid = bpf_get_current_pid_tgid();
119 u32 pid = pid_tgid;
120 u32 tgid = pid_tgid >> 32;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800121 u64 ts = bpf_ktime_get_ns();
Brendan Gregg74016c32015-09-21 15:49:21 -0700122
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800123 FILTER
124 ENTRYSTORE
125 start.update(&pid, &ts);
Brendan Gregg74016c32015-09-21 15:49:21 -0700126
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800127 return 0;
Brendan Gregg74016c32015-09-21 15:49:21 -0700128}
129
130int trace_func_return(struct pt_regs *ctx)
131{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800132 u64 *tsp, delta;
Sasha Goldshteina466c462016-10-06 21:17:59 +0300133 u64 pid_tgid = bpf_get_current_pid_tgid();
134 u32 pid = pid_tgid;
135 u32 tgid = pid_tgid >> 32;
Brendan Gregg74016c32015-09-21 15:49:21 -0700136
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800137 // calculate delta time
138 tsp = start.lookup(&pid);
139 if (tsp == 0) {
140 return 0; // missed start
141 }
142 delta = bpf_ktime_get_ns() - *tsp;
143 start.delete(&pid);
144 FACTOR
Brendan Gregg74016c32015-09-21 15:49:21 -0700145
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800146 // store as histogram
147 STORE
Brendan Gregg74016c32015-09-21 15:49:21 -0700148
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800149 return 0;
Brendan Gregg74016c32015-09-21 15:49:21 -0700150}
151"""
Brendan Gregg50bbca42015-09-25 12:47:53 -0700152
Sasha Goldshteina466c462016-10-06 21:17:59 +0300153# do we need to store the IP and pid for each invocation?
154need_key = args.function or (library and not args.pid)
155
Brendan Gregg50bbca42015-09-25 12:47:53 -0700156# code substitutions
Brendan Gregg74016c32015-09-21 15:49:21 -0700157if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800158 bpf_text = bpf_text.replace('FILTER',
htbegin5ac5d6e2017-05-24 22:53:17 +0800159 'if (tgid != %d) { return 0; }' % args.pid)
Brendan Gregg74016c32015-09-21 15:49:21 -0700160else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800161 bpf_text = bpf_text.replace('FILTER', '')
Brendan Gregg74016c32015-09-21 15:49:21 -0700162if args.milliseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800163 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
164 label = "msecs"
Brendan Gregg74016c32015-09-21 15:49:21 -0700165elif args.microseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800166 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
167 label = "usecs"
Brendan Gregg74016c32015-09-21 15:49:21 -0700168else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800169 bpf_text = bpf_text.replace('FACTOR', '')
170 label = "nsecs"
Sasha Goldshteina466c462016-10-06 21:17:59 +0300171if need_key:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800172 bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n' +
Sasha Goldshteina466c462016-10-06 21:17:59 +0300173 'BPF_HISTOGRAM(dist, hist_key_t);')
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800174 # stash the IP on entry, as on return it's kretprobe_trampoline:
175 bpf_text = bpf_text.replace('ENTRYSTORE',
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530176 'u64 ip = PT_REGS_IP(ctx); ipaddr.update(&pid, &ip);')
Sasha Goldshteina466c462016-10-06 21:17:59 +0300177 pid = '-1' if not library else 'tgid'
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800178 bpf_text = bpf_text.replace('STORE',
Sasha Goldshteina466c462016-10-06 21:17:59 +0300179 """
180 u64 ip, *ipp = ipaddr.lookup(&pid);
181 if (ipp) {
182 ip = *ipp;
183 hist_key_t key;
184 key.key.ip = ip;
185 key.key.pid = %s;
186 key.slot = bpf_log2l(delta);
187 dist.increment(key);
188 ipaddr.delete(&pid);
189 }
190 """ % pid)
Brendan Gregg50bbca42015-09-25 12:47:53 -0700191else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800192 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
193 bpf_text = bpf_text.replace('ENTRYSTORE', '')
194 bpf_text = bpf_text.replace('STORE',
195 'dist.increment(bpf_log2l(delta));')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100196if args.verbose or args.ebpf:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800197 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100198 if args.ebpf:
199 exit()
Brendan Gregg74016c32015-09-21 15:49:21 -0700200
201# signal handler
202def signal_ignore(signal, frame):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800203 print()
Brendan Gregg74016c32015-09-21 15:49:21 -0700204
205# load BPF program
206b = BPF(text=bpf_text)
Sasha Goldshteina466c462016-10-06 21:17:59 +0300207
208# attach probes
209if not library:
210 b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry")
211 b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return")
212 matched = b.num_open_kprobes()
213else:
Paul Chaignond73c58f2017-01-21 14:25:41 +0100214 b.attach_uprobe(name=library, sym_re=pattern, fn_name="trace_func_entry",
215 pid=args.pid or -1)
Sasha Goldshteinf41ae862016-10-19 01:14:30 +0300216 b.attach_uretprobe(name=library, sym_re=pattern,
Paul Chaignond73c58f2017-01-21 14:25:41 +0100217 fn_name="trace_func_return", pid=args.pid or -1)
Sasha Goldshteina466c462016-10-06 21:17:59 +0300218 matched = b.num_open_uprobes()
219
Brendan Gregg6b8add02015-09-25 11:16:33 -0700220if matched == 0:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800221 print("0 functions matched by \"%s\". Exiting." % args.pattern)
222 exit()
Brendan Gregg74016c32015-09-21 15:49:21 -0700223
224# header
Brendan Gregg6b8add02015-09-25 11:16:33 -0700225print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
226 (matched / 2, args.pattern))
Brendan Gregg74016c32015-09-21 15:49:21 -0700227
228# output
Sasha Goldshteina466c462016-10-06 21:17:59 +0300229def print_section(key):
230 if not library:
231 return BPF.sym(key[0], -1)
232 else:
233 return "%s [%d]" % (BPF.sym(key[0], key[1]), key[1])
234
Brendan Gregg74016c32015-09-21 15:49:21 -0700235exiting = 0 if args.interval else 1
Akilesh Kailash89967192018-05-18 13:36:54 -0700236seconds = 0
Brendan Gregg74016c32015-09-21 15:49:21 -0700237dist = b.get_table("dist")
238while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800239 try:
Akilesh Kailash89967192018-05-18 13:36:54 -0700240 sleep(args.interval)
241 seconds += args.interval
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800242 except KeyboardInterrupt:
243 exiting = 1
244 # as cleanup can take many seconds, trap Ctrl-C:
245 signal.signal(signal.SIGINT, signal_ignore)
Akilesh Kailash89967192018-05-18 13:36:54 -0700246 if args.duration and seconds >= args.duration:
247 exiting = 1
Brendan Gregg74016c32015-09-21 15:49:21 -0700248
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800249 print()
250 if args.timestamp:
251 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg74016c32015-09-21 15:49:21 -0700252
Sasha Goldshteina466c462016-10-06 21:17:59 +0300253 if need_key:
254 dist.print_log2_hist(label, "Function", section_print_fn=print_section,
255 bucket_fn=lambda k: (k.ip, k.pid))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800256 else:
257 dist.print_log2_hist(label)
258 dist.clear()
Brendan Gregg74016c32015-09-21 15:49:21 -0700259
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800260 if exiting:
261 print("Detaching...")
262 exit()