-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathassign_thread.py
More file actions
65 lines (54 loc) · 1.94 KB
/
assign_thread.py
File metadata and controls
65 lines (54 loc) · 1.94 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
63
64
#!/usr/bin/python
import subprocess
import argparse
import re
def set_pids(pname, tmapping):
proc = subprocess.Popen(['pidof',pname],stdout=subprocess.PIPE)
lines = (str(proc.stdout.readlines())).rstrip()
#print (lines)
split = lines.split()
for l in split:
pid = int(re.sub('[^0-9]','', l))
set_thread_cpu(str(pid), tmapping)
def get_tid_to_cpu(mapfile):
dict = {}
with open(mapfile) as f:
for line in f:
split = line.split();
dict[split[0]] = split[1]
return dict
def set_thread_cpu(pid, tmapping):
# top -H -p 10135 -n1 -b
tid_to_cpu = get_tid_to_cpu(tmapping)
dict = {} # thread id to cpu id
proc = subprocess.Popen(['top','-H', '-n', '1', '-b', '-p', pid ],stdout=subprocess.PIPE)
count = 0
for line in proc.stdout.readlines():
if count < 7:
count = count + 1
continue
threads = line.decode('utf-8').lstrip()
split = threads.split()
#print("thr=> " + split[0] + " " + split[11])
thread_name = split[11]
threadid = split[0]
if None != tid_to_cpu.get(thread_name):
cpulist = tid_to_cpu[thread_name]
if None == dict.get(cpulist):
dict[cpulist] = [];
dict.get(cpulist).append(threadid)
#print(dict)
assign_cpuid_to_thread(dict)
def assign_cpuid_to_thread(dictionary):
for cpulist, value in dictionary.items():
for tid in value:
# print ("cpulist" + " => " + tid)
proc = subprocess.Popen(['taskset', '-pc', cpulist, tid], stdout=subprocess.PIPE)
print(proc.stdout.readlines())
if __name__== "__main__":
parser = argparse.ArgumentParser("")
parser.add_argument("-n", "--pname", help="process name")
parser.add_argument("-m", "--tmapping", help="thread mapping file")
args = parser.parse_args()
set_pids(args.pname, args.tmapping)
get_tid_to_cpu(args.tmapping)