|
| 1 | +#!/usr/bin/env python |
| 2 | +# @lint-avoid-python-3-compatibility-imports |
| 3 | +# |
| 4 | +# numasched Trace task NUMA switch |
| 5 | +# For Linux, uses BCC, eBPF. |
| 6 | +# |
| 7 | +# USAGE: numasched [-p PID] [-t TID] [-c COMM] |
| 8 | +# |
| 9 | +# This script tracks NUMA migrations of tasks, and in general, frequent |
| 10 | +# NUMA migrations can cause poor performance. |
| 11 | +# |
| 12 | +# Copyright 2022 CESTC, Co. |
| 13 | +# Licensed under the Apache License, Version 2.0 (the "License") |
| 14 | +# |
| 15 | +# 14-Dec-2022 Rong Tao Created this. |
| 16 | + |
| 17 | +from __future__ import print_function |
| 18 | +from bcc import BPF |
| 19 | +import argparse |
| 20 | +from time import strftime |
| 21 | +from socket import inet_ntop, AF_INET, AF_INET6 |
| 22 | +from struct import pack |
| 23 | +from time import sleep |
| 24 | + |
| 25 | + |
| 26 | +# arguments |
| 27 | +examples = """examples: |
| 28 | + ./numasched # trace all processes |
| 29 | + ./numasched -p 185 # trace PID 185 only |
| 30 | +""" |
| 31 | +parser = argparse.ArgumentParser( |
| 32 | + description="Trace task NUMA switch", |
| 33 | + formatter_class=argparse.RawDescriptionHelpFormatter, |
| 34 | + epilog=examples) |
| 35 | +parser.add_argument("-p", "--pid", |
| 36 | + help="trace this PID only") |
| 37 | +parser.add_argument("-t", "--tid", |
| 38 | + help="trace this TID only") |
| 39 | +parser.add_argument("-c", "--comm", |
| 40 | + help="trace this COMM only") |
| 41 | +args = parser.parse_args() |
| 42 | + |
| 43 | + |
| 44 | +bpf_text = """ |
| 45 | +#include <linux/sched.h> |
| 46 | +#include <linux/topology.h> |
| 47 | +
|
| 48 | +struct data_t { |
| 49 | + char comm[TASK_COMM_LEN]; |
| 50 | + u32 pid; |
| 51 | + u32 tid; |
| 52 | + u32 old_nid; |
| 53 | + u32 new_nid; |
| 54 | +}; |
| 55 | +BPF_PERF_OUTPUT(events); |
| 56 | +
|
| 57 | +struct val_t { |
| 58 | + u32 nid; |
| 59 | +}; |
| 60 | +BPF_HASH(numaid_info, u32, struct val_t); |
| 61 | +
|
| 62 | +
|
| 63 | +TRACEPOINT_PROBE(sched, sched_switch) |
| 64 | +{ |
| 65 | + u64 pid_tgid = bpf_get_current_pid_tgid(); |
| 66 | + u32 pid = pid_tgid >> 32; |
| 67 | + u32 tid = (u32)pid_tgid; |
| 68 | + u32 new_nid = bpf_get_numa_node_id(); |
| 69 | + struct val_t val = {}, *valp; |
| 70 | + u32 old_nid; |
| 71 | +
|
| 72 | + if (FILTER_PID) |
| 73 | + return 0; |
| 74 | +
|
| 75 | + if (FILTER_TID) |
| 76 | + return 0; |
| 77 | +
|
| 78 | + val.nid = new_nid; |
| 79 | +
|
| 80 | + valp = numaid_info.lookup(&tid); |
| 81 | + if (!valp) |
| 82 | + goto update; |
| 83 | +
|
| 84 | + old_nid = valp->nid; |
| 85 | +
|
| 86 | + if (old_nid != new_nid) { |
| 87 | + struct data_t data = {}; |
| 88 | +
|
| 89 | + bpf_get_current_comm(&data.comm, sizeof(data.comm)); |
| 90 | + data.pid = pid; |
| 91 | + data.tid = tid; |
| 92 | + data.old_nid = old_nid; |
| 93 | + data.new_nid = new_nid; |
| 94 | +
|
| 95 | + events.perf_submit(args, &data, sizeof(data)); |
| 96 | + } |
| 97 | +
|
| 98 | +update: |
| 99 | + numaid_info.update(&tid, &val); |
| 100 | + return 0; |
| 101 | +} |
| 102 | +""" |
| 103 | + |
| 104 | +if args.pid: |
| 105 | + bpf_text = bpf_text.replace('FILTER_PID', 'pid != %s' % args.pid) |
| 106 | +else: |
| 107 | + # always skip PID=0 |
| 108 | + bpf_text = bpf_text.replace('FILTER_PID', 'pid == 0') |
| 109 | + |
| 110 | +if args.tid: |
| 111 | + bpf_text = bpf_text.replace('FILTER_TID', 'tid != %s' % args.tid) |
| 112 | +else: |
| 113 | + # always skip TID=0 |
| 114 | + bpf_text = bpf_text.replace('FILTER_TID', 'tid == 0') |
| 115 | + |
| 116 | +# process event |
| 117 | +def print_event(cpu, data, size): |
| 118 | + event = b["events"].event(data) |
| 119 | + |
| 120 | + # Filter events by comm |
| 121 | + if args.comm: |
| 122 | + if not args.comm == event.comm.decode('utf-8', 'replace'): |
| 123 | + return |
| 124 | + |
| 125 | + print("%-8s %-8d %-8d %-8d -> %-8d %-8s" % |
| 126 | + (strftime("%H:%M:%S"), |
| 127 | + event.pid, |
| 128 | + event.tid, |
| 129 | + event.old_nid, |
| 130 | + event.new_nid, |
| 131 | + event.comm)) |
| 132 | + |
| 133 | + |
| 134 | +b = BPF(text=bpf_text) |
| 135 | + |
| 136 | +print("Tracing task NUMA switch...") |
| 137 | +print("%-8s %-8s %-8s %-8s %-8s %-8s" % |
| 138 | + ("TIME", "PID", "TID", "SRC_NID", "DST_NID", "COMM")) |
| 139 | + |
| 140 | +b["events"].open_perf_buffer(print_event) |
| 141 | +while 1: |
| 142 | + try: |
| 143 | + b.perf_buffer_poll() |
| 144 | + except KeyboardInterrupt: |
| 145 | + exit() |
| 146 | + |
0 commit comments