From 93b1da90ee48630d57102faa989941ce46636644 Mon Sep 17 00:00:00 2001 From: Giampaolo Rodola Date: Tue, 26 Jun 2018 15:35:46 +0200 Subject: [PATCH] #1284: give credits to @amanusk + some minor adjustments --- CREDITS | 4 ++++ HISTORY.rst | 7 ++++++- docs/index.rst | 8 ++++++-- psutil/_psosx.py | 34 ++++++++++++++-------------------- psutil/_psutil_osx.c | 22 ++++++++-------------- psutil/arch/osx/smc.c | 6 ++++-- 6 files changed, 42 insertions(+), 39 deletions(-) diff --git a/CREDITS b/CREDITS index a797ff2f0..fd4f53d64 100644 --- a/CREDITS +++ b/CREDITS @@ -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 diff --git a/HISTORY.rst b/HISTORY.rst index ba2f1ed1a..3ba713145 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -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 diff --git a/docs/index.rst b/docs/index.rst index bde3d453c..208f59e9c 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -698,10 +698,12 @@ Sensors See also `temperatures.py `__ and `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 @@ -722,10 +724,12 @@ Sensors See also `fans.py `__ and `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 diff --git a/psutil/_psosx.py b/psutil/_psosx.py index 90c8be438..a0101df07 100644 --- a/psutil/_psosx.py +++ b/psutil/_psosx.py @@ -4,9 +4,9 @@ """OSX platform implementation.""" +import collections import contextlib import errno -import collections import functools import os from socket import AF_INET @@ -63,9 +63,7 @@ cext.SZOMB: _common.STATUS_ZOMBIE, } -temperatures = ( - # group, key, label - +SMC_TEMPERATURES = ( # --- CPU ("CPU", "TCXC", "PECI CPU"), ("CPU", "TCXc", "PECI CPU"), @@ -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: @@ -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) diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index 584df3b83..15f4d4f69 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -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; } diff --git a/psutil/arch/osx/smc.c b/psutil/arch/osx/smc.c index fb5668a1e..00de0a372 100644 --- a/psutil/arch/osx/smc.c +++ b/psutil/arch/osx/smc.c @@ -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,