adcsensor: prevent reading when power is not ready
Currently, the adcsensor is reading all of the sensors even their power
state is not ready. It made the system waste more resources to read
files.
This commit checks the power state before requesting to read I/O files.
It prevents reading sensors when the power state is not ready.
Tested:
1. Turn off the power.
$ipmitool power off
2. Check the adc sensors that have the "PowerState" property in
Entity-Manager is "On"/"ChassisOn" via dbus interfaces.
3. Those sensors have the "Available" property is "false" and the
"Value" is "nan".
4. The hwmon files that corresponding to those sensors are not read
5. Turn on the power.
$ipmitool power on
6. Check the adc sensors that have the "PowerState" property in
Entity-Manager is "On"/"ChassisOn" via dbus interfaces.
7. Those sensors have the "Available" property is "true" and the
"Value" is not "nan".
Change-Id: I52219f40fdd3b478425956c176ac40e3e16c10d1
Signed-off-by: Thang Tran <thuutran@amperecomputing.com>
diff --git a/src/adc/ADCSensor.cpp b/src/adc/ADCSensor.cpp
index 0b3fd28..170e79b 100644
--- a/src/adc/ADCSensor.cpp
+++ b/src/adc/ADCSensor.cpp
@@ -34,6 +34,7 @@
#include <cmath>
#include <cstddef>
#include <iostream>
+#include <limits>
#include <memory>
#include <optional>
#include <stdexcept>
@@ -104,6 +105,14 @@
void ADCSensor::setupRead()
{
+ if (!readingStateGood())
+ {
+ markAvailable(false);
+ updateValue(std::numeric_limits<double>::quiet_NaN());
+ restartRead();
+ return;
+ }
+
std::shared_ptr<boost::asio::streambuf> buffer =
std::make_shared<boost::asio::streambuf>();
@@ -158,6 +167,36 @@
}
}
+void ADCSensor::restartRead()
+{
+ std::weak_ptr<ADCSensor> weakRef = weak_from_this();
+ waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
+ waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
+ std::shared_ptr<ADCSensor> self = weakRef.lock();
+ if (ec == boost::asio::error::operation_aborted)
+ {
+ if (self)
+ {
+ std::cerr << "adcsensor " << self->name << " read cancelled\n";
+ }
+ else
+ {
+ std::cerr << "adcsensor read cancelled no self\n";
+ }
+ return; // we're being canceled
+ }
+
+ if (self)
+ {
+ self->setupRead();
+ }
+ else
+ {
+ std::cerr << "adcsensor weakref no self\n";
+ }
+ });
+}
+
void ADCSensor::handleResponse(const boost::system::error_code& err)
{
std::weak_ptr<ADCSensor> weakRef = weak_from_this();
@@ -206,31 +245,7 @@
return; // we're no longer valid
}
inputDev.assign(fd);
- waitTimer.expires_after(std::chrono::milliseconds(sensorPollMs));
- waitTimer.async_wait([weakRef](const boost::system::error_code& ec) {
- std::shared_ptr<ADCSensor> self = weakRef.lock();
- if (ec == boost::asio::error::operation_aborted)
- {
- if (self)
- {
- std::cerr << "adcsensor " << self->name << " read cancelled\n";
- }
- else
- {
- std::cerr << "adcsensor read cancelled no self\n";
- }
- return; // we're being canceled
- }
-
- if (self)
- {
- self->setupRead();
- }
- else
- {
- std::cerr << "adcsensor weakref no self\n";
- }
- });
+ restartRead();
}
void ADCSensor::checkThresholds()
diff --git a/src/adc/ADCSensor.hpp b/src/adc/ADCSensor.hpp
index f562405..16d9c66 100644
--- a/src/adc/ADCSensor.hpp
+++ b/src/adc/ADCSensor.hpp
@@ -89,4 +89,5 @@
thresholds::ThresholdTimer thresholdTimer;
void handleResponse(const boost::system::error_code& err);
void checkThresholds() override;
+ void restartRead();
};