[go: up one dir, main page]

Menu

[r305]: / trunk / cpuaffinity.py  Maximize  Restore  History

Download this file

60 lines (47 with data), 1.8 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
from PyQt4 import QtGui
import subprocess
import utils.procutils
import ui.cpuaffinity as affinityDialog
def doAffinity(cpuCount, process):
""" setup and handle the cpu affinity menu.
"""
global ui
dialog = QtGui.QDialog()
aff = affinityDialog.Ui_affinityDialog()
aff.setupUi(dialog)
#get affinity of process
affinityHexStr =subprocess.Popen(["taskset", "-p", str(process)], \
stdout=subprocess.PIPE).communicate()[0].strip().split("affinity mask: ")[1]
affinity = int(affinityHexStr, 16)
#disable cpu checkboxes of the cpu's.
for objName in aff.__dict__:
if objName.find("checkBox_") != -1:
cpuNr = int(objName.split("_")[1])
if cpuNr < cpuCount:
aff.__dict__[objName].setEnabled(True)
else:
aff.__dict__[objName].setEnabled(False)
#check CPU checkboxes
for cpu in xrange(cpuCount):
for objName in aff.__dict__:
if objName == "checkBox_%s" %cpu:
if affinity & 2**cpu == 2**cpu:
aff.__dict__[objName].setChecked(True)
else:
aff.__dict__[objName].setChecked(False)
ui = aff
dialog.setModal(True)
dialog.exec_()
#apply new affinity
newAff = 0
for cpu in xrange(cpuCount):
for objName in aff.__dict__:
if objName == "checkBox_%s" %cpu:
if aff.__dict__[objName].isChecked():
newAff = newAff | 2**cpu
if affinity != newAff:
proc = subprocess.Popen(["taskset", "-p", hex(newAff).replace("0x",""), str(process)], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
proc.communicate()
result = proc.returncode
if result != 0:
procutils.message("Setting process %s affinity value to %s failed" %(process, hex(newAff).replace("0x","")))