Skip to content

Commit

Permalink
#941: add doc
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jan 20, 2017
1 parent 17cbdf8 commit 35e9fda
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 22 deletions.
3 changes: 3 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,9 @@ CPU
>>>
>>> psutil.cpu_stats()
scpustats(ctx_switches=20455687, interrupts=6598984, soft_interrupts=2134212, syscalls=0)
>>>
>>> psutil.cpu_freq()
scpufreq(current=931.42925, min=800.0, max=3500.0)
Memory
======
Expand Down
26 changes: 26 additions & 0 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,32 @@ CPU
.. versionadded:: 4.1.0


.. function:: cpu_freq(percpu=False)

Return CPU frequency as a nameduple including *current*, *min* and *max*

This comment has been minimized.

Copy link
@g1itch

g1itch Feb 19, 2020

Was nameduple a typo?

frequencies expressed in Mhz.
If *percpu* is ``True`` and the system supports per-cpu frequency
retrieval (Linux only) a list of frequencies is returned for each CPU,
if not, a list with a single element is returned.

Example (Linux):

.. code-block:: python
>>> import psutil
>>> psutil.cpu_freq()
scpufreq(current=931.42925, min=800.0, max=3500.0)
>>> psutil.cpu_freq(percpu=True)
[scpufreq(current=2394.945, min=800.0, max=3500.0),
scpufreq(current=2236.812, min=800.0, max=3500.0),
scpufreq(current=1703.609, min=800.0, max=3500.0),
scpufreq(current=1754.289, min=800.0, max=3500.0)]
Availability: Linux, OSX, Windows

.. versionadded:: 5.1.0


Memory
------

Expand Down
36 changes: 23 additions & 13 deletions psutil/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1854,20 +1854,30 @@ def cpu_stats():

if hasattr(_psplatform, "cpu_freq"):

def cpu_freq():
"""Return CPU frequencies as a list of nameduples including
current, min and max CPU frequency.
The CPUs order is supposed to be consistent with other CPU
functions having a 'percpu' argument and returning results for
multiple CPUs (cpu_times(), cpu_percent(), cpu_times_percent()).
Values are expressed in Mhz.
Notes about OSX:
- it is not possible to get per-cpu freq
- reported freq never changes:
https://arstechnica.com/civis/viewtopic.php?f=19&t=465002
def cpu_freq(percpu=False):
"""Return CPU frequency as a nameduple including current,
min and max frequency expressed in Mhz.
If percpu is True and the system supports per-cpu frequency
retrieval (Linux only) a list of frequencies is returned for
each CPU. If not a list with one element is returned.
"""
return _psplatform.cpu_freq()
ret = _psplatform.cpu_freq()
if percpu:
return ret
else:
num_cpus = len(ret)
if num_cpus == 1:
return ret[0]
currs, mins, maxs = [], [], []
for cpu in ret:
currs.append(cpu.current)
mins.append(cpu.min)
maxs.append(cpu.max)
return _common.scpufreq(
sum(currs) / num_cpus,
sum(mins) / num_cpus,
sum(maxs) / num_cpus)

__all__.append("cpu_freq")

Expand Down
2 changes: 1 addition & 1 deletion psutil/_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ class NicDuplex(enum.IntEnum):
scpustats = namedtuple(
'scpustats', ['ctx_switches', 'interrupts', 'soft_interrupts', 'syscalls'])
# psutil.cpu_freq()
scpufreq = namedtuple('scpufreq', ['curr', 'min', 'max'])
scpufreq = namedtuple('scpufreq', ['current', 'min', 'max'])

# --- for Process methods

Expand Down
5 changes: 5 additions & 0 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,11 @@ def cpu_stats():


def cpu_freq():
"""Return CPU frequency.
On OSX per-cpu frequency is not supported.
Also, the returned frequency never changes, see:
https://arstechnica.com/civis/viewtopic.php?f=19&t=465002
"""
curr, min_, max_ = cext.cpu_freq()
return [_common.scpufreq(curr, min_, max_)]

Expand Down
7 changes: 5 additions & 2 deletions psutil/_pswindows.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,9 +300,12 @@ def cpu_stats():


def cpu_freq():
"""Return CPU frequency.
On Windows per-cpu frequency is not supported.
"""
curr, max_ = cext.cpu_freq()
min_ = 0
return [_common.scpufreq(curr, min_, max_)]
min_ = 0.0
return [_common.scpufreq(float(curr), min_, float(max_))]


# =====================================================================
Expand Down
2 changes: 1 addition & 1 deletion psutil/tests/test_osx.py
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ def test_cpu_count_physical(self):
def test_cpu_freq(self):
freq = psutil.cpu_freq()[0]
self.assertEqual(
freq.curr * 1000 * 1000, sysctl("sysctl hw.cpufrequency"))
freq.current * 1000 * 1000, sysctl("sysctl hw.cpufrequency"))
self.assertEqual(
freq.min * 1000 * 1000, sysctl("sysctl hw.cpufrequency_min"))
self.assertEqual(
Expand Down
18 changes: 13 additions & 5 deletions psutil/tests/test_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -700,13 +700,21 @@ def test_cpu_stats(self):
@unittest.skipUnless(hasattr(psutil, "cpu_freq"),
"platform not suported")
def test_cpu_freq(self):
ls = psutil.cpu_freq()
def check_ls(ls):
for nt in ls:
self.assertLessEqual(nt.current, nt.max)
for name in nt._fields:
value = getattr(nt, name)
self.assertGreaterEqual(value, 0)

ls = psutil.cpu_freq(percpu=True)
if not TRAVIS:
assert ls, ls
for nt in ls:
for name in nt._fields:
value = getattr(nt, name)
self.assertGreaterEqual(value, 0)

check_ls([psutil.cpu_freq(percpu=False)])

if LINUX:
self.assertEqual(len(ls), psutil.cpu_count())

def test_os_constants(self):
names = ["POSIX", "WINDOWS", "LINUX", "OSX", "FREEBSD", "OPENBSD",
Expand Down

0 comments on commit 35e9fda

Please sign in to comment.