Skip to content

Commit

Permalink
Reading min and max values from the rated files exposed by hwmon
Browse files Browse the repository at this point in the history
With kernel 5.10 hwmon will expose PSU capabilities files. They will
have endings `_rated_min` and `_rated_max`. This commit allows
PSUSensor to utilize these files. If `rated` files are available
for given sensor then values read from these files will be used to
overwrite the MaxValue and MinValue default values.
The min/max update will be done with frequency 8x times lower than
the sensor value is updated, because in most cases they will not
change too often and for now there is no need to read them more
often.

Tested:
   Test were done manually, no regression detected.
   CPU usage stays at the same level.
   Min and max values are updated properly.

Signed-off-by: Zbigniew Kurzynski <zbigniew.kurzynski@intel.com>
Change-Id: Icbefcbeb605d0ebd3e127613181fbc5023f118e8
zkurzyns committed Sep 17, 2020
1 parent e333852 commit 484b9b3
Showing 2 changed files with 41 additions and 1 deletion.
4 changes: 4 additions & 0 deletions include/PSUSensor.hpp
Original file line number Diff line number Diff line change
@@ -30,10 +30,14 @@ class PSUSensor : public Sensor, public std::enable_shared_from_this<PSUSensor>
boost::asio::deadline_timer waitTimer;
std::shared_ptr<boost::asio::streambuf> readBuf;
std::string path;
std::string pathRatedMin;
std::string pathRatedMax;
size_t errCount;
unsigned int sensorFactor;
uint8_t minMaxReadCounter;
void handleResponse(const boost::system::error_code& err);
void checkThresholds(void) override;
void updateMinMaxValues(void);

int fd;
static constexpr unsigned int sensorPollMs = 1000;
38 changes: 37 additions & 1 deletion src/PSUSensor.cpp
Original file line number Diff line number Diff line change
@@ -49,7 +49,8 @@ PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
std::move(_thresholds), sensorConfiguration, objectType, max, min,
conn),
std::enable_shared_from_this<PSUSensor>(), objServer(objectServer),
inputDev(io), waitTimer(io), path(path), sensorFactor(factor)
inputDev(io), waitTimer(io), path(path), pathRatedMax(""), pathRatedMin(""),
sensorFactor(factor), minMaxReadCounter(0)
{
if constexpr (DEBUG)
{
@@ -99,6 +100,23 @@ PSUSensor::PSUSensor(const std::string& path, const std::string& objectType,
association = objectServer.add_interface(dbusPath, association::interface);

createInventoryAssoc(conn, association, configurationPath);

if (auto fileParts = splitFileName(path))
{
auto [type, nr, item] = *fileParts;
if (item.compare("input") == 0)
{
pathRatedMax = boost::replace_all_copy(path, item, "rated_max");
pathRatedMin = boost::replace_all_copy(path, item, "rated_min");
}
}
if constexpr (DEBUG)
{
std::cerr << "File: " << pathRatedMax
<< " will be used to update MaxValue\n";
std::cerr << "File: " << pathRatedMin
<< " will be used to update MinValue\n";
}
}

PSUSensor::~PSUSensor()
@@ -129,6 +147,19 @@ void PSUSensor::setupRead(void)
});
}

void PSUSensor::updateMinMaxValues(void)
{
if (auto newVal = readFile(pathRatedMin, sensorFactor))
{
updateProperty(sensorInterface, minValue, *newVal, "MinValue");
}

if (auto newVal = readFile(pathRatedMax, sensorFactor))
{
updateProperty(sensorInterface, maxValue, *newVal, "MaxValue");
}
}

void PSUSensor::handleResponse(const boost::system::error_code& err)
{
if ((err == boost::system::errc::bad_file_descriptor) ||
@@ -149,6 +180,11 @@ void PSUSensor::handleResponse(const boost::system::error_code& err)
double nvalue = rawValue / sensorFactor;

updateValue(nvalue);

if (minMaxReadCounter++ % 8 == 0)
{
updateMinMaxValues();
}
}
catch (const std::invalid_argument&)
{

0 comments on commit 484b9b3

Please sign in to comment.