-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathcpuset_split.py
More file actions
executable file
·62 lines (49 loc) · 1.57 KB
/
Copy pathcpuset_split.py
File metadata and controls
executable file
·62 lines (49 loc) · 1.57 KB
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
#!/usr/bin/env python3
import argparse
def get_physical_core(i: int):
with open(f"/sys/devices/system/cpu/cpu{i}/topology/core_id", 'r') as f:
core = f.read()
return int(core.rstrip())
def cpu_set_to_list(s: str):
# Convert CPU SET mask to CPU ID list
cpus = []
i = 0
for c in reversed(s):
# the value of /sys/devices/system/node/node0/cpumap is delimited by ','
if c == ',':
continue
n = int(c, 16)
if n & 0b0001:
cpus.append(str(i))
i += 1
if n & 0b0010:
cpus.append(str(i))
i += 1
if n & 0b0100:
cpus.append(str(i))
i += 1
if n & 0b1000:
cpus.append(str(i))
i += 1
return cpus
def print_by_core(l: [int]):
core_map = {}
for cpu in l:
core = get_physical_core(cpu)
if core in core_map:
core_map[core].append(str(cpu))
else:
core_map[core] = [str(cpu)]
sorted_map = sorted(core_map)
for core in sorted_map:
cpus = ','.join(core_map[core])
print(f"CORE:{core}: {cpus}")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Split a CPU SET into parts on different cores')
parser.add_argument('set', nargs=1, help='The input CPU SET')
parser.add_argument('--by-core', action='store_true', help='List CPU ID group by CORE ID')
args = parser.parse_args()
cpu_list = cpu_set_to_list(args.set[0])
print("LIST: {}".format(','.join(cpu_list)));
if args.by_core:
print_by_core(cpu_list)