Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 1 | #!/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 Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 7 | # USAGE: runqlat [-h] [-T] [-m] [-P] [-L] [-p PID] [interval] [count] |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 8 | # |
| 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 Babrou | 5c48a3f | 2018-05-08 17:24:19 -0700 | [diff] [blame] | 15 | # and execution. This traces ttwu_do_wakeup(), wake_up_new_task() -> |
| 16 | # finish_task_switch() with either raw tracepoints (if supported) or kprobes |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 17 | # 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 | |
| 27 | from __future__ import print_function |
| 28 | from bcc import BPF |
| 29 | from time import sleep, strftime |
| 30 | import argparse |
| 31 | |
| 32 | # arguments |
| 33 | examples = """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 | """ |
| 40 | parser = argparse.ArgumentParser( |
Brendan Gregg | 8276938 | 2017-04-18 14:23:14 -0500 | [diff] [blame] | 41 | description="Summarize run queue (scheduler) latency as a histogram", |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 42 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 43 | epilog=examples) |
| 44 | parser.add_argument("-T", "--timestamp", action="store_true", |
| 45 | help="include timestamp on output") |
| 46 | parser.add_argument("-m", "--milliseconds", action="store_true", |
| 47 | help="millisecond histogram") |
| 48 | parser.add_argument("-P", "--pids", action="store_true", |
| 49 | help="print a histogram per process ID") |
Brendan Gregg | 8276938 | 2017-04-18 14:23:14 -0500 | [diff] [blame] | 50 | # PID options are --pid and --pids, so namespaces should be --pidns (not done |
| 51 | # yet) and --pidnss: |
| 52 | parser.add_argument("--pidnss", action="store_true", |
| 53 | help="print a histogram per PID namespace") |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 54 | parser.add_argument("-L", "--tids", action="store_true", |
| 55 | help="print a histogram per thread ID") |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 56 | parser.add_argument("-p", "--pid", |
| 57 | help="trace this PID only") |
| 58 | parser.add_argument("interval", nargs="?", default=99999999, |
| 59 | help="output interval, in seconds") |
| 60 | parser.add_argument("count", nargs="?", default=99999999, |
| 61 | help="number of outputs") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 62 | parser.add_argument("--ebpf", action="store_true", |
| 63 | help=argparse.SUPPRESS) |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 64 | args = parser.parse_args() |
| 65 | countdown = int(args.count) |
| 66 | debug = 0 |
| 67 | |
| 68 | # define BPF program |
| 69 | bpf_text = """ |
| 70 | #include <uapi/linux/ptrace.h> |
Brenden Blanco | 11a21c8 | 2016-02-10 15:42:16 -0800 | [diff] [blame] | 71 | #include <linux/sched.h> |
Brendan Gregg | 8276938 | 2017-04-18 14:23:14 -0500 | [diff] [blame] | 72 | #include <linux/nsproxy.h> |
| 73 | #include <linux/pid_namespace.h> |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 74 | |
| 75 | typedef struct pid_key { |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 76 | u64 id; // work around |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 77 | u64 slot; |
| 78 | } pid_key_t; |
Brendan Gregg | 8276938 | 2017-04-18 14:23:14 -0500 | [diff] [blame] | 79 | |
| 80 | typedef struct pidns_key { |
| 81 | u64 id; // work around |
| 82 | u64 slot; |
| 83 | } pidns_key_t; |
| 84 | |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 85 | BPF_HASH(start, u32); |
| 86 | STORAGE |
| 87 | |
Brenden Blanco | 11a21c8 | 2016-02-10 15:42:16 -0800 | [diff] [blame] | 88 | struct rq; |
| 89 | |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 90 | // record enqueue timestamp |
Cong Wang | e26019f | 2018-04-17 15:56:23 -0700 | [diff] [blame] | 91 | static int trace_enqueue(u32 tgid, u32 pid) |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 92 | { |
Ivan Babrou | 799f46a | 2018-05-08 23:10:27 -0700 | [diff] [blame] | 93 | if (FILTER || pid == 0) |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 94 | return 0; |
| 95 | u64 ts = bpf_ktime_get_ns(); |
| 96 | start.update(&pid, &ts); |
| 97 | return 0; |
| 98 | } |
Yonghong Song | 0d72237 | 2018-04-27 04:56:08 -0700 | [diff] [blame] | 99 | """ |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 100 | |
Yonghong Song | 0d72237 | 2018-04-27 04:56:08 -0700 | [diff] [blame] | 101 | bpf_text_kprobe = """ |
Cong Wang | e26019f | 2018-04-17 15:56:23 -0700 | [diff] [blame] | 102 | int trace_wake_up_new_task(struct pt_regs *ctx, struct task_struct *p) |
| 103 | { |
| 104 | return trace_enqueue(p->tgid, p->pid); |
| 105 | } |
| 106 | |
| 107 | int 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 Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 113 | // calculate latency |
| 114 | int trace_run(struct pt_regs *ctx, struct task_struct *prev) |
| 115 | { |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 116 | u32 pid, tgid; |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 117 | |
| 118 | // ivcsw: treat like an enqueue event and store timestamp |
| 119 | if (prev->state == TASK_RUNNING) { |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 120 | tgid = prev->tgid; |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 121 | pid = prev->pid; |
Ivan Babrou | 799f46a | 2018-05-08 23:10:27 -0700 | [diff] [blame] | 122 | if (!(FILTER || pid == 0)) { |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 123 | u64 ts = bpf_ktime_get_ns(); |
| 124 | start.update(&pid, &ts); |
| 125 | } |
| 126 | } |
| 127 | |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 128 | tgid = bpf_get_current_pid_tgid() >> 32; |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 129 | pid = bpf_get_current_pid_tgid(); |
Ivan Babrou | 799f46a | 2018-05-08 23:10:27 -0700 | [diff] [blame] | 130 | if (FILTER || pid == 0) |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 131 | 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 Song | 0d72237 | 2018-04-27 04:56:08 -0700 | [diff] [blame] | 150 | bpf_text_raw_tp = """ |
| 151 | RAW_TRACEPOINT_PROBE(sched_wakeup) |
| 152 | { |
| 153 | // TP_PROTO(struct task_struct *p) |
| 154 | struct task_struct *p = (struct task_struct *)ctx->args[0]; |
| 155 | u32 tgid, pid; |
| 156 | |
| 157 | bpf_probe_read(&tgid, sizeof(tgid), &p->tgid); |
| 158 | bpf_probe_read(&pid, sizeof(pid), &p->pid); |
| 159 | return trace_enqueue(tgid, pid); |
| 160 | } |
| 161 | |
| 162 | RAW_TRACEPOINT_PROBE(sched_wakeup_new) |
| 163 | { |
| 164 | // TP_PROTO(struct task_struct *p) |
| 165 | struct task_struct *p = (struct task_struct *)ctx->args[0]; |
| 166 | u32 tgid, pid; |
| 167 | |
| 168 | bpf_probe_read(&tgid, sizeof(tgid), &p->tgid); |
| 169 | bpf_probe_read(&pid, sizeof(pid), &p->pid); |
| 170 | return trace_enqueue(tgid, pid); |
| 171 | } |
| 172 | |
| 173 | RAW_TRACEPOINT_PROBE(sched_switch) |
| 174 | { |
| 175 | // TP_PROTO(bool preempt, struct task_struct *prev, struct task_struct *next) |
| 176 | struct task_struct *prev = (struct task_struct *)ctx->args[1]; |
| 177 | struct task_struct *next= (struct task_struct *)ctx->args[2]; |
| 178 | u32 pid, tgid; |
| 179 | long state; |
| 180 | |
| 181 | // ivcsw: treat like an enqueue event and store timestamp |
| 182 | bpf_probe_read(&state, sizeof(long), &prev->state); |
| 183 | if (state == TASK_RUNNING) { |
| 184 | bpf_probe_read(&tgid, sizeof(prev->tgid), &prev->tgid); |
| 185 | bpf_probe_read(&pid, sizeof(prev->pid), &prev->pid); |
Ivan Babrou | 799f46a | 2018-05-08 23:10:27 -0700 | [diff] [blame] | 186 | if (!(FILTER || pid == 0)) { |
Yonghong Song | 0d72237 | 2018-04-27 04:56:08 -0700 | [diff] [blame] | 187 | u64 ts = bpf_ktime_get_ns(); |
| 188 | start.update(&pid, &ts); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | bpf_probe_read(&tgid, sizeof(next->tgid), &next->tgid); |
| 193 | bpf_probe_read(&pid, sizeof(next->pid), &next->pid); |
Ivan Babrou | 799f46a | 2018-05-08 23:10:27 -0700 | [diff] [blame] | 194 | if (FILTER || pid == 0) |
Yonghong Song | 0d72237 | 2018-04-27 04:56:08 -0700 | [diff] [blame] | 195 | return 0; |
| 196 | u64 *tsp, delta; |
| 197 | |
| 198 | // fetch timestamp and calculate delta |
| 199 | tsp = start.lookup(&pid); |
| 200 | if (tsp == 0) { |
| 201 | return 0; // missed enqueue |
| 202 | } |
| 203 | delta = bpf_ktime_get_ns() - *tsp; |
| 204 | FACTOR |
| 205 | |
| 206 | // store as histogram |
| 207 | STORE |
| 208 | |
| 209 | start.delete(&pid); |
| 210 | return 0; |
| 211 | } |
| 212 | """ |
| 213 | |
| 214 | is_support_raw_tp = BPF.support_raw_tracepoint() |
| 215 | if is_support_raw_tp: |
| 216 | bpf_text += bpf_text_raw_tp |
| 217 | else: |
| 218 | bpf_text += bpf_text_kprobe |
| 219 | |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 220 | # code substitutions |
| 221 | if args.pid: |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 222 | # pid from userspace point of view is thread group from kernel pov |
| 223 | bpf_text = bpf_text.replace('FILTER', 'tgid != %s' % args.pid) |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 224 | else: |
| 225 | bpf_text = bpf_text.replace('FILTER', '0') |
| 226 | if args.milliseconds: |
| 227 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;') |
| 228 | label = "msecs" |
| 229 | else: |
| 230 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;') |
| 231 | label = "usecs" |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 232 | if args.pids or args.tids: |
| 233 | section = "pid" |
| 234 | pid = "tgid" |
| 235 | if args.tids: |
| 236 | pid = "pid" |
| 237 | section = "tid" |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 238 | bpf_text = bpf_text.replace('STORAGE', |
| 239 | 'BPF_HISTOGRAM(dist, pid_key_t);') |
| 240 | bpf_text = bpf_text.replace('STORE', |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 241 | 'pid_key_t key = {.id = ' + pid + ', .slot = bpf_log2l(delta)}; ' + |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 242 | 'dist.increment(key);') |
Brendan Gregg | 8276938 | 2017-04-18 14:23:14 -0500 | [diff] [blame] | 243 | elif args.pidnss: |
| 244 | section = "pidns" |
| 245 | bpf_text = bpf_text.replace('STORAGE', |
| 246 | 'BPF_HISTOGRAM(dist, pidns_key_t);') |
| 247 | bpf_text = bpf_text.replace('STORE', 'pidns_key_t key = ' + |
| 248 | '{.id = prev->nsproxy->pid_ns_for_children->ns.inum, ' + |
| 249 | '.slot = bpf_log2l(delta)}; dist.increment(key);') |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 250 | else: |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 251 | section = "" |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 252 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);') |
| 253 | bpf_text = bpf_text.replace('STORE', |
| 254 | 'dist.increment(bpf_log2l(delta));') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 255 | if debug or args.ebpf: |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 256 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 257 | if args.ebpf: |
| 258 | exit() |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 259 | |
| 260 | # load BPF program |
| 261 | b = BPF(text=bpf_text) |
Yonghong Song | 0d72237 | 2018-04-27 04:56:08 -0700 | [diff] [blame] | 262 | if not is_support_raw_tp: |
| 263 | b.attach_kprobe(event="ttwu_do_wakeup", fn_name="trace_ttwu_do_wakeup") |
| 264 | b.attach_kprobe(event="wake_up_new_task", fn_name="trace_wake_up_new_task") |
| 265 | b.attach_kprobe(event="finish_task_switch", fn_name="trace_run") |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 266 | |
| 267 | print("Tracing run queue latency... Hit Ctrl-C to end.") |
| 268 | |
| 269 | # output |
| 270 | exiting = 0 if args.interval else 1 |
| 271 | dist = b.get_table("dist") |
| 272 | while (1): |
| 273 | try: |
| 274 | sleep(int(args.interval)) |
| 275 | except KeyboardInterrupt: |
| 276 | exiting = 1 |
| 277 | |
| 278 | print() |
| 279 | if args.timestamp: |
| 280 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
| 281 | |
Brenden Blanco | a687560 | 2016-05-02 23:32:44 -0700 | [diff] [blame] | 282 | dist.print_log2_hist(label, section, section_print_fn=int) |
Brendan Gregg | 3a391c2 | 2016-02-08 01:20:31 -0800 | [diff] [blame] | 283 | dist.clear() |
| 284 | |
| 285 | countdown -= 1 |
| 286 | if exiting or countdown == 0: |
| 287 | exit() |