summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGiampaolo Rodola <g.rodola@gmail.com>2016-11-25 19:17:22 +0100
committerGiampaolo Rodola <g.rodola@gmail.com>2016-11-25 19:17:22 +0100
commit95b3d9680052366202a2b2bfac63431884826e70 (patch)
treee35896bf76f66a50b54606b580204c5dcbadf38e
parent6c3e3e188652781224c063e907658948600e7872 (diff)
downloadpsutil-95b3d9680052366202a2b2bfac63431884826e70.tar.gz
#357: implement process cpu_num() on Linux
-rw-r--r--docs/index.rst12
-rw-r--r--psutil/__init__.py12
-rw-r--r--psutil/_pslinux.py5
-rwxr-xr-xpsutil/tests/test_process.py9
4 files changed, 38 insertions, 0 deletions
diff --git a/docs/index.rst b/docs/index.rst
index 7fd79a1e..4f041675 100644
--- a/docs/index.rst
+++ b/docs/index.rst
@@ -1180,6 +1180,18 @@ Process class
.. versionchanged:: 2.2.0 added support for FreeBSD
+ .. method:: cpu_num()
+
+ Return what CPU this process is currently running on.
+ The returned number should be ``<=`` :func:`psutil.cpu_count()` and
+ ``<= len(psutil.cpu_percent(percpu=True))``.
+ It may be used in conjunction with ``psutil.cpu_percent(percpu=True)`` to
+ observe the system workload distributed across multiple CPUs.
+
+ Availability: Linux
+
+ .. versionadded:: 5.1.0
+
.. method:: memory_info()
Return a namedtuple with variable fields depending on the platform
diff --git a/psutil/__init__.py b/psutil/__init__.py
index 3287a2c7..0ab33783 100644
--- a/psutil/__init__.py
+++ b/psutil/__init__.py
@@ -836,6 +836,18 @@ class Process(object):
else:
self._proc.cpu_affinity_set(list(set(cpus)))
+ if hasattr(_psplatform.Process, "cpu_num"):
+
+ def cpu_num(self):
+ """Return what CPU this process is currently running on.
+ The returned number should be <= psutil.cpu_count()
+ and <= len(psutil.cpu_percent(percpu=True)).
+ It may be used in conjunction with
+ psutil.cpu_percent(percpu=True) to observe the system
+ workload distributed across CPUs.
+ """
+ return self._proc.cpu_num()
+
# Linux, OSX and Windows only
if hasattr(_psplatform.Process, "environ"):
diff --git a/psutil/_pslinux.py b/psutil/_pslinux.py
index 91fdae4f..c2c17a9a 100644
--- a/psutil/_pslinux.py
+++ b/psutil/_pslinux.py
@@ -1240,6 +1240,11 @@ class Process(object):
return _common.pcputimes(utime, stime, children_utime, children_stime)
@wrap_exceptions
+ def cpu_num(self):
+ """What CPU the process is on."""
+ return int(self._parse_stat_file()[37])
+
+ @wrap_exceptions
def wait(self, timeout=None):
try:
return _psposix.wait_pid(self.pid, timeout)
diff --git a/psutil/tests/test_process.py b/psutil/tests/test_process.py
index b0176083..49aa4d86 100755
--- a/psutil/tests/test_process.py
+++ b/psutil/tests/test_process.py
@@ -281,6 +281,12 @@ class TestProcess(unittest.TestCase):
if (max([kernel_time, ktime]) - min([kernel_time, ktime])) > 0.1:
self.fail("expected: %s, found: %s" % (ktime, kernel_time))
+ @unittest.skipUnless(hasattr(psutil.Process, "cpu_num"),
+ "platform not supported")
+ def test_cpu_num(self):
+ p = psutil.Process()
+ self.assertIn(p.cpu_num(), range(psutil.cpu_count()))
+
def test_create_time(self):
sproc = get_test_subprocess()
now = time.time()
@@ -1728,6 +1734,9 @@ class TestFetchAllProcesses(unittest.TestCase):
self.assertTrue(ret.user >= 0)
self.assertTrue(ret.system >= 0)
+ def cpu_num(self, ret, proc):
+ self.assertIn(ret, range(psutil.cpu_count()))
+
def memory_info(self, ret, proc):
for name in ret._fields:
self.assertGreaterEqual(getattr(ret, name), 0)