Skip to content

Instantly share code, notes, and snippets.

@jenrik
Last active July 1, 2019 10:03
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 jenrik/ec70c1da8066e53e7d0e3f4c844eb9c9 to your computer and use it in GitHub Desktop.
Save jenrik/ec70c1da8066e53e7d0e3f4c844eb9c9 to your computer and use it in GitHub Desktop.
Enable/Disable a number of cores on a Linux system
#!/usr/bin/env python3
import sys
MAX_CORES = 12
if len(sys.argv) >= 2:
n = int(sys.argv[1])
if n > MAX_CORES or n < 1:
sys.exit(1)
for i in range(1, n):
path = "/sys/devices/system/cpu/cpu{}/online".format(i)
with open(path, "w") as f:
f.write("1")
for i in range(n, MAX_CORES):
path = "/sys/devices/system/cpu/cpu{}/online".format(i)
with open(path, "w") as f:
f.write("0")
else:
enabled = 1
for i in range(1, MAX_CORES):
path = "/sys/devices/system/cpu/cpu{}/online".format(i)
with open(path, "r") as f:
c = f.read(1)
if c == "1":
enabled += 1
print(enabled)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment