未验证 提交 8f809cb2 编写于 作者: C Cleber Rosa

Merge remote-tracking branch 'cacarrara/pid-cpu-v2'

Signed-off-by: NCleber Rosa <crosa@redhat.com>
......@@ -340,3 +340,30 @@ def get_cpufreq_governor():
except IOError as err:
logging.error("Unable to get the current governor\n %s", err)
return ""
def get_pid_cpus(pid):
"""
Get all the cpus being used by the process according to pid informed
:param pid: process id
:type pid: string
:return: A list include all cpus the process is using
:rtype: list
"""
# processor id index is defined according proc documentation
# the negative index is necessary because backward data
# access has no misleading whitespaces
processor_id_index = -14
cpus = set()
proc_stat_files = glob.glob('/proc/%s/task/[123456789]*/stat' % pid)
for proc_stat_file in proc_stat_files:
try:
with open(proc_stat_file) as proc_stat:
cpus.add(
proc_stat.read().split(' ')[processor_id_index]
)
except IOError:
continue
return list(cpus)
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册