-
Notifications
You must be signed in to change notification settings - Fork 0
/
set-irq-affinity.py
executable file
·94 lines (76 loc) · 1.86 KB
/
set-irq-affinity.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#!/usr/bin/python
# Supported drivers: ixgbe
import getopt
import os
import re
import sys
first_core = None
interface = None
verbose = 0
dry_run = False
def die(s):
print(s)
exit(1)
def usage():
print("set_irq_affinity.py {-i INTERFACE} [--dry-run] [-c core] [-hv]")
try:
opts, args = getopt.getopt(sys.argv[1:], "hvi:c:",["dry-run"])
except getopt.GetoptError as err:
die(err)
for o, a in opts:
if o in ("-i"):
interface = a
elif o in ("-h"):
usage()
sys.exit(0)
elif o in ("-v"):
verbose += 1
elif o in ("--dry-run"):
dry_run = True
elif o in ("-c"):
first_core = int(a)
if interface == None:
usage()
die("Interface not specidied")
driver_path = os.readlink("/sys/class/net/%s/device/driver" % interface)
driver = os.path.basename(os.path.normpath(driver_path))
with open("/proc/interrupts", 'r') as f:
lines = f.readlines()
if driver == "ixgbe":
pattern = "^%s-TxRx-[0-9]*$" % interface
elif driver == "i40e":
pattern = "^i40e-%s-TxRx-[0-9]*$" % interface
else:
die("Driver '%s' not supported" % driver)
irqs = []
p = re.compile(pattern)
for i in range (1, len(lines)):
columns = lines[i].split()
for col in columns:
m = re.match(p, col.strip())
if m != None:
irq = columns[0].strip(" :")
if not irq.isdigit():
print("/proc/interrupts:%d: Invalid irq" % i + 1)
sys.exit(1)
irqs.append(int(irq))
if verbose > 1:
print("irqs=", irqs)
for i in range(0, len(irqs)):
with open("/proc/irq/%d/smp_affinity" % irqs[i], 'w+') as f:
if first_core == None:
affinity = None
else:
affinity = 1 << (first_core + i)
if verbose > 0:
lines = f.readlines()
assert(len(lines) > 0)
old_affinity = lines[0].strip()
log = "irq %d, affinity 0x%s" % (irqs[i], old_affinity)
if affinity != None:
log += "->0x%08x" % affinity
print(log)
if affinity != None:
if not dry_run:
f.write("%x" % affinity)
sys.exit(0)