diff options
Diffstat (limited to 'psutil/_pslinux.py')
-rw-r--r-- | psutil/_pslinux.py | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py index 833cad01..16de79e5 100644 --- a/psutil/_pslinux.py +++ b/psutil/_pslinux.py @@ -1564,18 +1564,33 @@ class Process(object): def cpu_affinity_get(self): return cext.proc_cpu_affinity_get(self.pid) + def _get_eligible_cpus( + self, _re=re.compile(b"Cpus_allowed_list:\t(\d+)-(\d+)")): + # See: https://github.com/giampaolo/psutil/issues/956 + data = self._read_status_file() + match = _re.findall(data) + if match: + return list(range(int(match[0][0]), int(match[0][1]) + 1)) + else: + return list(range(len(per_cpu_times()))) + @wrap_exceptions def cpu_affinity_set(self, cpus): try: cext.proc_cpu_affinity_set(self.pid, cpus) except (OSError, ValueError) as err: if isinstance(err, ValueError) or err.errno == errno.EINVAL: - allcpus = tuple(range(len(per_cpu_times()))) + eligible_cpus = self._get_eligible_cpus() + all_cpus = tuple(range(len(per_cpu_times()))) for cpu in cpus: - if cpu not in allcpus: + if cpu not in all_cpus: raise ValueError( "invalid CPU number %r; choose between %s" % ( - cpu, allcpus)) + cpu, eligible_cpus)) + if cpu not in eligible_cpus: + raise ValueError( + "CPU number %r is not eligible; choose " + "between %s" % (cpu, eligible_cpus)) raise # only starting from kernel 2.6.13 |