-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
FreeBSD adding temperature sensors (WIP) #1350
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -432,6 +432,22 @@ def sensors_battery(): | |
secsleft = minsleft * 60 | ||
return _common.sbattery(percent, secsleft, power_plugged) | ||
|
||
def sensors_temperatures(): | ||
"""Return systemp temperatures""" | ||
ret = dict() | ||
ret["coretemp"] = list() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not correct because if there are no temperatures you will return a
...and when you add elements to it do:
Finally, at the end of the function transform it to a plain dict with:
|
||
num_cpus = cpu_count_logical() | ||
try: | ||
for cpu in range(num_cpus): | ||
current, tjmax = cext.sensors_temperatures(cpu) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's better to catch Also |
||
name = "Core {}".format(cpu) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use |
||
ret["coretemp"].append( | ||
_common.shwtemp(name, current, tjmax, tjmax)) | ||
except NotImplementedError: | ||
return None | ||
|
||
return ret | ||
|
||
|
||
# ===================================================================== | ||
# --- other system functions | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -981,6 +981,8 @@ PsutilMethods[] = { | |
#if defined(PSUTIL_FREEBSD) | ||
{"sensors_battery", psutil_sensors_battery, METH_VARARGS, | ||
"Return battery information."}, | ||
{"sensors_temperatures", psutil_sensors_temperatures, METH_VARARGS, | ||
"Return temperatures information."}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Return temperature information for a given CPU core number." |
||
#endif | ||
|
||
// --- others | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 SYSCTLTEMP(t) ((int)((t + 270)) - 3000) / 10 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What's the logic here? Where did you take this? Are we returning Celsius? If there's some source code of reference it would be worth mentioning it here as a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah.. this bit kinda looks like a hack.. Explanation: Calling Always ending with By comparing this with I wasn't able go find official documentation of this, so if you or a FreeBSD expert have a better idea of how to get actual values, that would be great. Link to There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. It looks like coretemp.c implements the conversion logic like this: There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Paragraph 14.8 there. Let me read myself :) and come with review . There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks @glebius. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Okay, I reviewed and I don't understand why do you want to do conversion in the python module? The sysctl already returns Celsius. Looks like on your box it returns some strange data. What version of FreeBSD do you use @amanusk and what is the hardware? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @glebius, Thanks for you help with this. System is Thinkpad T430, with i7-3740QM Running |
||
#ifndef _PATH_DEVNULL | ||
#define _PATH_DEVNULL "/dev/null" | ||
#endif | ||
|
@@ -1010,3 +1011,38 @@ psutil_sensors_battery(PyObject *self, PyObject *args) { | |
PyErr_SetFromErrno(PyExc_OSError); | ||
return NULL; | ||
} | ||
|
||
|
||
/* | ||
* Return temperature information. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "Return temperature information for a given CPU core number." |
||
*/ | ||
PyObject * | ||
psutil_sensors_temperatures(PyObject *self, PyObject *args) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would rename this to |
||
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, ¤t, &size, NULL, 0)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @glebius: This call returns a 4 digit integer, as explained. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I reproduced that. Looks strange indeed. Investigating. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = SYSCTLTEMP(current); | ||
|
||
sprintf(sensor, "dev.cpu.%d.coretemp.tjmax", core); | ||
if (sysctlbyname(sensor, &tjmax, &size, NULL, 0)) | ||
goto error; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Considering we can have a valid "current" temp I think it's better to ignore this error and set "max" temp to |
||
tjmax = SYSCTLTEMP(tjmax); | ||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would rephrase this as: "Return CPU cores temperatures if available, else an empty dict."