Skip to content

Instantly share code, notes, and snippets.

@bjorand
Created July 1, 2016 12:24
Show Gist options
  • Save bjorand/7c3687b44ebcac9db80e049efeb96124 to your computer and use it in GitHub Desktop.
Save bjorand/7c3687b44ebcac9db80e049efeb96124 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
import re
import sys
import random
interrupts = {}
count = int(sys.argv[1])
for line in sys.stdin.readlines():
if re.match(r'^\d+:', line.strip()):
data = line.split()
irq = int(data.pop(0)[:-1])
value = sum([int(d) for d in data if re.match(r'^\d+$', d)])
interrupts[value] = irq
class Bin(object):
def __init__(self):
self.items = []
self.sum = 0
def append(self, item):
self.items.append(item)
self.sum += item
def __str__(self):
return 'Bin(sum=%d, items=%s)' % (self.sum, str(self.items))
def pack(values, maxValue):
values = sorted(values, reverse=True)
bins = []
for item in values:
for bin in bins:
if bin.sum + item <= maxValue:
bin.append(item)
break
else:
bin = Bin()
bin.append(item)
bins.append(bin)
return bins
def packAndShow(aList, nbBins):
start = max(aList)
while True:
bins = pack(aList, start)
if len(bins) == nbBins:
break
start = start + 1
if random.random() < 0.00001:
print "Cannot pack to %s buckets (%s), increment max bucket size to %s" % (nbBins, len(bins), start)
return bins
bins = packAndShow(interrupts.keys(), count)
for counter,bin in enumerate(bins):
print 'cpu %d' % counter, [ interrupts[d] for d in bin.items ]
#!/usr/bin/env python
import sys
def help():
print 'Usage: %s [IRQ] [CPU ...]' % sys.argv[0]
print 'Example: pin IRQ 14 to CPU0 to 4 \n%s 14 0 1 2 3' % sys.argv[0]
try:
irq = sys.argv[1]
except IndexError:
help()
sys.exit(1)
mask = hex(sum(map(lambda x:2**int(x), sys.argv[2:]))).lstrip('0x')
print 'echo %s > /proc/irq/%s/smp_affinity' % (mask, irq)
try:
with open('/proc/irq/%s/smp_affinity' % irq) as f:
f.write(mask)
print 'OK'
except IOError:
sys.stderr.write('Unknown IRQ %s!\n' % irq)
help()
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment