From 69509cc42eccb18e9eec0c6f60bbdc92f8782ae2 Mon Sep 17 00:00:00 2001 From: Caio Carrara Date: Fri, 4 Jan 2019 16:10:29 -0200 Subject: [PATCH] utils.cpu: adds function to get cpus used by process This change adds a new utility function that receives a process id and returns all processors (id) being used by the process (and its threads) informed. Signed-off-by: Caio Carrara --- avocado/utils/cpu.py | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/avocado/utils/cpu.py b/avocado/utils/cpu.py index d4897c69..63601e21 100644 --- a/avocado/utils/cpu.py +++ b/avocado/utils/cpu.py @@ -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) -- GitLab