Skip to content

Instantly share code, notes, and snippets.

@ms1995
Created June 2, 2021 04:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms1995/59721f3b214825fd2d04610dc96177a1 to your computer and use it in GitHub Desktop.
Save ms1995/59721f3b214825fd2d04610dc96177a1 to your computer and use it in GitHub Desktop.
import sys
import psutil
import random
CPUSET = [int(x) for x in sys.argv[1].split(',')]
PID = int(sys.argv[2])
threads = []
for p in psutil.Process(PID).children(recursive=True):
if 'qemu-system' in p.name():
threads += p.threads()
# random.shuffle(threads)
threads.sort(key=lambda thread: thread.user_time + thread.system_time, reverse=True)
random.shuffle(CPUSET)
for i, thread in enumerate(threads):
cpu = CPUSET[i % len(CPUSET)]
psutil.Process(thread.id).cpu_affinity([cpu])
# print("Thread %d bound to CPU #%d" % (thread.id, cpu))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment