Skip to content

Instantly share code, notes, and snippets.

@nomeaning777
Created January 16, 2024 07:30
Show Gist options
  • Save nomeaning777/0107b023f379ac87d7cca97eff59a332 to your computer and use it in GitHub Desktop.
Save nomeaning777/0107b023f379ac87d7cca97eff59a332 to your computer and use it in GitHub Desktop.
List up the threads of a specific process and their CPU affinity
#!/usr/bin/python3
import os
import sys
for pid in sys.argv[1:]:
path = '/proc/' + pid + '/task'
if os.path.exists(path):
for tid in sorted(os.listdir(path)):
with open(f"{path}/{tid}/comm", 'r', encoding='utf-8') as r:
thread_name = r.read().strip()
print(f"TID={tid} Name={thread_name} Affinity={os.sched_getaffinity(int(tid))}")
else:
print(f'PID: {pid} not found', file=sys.stderr)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment