Created
May 14, 2020 13:07
-
-
Save ethercflow/33df25a81a1a1d0cd10ed2385c1121e1 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
# @lint-avoid-python-3-compatibility-imports | |
from __future__ import print_function | |
from bcc import BPF | |
from time import sleep, strftime | |
import argparse | |
examples = """examples: | |
""" | |
parser = argparse.ArgumentParser( | |
description="Summarize load balance moved per task as a histogram.", | |
formatter_class=argparse.RawDescriptionHelpFormatter, | |
epilog=examples) | |
parser.add_argument("-T", "--timestamp", action="store_true", | |
help="include timestamp on output") | |
parser.add_argument("-P", "--pids", action="store_true", | |
help="print a histogram per process ID") | |
parser.add_argument("-L", "--tids", action="store_true", | |
help="print a histogram per thread ID") | |
parser.add_argument("-p", "--pid", | |
help="trace this PID only") | |
parser.add_argument("-t", "--tid", | |
help="trace this TID only") | |
parser.add_argument("interval", nargs="?", default=99999999, | |
help="output interval, in seconds") | |
parser.add_argument("count", nargs="?", default=99999999, | |
help="number of outputs") | |
parser.add_argument("--ebpf", action="store_true", | |
help=argparse.SUPPRESS) | |
args = parser.parse_args() | |
countdown = int(args.count) | |
debug = 0 | |
bpf_text = """ | |
#include <uapi/linux/ptrace.h> | |
#include <linux/sched/topology.h> | |
#include <linux/sched.h> | |
struct pid_key { | |
u64 id; | |
u64 slot; | |
}; | |
STORAGE | |
int trace_load_balance_return(struct pt_regs *ctx) | |
{ | |
u64 pid_tgid = bpf_get_current_pid_tgid(); | |
u32 tgid = pid_tgid >> 32, pid = pid_tgid; | |
int ld_moved = PT_REGS_RC(ctx); | |
if (FILTER) | |
return 0; | |
STORE | |
return 0; | |
} | |
""" | |
if args.pid: | |
bpf_text = bpf_text.replace('FILTER', 'tgid != %s' % args.pid) | |
elif args.tid: | |
bpf_text = bpf_text.replace('FILTER', 'pid != %s' % args.tid) | |
else: | |
bpf_text = bpf_text.replace('FILTER', '0') | |
if args.pids or args.tids: | |
section = "pid" | |
pid = "tgid" | |
if args.tids: | |
pid = "pid" | |
section = "tid" | |
bpf_text = bpf_text.replace('STORAGE', | |
'BPF_HISTOGRAM(dist, struct pid_key, MAX_PID);') | |
bpf_text = bpf_text.replace('STORE', | |
'struct pid_key key = {.id = ' + pid + ', .slot = ld_moved}; ' + | |
'dist.increment(key);') | |
else: | |
section = "" | |
bpf_text = bpf_text.replace('STORAGE', 'BPF_HISTOGRAM(dist);') | |
bpf_text = bpf_text.replace('STORE', | |
'dist.increment(ld_moved);') | |
if debug or args.ebpf: | |
print(bpf_text) | |
if args.ebpf: | |
exit() | |
max_pid = int(open("/proc/sys/kernel/pid_max").read()) | |
b = BPF(text=bpf_text, cflags=["-DMAX_PID=%d" % max_pid]) | |
b.attach_kretprobe(event="load_balance", fn_name="trace_load_balance_return") | |
print("Tracing load balance latency... Hit Ctrl-C to end.") | |
exiting = 0 if args.interval else 1 | |
dist = b.get_table("dist") | |
while (1): | |
try: | |
sleep(int(args.interval)) | |
except KeyboardInterrupt: | |
exiting = 1 | |
print() | |
if args.timestamp: | |
print("%-8s\n" % strftime("%H:%M:%S"), end="") | |
def pid_to_comm(pid): | |
try: | |
comm = open("/proc/%d/comm" % pid, "r").read() | |
return "%d %s" % (pid, comm) | |
except IOError: | |
return str(pid) | |
dist.print_linear_hist("moved") | |
dist.clear() | |
countdown -= 1 | |
if exiting or countdown == 0: | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment