blob: 40ccfa0c099b538a78b5a634cf05e6512f381d4d [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
Sasha Goldshteinbee8d362016-06-30 10:46:27 -070085 if (ts < *tsp) {
86 // Probably a clock issue where the recorded on-CPU event had a
87 // timestamp later than the recorded off-CPU event, or vice versa.
88 return;
89 }
Sasha Goldshtein9972f272016-06-29 01:48:08 -070090 u64 delta = ts - *tsp;
91 FACTOR
92 STORE
93}
94
Sasha Goldshtein06d90d32016-06-30 07:39:27 -070095int sched_switch(struct pt_regs *ctx, struct task_struct *prev)
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030096{
Sasha Goldshtein9972f272016-06-29 01:48:08 -070097 u64 ts = bpf_ktime_get_ns();
Sasha Goldshtein40975ab2016-06-29 03:57:01 +030098 u64 pid_tgid = bpf_get_current_pid_tgid();
Sasha Goldshtein9972f272016-06-29 01:48:08 -070099 u32 tgid = pid_tgid >> 32, pid = pid_tgid;
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300100
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700101#ifdef ONCPU
Sasha Goldshtein06d90d32016-06-30 07:39:27 -0700102 if (prev->state == TASK_RUNNING) {
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700103#else
Sasha Goldshtein3c976bb2016-06-29 23:35:43 -0700104 if (1) {
105#endif
Sasha Goldshtein06d90d32016-06-30 07:39:27 -0700106 u32 prev_pid = prev->pid;
107 u32 prev_tgid = prev->tgid;
Sasha Goldshtein3c976bb2016-06-29 23:35:43 -0700108#ifdef ONCPU
109 update_hist(prev_tgid, prev_pid, ts);
110#else
111 store_start(prev_tgid, prev_pid, ts);
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700112#endif
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300113 }
114
Sasha Goldshtein3c976bb2016-06-29 23:35:43 -0700115BAIL:
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700116#ifdef ONCPU
117 store_start(tgid, pid, ts);
118#else
119 update_hist(tgid, pid, ts);
120#endif
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300121
122 return 0;
123}
124"""
125
126if args.pid:
127 bpf_text = bpf_text.replace('FILTER', 'tgid != %s' % args.pid)
128else:
129 bpf_text = bpf_text.replace('FILTER', '0')
130if args.milliseconds:
131 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000000;')
132 label = "msecs"
133else:
134 bpf_text = bpf_text.replace('FACTOR', 'delta /= 1000;')
135 label = "usecs"
136if args.pids or args.tids:
137 section = "pid"
138 pid = "tgid"
139 if args.tids:
140 pid = "pid"
141 section = "tid"
142 bpf_text = bpf_text.replace('STORAGE',
143 'BPF_HISTOGRAM(dist, pid_key_t);')
144 bpf_text = bpf_text.replace('STORE',
145 'pid_key_t key = {.id = ' + pid + ', .slot = bpf_log2l(delta)}; ' +
146 'dist.increment(key);')
147else:
148 section = ""
149 bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);')
150 bpf_text = bpf_text.replace('STORE',
151 'dist.increment(bpf_log2l(delta));')
152if debug:
153 print(bpf_text)
154
155b = BPF(text=bpf_text)
Sasha Goldshtein06d90d32016-06-30 07:39:27 -0700156b.attach_kprobe(event="finish_task_switch", fn_name="sched_switch")
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300157
Sasha Goldshtein9972f272016-06-29 01:48:08 -0700158print("Tracing %s-CPU time... Hit Ctrl-C to end." %
159 ("off" if args.offcpu else "on"))
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300160
161exiting = 0 if args.interval else 1
162dist = b.get_table("dist")
163while (1):
164 try:
165 sleep(int(args.interval))
166 except KeyboardInterrupt:
167 exiting = 1
168
169 print()
170 if args.timestamp:
171 print("%-8s\n" % strftime("%H:%M:%S"), end="")
172
Sasha Goldshtein4b72f052016-06-29 02:18:06 -0700173 def pid_to_comm(pid):
174 try:
175 comm = open("/proc/%d/comm" % pid, "r").read()
176 return "%d %s" % (pid, comm)
177 except IOError:
178 return str(pid)
179
180 dist.print_log2_hist(label, section, section_print_fn=pid_to_comm)
Sasha Goldshtein40975ab2016-06-29 03:57:01 +0300181 dist.clear()
182
183 countdown -= 1
184 if exiting or countdown == 0:
185 exit()
186