Skip to content
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

Add parsing for /sys/class/thermal #1345

Merged
merged 5 commits into from
Oct 1, 2018
Merged
Show file tree
Hide file tree
Changes from 4 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
44 changes: 44 additions & 0 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1229,6 +1229,50 @@ def sensors_temperatures():

ret[unit_name].append((label, current, high, critical))

# Indication that no sensors were detected in /sys/class/hwmon/
if not basenames:
basenames = glob.glob('/sys/class/thermal/thermal_zone*')
basenames = sorted(set(basenames))

for base in basenames:
try:
path = os.path.join(base, 'temp')
current = float(cat(path)) / 1000.0
path = os.path.join(base, 'type')
unit_name = cat(path, binary=False)
except (IOError, OSError, ValueError) as err:
warnings.warn("ignoring %r for file %r" % (err, path),
RuntimeWarning)
continue

trip_paths = glob.glob(base + '/trip_point*')
trip_points = set(['_'.join(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Squashed 2 for loops into one

os.path.basename(p).split('_')[0:3]) for p in trip_paths])
critical = None
high = None
for trip_point in trip_points:
path = os.path.join(base, trip_point + "_type")
trip_type = cat(path, fallback='', binary=False)
if trip_type == 'critical':
critical = cat(os.path.join(base, trip_point + "_temp"),
fallback=None)
elif trip_type == 'high':
high = cat(os.path.join(base, trip_point + "_temp"),
fallback=None)

if high is not None:
try:
high = float(high) / 1000.0
except ValueError:
high = None
if critical is not None:
try:
critical = float(critical) / 1000.0
except ValueError:
critical = None

ret[unit_name].append(('', current, high, critical))

return ret


Expand Down
31 changes: 31 additions & 0 deletions psutil/tests/test_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1466,6 +1466,8 @@ def test_emulate_eio_error(self):
def open_mock(name, *args, **kwargs):
if name.endswith("_input"):
raise OSError(errno.EIO, "")
if name.endswith("temp"):
Copy link
Owner

Choose a reason for hiding this comment

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

elif

raise OSError(errno.EIO, "")
else:
return orig_open(name, *args, **kwargs)

Expand All @@ -1489,12 +1491,33 @@ def open_mock(name, *args, **kwargs):
return io.BytesIO(b"40000")
elif name.endswith('/temp1_crit'):
return io.BytesIO(b"50000")
# Files found in /sys/class/thermal/
elif name.endswith('0_temp'):
return io.BytesIO(b"50000")
elif name.endswith('temp'):
return io.BytesIO(b"30000")
elif name.endswith('0_type'):
return io.StringIO(u("critical"))
elif name.endswith('type'):
return io.StringIO(u("name"))
else:
return orig_open(name, *args, **kwargs)

def glob_mock(path):
if path == '/sys/class/hwmon/hwmon*/temp*_*':
return []
elif path == '/sys/class/hwmon/hwmon*/device/temp*_*':
return []
elif path == '/sys/class/thermal/thermal_zone*':
return ['/sys/class/thermal/thermal_zone0']
elif path == '/sys/class/thermal/thermal_zone0/trip_point*':
return ['/sys/class/thermal/thermal_zone1/trip_point_0_type',
'/sys/class/thermal/thermal_zone1/trip_point_0_temp']

orig_open = open
patch_point = 'builtins.open' if PY3 else '__builtin__.open'
with mock.patch(patch_point, side_effect=open_mock):
# Test case with /sys/class/hwmon
with mock.patch('glob.glob',
return_value=['/sys/class/hwmon/hwmon0/temp1']):
temp = psutil.sensors_temperatures()['name'][0]
Expand All @@ -1503,6 +1526,14 @@ def open_mock(name, *args, **kwargs):
self.assertEqual(temp.high, 40.0)
self.assertEqual(temp.critical, 50.0)

# Test case with only /sys/class/thermal
with mock.patch('glob.glob', create=True, side_effect=glob_mock):
temp = psutil.sensors_temperatures()['name'][0]
self.assertEqual(temp.label, '')
self.assertEqual(temp.current, 30.0)
self.assertEqual(temp.high, 50.0)
self.assertEqual(temp.critical, 50.0)


@unittest.skipIf(not LINUX, "LINUX only")
class TestSensorsFans(unittest.TestCase):
Expand Down