blob: 486f413f97ba20d2f9c9806c8808f4827367e538 [file]
#ifndef THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_THERMAL_SENSOR_INFO_SENSOR_INFO_H_
#define THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_THERMAL_SENSOR_INFO_SENSOR_INFO_H_
#include <limits>
#include <string>
#include "absl/container/flat_hash_map.h"
#include "absl/status/statusor.h"
#include "absl/strings/string_view.h"
#include "absl/synchronization/mutex.h"
#include "tlbmc/thermal/thermal_control_utils.h"
namespace milotic_tlbmc {
namespace thermal {
constexpr double kDefaultSensorScale = 0.0;
constexpr double kDefaultSensorValue = 0.0;
constexpr double kDefaultSensorFailsafePercent = 100;
struct SensorValues {
// Normalized sensor reading value.
double scaled_value = kDefaultSensorValue;
// Raw sensor reading value, as received directly from the sensor.
double unscaled_value = kDefaultSensorValue;
SensorValues() = default;
SensorValues(double scaled_value, double unscaled_value)
: scaled_value(scaled_value), unscaled_value(unscaled_value) {}
};
struct SensorThresholds {
double max_threshold_critical = std::numeric_limits<double>::max();
double min_threshold_critical = std::numeric_limits<double>::lowest();
};
struct SensorMetadata {
bool is_missing_acceptable = false;
double scale = kDefaultSensorScale;
double failsafe_percent = kDefaultSensorFailsafePercent;
SensorThresholds critical_thresholds;
};
/*
* `SensorInfo` stores the sensor names with their info (e.g., readings, and
* failsafe percentages) of a thermal zone.
*
* This class is conditionally thread-safe:
* When tuning mode is enabled, it is thread-safe at all times. Otherwise, it is
* expected to be read-only after the initialization of each thermal control
* iteration, and no mutex protection will be enforced.
*/
class SensorInfo {
public:
// `AddSensor` adds a sensor to the zone manager.
// It is expected to be used only during initialization of tlBMC thermal
// control service. It is thread-safe at all times.
void AddSensor(absl::string_view sensor_name,
const SensorMetadata& sensor_metadata);
// `SetSensorValues` updates the reading of a sensor. It is thread-safe only
// if tuning mode is enabled.
void SetSensorValues(absl::string_view sensor_name, double sensor_reading,
ThreadSafetyMode thread_safety_mode =
ThreadSafetyMode::DisableThreadSafety);
// `GetSensorValues` returns both the scaled and unscaled sensor reading
// value. It is thread-safe only if tuning mode is enabled, as the sensor
// readings should not change once a thermal loop begins.
SensorValues GetSensorValues(absl::string_view sensor_name,
ThreadSafetyMode thread_safety_mode =
ThreadSafetyMode::DisableThreadSafety);
// `GetSensorMetadata` returns the metadata of a sensor. It is thread-safe, as
// the metadata should not change after the initialization of tlBMC thermal
// control service.
absl::StatusOr<const SensorMetadata&> GetSensorMetadata(
absl::string_view sensor_name);
bool IsSensorMissingAcceptable(absl::string_view sensor_name);
// `GetSensorScale` returns the scale of a sensor. It is thread-safe, as the
// scale should not change after the initialization of tlBMC thermal control
// service.
double GetSensorScale(absl::string_view sensor_name);
// `GetFailsafePercent` returns the failsafe percentage of a sensor. It is
// thread-safe, as the failsafe percentage should not change after the
// initialization of tlBMC thermal control service.
double GetFailsafePercent(absl::string_view sensor_name);
// `GetCriticalThresholds` returns the critical thresholds of a sensor. It is
// thread-safe, as the failsafe percentage should not change after the
// initialization of tlBMC thermal control service.
SensorThresholds GetCriticalThresholds(absl::string_view sensor_name);
absl::Mutex mutex_;
// `sensor_name_to_readings` will be updated during each thermal control
// iteration. It is conditionally thread-safe: when tuning mode is enabled,
// it is thread-safe at all times. Otherwise, it is expected to be read-only
// after the initialization of each thermal control iteration, and no mutex
// protection will be enforced.
absl::flat_hash_map<std::string, SensorValues> sensor_name_to_readings_;
// `sensor_name_to_metadata_` will only be modified during the
// initialization of the thermal control service. It is thread-safe at all
// times.
absl::flat_hash_map<std::string, SensorMetadata> sensor_name_to_metadata_;
private:
void SetSensorValuesImpl(absl::string_view sensor_name,
double sensor_reading);
};
} // namespace thermal
} // namespace milotic_tlbmc
#endif // THIRD_PARTY_MILOTIC_EXTERNAL_CC_TLBMC_THERMAL_SENSOR_INFO_SENSOR_INFO_H_