Alexey Ivanov | cc01a9c | 2019-01-16 09:50:46 -0800 | [diff] [blame] | 1 | #!/usr/bin/python |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 2 | # @lint-avoid-python-3-compatibility-imports |
| 3 | # |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 4 | # cpudist Summarize on- and off-CPU time per task as a histogram. |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 5 | # |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 6 | # USAGE: cpudist [-h] [-O] [-T] [-m] [-P] [-L] [-p PID] [-I] [interval] [count] |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 7 | # |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 8 | # This measures the time a task spends on or off the CPU, and shows this time |
| 9 | # as a histogram, optionally per-process. |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 10 | # |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 11 | # By default CPU idle time are excluded by simply excluding PID 0. |
| 12 | # |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 13 | # Copyright 2016 Sasha Goldshtein |
| 14 | # Licensed under the Apache License, Version 2.0 (the "License") |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 15 | # |
| 16 | # 27-Mar-2022 Rocky Xing Changed to exclude CPU idle time by default. |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 17 | |
| 18 | from __future__ import print_function |
Rafael Fonseca | af236e7 | 2017-02-15 17:28:26 +0100 | [diff] [blame] | 19 | from bcc import BPF |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 20 | from time import sleep, strftime |
| 21 | import argparse |
| 22 | |
| 23 | examples = """examples: |
| 24 | cpudist # summarize on-CPU time as a histogram |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 25 | cpudist -O # summarize off-CPU time as a histogram |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 26 | cpudist 1 10 # print 1 second summaries, 10 times |
| 27 | cpudist -mT 1 # 1s summaries, milliseconds, and timestamps |
| 28 | cpudist -P # show each PID separately |
| 29 | cpudist -p 185 # trace PID 185 only |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 30 | cpudist -I # include CPU idle time |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 31 | """ |
| 32 | parser = argparse.ArgumentParser( |
| 33 | description="Summarize on-CPU time per task as a histogram.", |
| 34 | formatter_class=argparse.RawDescriptionHelpFormatter, |
| 35 | epilog=examples) |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 36 | parser.add_argument("-O", "--offcpu", action="store_true", |
| 37 | help="measure off-CPU time") |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 38 | parser.add_argument("-T", "--timestamp", action="store_true", |
| 39 | help="include timestamp on output") |
| 40 | parser.add_argument("-m", "--milliseconds", action="store_true", |
| 41 | help="millisecond histogram") |
| 42 | parser.add_argument("-P", "--pids", action="store_true", |
| 43 | help="print a histogram per process ID") |
| 44 | parser.add_argument("-L", "--tids", action="store_true", |
| 45 | help="print a histogram per thread ID") |
| 46 | parser.add_argument("-p", "--pid", |
| 47 | help="trace this PID only") |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 48 | parser.add_argument("-I", "--include-idle", action="store_true", |
| 49 | help="include CPU idle time") |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 50 | parser.add_argument("interval", nargs="?", default=99999999, |
| 51 | help="output interval, in seconds") |
| 52 | parser.add_argument("count", nargs="?", default=99999999, |
| 53 | help="number of outputs") |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 54 | parser.add_argument("--ebpf", action="store_true", |
| 55 | help=argparse.SUPPRESS) |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 56 | args = parser.parse_args() |
| 57 | countdown = int(args.count) |
| 58 | debug = 0 |
| 59 | |
Sasha Goldshtein | 06d90d3 | 2016-06-30 07:39:27 -0700 | [diff] [blame] | 60 | bpf_text = """#include <uapi/linux/ptrace.h> |
| 61 | #include <linux/sched.h> |
| 62 | """ |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 63 | |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 64 | if not args.offcpu: |
| 65 | bpf_text += "#define ONCPU\n" |
| 66 | |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 67 | bpf_text += """ |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 68 | typedef struct entry_key { |
| 69 | u32 pid; |
| 70 | u32 cpu; |
| 71 | } entry_key_t; |
| 72 | |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 73 | typedef struct pid_key { |
| 74 | u64 id; |
| 75 | u64 slot; |
| 76 | } pid_key_t; |
| 77 | |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 78 | |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 79 | BPF_HASH(start, entry_key_t, u64, MAX_PID); |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 80 | STORAGE |
| 81 | |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 82 | static inline void store_start(u32 tgid, u32 pid, u32 cpu, u64 ts) |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 83 | { |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 84 | if (PID_FILTER) |
| 85 | return; |
| 86 | |
| 87 | if (IDLE_FILTER) |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 88 | return; |
| 89 | |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 90 | entry_key_t entry_key = { .pid = pid, .cpu = cpu }; |
| 91 | start.update(&entry_key, &ts); |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 92 | } |
| 93 | |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 94 | static inline void update_hist(u32 tgid, u32 pid, u32 cpu, u64 ts) |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 95 | { |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 96 | if (PID_FILTER) |
| 97 | return; |
| 98 | |
| 99 | if (IDLE_FILTER) |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 100 | return; |
| 101 | |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 102 | entry_key_t entry_key = { .pid = pid, .cpu = cpu }; |
| 103 | u64 *tsp = start.lookup(&entry_key); |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 104 | if (tsp == 0) |
| 105 | return; |
| 106 | |
Sasha Goldshtein | bee8d36 | 2016-06-30 10:46:27 -0700 | [diff] [blame] | 107 | if (ts < *tsp) { |
| 108 | // Probably a clock issue where the recorded on-CPU event had a |
| 109 | // timestamp later than the recorded off-CPU event, or vice versa. |
| 110 | return; |
| 111 | } |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 112 | u64 delta = ts - *tsp; |
| 113 | FACTOR |
| 114 | STORE |
| 115 | } |
| 116 | |
Sasha Goldshtein | 06d90d3 | 2016-06-30 07:39:27 -0700 | [diff] [blame] | 117 | int sched_switch(struct pt_regs *ctx, struct task_struct *prev) |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 118 | { |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 119 | u64 ts = bpf_ktime_get_ns(); |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 120 | u64 pid_tgid = bpf_get_current_pid_tgid(); |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 121 | u32 tgid = pid_tgid >> 32, pid = pid_tgid; |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 122 | u32 cpu = bpf_get_smp_processor_id(); |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 123 | |
Nick-nizhen | ab14faf | 2021-05-27 13:21:59 +0800 | [diff] [blame] | 124 | u32 prev_pid = prev->pid; |
| 125 | u32 prev_tgid = prev->tgid; |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 126 | #ifdef ONCPU |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 127 | update_hist(prev_tgid, prev_pid, cpu, ts); |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 128 | #else |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 129 | store_start(prev_tgid, prev_pid, cpu, ts); |
Sasha Goldshtein | 3c976bb | 2016-06-29 23:35:43 -0700 | [diff] [blame] | 130 | #endif |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 131 | |
Sasha Goldshtein | 3c976bb | 2016-06-29 23:35:43 -0700 | [diff] [blame] | 132 | BAIL: |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 133 | #ifdef ONCPU |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 134 | store_start(tgid, pid, cpu, ts); |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 135 | #else |
xingfeng2510 | e683daa | 2022-03-27 11:10:55 +0800 | [diff] [blame] | 136 | update_hist(tgid, pid, cpu, ts); |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 137 | #endif |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 138 | |
| 139 | return 0; |
| 140 | } |
| 141 | """ |
| 142 | |
| 143 | if args.pid: |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 144 | bpf_text = bpf_text.replace('PID_FILTER', 'tgid != %s' % args.pid) |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 145 | else: |
xingfeng2510 | c1a767b | 2022-03-27 11:52:17 +0800 | [diff] [blame] | 146 | bpf_text = bpf_text.replace('PID_FILTER', '0') |
| 147 | |
| 148 | # set idle filter |
| 149 | idle_filter = 'pid == 0' |
| 150 | if args.include_idle: |
| 151 | idle_filter = '0' |
| 152 | bpf_text = bpf_text.replace('IDLE_FILTER', idle_filter) |
| 153 | |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 154 | if args.milliseconds: |
| 155 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;') |
| 156 | label = "msecs" |
| 157 | else: |
| 158 | bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;') |
| 159 | label = "usecs" |
| 160 | if args.pids or args.tids: |
| 161 | section = "pid" |
| 162 | pid = "tgid" |
| 163 | if args.tids: |
| 164 | pid = "pid" |
| 165 | section = "tid" |
| 166 | bpf_text = bpf_text.replace('STORAGE', |
Tristan Cacqueray | 9b433f2 | 2019-10-23 15:02:03 +0000 | [diff] [blame] | 167 | 'BPF_HISTOGRAM(dist, pid_key_t, MAX_PID);') |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 168 | bpf_text = bpf_text.replace('STORE', |
| 169 | 'pid_key_t key = {.id = ' + pid + ', .slot = bpf_log2l(delta)}; ' + |
| 170 | 'dist.increment(key);') |
| 171 | else: |
| 172 | section = "" |
| 173 | bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);') |
| 174 | bpf_text = bpf_text.replace('STORE', |
zcy | 80242fb | 2021-07-02 00:12:32 +0800 | [diff] [blame] | 175 | 'dist.atomic_increment(bpf_log2l(delta));') |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 176 | if debug or args.ebpf: |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 177 | print(bpf_text) |
Nathan Scott | cf0792f | 2018-02-02 16:56:50 +1100 | [diff] [blame] | 178 | if args.ebpf: |
| 179 | exit() |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 180 | |
Tristan Cacqueray | 9b433f2 | 2019-10-23 15:02:03 +0000 | [diff] [blame] | 181 | max_pid = int(open("/proc/sys/kernel/pid_max").read()) |
| 182 | |
| 183 | b = BPF(text=bpf_text, cflags=["-DMAX_PID=%d" % max_pid]) |
Guodong Xu | 00b72fd | 2021-03-13 02:23:47 +0000 | [diff] [blame] | 184 | b.attach_kprobe(event_re="^finish_task_switch$|^finish_task_switch\.isra\.\d$", |
| 185 | fn_name="sched_switch") |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 186 | |
Sasha Goldshtein | 9972f27 | 2016-06-29 01:48:08 -0700 | [diff] [blame] | 187 | print("Tracing %s-CPU time... Hit Ctrl-C to end." % |
| 188 | ("off" if args.offcpu else "on")) |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 189 | |
| 190 | exiting = 0 if args.interval else 1 |
| 191 | dist = b.get_table("dist") |
| 192 | while (1): |
| 193 | try: |
| 194 | sleep(int(args.interval)) |
| 195 | except KeyboardInterrupt: |
| 196 | exiting = 1 |
| 197 | |
| 198 | print() |
| 199 | if args.timestamp: |
| 200 | print("%-8s\n" % strftime("%H:%M:%S"), end="") |
| 201 | |
Sasha Goldshtein | 4b72f05 | 2016-06-29 02:18:06 -0700 | [diff] [blame] | 202 | def pid_to_comm(pid): |
| 203 | try: |
| 204 | comm = open("/proc/%d/comm" % pid, "r").read() |
| 205 | return "%d %s" % (pid, comm) |
| 206 | except IOError: |
| 207 | return str(pid) |
| 208 | |
| 209 | dist.print_log2_hist(label, section, section_print_fn=pid_to_comm) |
Sasha Goldshtein | 40975ab | 2016-06-29 03:57:01 +0300 | [diff] [blame] | 210 | dist.clear() |
| 211 | |
| 212 | countdown -= 1 |
| 213 | if exiting or countdown == 0: |
| 214 | exit() |