blob: 9fd40642beb913089086126b3523f2c1d19a1c66 [file] [log] [blame]
Brendan Gregg3a391c22016-02-08 01:20:31 -08001#!/usr/bin/python
2# @lint-avoid-python-3-compatibility-imports
3#
4# runqlat Run queue (scheduler) latency as a histogram.
5# For Linux, uses BCC, eBPF.
6#
Brenden Blancoa6875602016-05-02 23:32:44 -07007# USAGE: runqlat [-h] [-T] [-m] [-P] [-L] [-p PID] [interval] [count]
Brendan Gregg3a391c22016-02-08 01:20:31 -08008#
9# This measures the time a task spends waiting on a run queue for a turn
10# on-CPU, and shows this time as a histogram. This time should be small, but a
11# task may need to wait its turn due to CPU load.
12#
13# This measures two types of run queue latency:
14# 1. The time from a task being enqueued on a run queue to its context switch
Ivan Babrou5c48a3f2018-05-08 17:24:19 -070015# and execution. This traces ttwu_do_wakeup(), wake_up_new_task() ->
16# finish_task_switch() with either raw tracepoints (if supported) or kprobes
Brendan Gregg3a391c22016-02-08 01:20:31 -080017# and instruments the run queue latency after a voluntary context switch.
18# 2. The time from when a task was involuntary context switched and still
19# in the runnable state, to when it next executed. This is instrumented
20# from finish_task_switch() alone.
21#
22# Copyright 2016 Netflix, Inc.
23# Licensed under the Apache License, Version 2.0 (the "License")
24#
25# 07-Feb-2016 Brendan Gregg Created this.
26
27from __future__ import print_function
28from bcc import BPF
29from time import sleep, strftime
30import argparse
31
32# arguments
33examples = """examples:
34 ./runqlat # summarize run queue latency as a histogram
35 ./runqlat 1 10 # print 1 second summaries, 10 times
36 ./runqlat -mT 1 # 1s summaries, milliseconds, and timestamps
37 ./runqlat -P # show each PID separately
38 ./runqlat -p 185 # trace PID 185 only
39"""
40parser = argparse.ArgumentParser(
Brendan Gregg82769382017-04-18 14:23:14 -050041 description="Summarize run queue (scheduler) latency as a histogram",
Brendan Gregg3a391c22016-02-08 01:20:31 -080042 formatter_class=argparse.RawDescriptionHelpFormatter,
43 epilog=examples)
44parser.add_argument("-T", "--timestamp", action="store_true",
45 help="include timestamp on output")
46parser.add_argument("-m", "--milliseconds", action="store_true",
47 help="millisecond histogram")
48parser.add_argument("-P", "--pids", action="store_true",
49 help="print a histogram per process ID")
Brendan Gregg82769382017-04-18 14:23:14 -050050# PID options are --pid and --pids, so namespaces should be --pidns (not done
51# yet) and --pidnss:
52parser.add_argument("--pidnss", action="store_true",
53 help="print a histogram per PID namespace")
Brenden Blancoa6875602016-05-02 23:32:44 -070054parser.add_argument("-L", "--tids", action="store_true",
55 help="print a histogram per thread ID")
Brendan Gregg3a391c22016-02-08 01:20:31 -080056parser.add_argument("-p", "--pid",
57 help="trace this PID only")
58parser.add_argument("interval", nargs="?", default=99999999,
59 help="output interval, in seconds")
60parser.add_argument("count", nargs="?", default=99999999,
61 help="number of outputs")
Nathan Scottcf0792f2018-02-02 16:56:50 +110062parser.add_argument("--ebpf", action="store_true",
63 help=argparse.SUPPRESS)
Brendan Gregg3a391c22016-02-08 01:20:31 -080064args = parser.parse_args()
65countdown = int(args.count)
66debug = 0
67
68# define BPF program
69bpf_text = """
70#include <uapi/linux/ptrace.h>
Brenden Blanco11a21c82016-02-10 15:42:16 -080071#include <linux/sched.h>
Brendan Gregg82769382017-04-18 14:23:14 -050072#include <linux/nsproxy.h>
73#include <linux/pid_namespace.h>
Brendan Gregg3a391c22016-02-08 01:20:31 -080074
75typedef struct pid_key {
Brenden Blancoa6875602016-05-02 23:32:44 -070076 u64 id; // work around
Brendan Gregg3a391c22016-02-08 01:20:31 -080077 u64 slot;
78} pid_key_t;
Brendan Gregg82769382017-04-18 14:23:14 -050079
80typedef struct pidns_key {
81 u64 id; // work around
82 u64 slot;
83} pidns_key_t;
84
Brendan Gregg3a391c22016-02-08 01:20:31 -080085BPF_HASH(start, u32);
86STORAGE
87
Brenden Blanco11a21c82016-02-10 15:42:16 -080088struct rq;
89
Brendan Gregg3a391c22016-02-08 01:20:31 -080090// record enqueue timestamp
Cong Wange26019f2018-04-17 15:56:23 -070091static int trace_enqueue(u32 tgid, u32 pid)
Brendan Gregg3a391c22016-02-08 01:20:31 -080092{
Ivan Babrou799f46a2018-05-08 23:10:27 -070093 if (FILTER || pid == 0)
Brendan Gregg3a391c22016-02-08 01:20:31 -080094 return 0;
95 u64 ts = bpf_ktime_get_ns();
96 start.update(&pid, &ts);
97 return 0;
98}
Yonghong Song0d722372018-04-27 04:56:08 -070099"""
Brendan Gregg3a391c22016-02-08 01:20:31 -0800100
Yonghong Song0d722372018-04-27 04:56:08 -0700101bpf_text_kprobe = """
Cong Wange26019f2018-04-17 15:56:23 -0700102int trace_wake_up_new_task(struct pt_regs *ctx, struct task_struct *p)
103{
104 return trace_enqueue(p->tgid, p->pid);
105}
106
107int trace_ttwu_do_wakeup(struct pt_regs *ctx, struct rq *rq, struct task_struct *p,
108 int wake_flags)
109{
110 return trace_enqueue(p->tgid, p->pid);
111}
112
Brendan Gregg3a391c22016-02-08 01:20:31 -0800113// calculate latency
114int trace_run(struct pt_regs *ctx, struct task_struct *prev)
115{
Brenden Blancoa6875602016-05-02 23:32:44 -0700116 u32 pid, tgid;
Brendan Gregg3a391c22016-02-08 01:20:31 -0800117
118 // ivcsw: treat like an enqueue event and store timestamp
119 if (prev->state == TASK_RUNNING) {
Brenden Blancoa6875602016-05-02 23:32:44 -0700120 tgid = prev->tgid;
Brendan Gregg3a391c22016-02-08 01:20:31 -0800121 pid = prev->pid;
Ivan Babrou799f46a2018-05-08 23:10:27 -0700122 if (!(FILTER || pid == 0)) {
Brendan Gregg3a391c22016-02-08 01:20:31 -0800123 u64 ts = bpf_ktime_get_ns();
124 start.update(&pid, &ts);
125 }
126 }
127
Brenden Blancoa6875602016-05-02 23:32:44 -0700128 tgid = bpf_get_current_pid_tgid() >> 32;
Brendan Gregg3a391c22016-02-08 01:20:31 -0800129 pid = bpf_get_current_pid_tgid();
Ivan Babrou799f46a2018-05-08 23:10:27 -0700130 if (FILTER || pid == 0)
Brendan Gregg3a391c22016-02-08 01:20:31 -0800131 return 0;
132 u64 *tsp, delta;
133
134 // fetch timestamp and calculate delta
135 tsp = start.lookup(&pid);
136 if (tsp == 0) {
137 return 0; // missed enqueue
138 }
139 delta = bpf_ktime_get_ns() - *tsp;
140 FACTOR
141
142 // store as histogram
143 STORE
144
145 start.delete(&pid);
146 return 0;
147}
148"""
149
Yonghong Song0d722372018-04-27 04:56:08 -0700150bpf_text_raw_tp = """
151RAW_TRACEPOINT_PROBE(sched_wakeup)
152{
153 // TP_PROTO(struct task_struct *p)
154 struct task_struct *p = (struct task_struct *)ctx->args[0];
Paul Chaignona9f96c02018-06-15 00:27:08 +0200155 return trace_enqueue(p->tgid, p->pid);
Yonghong Song0d722372018-04-27 04:56:08 -0700156}
157
158RAW_TRACEPOINT_PROBE(sched_wakeup_new)
159{
160 // TP_PROTO(struct task_struct *p)
161 struct task_struct *p = (struct task_struct *)ctx->args[0];
Paul Chaignona9f96c02018-06-15 00:27:08 +0200162 return trace_enqueue(p->tgid, p->pid);
Yonghong Song0d722372018-04-27 04:56:08 -0700163}
164
165RAW_TRACEPOINT_PROBE(sched_switch)
166{
167 // TP_PROTO(bool preempt, struct task_struct *prev, struct task_struct *next)
168 struct task_struct *prev = (struct task_struct *)ctx->args[1];
Paul Chaignona9f96c02018-06-15 00:27:08 +0200169 struct task_struct *next = (struct task_struct *)ctx->args[2];
Yonghong Song0d722372018-04-27 04:56:08 -0700170 u32 pid, tgid;
Yonghong Song0d722372018-04-27 04:56:08 -0700171
172 // ivcsw: treat like an enqueue event and store timestamp
Paul Chaignon8d78edd2018-06-29 07:47:44 +0200173 if (prev->state == TASK_RUNNING) {
Paul Chaignona9f96c02018-06-15 00:27:08 +0200174 tgid = prev->tgid;
175 pid = prev->pid;
Ivan Babrou799f46a2018-05-08 23:10:27 -0700176 if (!(FILTER || pid == 0)) {
Yonghong Song0d722372018-04-27 04:56:08 -0700177 u64 ts = bpf_ktime_get_ns();
178 start.update(&pid, &ts);
179 }
180 }
181
Paul Chaignona9f96c02018-06-15 00:27:08 +0200182 tgid = next->tgid;
183 pid = next->pid;
Ivan Babrou799f46a2018-05-08 23:10:27 -0700184 if (FILTER || pid == 0)
Yonghong Song0d722372018-04-27 04:56:08 -0700185 return 0;
186 u64 *tsp, delta;
187
188 // fetch timestamp and calculate delta
189 tsp = start.lookup(&pid);
190 if (tsp == 0) {
191 return 0; // missed enqueue
192 }
193 delta = bpf_ktime_get_ns() - *tsp;
194 FACTOR
195
196 // store as histogram
197 STORE
198
199 start.delete(&pid);
200 return 0;
201}
202"""
203
204is_support_raw_tp = BPF.support_raw_tracepoint()
205if is_support_raw_tp:
206 bpf_text += bpf_text_raw_tp
207else:
208 bpf_text += bpf_text_kprobe
209
Brendan Gregg3a391c22016-02-08 01:20:31 -0800210# code substitutions
211if args.pid:
Brenden Blancoa6875602016-05-02 23:32:44 -0700212 # pid from userspace point of view is thread group from kernel pov
213 bpf_text = bpf_text.replace('FILTER', 'tgid != %s' % args.pid)
Brendan Gregg3a391c22016-02-08 01:20:31 -0800214else:
215 bpf_text = bpf_text.replace('FILTER', '0')
216if args.milliseconds:
217 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
218 label = "msecs"
219else:
220 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
221 label = "usecs"
Brenden Blancoa6875602016-05-02 23:32:44 -0700222if args.pids or args.tids:
223 section = "pid"
224 pid = "tgid"
225 if args.tids:
226 pid = "pid"
227 section = "tid"
Brendan Gregg3a391c22016-02-08 01:20:31 -0800228 bpf_text = bpf_text.replace('STORAGE',
229 'BPF_HISTOGRAM(dist, pid_key_t);')
230 bpf_text = bpf_text.replace('STORE',
Brenden Blancoa6875602016-05-02 23:32:44 -0700231 'pid_key_t key = {.id = ' + pid + ', .slot = bpf_log2l(delta)}; ' +
Brendan Gregg3a391c22016-02-08 01:20:31 -0800232 'dist.increment(key);')
Brendan Gregg82769382017-04-18 14:23:14 -0500233elif args.pidnss:
234 section = "pidns"
235 bpf_text = bpf_text.replace('STORAGE',
236 'BPF_HISTOGRAM(dist, pidns_key_t);')
237 bpf_text = bpf_text.replace('STORE', 'pidns_key_t key = ' +
238 '{.id = prev->nsproxy->pid_ns_for_children->ns.inum, ' +
239 '.slot = bpf_log2l(delta)}; dist.increment(key);')
Brendan Gregg3a391c22016-02-08 01:20:31 -0800240else:
Brenden Blancoa6875602016-05-02 23:32:44 -0700241 section = ""
Brendan Gregg3a391c22016-02-08 01:20:31 -0800242 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
243 bpf_text = bpf_text.replace('STORE',
244 'dist.increment(bpf_log2l(delta));')
Nathan Scottcf0792f2018-02-02 16:56:50 +1100245if debug or args.ebpf:
Brendan Gregg3a391c22016-02-08 01:20:31 -0800246 print(bpf_text)
Nathan Scottcf0792f2018-02-02 16:56:50 +1100247 if args.ebpf:
248 exit()
Brendan Gregg3a391c22016-02-08 01:20:31 -0800249
250# load BPF program
251b = BPF(text=bpf_text)
Yonghong Song0d722372018-04-27 04:56:08 -0700252if not is_support_raw_tp:
253 b.attach_kprobe(event="ttwu_do_wakeup", fn_name="trace_ttwu_do_wakeup")
254 b.attach_kprobe(event="wake_up_new_task", fn_name="trace_wake_up_new_task")
255 b.attach_kprobe(event="finish_task_switch", fn_name="trace_run")
Brendan Gregg3a391c22016-02-08 01:20:31 -0800256
257print("Tracing run queue latency... Hit Ctrl-C to end.")
258
259# output
260exiting = 0 if args.interval else 1
261dist = b.get_table("dist")
262while (1):
263 try:
264 sleep(int(args.interval))
265 except KeyboardInterrupt:
266 exiting = 1
267
268 print()
269 if args.timestamp:
270 print("%-8s\n" % strftime("%H:%M:%S"), end="")
271
Brenden Blancoa6875602016-05-02 23:32:44 -0700272 dist.print_log2_hist(label, section, section_print_fn=int)
Brendan Gregg3a391c22016-02-08 01:20:31 -0800273 dist.clear()
274
275 countdown -= 1
276 if exiting or countdown == 0:
277 exit()