Support averaging power values
Support new env variables 'AVERAGE_power* = "true"' in hwmon config file.
When this env variable is set, power value is the calculated average value.
Otherwise, power value is from power*_input by default.
The new average of power is calculated since the last time the sensor's
values were changed and read.
average =
(cur_average*cur_average_interval - pre_average*pre_average_interval) /
(cur_average_interval - pre_average_interval)
hwmon config example:
AVERAGE_power2 = "true"
AVERAGE_power3 = "true"
AVERAGE_power4 = "true"
Tested: Set AVERAGE_power* in p0 OCC hwmon conf but not in p1 OCC hwmon conf,
then get power sensor info with restapi to check the values.
1. The values of p0*power are all average values.
2. The values of p1*power are all input values.
Note:
Delete $(CODE_COVERAGE_CPPFLAGS) in AM_CPPFLAGS in test/Makefile.am.
This option will define NDEBUG during configuration, then assert in
code doesn't work.
Resolves: openbmc/openbmc#3187
Signed-off-by: Carol Wang <wangkair@cn.ibm.com>
Change-Id: I8d97a7b2905c79cd4f2c276b32e7f5590ffc0483
diff --git a/average.hpp b/average.hpp
new file mode 100644
index 0000000..5474065
--- /dev/null
+++ b/average.hpp
@@ -0,0 +1,71 @@
+#pragma once
+
+#include "sensorset.hpp"
+
+#include <optional>
+#include <string>
+#include <vector>
+
+/** @class AverageHandling
+ * @brief Handle avergae value when AVERAGE_* is set in env
+ */
+class Average
+{
+ public:
+ /** @brief The key type of average_set */
+ using averageKey = SensorSet::key_type;
+
+ /** @brief <average, average_interval>
+ * average is the value of power*_average.
+ * average_interval is the value of power*_average_interval.
+ */
+ using averageValue = std::pair<int64_t, int64_t>;
+
+ /** @brief Store sensors' <averageKey, averageValue> map */
+ using averageMap = std::map<averageKey, averageValue>;
+
+ /** @brief Get averageValue in averageMap based on averageKey.
+ * This function will be called only when the env AVERAGE_xxx is set to
+ * true.
+ *
+ * @param[in] sensorKey - Sensor details
+ *
+ * @return - Optional
+ * return {}, if sensorKey can not be found in averageMap
+ * return averageValue, if sensorKey can be found in averageMap
+ */
+ std::optional<averageValue>
+ getAverageValue(const averageKey& sensorKey) const;
+
+ /** @brief Set average value in averageMap based on sensor key.
+ * This function will be called only when the env AVERAGE_xxx is set to
+ * true.
+ *
+ * @param[in] sensorKey - Sensor details
+ * @param[in] sensorValue - The related average values of this sensor
+ */
+ void setAverageValue(const averageKey& sensorKey,
+ const averageValue& sensorValue);
+
+ /** @brief Calculate the average value.
+ *
+ * @param[in] preAverage - The previous average value from *_average file
+ * @param[in] preInterval - The previous interval value from
+ * *_average_interval file
+ * @param[in] curAverage - The current average value from *_average file
+ * @param[in] curInterval - The current interval value from
+ * *_average_interval file
+ *
+ * @return value - Optional
+ * return {}, if curInterval-preInterval=0
+ * return new calculated average value, if curInterval-preInterval>0
+ */
+ static std::optional<int64_t> calcAverage(int64_t preAverage,
+ int64_t preInterval,
+ int64_t curAverage,
+ int64_t curInterval);
+
+ private:
+ /** @brief Store the previous average sensor map */
+ averageMap _previousAverageMap;
+};
\ No newline at end of file