Skip to content

Commit

Permalink
#792 / cpu_stats: linux impl
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Mar 5, 2016
1 parent 29fde6e commit 31808c0
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 1 deletion.
36 changes: 36 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,42 @@ CPU
2
>>>

.. function:: cpu_stats()

Return various CPU statistics as a namedtuple whose fields change depending
on the platform.

+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| Linux | OSX | Windows | SunOS | FreeBSD | OpenBSD | NetBSD |
+===============+==============+==============+==============+============+=============+==============+
| ctx_switches | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| interrupts | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| procs_running | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| procs_blocked | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+
| | | | | | | |
+---------------+--------------+--------------+--------------+------------+-------------+--------------+

Example (Linux):

.. code-block:: python
>>> import psutil
>>> psutil.cpu_stats()
scpustats(ctx_switches=20455687, interrupts=6598984, procs_running=1, procs_blocked=0)
.. versionadded:: 4.1.0


Memory
------

Expand Down
5 changes: 5 additions & 0 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1687,6 +1687,11 @@ def calculate(t1, t2):
return ret


def cpu_stats():
"""Return CPU statistics."""
return _psplatform.cpu_stats()


# =====================================================================
# --- system memory related functions
# =====================================================================
Expand Down
24 changes: 23 additions & 1 deletion psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,8 @@ def set_scputimes_ntuple(procfs_path):
'read_time', 'write_time',
'read_merged_count', 'write_merged_count',
'busy_time'])

scpustats = namedtuple('scpustats', ['ctx_switches', 'interrupts',
'procs_running', 'procs_blocked'])
popenfile = namedtuple('popenfile',
['path', 'fd', 'position', 'mode', 'flags'])
pmem = namedtuple('pmem', 'rss vms shared text lib data dirty')
Expand Down Expand Up @@ -429,6 +430,27 @@ def cpu_count_physical():
return sum(mapping.values()) or None


def cpu_stats():
with open_binary('%s/stat' % get_procfs_path()) as f:
ctx_switches = None
procs_running = None
procs_blocked = None
interrupts = None
for line in f:
if line.startswith(b'ctxt'):
ctx_switches = int(line.split()[1])
elif line.startswith(b'procs_running'):
procs_running = int(line.split()[1])
elif line.startswith(b'procs_blocked'):
procs_blocked = int(line.split()[1])
elif line.startswith(b'intr'):
interrupts = int(line.split()[1])
if ctx_switches is not None and procs_running is not None and \
procs_blocked is not None and interrupts is not None:
break
return scpustats(ctx_switches, interrupts, procs_running, procs_blocked)


# --- other system functions

def users():
Expand Down
3 changes: 3 additions & 0 deletions psutil/tests/test_memory_leaks.py
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,9 @@ def test_net_if_addrs(self):
def test_net_if_stats(self):
self.execute('net_if_stats')

def test_cpu_stats(self):
self.execute('cpu_stats')


if __name__ == '__main__':
run_test_module_by_name(__file__)
9 changes: 9 additions & 0 deletions psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -678,6 +678,15 @@ def test_users(self):
assert user.started > 0.0, user
datetime.datetime.fromtimestamp(user.started)

def test_cpu_stats(self):
# Tested more extensively in per-platform test modules.
infos = psutil.cpu_stats()
for name in infos._fields:
value = getattr(infos, name)
self.assertGreaterEqual(value, 0)
if name in ('ctx_switches', 'interrupts', 'syscalls'):
self.assertGreater(value, 0)


if __name__ == '__main__':
run_test_module_by_name(__file__)

0 comments on commit 31808c0

Please sign in to comment.