blob: 28549974af103392e09347430ca621c396de9393 [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 Feist22c257a2018-08-31 14:07:12 -07005#include "pidcontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07006#include "sensors/manager.hpp"
7#include "sensors/sensor.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -08008#include "tuning.hpp"
Patrick Venture1a153792020-08-11 08:41:47 -07009#include "zone_interface.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070010
Patrick Venturea83a3ec2020-08-04 09:52:05 -070011#include <sdbusplus/bus.hpp>
12#include <sdbusplus/server.hpp>
13#include <xyz/openbmc_project/Control/Mode/server.hpp>
Harvey Wu37180062023-10-02 09:42:50 +080014#include <xyz/openbmc_project/Debug/Pid/ThermalPower/server.hpp>
Harvey Wucc0232a2023-02-09 14:58:55 +080015#include <xyz/openbmc_project/Debug/Pid/Zone/server.hpp>
ykchiu7c6d35d2023-05-10 17:01:46 +080016#include <xyz/openbmc_project/Object/Enable/server.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070017
Patrick Ventured8012182018-03-08 08:21:38 -080018#include <fstream>
Tom Tungdf1f1832022-11-14 19:26:52 +080019#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080020#include <map>
21#include <memory>
22#include <set>
23#include <string>
24#include <vector>
25
Patrick Ventured8012182018-03-08 08:21:38 -080026template <typename... T>
Patrick Williamsb228bc32022-07-22 19:26:56 -050027using ServerObject = typename sdbusplus::server::object_t<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Harvey Wucc0232a2023-02-09 14:58:55 +080029using DebugZoneInterface =
30 sdbusplus::xyz::openbmc_project::Debug::Pid::server::Zone;
31using ModeObject = ServerObject<ModeInterface, DebugZoneInterface>;
ykchiu7c6d35d2023-05-10 17:01:46 +080032using ProcessInterface =
33 sdbusplus::xyz::openbmc_project::Object::server::Enable;
Harvey Wu37180062023-10-02 09:42:50 +080034using DebugThermalPowerInterface =
35 sdbusplus::xyz::openbmc_project::Debug::Pid::server::ThermalPower;
36using ProcessObject =
37 ServerObject<ProcessInterface, DebugThermalPowerInterface>;
Patrick Ventured8012182018-03-08 08:21:38 -080038
Patrick Venturea0764872020-08-08 07:48:43 -070039namespace pid_control
40{
41
Patrick Ventured8012182018-03-08 08:21:38 -080042/*
Patrick Venture597ebd62020-08-11 08:48:19 -070043 * The DbusPidZone inherits from the Mode object so that it can listen for
44 * control mode changes. It primarily holds all PID loops and holds the sensor
45 * value cache that's used per iteration of the PID loops.
Patrick Ventured8012182018-03-08 08:21:38 -080046 */
Patrick Venture597ebd62020-08-11 08:48:19 -070047class DbusPidZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080048{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049 public:
Patrick Venture597ebd62020-08-11 08:48:19 -070050 DbusPidZone(int64_t zone, double minThermalOutput, double failSafePercent,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080051 conf::CycleTime cycleTime, const SensorManager& mgr,
52 sdbusplus::bus_t& bus, const char* objPath, bool defer) :
Patrick Williamsd8c5a452022-04-07 16:08:56 -050053 ModeObject(bus, objPath,
54 defer ? ModeObject::action::defer_emit
55 : ModeObject::action::emit_object_added),
Patrick Venturef7a2dd52019-07-16 14:31:13 -070056 _zoneId(zone), _maximumSetPoint(),
James Feist3484bed2019-02-25 13:28:18 -080057 _minThermalOutputSetPt(minThermalOutput),
ykchiu9fe3a3c2023-05-11 13:43:54 +080058 _zoneFailSafePercent(failSafePercent), _cycleTime(cycleTime), _mgr(mgr)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070059 {
Patrick Venturede79ee02019-05-08 14:50:00 -070060 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -080061 {
Patrick Venture89002db2019-05-08 15:02:55 -070062 _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
Patrick Venturec32e3fc2019-02-28 10:01:11 -080063 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070064 }
Patrick Ventured8012182018-03-08 08:21:38 -080065
Patrick Venture7a98c192020-08-12 08:35:16 -070066 bool getManualMode(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070067 /* Could put lock around this since it's accessed from two threads, but
68 * only one reader/one writer.
69 */
Josh Lehana4146eb2020-10-01 11:49:09 -070070
71 bool getRedundantWrite(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070072 void setManualMode(bool mode);
73 bool getFailSafeMode(void) const override;
Patrick Venture7a98c192020-08-12 08:35:16 -070074
Bonnie Loc51ba912022-10-12 14:07:22 +080075 int64_t getZoneID(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080076 void addSetPoint(double setPoint, const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070077 double getMaxSetPointRequest(void) const override;
James Feist608304d2019-02-25 10:01:42 -080078 void addRPMCeiling(double ceiling) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070079 void clearSetPoints(void) override;
80 void clearRPMCeilings(void) override;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080081 double getFailSafePercent(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080082 double getMinThermalSetPoint(void) const;
Bonnie Lo0e8fc392022-10-05 10:20:55 +080083 uint64_t getCycleIntervalTime(void) const override;
84 uint64_t getUpdateThermalsCycle(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080085
Patrick Venture2d8e7852018-10-30 19:14:07 -070086 Sensor* getSensor(const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070087 void determineMaxSetPointRequest(void) override;
88 void updateFanTelemetry(void) override;
89 void updateSensors(void) override;
90 void initializeCache(void) override;
Josh Lehanb3005752022-02-22 20:48:07 -080091 void setOutputCache(std::string_view, const ValueCacheEntry&) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070092 void dumpCache(void);
Josh Lehana4146eb2020-10-01 11:49:09 -070093
Patrick Venture7a98c192020-08-12 08:35:16 -070094 void processFans(void) override;
95 void processThermals(void) override;
Patrick Ventured8012182018-03-08 08:21:38 -080096
James Feist22c257a2018-08-31 14:07:12 -070097 void addFanPID(std::unique_ptr<Controller> pid);
98 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070099 double getCachedValue(const std::string& name) override;
Josh Lehanb3005752022-02-22 20:48:07 -0800100 ValueCacheEntry getCachedValues(const std::string& name) override;
101
Patrick Venturec399f6f2018-10-30 19:24:47 -0700102 void addFanInput(const std::string& fan);
103 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -0800104
Patrick Venture7a98c192020-08-12 08:35:16 -0700105 void initializeLog(void) override;
106 void writeLog(const std::string& value) override;
Patrick Ventured8012182018-03-08 08:21:38 -0800107
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700108 /* Method for setting the manual mode over dbus */
109 bool manual(bool value) override;
110 /* Method for reading whether in fail-safe mode over dbus */
111 bool failSafe() const override;
Harvey Wucc0232a2023-02-09 14:58:55 +0800112 /* Method for recording the maximum SetPoint PID config name */
113 std::string leader() const override;
ykchiu7c6d35d2023-05-10 17:01:46 +0800114 /* Method for control process for each loop at runtime */
Harvey Wu37180062023-10-02 09:42:50 +0800115 void addPidControlProcess(std::string name, std::string type,
116 double setpoint, sdbusplus::bus_t& bus,
ykchiu7c6d35d2023-05-10 17:01:46 +0800117 std::string objPath, bool defer);
118 bool isPidProcessEnabled(std::string name);
Patrick Ventured8012182018-03-08 08:21:38 -0800119
ykchiu9fe3a3c2023-05-11 13:43:54 +0800120 void initPidFailSafePercent(void);
121 void addPidFailSafePercent(std::string name, double percent);
122
Harvey Wu37180062023-10-02 09:42:50 +0800123 void updateThermalPowerDebugInterface(std::string pidName,
124 std::string leader, double input,
125 double output) override;
126
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700127 private:
Tom Tungdf1f1832022-11-14 19:26:52 +0800128 template <bool fanSensorLogging>
129 void processSensorInputs(const std::vector<std::string>& sensorInputs,
130 std::chrono::high_resolution_clock::time_point now)
131 {
132 for (const auto& sensorInput : sensorInputs)
133 {
134 auto sensor = _mgr.getSensor(sensorInput);
135 ReadReturn r = sensor->read();
136 _cachedValuesByName[sensorInput] = {r.value, r.unscaled};
137 int64_t timeout = sensor->getTimeout();
138 std::chrono::high_resolution_clock::time_point then = r.updated;
139
140 auto duration =
141 std::chrono::duration_cast<std::chrono::seconds>(now - then)
142 .count();
143 auto period = std::chrono::seconds(timeout).count();
144 /*
145 * TODO(venture): We should check when these were last read.
146 * However, these are the fans, so if I'm not getting updated values
147 * for them... what should I do?
148 */
149 if constexpr (fanSensorLogging)
150 {
151 if (loggingEnabled)
152 {
153 const auto& v = _cachedValuesByName[sensorInput];
154 _log << "," << v.scaled << "," << v.unscaled;
155 const auto& p = _cachedFanOutputs[sensorInput];
156 _log << "," << p.scaled << "," << p.unscaled;
157 }
158 }
159
160 if (debugEnabled)
161 {
162 std::cerr << sensorInput << " sensor reading: " << r.value
163 << "\n";
164 }
165
166 // check if fan fail.
167 if (sensor->getFailed())
168 {
169 _failSafeSensors.insert(sensorInput);
170 if (debugEnabled)
171 {
172 std::cerr << sensorInput << " sensor get failed\n";
173 }
174 }
175 else if (timeout != 0 && duration >= period)
176 {
177 _failSafeSensors.insert(sensorInput);
178 if (debugEnabled)
179 {
180 std::cerr << sensorInput << " sensor timeout\n";
181 }
182 }
183 else
184 {
185 // Check if it's in there: remove it.
186 auto kt = _failSafeSensors.find(sensorInput);
187 if (kt != _failSafeSensors.end())
188 {
189 if (debugEnabled)
190 {
191 std::cerr << sensorInput
192 << " is erased from failsafe sensor set\n";
193 }
194 _failSafeSensors.erase(kt);
195 }
196 }
197 }
198 }
199
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700200 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800201
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700202 const int64_t _zoneId;
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700203 double _maximumSetPoint = 0;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800204 std::string _maximumSetPointName;
205 std::string _maximumSetPointNamePrev;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700206 bool _manualMode = false;
Josh Lehana4146eb2020-10-01 11:49:09 -0700207 bool _redundantWrite = false;
James Feist3484bed2019-02-25 13:28:18 -0800208 const double _minThermalOutputSetPt;
ykchiu9fe3a3c2023-05-11 13:43:54 +0800209 // Current fail safe Percent.
210 double _failSafePercent;
211 // Zone fail safe Percent setting by configuration.
212 const double _zoneFailSafePercent;
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800213 const conf::CycleTime _cycleTime;
Patrick Ventured8012182018-03-08 08:21:38 -0800214
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700215 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800216
Patrick Venture9bbf3332019-07-16 10:50:37 -0700217 std::vector<double> _SetPoints;
James Feist608304d2019-02-25 10:01:42 -0800218 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700219 std::vector<std::string> _fanInputs;
220 std::vector<std::string> _thermalInputs;
Josh Lehanb3005752022-02-22 20:48:07 -0800221 std::map<std::string, ValueCacheEntry> _cachedValuesByName;
222 std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700223 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800224
James Feist22c257a2018-08-31 14:07:12 -0700225 std::vector<std::unique_ptr<Controller>> _fans;
226 std::vector<std::unique_ptr<Controller>> _thermals;
ykchiu7c6d35d2023-05-10 17:01:46 +0800227
228 std::map<std::string, std::unique_ptr<ProcessObject>> _pidsControlProcess;
ykchiu9fe3a3c2023-05-11 13:43:54 +0800229 /*
230 * <key = pidname, value = pid failsafe percent>
231 * Pid fail safe Percent setting by each pid controller configuration.
232 */
233 std::map<std::string, double> _pidsFailSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800234};
Patrick Venturea0764872020-08-08 07:48:43 -0700235
236} // namespace pid_control