Skip to content

FreeBSD adding temperature sensors (WIP) #1350

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Nov 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions psutil/_psbsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import xml.etree.ElementTree as ET
from collections import namedtuple
from socket import AF_INET
from collections import defaultdict

from . import _common
from . import _psposix
Expand Down Expand Up @@ -432,6 +433,23 @@ def sensors_battery():
secsleft = minsleft * 60
return _common.sbattery(percent, secsleft, power_plugged)

def sensors_temperatures():
"Return CPU cores temperatures if available, else an empty dict."
ret = defaultdict(list)
num_cpus = cpu_count_logical()
for cpu in range(num_cpus):
try:
current, high = cext.sensors_cpu_temperature(cpu)
if high <= 0:
high = None
name = "Core %s" % cpu
ret["coretemp"].append(
_common.shwtemp(name, current, high, high))
except NotImplementedError:
pass

return ret


# =====================================================================
# --- other system functions
Expand Down
2 changes: 2 additions & 0 deletions psutil/_psutil_bsd.c
Original file line number Diff line number Diff line change
Expand Up @@ -981,6 +981,8 @@ PsutilMethods[] = {
#if defined(PSUTIL_FREEBSD)
{"sensors_battery", psutil_sensors_battery, METH_VARARGS,
"Return battery information."},
{"sensors_cpu_temperature", psutil_sensors_cpu_temperature, METH_VARARGS,
"Return temperature information for a given CPU core number."},
#endif

// --- others
Expand Down
36 changes: 36 additions & 0 deletions psutil/arch/freebsd/specific.c
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
#define PSUTIL_TV2DOUBLE(t) ((t).tv_sec + (t).tv_usec / 1000000.0)
#define PSUTIL_BT2MSEC(bt) (bt.sec * 1000 + (((uint64_t) 1000000000 * (uint32_t) \
(bt.frac >> 32) ) >> 32 ) / 1000000)
#define DECIKELVIN_2_CELCIUS(t) (t - 2731) / 10
#ifndef _PATH_DEVNULL
#define _PATH_DEVNULL "/dev/null"
#endif
Expand Down Expand Up @@ -1010,3 +1011,38 @@ psutil_sensors_battery(PyObject *self, PyObject *args) {
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}


/*
* Return temperature information for a given CPU core number.
*/
PyObject *
psutil_sensors_cpu_temperature(PyObject *self, PyObject *args) {
int current;
int tjmax;
int core;
char sensor[26];
size_t size = sizeof(current);

if (! PyArg_ParseTuple(args, "i", &core))
return NULL;
sprintf(sensor, "dev.cpu.%d.temperature", core);
if (sysctlbyname(sensor, &current, &size, NULL, 0))
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@glebius: This call returns a 4 digit integer, as explained.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I reproduced that. Looks strange indeed. Investigating.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see. The sysctl has special format. It reports in deciKelvin. Here is example parser:

https://github.com/freebsd/freebsd/blob/master/sbin/sysctl/sysctl.c#L790

Just copy math from there. Your math is almost correct.

goto error;
current = DECIKELVIN_2_CELCIUS(current);

// Return -273 in case of faliure.
sprintf(sensor, "dev.cpu.%d.coretemp.tjmax", core);
if (sysctlbyname(sensor, &tjmax, &size, NULL, 0))
tjmax = 0;
tjmax = DECIKELVIN_2_CELCIUS(tjmax);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

extra line

return Py_BuildValue("ii", current, tjmax);

error:
if (errno == ENOENT)
PyErr_SetString(PyExc_NotImplementedError, "no temperature sensors");
else
PyErr_SetFromErrno(PyExc_OSError);
return NULL;
}
1 change: 1 addition & 0 deletions psutil/arch/freebsd/specific.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,4 +29,5 @@ PyObject* psutil_virtual_mem(PyObject* self, PyObject* args);
PyObject* psutil_cpu_stats(PyObject* self, PyObject* args);
#if defined(PSUTIL_FREEBSD)
PyObject* psutil_sensors_battery(PyObject* self, PyObject* args);
PyObject* psutil_sensors_cpu_temperature(PyObject* self, PyObject* args);
#endif
17 changes: 17 additions & 0 deletions psutil/tests/test_bsd.py
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,23 @@ def test_sensors_battery_no_battery(self):
sysctl("hw.acpi.acline")
self.assertIsNone(psutil.sensors_battery())

# --- sensors_temperatures

def test_sensors_temperatures_against_sysctl(self):
num_cpus = psutil.cpu_count(True)
for cpu in range(num_cpus):
sensor = "dev.cpu.%s.temperature" % cpu
# sysctl returns a string in the format 46.0C
sysctl_result = int(float(sysctl(sensor)[:-1]))
self.assertAlmostEqual(
psutil.sensors_temperatures()["coretemp"][cpu].current,
sysctl_result, delta=10)

sensor = "dev.cpu.%s.coretemp.tjmax" % cpu
sysctl_result = int(float(sysctl(sensor)[:-1]))
self.assertEqual(
psutil.sensors_temperatures()["coretemp"][cpu].high,
sysctl_result)

# =====================================================================
# --- OpenBSD
Expand Down
4 changes: 2 additions & 2 deletions psutil/tests/test_contracts.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ def test_cpu_freq(self):

def test_sensors_temperatures(self):
self.assertEqual(
hasattr(psutil, "sensors_temperatures"), LINUX)
hasattr(psutil, "sensors_temperatures"), LINUX or FREEBSD)

def test_sensors_fans(self):
self.assertEqual(hasattr(psutil, "sensors_fans"), LINUX)
Expand Down Expand Up @@ -337,7 +337,7 @@ def test_fetch_all(self):
self.assertEqual(err.name, p.name())
assert str(err)
assert err.msg
except Exception as err:
except Exception:
s = '\n' + '=' * 70 + '\n'
s += "FAIL: test_%s (proc=%s" % (name, p)
if ret != default:
Expand Down