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

Added device address in temperature sensors dict #2304

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions psutil/_pslinux.py
Original file line number Diff line number Diff line change
Expand Up @@ -1345,6 +1345,18 @@ def sensors_temperatures():
current = float(bcat(path)) / 1000.0
path = os.path.join(os.path.dirname(base), 'name')
unit_name = cat(path).strip()

address = os.path.join(os.path.dirname(base), 'device', 'address')
Copy link
Owner

Choose a reason for hiding this comment

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

What's the content of this file? Can you paste it here? The parsing logic that you apply later on:

unit_name += '-' + ''.join(cat(address).strip().split(':')[1:3]).split('.')[0]

...looks a bit convoluted, so I'd like to simplify it if possible.

Copy link
Author

Choose a reason for hiding this comment

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

Of course, I have two files:

  • /sys/class/hwmon/hwmon0/device/address containing
    0000:b3:00.0
  • /sys/class/hwmon/hwmon1/device/address containing
    0000:b4:00.0

These correspond to the PCIe locations of my SSDs (more about pcie addressing here https://dassencio.org/75)

domain:bus:device.function
0000  :b3 :00    .0

The line you asked me about:

  1. takes the content of the file (stripping any white-spaces)
cat(address).strip()
# '0000:b3:00.0'
  1. splits the content at each :
cat(address).strip().split(':')
# ['0000', 'b3', '00.0']
  1. discards the domain number (quite always zero), takes the second and third part of the address (bus, device and function number)
cat(address).strip().split(':')[1:3]
# ['b3', '00.0']
  1. joins them together
''.join(cat(address).strip().split(':')[1:3])
# 'b300.0'
  1. splits the string at the . and then takes only the first part (function number is not needed since SSDs will only have one function, ones with more are not likely to have more sensors on different functions)
''.join(cat(address).strip().split(':')[1:3]).split('.')[0]
# 'b300'
  1. finally appends the address to the unit name separated by a -
unit_name += '-' + ''.join(cat(address).strip().split(':')[1:3]).split('.')[0]

# Some systems may have multiple devices with same unit_name,
# for example multiple nvme SSDs, both will have their sensors
# inside ret['nvme'] making it difficult to know which
# SSD they refer to
try:
unit_name += '-' + ''.join(cat(address).strip().split(':')[1:3]).split('.')[0]
except:
# device address file may not always exist, be readable or
# correctly parsable
pass
except (IOError, OSError, ValueError):
# A lot of things can go wrong here, so let's just skip the
# whole entry. Sure thing is Linux's /sys/class/hwmon really
Expand Down