blob: 6a0e04b5f50e96c36e9464b94b12354e8232dcfe [file] [log] [blame]
Sasha Goldshtein40975ab2016-06-29 03:57:01 +03001#!/usr/bin/python
2# @lint-avoid-python-3-compatibility-imports
3#
Sasha Goldshtein9972f272016-06-29 01:48:08 -07004# cpudist Summarize on- and off-CPU time per task as a histogram.
Sasha Goldshtein40975ab2016-06-29 03:57:01 +03005#
Sasha Goldshtein9972f272016-06-29 01:48:08 -07006# USAGE: cpudist [-h] [-O] [-T] [-m] [-P] [-L] [-p PID] [interval] [count]
Sasha Goldshtein40975ab2016-06-29 03:57:01 +03007#
Sasha Goldshtein9972f272016-06-29 01:48:08 -07008# This measures the time a task spends on or off the CPU, and shows this time
9# as a histogram, optionally per-process.
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030010#
11# Copyright 2016 Sasha Goldshtein
12# Licensed under the Apache License, Version 2.0 (the "License")
13
14from __future__ import print_function
15from bcc import BPF, Tracepoint
16from time import sleep, strftime
17import argparse
18
19examples = """examples:
20 cpudist # summarize on-CPU time as a histogram
Sasha Goldshtein9972f272016-06-29 01:48:08 -070021 cpudist -O # summarize off-CPU time as a histogram
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030022 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"""
27parser = argparse.ArgumentParser(
28 description="Summarize on-CPU time per task as a histogram.",
29 formatter_class=argparse.RawDescriptionHelpFormatter,
30 epilog=examples)
Sasha Goldshtein9972f272016-06-29 01:48:08 -070031parser.add_argument("-O", "--offcpu", action="store_true",
32 help="measure off-CPU time")
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030033parser.add_argument("-T", "--timestamp", action="store_true",
34 help="include timestamp on output")
35parser.add_argument("-m", "--milliseconds", action="store_true",
36 help="millisecond histogram")
37parser.add_argument("-P", "--pids", action="store_true",
38 help="print a histogram per process ID")
39parser.add_argument("-L", "--tids", action="store_true",
40 help="print a histogram per thread ID")
41parser.add_argument("-p", "--pid",
42 help="trace this PID only")
43parser.add_argument("interval", nargs="?", default=99999999,
44 help="output interval, in seconds")
45parser.add_argument("count", nargs="?", default=99999999,
46 help="number of outputs")
47args = parser.parse_args()
48countdown = int(args.count)
49debug = 0
50
Sasha Goldshtein06d90d32016-06-30 07:39:27 -070051bpf_text = """#include <uapi/linux/ptrace.h>
52#include <linux/sched.h>
53"""
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030054
Sasha Goldshtein9972f272016-06-29 01:48:08 -070055if not args.offcpu:
56 bpf_text += "#define ONCPU\n"
57
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030058bpf_text += """
59typedef struct pid_key {
60 u64 id;
61 u64 slot;
62} pid_key_t;
63
Sasha Goldshtein9972f272016-06-29 01:48:08 -070064
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030065BPF_HASH(start, u32, u64);
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030066STORAGE
67
Sasha Goldshtein9972f272016-06-29 01:48:08 -070068static inline void store_start(u32 tgid, u32 pid, u64 ts)
69{
Sasha Goldshtein9972f272016-06-29 01:48:08 -070070 if (FILTER)
71 return;
72
73 start.update(&pid, &ts);
74}
75
76static inline void update_hist(u32 tgid, u32 pid, u64 ts)
77{
Sasha Goldshtein9972f272016-06-29 01:48:08 -070078 if (FILTER)
79 return;
80
81 u64 *tsp = start.lookup(&pid);
82 if (tsp == 0)
83 return;
84
85 u64 delta = ts - *tsp;
86 FACTOR
87 STORE
88}
89
Sasha Goldshtein06d90d32016-06-30 07:39:27 -070090int sched_switch(struct pt_regs *ctx, struct task_struct *prev)
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030091{
Sasha Goldshtein9972f272016-06-29 01:48:08 -070092 u64 ts = bpf_ktime_get_ns();
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030093 u64 pid_tgid = bpf_get_current_pid_tgid();
Sasha Goldshtein9972f272016-06-29 01:48:08 -070094 u32 tgid = pid_tgid >> 32, pid = pid_tgid;
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030095
Sasha Goldshtein9972f272016-06-29 01:48:08 -070096#ifdef ONCPU
Sasha Goldshtein06d90d32016-06-30 07:39:27 -070097 if (prev->state == TASK_RUNNING) {
Sasha Goldshtein9972f272016-06-29 01:48:08 -070098#else
Sasha Goldshtein3c976bb2016-06-29 23:35:43 -070099 if (1) {
100#endif
Sasha Goldshtein06d90d32016-06-30 07:39:27 -0700101 u32 prev_pid = prev->pid;
102 u32 prev_tgid = prev->tgid;
Sasha Goldshtein3c976bb2016-06-29 23:35:43 -0700103#ifdef ONCPU
104 update_hist(prev_tgid, prev_pid, ts);
105#else
106 store_start(prev_tgid, prev_pid, ts);
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700107#endif
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300108 }
109
Sasha Goldshtein3c976bb2016-06-29 23:35:43 -0700110BAIL:
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700111#ifdef ONCPU
112 store_start(tgid, pid, ts);
113#else
114 update_hist(tgid, pid, ts);
115#endif
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300116
117 return 0;
118}
119"""
120
121if args.pid:
122 bpf_text = bpf_text.replace('FILTER', 'tgid != %s' % args.pid)
123else:
124 bpf_text = bpf_text.replace('FILTER', '0')
125if args.milliseconds:
126 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
127 label = "msecs"
128else:
129 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
130 label = "usecs"
131if args.pids or args.tids:
132 section = "pid"
133 pid = "tgid"
134 if args.tids:
135 pid = "pid"
136 section = "tid"
137 bpf_text = bpf_text.replace('STORAGE',
138 'BPF_HISTOGRAM(dist, pid_key_t);')
139 bpf_text = bpf_text.replace('STORE',
140 'pid_key_t key = {.id = ' + pid + ', .slot = bpf_log2l(delta)}; ' +
141 'dist.increment(key);')
142else:
143 section = ""
144 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
145 bpf_text = bpf_text.replace('STORE',
146 'dist.increment(bpf_log2l(delta));')
147if debug:
148 print(bpf_text)
149
150b = BPF(text=bpf_text)
Sasha Goldshtein06d90d32016-06-30 07:39:27 -0700151b.attach_kprobe(event="finish_task_switch", fn_name="sched_switch")
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300152
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700153print("Tracing %s-CPU time... Hit Ctrl-C to end." %
154 ("off" if args.offcpu else "on"))
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300155
156exiting = 0 if args.interval else 1
157dist = b.get_table("dist")
158while (1):
159 try:
160 sleep(int(args.interval))
161 except KeyboardInterrupt:
162 exiting = 1
163
164 print()
165 if args.timestamp:
166 print("%-8s\n" % strftime("%H:%M:%S"), end="")
167
Sasha Goldshtein4b72f052016-06-29 02:18:06 -0700168 def pid_to_comm(pid):
169 try:
170 comm = open("/proc/%d/comm" % pid, "r").read()
171 return "%d %s" % (pid, comm)
172 except IOError:
173 return str(pid)
174
175 dist.print_log2_hist(label, section, section_print_fn=pid_to_comm)
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300176 dist.clear()
177
178 countdown -= 1
179 if exiting or countdown == 0:
180 exit()
181