blob: 14017b8613b6071087e0c2682be0a1868c045ac0 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001#pragma once
2
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07003#include "conf.hpp"
4#include "controller.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +00005#include "failsafeloggers/failsafe_logger_utility.hpp"
James Feist22c257a2018-08-31 14:07:12 -07006#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07007#include "sensors/manager.hpp"
8#include "sensors/sensor.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -08009#include "tuning.hpp"
Patrick Venture1a153792020-08-11 08:41:47 -070010#include "zone_interface.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070011
Patrick Venturea83a3ec2020-08-04 09:52:05 -070012#include <sdbusplus/bus.hpp>
13#include <sdbusplus/server.hpp>
14#include <xyz/openbmc_project/Control/Mode/server.hpp>
Harvey Wu37180062023-10-02 09:42:50 +080015#include <xyz/openbmc_project/Debug/Pid/ThermalPower/server.hpp>
Harvey Wucc0232a2023-02-09 14:58:55 +080016#include <xyz/openbmc_project/Debug/Pid/Zone/server.hpp>
ykchiu7c6d35d2023-05-10 17:01:46 +080017#include <xyz/openbmc_project/Object/Enable/server.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070018
Patrick Ventured8012182018-03-08 08:21:38 -080019#include <fstream>
Tom Tungdf1f1832022-11-14 19:26:52 +080020#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080021#include <map>
22#include <memory>
23#include <set>
24#include <string>
25#include <vector>
26
Patrick Ventured8012182018-03-08 08:21:38 -080027template <typename... T>
Patrick Williamsb228bc32022-07-22 19:26:56 -050028using ServerObject = typename sdbusplus::server::object_t<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070029using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Harvey Wucc0232a2023-02-09 14:58:55 +080030using DebugZoneInterface =
31 sdbusplus::xyz::openbmc_project::Debug::Pid::server::Zone;
32using ModeObject = ServerObject<ModeInterface, DebugZoneInterface>;
ykchiu7c6d35d2023-05-10 17:01:46 +080033using ProcessInterface =
34 sdbusplus::xyz::openbmc_project::Object::server::Enable;
Harvey Wu37180062023-10-02 09:42:50 +080035using DebugThermalPowerInterface =
36 sdbusplus::xyz::openbmc_project::Debug::Pid::server::ThermalPower;
37using ProcessObject =
38 ServerObject<ProcessInterface, DebugThermalPowerInterface>;
Patrick Ventured8012182018-03-08 08:21:38 -080039
Patrick Venturea0764872020-08-08 07:48:43 -070040namespace pid_control
41{
42
Patrick Ventured8012182018-03-08 08:21:38 -080043/*
Patrick Venture597ebd62020-08-11 08:48:19 -070044 * The DbusPidZone inherits from the Mode object so that it can listen for
45 * control mode changes. It primarily holds all PID loops and holds the sensor
46 * value cache that's used per iteration of the PID loops.
Patrick Ventured8012182018-03-08 08:21:38 -080047 */
Patrick Venture597ebd62020-08-11 08:48:19 -070048class DbusPidZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080049{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070050 public:
Patrick Venture597ebd62020-08-11 08:48:19 -070051 DbusPidZone(int64_t zone, double minThermalOutput, double failSafePercent,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080052 conf::CycleTime cycleTime, const SensorManager& mgr,
Delphine CC Chiu97889632023-11-06 11:32:46 +080053 sdbusplus::bus_t& bus, const char* objPath, bool defer,
54 bool accumulateSetPoint) :
Patrick Williamsd8c5a452022-04-07 16:08:56 -050055 ModeObject(bus, objPath,
56 defer ? ModeObject::action::defer_emit
57 : ModeObject::action::emit_object_added),
Patrick Venturef7a2dd52019-07-16 14:31:13 -070058 _zoneId(zone), _maximumSetPoint(),
Delphine CC Chiu97889632023-11-06 11:32:46 +080059 _accumulateSetPoint(accumulateSetPoint),
James Feist3484bed2019-02-25 13:28:18 -080060 _minThermalOutputSetPt(minThermalOutput),
ykchiu9fe3a3c2023-05-11 13:43:54 +080061 _zoneFailSafePercent(failSafePercent), _cycleTime(cycleTime), _mgr(mgr)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070062 {
Patrick Venturede79ee02019-05-08 14:50:00 -070063 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -080064 {
Patrick Venture89002db2019-05-08 15:02:55 -070065 _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
Patrick Venturec32e3fc2019-02-28 10:01:11 -080066 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 }
Patrick Ventured8012182018-03-08 08:21:38 -080068
Patrick Venture7a98c192020-08-12 08:35:16 -070069 bool getManualMode(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070070 /* Could put lock around this since it's accessed from two threads, but
71 * only one reader/one writer.
72 */
Josh Lehana4146eb2020-10-01 11:49:09 -070073
74 bool getRedundantWrite(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070075 void setManualMode(bool mode);
76 bool getFailSafeMode(void) const override;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -080077 void markSensorMissing(const std::string& name);
Delphine CC Chiu97889632023-11-06 11:32:46 +080078 bool getAccSetPoint(void) const override;
Patrick Venture7a98c192020-08-12 08:35:16 -070079
Bonnie Loc51ba912022-10-12 14:07:22 +080080 int64_t getZoneID(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080081 void addSetPoint(double setPoint, const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070082 double getMaxSetPointRequest(void) const override;
James Feist608304d2019-02-25 10:01:42 -080083 void addRPMCeiling(double ceiling) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070084 void clearSetPoints(void) override;
85 void clearRPMCeilings(void) override;
Harvey Wu92f9f3c2023-11-07 09:23:35 +080086 double getFailSafePercent(void) override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080087 double getMinThermalSetPoint(void) const;
Bonnie Lo0e8fc392022-10-05 10:20:55 +080088 uint64_t getCycleIntervalTime(void) const override;
89 uint64_t getUpdateThermalsCycle(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080090
Patrick Venture2d8e7852018-10-30 19:14:07 -070091 Sensor* getSensor(const std::string& name) override;
James Zheng6df8bb52024-11-27 23:38:47 +000092 std::vector<std::string> getSensorNames(void) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070093 void determineMaxSetPointRequest(void) override;
94 void updateFanTelemetry(void) override;
95 void updateSensors(void) override;
96 void initializeCache(void) override;
Josh Lehanb3005752022-02-22 20:48:07 -080097 void setOutputCache(std::string_view, const ValueCacheEntry&) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070098 void dumpCache(void);
Josh Lehana4146eb2020-10-01 11:49:09 -070099
Patrick Venture7a98c192020-08-12 08:35:16 -0700100 void processFans(void) override;
101 void processThermals(void) override;
Patrick Ventured8012182018-03-08 08:21:38 -0800102
James Feist22c257a2018-08-31 14:07:12 -0700103 void addFanPID(std::unique_ptr<Controller> pid);
104 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700105 double getCachedValue(const std::string& name) override;
Josh Lehanb3005752022-02-22 20:48:07 -0800106 ValueCacheEntry getCachedValues(const std::string& name) override;
107
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800108 void addFanInput(const std::string& fan, bool missingAcceptable);
109 void addThermalInput(const std::string& therm, bool missingAcceptable);
Patrick Ventured8012182018-03-08 08:21:38 -0800110
Patrick Venture7a98c192020-08-12 08:35:16 -0700111 void initializeLog(void) override;
112 void writeLog(const std::string& value) override;
Patrick Ventured8012182018-03-08 08:21:38 -0800113
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700114 /* Method for setting the manual mode over dbus */
115 bool manual(bool value) override;
116 /* Method for reading whether in fail-safe mode over dbus */
117 bool failSafe() const override;
Harvey Wucc0232a2023-02-09 14:58:55 +0800118 /* Method for recording the maximum SetPoint PID config name */
119 std::string leader() const override;
ykchiu7c6d35d2023-05-10 17:01:46 +0800120 /* Method for control process for each loop at runtime */
Harvey Wu37180062023-10-02 09:42:50 +0800121 void addPidControlProcess(std::string name, std::string type,
122 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800123 std::string objPath, bool defer);
124 bool isPidProcessEnabled(std::string name);
Patrick Ventured8012182018-03-08 08:21:38 -0800125
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800126 void addPidFailSafePercent(std::vector<std::string> inputs, double percent);
ykchiu9fe3a3c2023-05-11 13:43:54 +0800127
Harvey Wu37180062023-10-02 09:42:50 +0800128 void updateThermalPowerDebugInterface(std::string pidName,
129 std::string leader, double input,
130 double output) override;
131
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700132 private:
Tom Tungdf1f1832022-11-14 19:26:52 +0800133 template <bool fanSensorLogging>
134 void processSensorInputs(const std::vector<std::string>& sensorInputs,
135 std::chrono::high_resolution_clock::time_point now)
136 {
137 for (const auto& sensorInput : sensorInputs)
138 {
139 auto sensor = _mgr.getSensor(sensorInput);
140 ReadReturn r = sensor->read();
141 _cachedValuesByName[sensorInput] = {r.value, r.unscaled};
142 int64_t timeout = sensor->getTimeout();
143 std::chrono::high_resolution_clock::time_point then = r.updated;
144
145 auto duration =
146 std::chrono::duration_cast<std::chrono::seconds>(now - then)
147 .count();
148 auto period = std::chrono::seconds(timeout).count();
149 /*
150 * TODO(venture): We should check when these were last read.
151 * However, these are the fans, so if I'm not getting updated values
152 * for them... what should I do?
153 */
154 if constexpr (fanSensorLogging)
155 {
156 if (loggingEnabled)
157 {
158 const auto& v = _cachedValuesByName[sensorInput];
159 _log << "," << v.scaled << "," << v.unscaled;
160 const auto& p = _cachedFanOutputs[sensorInput];
161 _log << "," << p.scaled << "," << p.unscaled;
162 }
163 }
164
165 if (debugEnabled)
166 {
167 std::cerr << sensorInput << " sensor reading: " << r.value
168 << "\n";
169 }
170
171 // check if fan fail.
172 if (sensor->getFailed())
173 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800174 markSensorMissing(sensorInput);
175
Tom Tungdf1f1832022-11-14 19:26:52 +0800176 if (debugEnabled)
177 {
178 std::cerr << sensorInput << " sensor get failed\n";
179 }
180 }
181 else if (timeout != 0 && duration >= period)
182 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800183 markSensorMissing(sensorInput);
184
Tom Tungdf1f1832022-11-14 19:26:52 +0800185 if (debugEnabled)
186 {
187 std::cerr << sensorInput << " sensor timeout\n";
188 }
James Zheng6df8bb52024-11-27 23:38:47 +0000189 outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(),
190 sensorInput,
191 "The sensor has timed out.");
Tom Tungdf1f1832022-11-14 19:26:52 +0800192 }
193 else
194 {
195 // Check if it's in there: remove it.
196 auto kt = _failSafeSensors.find(sensorInput);
197 if (kt != _failSafeSensors.end())
198 {
199 if (debugEnabled)
200 {
201 std::cerr << sensorInput
202 << " is erased from failsafe sensor set\n";
203 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800204
Tom Tungdf1f1832022-11-14 19:26:52 +0800205 _failSafeSensors.erase(kt);
James Zheng6df8bb52024-11-27 23:38:47 +0000206 outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(),
207 sensorInput,
208 "The sensor has recovered.");
Tom Tungdf1f1832022-11-14 19:26:52 +0800209 }
210 }
211 }
212 }
213
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700214 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800215
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700216 const int64_t _zoneId;
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700217 double _maximumSetPoint = 0;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800218 std::string _maximumSetPointName;
219 std::string _maximumSetPointNamePrev;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700220 bool _manualMode = false;
Josh Lehana4146eb2020-10-01 11:49:09 -0700221 bool _redundantWrite = false;
Delphine CC Chiu97889632023-11-06 11:32:46 +0800222 bool _accumulateSetPoint = false;
James Feist3484bed2019-02-25 13:28:18 -0800223 const double _minThermalOutputSetPt;
ykchiu9fe3a3c2023-05-11 13:43:54 +0800224 // Zone fail safe Percent setting by configuration.
225 const double _zoneFailSafePercent;
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800226 const conf::CycleTime _cycleTime;
Patrick Ventured8012182018-03-08 08:21:38 -0800227
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800228 std::map<std::string, double> _failSafeSensors;
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800229 std::set<std::string> _missingAcceptable;
Patrick Ventured8012182018-03-08 08:21:38 -0800230
Delphine CC Chiu97889632023-11-06 11:32:46 +0800231 std::map<std::string, double> _SetPoints;
James Feist608304d2019-02-25 10:01:42 -0800232 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700233 std::vector<std::string> _fanInputs;
234 std::vector<std::string> _thermalInputs;
Josh Lehanb3005752022-02-22 20:48:07 -0800235 std::map<std::string, ValueCacheEntry> _cachedValuesByName;
236 std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700237 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800238
James Feist22c257a2018-08-31 14:07:12 -0700239 std::vector<std::unique_ptr<Controller>> _fans;
240 std::vector<std::unique_ptr<Controller>> _thermals;
ykchiu7c6d35d2023-05-10 17:01:46 +0800241
242 std::map<std::string, std::unique_ptr<ProcessObject>> _pidsControlProcess;
ykchiu9fe3a3c2023-05-11 13:43:54 +0800243 /*
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800244 * <key = sensor name, value = sensor failsafe percent>
245 * sensor fail safe Percent setting by each pid controller configuration.
ykchiu9fe3a3c2023-05-11 13:43:54 +0800246 */
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800247 std::map<std::string, double> _sensorFailSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800248};
Patrick Venturea0764872020-08-08 07:48:43 -0700249
250} // namespace pid_control