blob: 53d90fd346f6ef93cf644776287afc4aeba35465 [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>
ykchiu7c6d35d2023-05-10 17:01:46 +080014#include <xyz/openbmc_project/Object/Enable/server.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070015
Patrick Ventured8012182018-03-08 08:21:38 -080016#include <fstream>
Tom Tungdf1f1832022-11-14 19:26:52 +080017#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080018#include <map>
19#include <memory>
20#include <set>
21#include <string>
22#include <vector>
23
Patrick Ventured8012182018-03-08 08:21:38 -080024template <typename... T>
Patrick Williamsb228bc32022-07-22 19:26:56 -050025using ServerObject = typename sdbusplus::server::object_t<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070026using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Patrick Ventured8012182018-03-08 08:21:38 -080027using ModeObject = ServerObject<ModeInterface>;
ykchiu7c6d35d2023-05-10 17:01:46 +080028using ProcessInterface =
29 sdbusplus::xyz::openbmc_project::Object::server::Enable;
30using ProcessObject = ServerObject<ProcessInterface>;
Patrick Ventured8012182018-03-08 08:21:38 -080031
Patrick Venturea0764872020-08-08 07:48:43 -070032namespace pid_control
33{
34
Patrick Ventured8012182018-03-08 08:21:38 -080035/*
Patrick Venture597ebd62020-08-11 08:48:19 -070036 * The DbusPidZone inherits from the Mode object so that it can listen for
37 * control mode changes. It primarily holds all PID loops and holds the sensor
38 * value cache that's used per iteration of the PID loops.
Patrick Ventured8012182018-03-08 08:21:38 -080039 */
Patrick Venture597ebd62020-08-11 08:48:19 -070040class DbusPidZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080041{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042 public:
Patrick Venture597ebd62020-08-11 08:48:19 -070043 DbusPidZone(int64_t zone, double minThermalOutput, double failSafePercent,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080044 conf::CycleTime cycleTime, const SensorManager& mgr,
45 sdbusplus::bus_t& bus, const char* objPath, bool defer) :
Patrick Williamsd8c5a452022-04-07 16:08:56 -050046 ModeObject(bus, objPath,
47 defer ? ModeObject::action::defer_emit
48 : ModeObject::action::emit_object_added),
Patrick Venturef7a2dd52019-07-16 14:31:13 -070049 _zoneId(zone), _maximumSetPoint(),
James Feist3484bed2019-02-25 13:28:18 -080050 _minThermalOutputSetPt(minThermalOutput),
ykchiu9fe3a3c2023-05-11 13:43:54 +080051 _zoneFailSafePercent(failSafePercent), _cycleTime(cycleTime), _mgr(mgr)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070052 {
Patrick Venturede79ee02019-05-08 14:50:00 -070053 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -080054 {
Patrick Venture89002db2019-05-08 15:02:55 -070055 _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
Patrick Venturec32e3fc2019-02-28 10:01:11 -080056 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057 }
Patrick Ventured8012182018-03-08 08:21:38 -080058
Patrick Venture7a98c192020-08-12 08:35:16 -070059 bool getManualMode(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070060 /* Could put lock around this since it's accessed from two threads, but
61 * only one reader/one writer.
62 */
Josh Lehana4146eb2020-10-01 11:49:09 -070063
64 bool getRedundantWrite(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070065 void setManualMode(bool mode);
66 bool getFailSafeMode(void) const override;
Patrick Venture7a98c192020-08-12 08:35:16 -070067
Bonnie Loc51ba912022-10-12 14:07:22 +080068 int64_t getZoneID(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080069 void addSetPoint(double setPoint, const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070070 double getMaxSetPointRequest(void) const override;
James Feist608304d2019-02-25 10:01:42 -080071 void addRPMCeiling(double ceiling) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070072 void clearSetPoints(void) override;
73 void clearRPMCeilings(void) override;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080074 double getFailSafePercent(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080075 double getMinThermalSetPoint(void) const;
Bonnie Lo0e8fc392022-10-05 10:20:55 +080076 uint64_t getCycleIntervalTime(void) const override;
77 uint64_t getUpdateThermalsCycle(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080078
Patrick Venture2d8e7852018-10-30 19:14:07 -070079 Sensor* getSensor(const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070080 void determineMaxSetPointRequest(void) override;
81 void updateFanTelemetry(void) override;
82 void updateSensors(void) override;
83 void initializeCache(void) override;
Josh Lehanb3005752022-02-22 20:48:07 -080084 void setOutputCache(std::string_view, const ValueCacheEntry&) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070085 void dumpCache(void);
Josh Lehana4146eb2020-10-01 11:49:09 -070086
Patrick Venture7a98c192020-08-12 08:35:16 -070087 void processFans(void) override;
88 void processThermals(void) override;
Patrick Ventured8012182018-03-08 08:21:38 -080089
James Feist22c257a2018-08-31 14:07:12 -070090 void addFanPID(std::unique_ptr<Controller> pid);
91 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070092 double getCachedValue(const std::string& name) override;
Josh Lehanb3005752022-02-22 20:48:07 -080093 ValueCacheEntry getCachedValues(const std::string& name) override;
94
Patrick Venturec399f6f2018-10-30 19:24:47 -070095 void addFanInput(const std::string& fan);
96 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -080097
Patrick Venture7a98c192020-08-12 08:35:16 -070098 void initializeLog(void) override;
99 void writeLog(const std::string& value) override;
Patrick Ventured8012182018-03-08 08:21:38 -0800100
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700101 /* Method for setting the manual mode over dbus */
102 bool manual(bool value) override;
103 /* Method for reading whether in fail-safe mode over dbus */
104 bool failSafe() const override;
ykchiu7c6d35d2023-05-10 17:01:46 +0800105 /* Method for control process for each loop at runtime */
106 void addPidControlProcess(std::string name, sdbusplus::bus_t& bus,
107 std::string objPath, bool defer);
108 bool isPidProcessEnabled(std::string name);
Patrick Ventured8012182018-03-08 08:21:38 -0800109
ykchiu9fe3a3c2023-05-11 13:43:54 +0800110 void initPidFailSafePercent(void);
111 void addPidFailSafePercent(std::string name, double percent);
112
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700113 private:
Tom Tungdf1f1832022-11-14 19:26:52 +0800114 template <bool fanSensorLogging>
115 void processSensorInputs(const std::vector<std::string>& sensorInputs,
116 std::chrono::high_resolution_clock::time_point now)
117 {
118 for (const auto& sensorInput : sensorInputs)
119 {
120 auto sensor = _mgr.getSensor(sensorInput);
121 ReadReturn r = sensor->read();
122 _cachedValuesByName[sensorInput] = {r.value, r.unscaled};
123 int64_t timeout = sensor->getTimeout();
124 std::chrono::high_resolution_clock::time_point then = r.updated;
125
126 auto duration =
127 std::chrono::duration_cast<std::chrono::seconds>(now - then)
128 .count();
129 auto period = std::chrono::seconds(timeout).count();
130 /*
131 * TODO(venture): We should check when these were last read.
132 * However, these are the fans, so if I'm not getting updated values
133 * for them... what should I do?
134 */
135 if constexpr (fanSensorLogging)
136 {
137 if (loggingEnabled)
138 {
139 const auto& v = _cachedValuesByName[sensorInput];
140 _log << "," << v.scaled << "," << v.unscaled;
141 const auto& p = _cachedFanOutputs[sensorInput];
142 _log << "," << p.scaled << "," << p.unscaled;
143 }
144 }
145
146 if (debugEnabled)
147 {
148 std::cerr << sensorInput << " sensor reading: " << r.value
149 << "\n";
150 }
151
152 // check if fan fail.
153 if (sensor->getFailed())
154 {
155 _failSafeSensors.insert(sensorInput);
156 if (debugEnabled)
157 {
158 std::cerr << sensorInput << " sensor get failed\n";
159 }
160 }
161 else if (timeout != 0 && duration >= period)
162 {
163 _failSafeSensors.insert(sensorInput);
164 if (debugEnabled)
165 {
166 std::cerr << sensorInput << " sensor timeout\n";
167 }
168 }
169 else
170 {
171 // Check if it's in there: remove it.
172 auto kt = _failSafeSensors.find(sensorInput);
173 if (kt != _failSafeSensors.end())
174 {
175 if (debugEnabled)
176 {
177 std::cerr << sensorInput
178 << " is erased from failsafe sensor set\n";
179 }
180 _failSafeSensors.erase(kt);
181 }
182 }
183 }
184 }
185
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700186 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800187
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700188 const int64_t _zoneId;
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700189 double _maximumSetPoint = 0;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800190 std::string _maximumSetPointName;
191 std::string _maximumSetPointNamePrev;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700192 bool _manualMode = false;
Josh Lehana4146eb2020-10-01 11:49:09 -0700193 bool _redundantWrite = false;
James Feist3484bed2019-02-25 13:28:18 -0800194 const double _minThermalOutputSetPt;
ykchiu9fe3a3c2023-05-11 13:43:54 +0800195 // Current fail safe Percent.
196 double _failSafePercent;
197 // Zone fail safe Percent setting by configuration.
198 const double _zoneFailSafePercent;
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800199 const conf::CycleTime _cycleTime;
Patrick Ventured8012182018-03-08 08:21:38 -0800200
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700201 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800202
Patrick Venture9bbf3332019-07-16 10:50:37 -0700203 std::vector<double> _SetPoints;
James Feist608304d2019-02-25 10:01:42 -0800204 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700205 std::vector<std::string> _fanInputs;
206 std::vector<std::string> _thermalInputs;
Josh Lehanb3005752022-02-22 20:48:07 -0800207 std::map<std::string, ValueCacheEntry> _cachedValuesByName;
208 std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700209 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800210
James Feist22c257a2018-08-31 14:07:12 -0700211 std::vector<std::unique_ptr<Controller>> _fans;
212 std::vector<std::unique_ptr<Controller>> _thermals;
ykchiu7c6d35d2023-05-10 17:01:46 +0800213
214 std::map<std::string, std::unique_ptr<ProcessObject>> _pidsControlProcess;
ykchiu9fe3a3c2023-05-11 13:43:54 +0800215 /*
216 * <key = pidname, value = pid failsafe percent>
217 * Pid fail safe Percent setting by each pid controller configuration.
218 */
219 std::map<std::string, double> _pidsFailSafePercent;
Patrick Ventured8012182018-03-08 08:21:38 -0800220};
Patrick Venturea0764872020-08-08 07:48:43 -0700221
222} // namespace pid_control