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