diff options
author | Alex Manuskin <amanusk@protonmail.com> | 2018-06-26 16:19:54 +0300 |
---|---|---|
committer | Giampaolo Rodola <g.rodola@gmail.com> | 2018-06-26 06:19:54 -0700 |
commit | 196802dd34b7134c1fff99ad4755d0bc0e04cc98 (patch) | |
tree | 6afb9396ae0231bef6df741b6b136f4a9f1e6b90 /psutil/_psutil_osx.c | |
parent | d20b9939b985d871177bee2c1271a04a1810a100 (diff) | |
download | psutil-196802dd34b7134c1fff99ad4755d0bc0e04cc98.tar.gz |
Osx temps (#1284)
OSX: add temperatures() and fans()
Diffstat (limited to 'psutil/_psutil_osx.c')
-rw-r--r-- | psutil/_psutil_osx.c | 68 |
1 files changed, 68 insertions, 0 deletions
diff --git a/psutil/_psutil_osx.c b/psutil/_psutil_osx.c index b8e3b85d..584df3b8 100644 --- a/psutil/_psutil_osx.c +++ b/psutil/_psutil_osx.c @@ -44,6 +44,7 @@ #include "_psutil_common.h" #include "_psutil_posix.h" #include "arch/osx/process_info.h" +#include "arch/osx/smc.h" #define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0) @@ -896,6 +897,68 @@ psutil_boot_time(PyObject *self, PyObject *args) { return Py_BuildValue("f", (float)boot_time); } +/* + * Return a Python float indicating the value of the temperature + * measured by an SMC key + */ +static PyObject * +psutil_smc_get_temperature(PyObject *self, PyObject *args) { + char* key; + float temp; + + if (! PyArg_ParseTuple(args, "s", &key)) { + return NULL; + } + temp = SMCGetTemperature(key); + return Py_BuildValue("d", temp); +} + + +/* + * Return a Python list of tuples of fan label and speed + */ +static PyObject * +psutil_sensors_fans(PyObject *self, PyObject *args) { + int key; + int speed; + char fan[7]; + int fan_count; + PyObject *py_tuple = NULL; + PyObject *py_retlist = PyList_New(0); + + if (py_retlist == NULL) + return NULL; + + fan_count = SMCGetFanNumber(SMC_KEY_FAN_NUM); + if (fan_count < 0) { + fan_count = 0; + } + for (key =0; key < fan_count; key++) { + sprintf(fan, "Fan %d", key); + speed = SMCGetFanSpeed(key); + if (speed < 0) { + continue; + } + py_tuple = Py_BuildValue( + "(si)", + fan, // label + speed // value + ); + if (!py_tuple) + goto error; + + 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); + return NULL; +} /* * Return a list of tuples including device, mount point and fs type @@ -1828,6 +1891,7 @@ psutil_cpu_stats(PyObject *self, PyObject *args) { } + /* * Return battery information. */ @@ -1980,6 +2044,10 @@ PsutilMethods[] = { "Return currently connected users as a list of tuples"}, {"cpu_stats", psutil_cpu_stats, METH_VARARGS, "Return CPU statistics"}, + {"smc_get_temperature", psutil_smc_get_temperature, METH_VARARGS, + "Temperature of SMC key as float"}, + {"sensors_fans", psutil_sensors_fans, METH_VARARGS, + "Return the RPM of the fan with SMC key"}, {"sensors_battery", psutil_sensors_battery, METH_VARARGS, "Return battery information."}, |