Skip to content

Commit

Permalink
#1284: give credits to @amanusk + some minor adjustments
Browse files Browse the repository at this point in the history
  • Loading branch information
giampaolo committed Jun 26, 2018
1 parent ca202d8 commit 93b1da9
Show file tree
Hide file tree
Showing 6 changed files with 42 additions and 39 deletions.
4 changes: 4 additions & 0 deletions CREDITS
Original file line number Diff line number Diff line change
Expand Up @@ -543,3 +543,7 @@ I: 1258, 1289
N: Nikhil Marathe
W: https://github.com/nikhilm
I: 1278

N: Alex Manuskin
W: https://github.com/amanusk
I: 1284
7 changes: 6 additions & 1 deletion HISTORY.rst
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
*Bug tracker at https://github.com/giampaolo/psutil/issues*

5.4.7
5.5.0
=====

XXXX-XX-XX

**Enhancements**

- 1284_: [OSX] added support for sensors_temperatures() and sensors_fans().
(patch by Alex Manuskin)

**Bug fixes**

- 1209_: [OSX] Process.memory_maps() may fail with EINVAL due to poor
Expand Down
8 changes: 6 additions & 2 deletions docs/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -698,10 +698,12 @@ Sensors
See also `temperatures.py <https://github.com/giampaolo/psutil/blob/master/scripts/temperatures.py>`__ and `sensors.py <https://github.com/giampaolo/psutil/blob/master/scripts/sensors.py>`__
for an example application.

Availability: Linux
Availability: Linux, OSX

.. versionadded:: 5.1.0

.. versionchanged:: 5.5.0: added OSX support

.. warning::

this API is experimental. Backward incompatible changes may occur if
Expand All @@ -722,10 +724,12 @@ Sensors
See also `fans.py <https://github.com/giampaolo/psutil/blob/master/scripts/fans.py>`__ and `sensors.py <https://github.com/giampaolo/psutil/blob/master/scripts/sensors.py>`__
for an example application.

Availability: Linux
Availability: Linux, OSX

.. versionadded:: 5.2.0

.. versionchanged:: 5.5.0: added OSX support

.. warning::

this API is experimental. Backward incompatible changes may occur if
Expand Down
34 changes: 14 additions & 20 deletions psutil/_psosx.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

"""OSX platform implementation."""

import collections
import contextlib
import errno
import collections
import functools
import os
from socket import AF_INET
Expand Down Expand Up @@ -63,9 +63,7 @@
cext.SZOMB: _common.STATUS_ZOMBIE,
}

temperatures = (
# group, key, label

SMC_TEMPERATURES = (
# --- CPU
("CPU", "TCXC", "PECI CPU"),
("CPU", "TCXc", "PECI CPU"),
Expand Down Expand Up @@ -319,36 +317,34 @@ def disk_partitions(all=False):

def sensors_temperatures():
"""Returns a dictionary of regions of temperature sensors:
CPU/GPU/Memory/Others
Each entry contains a list of results of all the successfully polled
SMC keys from the system.
CPU/GPU/Memory/HDD/Battery/Others.
Each entry contains a list of results of all the SMC keys successfully
polled from the system.
References for SMC keys and meaning:
https://stackoverflow.com/questions/28568775/
* https://stackoverflow.com/questions/28568775/
description-for-apples-smc-keys/31033665#31033665
https://github.com/Chris911/iStats/blob/
* https://github.com/Chris911/iStats/blob/
09b159f85a9481b59f347a37259f6d272f65cc05/lib/iStats/smc.rb
http://web.archive.org/web/20140714090133/http://www.parhelia.ch:80/
* http://web.archive.org/web/20140714090133/http://www.parhelia.ch:80/
blog/statics/k3_keys.html
"""
# TODO: this should be rewritten in C:
# https://github.com/giampaolo/psutil/pull/1284#issuecomment-399480983
ret = collections.defaultdict(list)

for group, key, label in temperatures:
for group, key, label in SMC_TEMPERATURES:
result = cext.smc_get_temperature(key)
result = round(result, 1)
if result <= 0:
continue
ret[group].append((label, result, None, None))

return dict(ret)


def sensors_battery():
"""Return battery information.
"""
"""Return battery information."""
try:
percent, minsleft, power_plugged = cext.sensors_battery()
except NotImplementedError:
Expand All @@ -369,10 +365,8 @@ def sensors_fans():
"""
ret = collections.defaultdict(list)
rawlist = cext.sensors_fans()
if rawlist is not None:
for fan in rawlist:
ret["Fans"].append(_common.sfan(fan[0], fan[1]))

for fan in rawlist:
ret["Fans"].append(_common.sfan(fan[0], fan[1]))
return dict(ret)


Expand Down
22 changes: 8 additions & 14 deletions psutil/_psutil_osx.c
Original file line number Diff line number Diff line change
Expand Up @@ -930,33 +930,27 @@ psutil_sensors_fans(PyObject *self, PyObject *args) {
return NULL;

fan_count = SMCGetFanNumber(SMC_KEY_FAN_NUM);
if (fan_count < 0) {
if (fan_count < 0)
fan_count = 0;
}
for (key =0; key < fan_count; key++) {

for (key = 0; key < fan_count; key++) {
sprintf(fan, "Fan %d", key);
speed = SMCGetFanSpeed(key);
if (speed < 0) {
if (speed < 0)
continue;
}
py_tuple = Py_BuildValue(
"(si)",
fan, // label
speed // value
);
py_tuple = Py_BuildValue("(si)", fan, speed);
if (!py_tuple)
goto error;

if (PyList_Append(py_retlist, py_tuple)) {
if (PyList_Append(py_retlist, py_tuple))
goto error;
}
Py_XDECREF(py_tuple);
}

return py_retlist;

error:
Py_XDECREF(py_tuple);
Py_XDECREF(py_retlist);
Py_DECREF(py_retlist);
return NULL;
}

Expand Down
6 changes: 4 additions & 2 deletions psutil/arch/osx/smc.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,16 @@ kern_return_t SMCCall(io_connect_t conn,

#if MAC_OS_X_VERSION_10_5
return IOConnectCallStructMethod(
conn, index,
conn,
index,
inputStructure,
structureInputSize,
outputStructure,
&structureOutputSize);
#else
return IOConnectMethodStructureIStructureO(
conn, index,
conn,
index,
structureInputSize,
&structureOutputSize,
inputStructure,
Expand Down

0 comments on commit 93b1da9

Please sign in to comment.