blob: 4e6407c02727e13adef5b149b4acd61ac5cffee4 [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#
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#
12# The pattern is a string with optional '*' wildcards, similar to file globbing.
Sasha Goldshteina466c462016-10-06 21:17:59 +030013# 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
38 ./funclatency -mTi 5 vfs_read # output every 5 seconds, with timestamps
39 ./funclatency -p 181 vfs_read # time process 181 only
40 ./funclatency 'vfs_fstat*' # time both vfs_fstat() and vfs_fstatat()
Sasha Goldshteina466c462016-10-06 21:17:59 +030041 ./funclatency 'c:*printf' # time the *printf family of functions
Brendan Gregg50bbca42015-09-25 12:47:53 -070042 ./funclatency -F 'vfs_r*' # show one histogram per matched function
Brendan Gregg74016c32015-09-21 15:49:21 -070043"""
44parser = argparse.ArgumentParser(
Sasha Goldshteina466c462016-10-06 21:17:59 +030045 description="Time functions and print latency as a histogram",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080046 formatter_class=argparse.RawDescriptionHelpFormatter,
47 epilog=examples)
Brendan Gregg74016c32015-09-21 15:49:21 -070048parser.add_argument("-p", "--pid",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080049 help="trace this PID only")
Brendan Gregg74016c32015-09-21 15:49:21 -070050parser.add_argument("-i", "--interval", default=99999999,
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080051 help="summary interval, seconds")
Brendan Gregg74016c32015-09-21 15:49:21 -070052parser.add_argument("-T", "--timestamp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080053 help="include timestamp on output")
Brendan Gregg74016c32015-09-21 15:49:21 -070054parser.add_argument("-u", "--microseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080055 help="microsecond histogram")
Brendan Gregg74016c32015-09-21 15:49:21 -070056parser.add_argument("-m", "--milliseconds", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080057 help="millisecond histogram")
Brendan Gregg50bbca42015-09-25 12:47:53 -070058parser.add_argument("-F", "--function", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080059 help="show a separate histogram per function")
Brendan Gregg74016c32015-09-21 15:49:21 -070060parser.add_argument("-r", "--regexp", action="store_true",
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080061 help="use regular expressions. Default is \"*\" wildcards only.")
Sasha Goldshteina466c462016-10-06 21:17:59 +030062parser.add_argument("-v", "--verbose", action="store_true",
63 help="print the BPF program (for debugging purposes)")
Brendan Gregg74016c32015-09-21 15:49:21 -070064parser.add_argument("pattern",
Sasha Goldshteina466c462016-10-06 21:17:59 +030065 help="search expression for functions")
Brendan Gregg74016c32015-09-21 15:49:21 -070066args = parser.parse_args()
Sasha Goldshteina466c462016-10-06 21:17:59 +030067
68def bail(error):
69 print("Error: " + error)
70 exit(1)
71
72parts = args.pattern.split(':')
73if len(parts) == 1:
74 library = None
75 pattern = args.pattern
76elif len(parts) == 2:
77 library = parts[0]
78 libpath = BPF.find_library(library) or BPF.find_exe(library)
79 if not libpath:
80 bail("can't resolve library %s" % library)
81 library = libpath
82 pattern = parts[1]
83else:
84 bail("unrecognized pattern format '%s'" % pattern)
85
Brendan Gregg74016c32015-09-21 15:49:21 -070086if not args.regexp:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080087 pattern = pattern.replace('*', '.*')
88 pattern = '^' + pattern + '$'
Brendan Gregg74016c32015-09-21 15:49:21 -070089
90# define BPF program
91bpf_text = """
92#include <uapi/linux/ptrace.h>
93#include <linux/blkdev.h>
94
Sasha Goldshteina466c462016-10-06 21:17:59 +030095typedef struct ip_pid {
Alexei Starovoitovbdf07732016-01-14 10:09:20 -080096 u64 ip;
Sasha Goldshteina466c462016-10-06 21:17:59 +030097 u64 pid;
98} ip_pid_t;
99
100typedef struct hist_key {
101 ip_pid_t key;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800102 u64 slot;
Sasha Goldshteina466c462016-10-06 21:17:59 +0300103} hist_key_t;
Brendan Gregg50bbca42015-09-25 12:47:53 -0700104
Brendan Gregg74016c32015-09-21 15:49:21 -0700105BPF_HASH(start, u32);
Brendan Gregg50bbca42015-09-25 12:47:53 -0700106STORAGE
Brendan Gregg74016c32015-09-21 15:49:21 -0700107
108int trace_func_entry(struct pt_regs *ctx)
109{
Sasha Goldshteina466c462016-10-06 21:17:59 +0300110 u64 pid_tgid = bpf_get_current_pid_tgid();
111 u32 pid = pid_tgid;
112 u32 tgid = pid_tgid >> 32;
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800113 u64 ts = bpf_ktime_get_ns();
Brendan Gregg74016c32015-09-21 15:49:21 -0700114
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800115 FILTER
116 ENTRYSTORE
117 start.update(&pid, &ts);
Brendan Gregg74016c32015-09-21 15:49:21 -0700118
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800119 return 0;
Brendan Gregg74016c32015-09-21 15:49:21 -0700120}
121
122int trace_func_return(struct pt_regs *ctx)
123{
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800124 u64 *tsp, delta;
Sasha Goldshteina466c462016-10-06 21:17:59 +0300125 u64 pid_tgid = bpf_get_current_pid_tgid();
126 u32 pid = pid_tgid;
127 u32 tgid = pid_tgid >> 32;
Brendan Gregg74016c32015-09-21 15:49:21 -0700128
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800129 // calculate delta time
130 tsp = start.lookup(&pid);
131 if (tsp == 0) {
132 return 0; // missed start
133 }
134 delta = bpf_ktime_get_ns() - *tsp;
135 start.delete(&pid);
136 FACTOR
Brendan Gregg74016c32015-09-21 15:49:21 -0700137
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800138 // store as histogram
139 STORE
Brendan Gregg74016c32015-09-21 15:49:21 -0700140
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800141 return 0;
Brendan Gregg74016c32015-09-21 15:49:21 -0700142}
143"""
Brendan Gregg50bbca42015-09-25 12:47:53 -0700144
Sasha Goldshteina466c462016-10-06 21:17:59 +0300145# do we need to store the IP and pid for each invocation?
146need_key = args.function or (library and not args.pid)
147
Brendan Gregg50bbca42015-09-25 12:47:53 -0700148# code substitutions
Brendan Gregg74016c32015-09-21 15:49:21 -0700149if args.pid:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800150 bpf_text = bpf_text.replace('FILTER',
Sasha Goldshteina466c462016-10-06 21:17:59 +0300151 'if (tgid != %s) { return 0; }' % args.pid)
Brendan Gregg74016c32015-09-21 15:49:21 -0700152else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800153 bpf_text = bpf_text.replace('FILTER', '')
Brendan Gregg74016c32015-09-21 15:49:21 -0700154if args.milliseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800155 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
156 label = "msecs"
Brendan Gregg74016c32015-09-21 15:49:21 -0700157elif args.microseconds:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800158 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
159 label = "usecs"
Brendan Gregg74016c32015-09-21 15:49:21 -0700160else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800161 bpf_text = bpf_text.replace('FACTOR', '')
162 label = "nsecs"
Sasha Goldshteina466c462016-10-06 21:17:59 +0300163if need_key:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800164 bpf_text = bpf_text.replace('STORAGE', 'BPF_HASH(ipaddr, u32);\n' +
Sasha Goldshteina466c462016-10-06 21:17:59 +0300165 'BPF_HISTOGRAM(dist, hist_key_t);')
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800166 # stash the IP on entry, as on return it's kretprobe_trampoline:
167 bpf_text = bpf_text.replace('ENTRYSTORE',
Naveen N. Rao4afa96a2016-05-03 14:54:21 +0530168 'u64 ip = PT_REGS_IP(ctx); ipaddr.update(&pid, &ip);')
Sasha Goldshteina466c462016-10-06 21:17:59 +0300169 pid = '-1' if not library else 'tgid'
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800170 bpf_text = bpf_text.replace('STORE',
Sasha Goldshteina466c462016-10-06 21:17:59 +0300171 """
172 u64 ip, *ipp = ipaddr.lookup(&pid);
173 if (ipp) {
174 ip = *ipp;
175 hist_key_t key;
176 key.key.ip = ip;
177 key.key.pid = %s;
178 key.slot = bpf_log2l(delta);
179 dist.increment(key);
180 ipaddr.delete(&pid);
181 }
182 """ % pid)
Brendan Gregg50bbca42015-09-25 12:47:53 -0700183else:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800184 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
185 bpf_text = bpf_text.replace('ENTRYSTORE', '')
186 bpf_text = bpf_text.replace('STORE',
187 'dist.increment(bpf_log2l(delta));')
Sasha Goldshteina466c462016-10-06 21:17:59 +0300188if args.verbose:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800189 print(bpf_text)
Brendan Gregg74016c32015-09-21 15:49:21 -0700190
191# signal handler
192def signal_ignore(signal, frame):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800193 print()
Brendan Gregg74016c32015-09-21 15:49:21 -0700194
195# load BPF program
196b = BPF(text=bpf_text)
Sasha Goldshteina466c462016-10-06 21:17:59 +0300197
198# attach probes
199if not library:
200 b.attach_kprobe(event_re=pattern, fn_name="trace_func_entry")
201 b.attach_kretprobe(event_re=pattern, fn_name="trace_func_return")
202 matched = b.num_open_kprobes()
203else:
204 b.attach_uprobe(name=library, sym_re=pattern, fn_name="trace_func_entry")
205 b.attach_uretprobe(name=library, sym_re=pattern, fn_name="trace_func_return")
206 matched = b.num_open_uprobes()
207
Brendan Gregg6b8add02015-09-25 11:16:33 -0700208if matched == 0:
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800209 print("0 functions matched by \"%s\". Exiting." % args.pattern)
210 exit()
Brendan Gregg74016c32015-09-21 15:49:21 -0700211
212# header
Brendan Gregg6b8add02015-09-25 11:16:33 -0700213print("Tracing %d functions for \"%s\"... Hit Ctrl-C to end." %
214 (matched / 2, args.pattern))
Brendan Gregg74016c32015-09-21 15:49:21 -0700215
216# output
Sasha Goldshteina466c462016-10-06 21:17:59 +0300217def print_section(key):
218 if not library:
219 return BPF.sym(key[0], -1)
220 else:
221 return "%s [%d]" % (BPF.sym(key[0], key[1]), key[1])
222
Brendan Gregg74016c32015-09-21 15:49:21 -0700223exiting = 0 if args.interval else 1
224dist = b.get_table("dist")
225while (1):
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800226 try:
227 sleep(int(args.interval))
228 except KeyboardInterrupt:
229 exiting = 1
230 # as cleanup can take many seconds, trap Ctrl-C:
231 signal.signal(signal.SIGINT, signal_ignore)
Brendan Gregg74016c32015-09-21 15:49:21 -0700232
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800233 print()
234 if args.timestamp:
235 print("%-8s\n" % strftime("%H:%M:%S"), end="")
Brendan Gregg74016c32015-09-21 15:49:21 -0700236
Sasha Goldshteina466c462016-10-06 21:17:59 +0300237 if need_key:
238 dist.print_log2_hist(label, "Function", section_print_fn=print_section,
239 bucket_fn=lambda k: (k.ip, k.pid))
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800240 else:
241 dist.print_log2_hist(label)
242 dist.clear()
Brendan Gregg74016c32015-09-21 15:49:21 -0700243
Alexei Starovoitovbdf07732016-01-14 10:09:20 -0800244 if exiting:
245 print("Detaching...")
246 exit()