blob: b985f5f49e3dc7b2c1d5d05f7efcf015b0815cb3 [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>
14
Patrick Ventured8012182018-03-08 08:21:38 -080015#include <fstream>
Tom Tungdf1f1832022-11-14 19:26:52 +080016#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080017#include <map>
18#include <memory>
19#include <set>
20#include <string>
21#include <vector>
22
Patrick Ventured8012182018-03-08 08:21:38 -080023template <typename... T>
Patrick Williamsb228bc32022-07-22 19:26:56 -050024using ServerObject = typename sdbusplus::server::object_t<T...>;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
Patrick Ventured8012182018-03-08 08:21:38 -080026using ModeObject = ServerObject<ModeInterface>;
27
Patrick Venturea0764872020-08-08 07:48:43 -070028namespace pid_control
29{
30
Patrick Ventured8012182018-03-08 08:21:38 -080031/*
Patrick Venture597ebd62020-08-11 08:48:19 -070032 * The DbusPidZone inherits from the Mode object so that it can listen for
33 * control mode changes. It primarily holds all PID loops and holds the sensor
34 * value cache that's used per iteration of the PID loops.
Patrick Ventured8012182018-03-08 08:21:38 -080035 */
Patrick Venture597ebd62020-08-11 08:48:19 -070036class DbusPidZone : public ZoneInterface, public ModeObject
Patrick Ventured8012182018-03-08 08:21:38 -080037{
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070038 public:
Patrick Venture597ebd62020-08-11 08:48:19 -070039 DbusPidZone(int64_t zone, double minThermalOutput, double failSafePercent,
Bonnie Lo0e8fc392022-10-05 10:20:55 +080040 conf::CycleTime cycleTime, const SensorManager& mgr,
41 sdbusplus::bus_t& bus, const char* objPath, bool defer) :
Patrick Williamsd8c5a452022-04-07 16:08:56 -050042 ModeObject(bus, objPath,
43 defer ? ModeObject::action::defer_emit
44 : ModeObject::action::emit_object_added),
Patrick Venturef7a2dd52019-07-16 14:31:13 -070045 _zoneId(zone), _maximumSetPoint(),
James Feist3484bed2019-02-25 13:28:18 -080046 _minThermalOutputSetPt(minThermalOutput),
Bonnie Lo0e8fc392022-10-05 10:20:55 +080047 _failSafePercent(failSafePercent), _cycleTime(cycleTime), _mgr(mgr)
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070048 {
Patrick Venturede79ee02019-05-08 14:50:00 -070049 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -080050 {
Patrick Venture89002db2019-05-08 15:02:55 -070051 _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
Patrick Venturec32e3fc2019-02-28 10:01:11 -080052 }
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070053 }
Patrick Ventured8012182018-03-08 08:21:38 -080054
Patrick Venture7a98c192020-08-12 08:35:16 -070055 bool getManualMode(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070056 /* Could put lock around this since it's accessed from two threads, but
57 * only one reader/one writer.
58 */
Josh Lehana4146eb2020-10-01 11:49:09 -070059
60 bool getRedundantWrite(void) const override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070061 void setManualMode(bool mode);
62 bool getFailSafeMode(void) const override;
Patrick Venture7a98c192020-08-12 08:35:16 -070063
Bonnie Loc51ba912022-10-12 14:07:22 +080064 int64_t getZoneID(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080065 void addSetPoint(double setPoint, const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070066 double getMaxSetPointRequest(void) const override;
James Feist608304d2019-02-25 10:01:42 -080067 void addRPMCeiling(double ceiling) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070068 void clearSetPoints(void) override;
69 void clearRPMCeilings(void) override;
Patrick Venture5f59c0f2018-11-11 12:55:14 -080070 double getFailSafePercent(void) const override;
Nirav Shahccc8bb62022-02-17 21:06:51 -080071 double getMinThermalSetPoint(void) const;
Bonnie Lo0e8fc392022-10-05 10:20:55 +080072 uint64_t getCycleIntervalTime(void) const override;
73 uint64_t getUpdateThermalsCycle(void) const override;
Patrick Ventured8012182018-03-08 08:21:38 -080074
Patrick Venture2d8e7852018-10-30 19:14:07 -070075 Sensor* getSensor(const std::string& name) override;
Patrick Venture7a98c192020-08-12 08:35:16 -070076 void determineMaxSetPointRequest(void) override;
77 void updateFanTelemetry(void) override;
78 void updateSensors(void) override;
79 void initializeCache(void) override;
Josh Lehanb3005752022-02-22 20:48:07 -080080 void setOutputCache(std::string_view, const ValueCacheEntry&) override;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070081 void dumpCache(void);
Josh Lehana4146eb2020-10-01 11:49:09 -070082
Patrick Venture7a98c192020-08-12 08:35:16 -070083 void processFans(void) override;
84 void processThermals(void) override;
Patrick Ventured8012182018-03-08 08:21:38 -080085
James Feist22c257a2018-08-31 14:07:12 -070086 void addFanPID(std::unique_ptr<Controller> pid);
87 void addThermalPID(std::unique_ptr<Controller> pid);
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070088 double getCachedValue(const std::string& name) override;
Josh Lehanb3005752022-02-22 20:48:07 -080089 ValueCacheEntry getCachedValues(const std::string& name) override;
90
Patrick Venturec399f6f2018-10-30 19:24:47 -070091 void addFanInput(const std::string& fan);
92 void addThermalInput(const std::string& therm);
Patrick Ventured8012182018-03-08 08:21:38 -080093
Patrick Venture7a98c192020-08-12 08:35:16 -070094 void initializeLog(void) override;
95 void writeLog(const std::string& value) override;
Patrick Ventured8012182018-03-08 08:21:38 -080096
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070097 /* Method for setting the manual mode over dbus */
98 bool manual(bool value) override;
99 /* Method for reading whether in fail-safe mode over dbus */
100 bool failSafe() const override;
Patrick Ventured8012182018-03-08 08:21:38 -0800101
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700102 private:
Tom Tungdf1f1832022-11-14 19:26:52 +0800103 template <bool fanSensorLogging>
104 void processSensorInputs(const std::vector<std::string>& sensorInputs,
105 std::chrono::high_resolution_clock::time_point now)
106 {
107 for (const auto& sensorInput : sensorInputs)
108 {
109 auto sensor = _mgr.getSensor(sensorInput);
110 ReadReturn r = sensor->read();
111 _cachedValuesByName[sensorInput] = {r.value, r.unscaled};
112 int64_t timeout = sensor->getTimeout();
113 std::chrono::high_resolution_clock::time_point then = r.updated;
114
115 auto duration =
116 std::chrono::duration_cast<std::chrono::seconds>(now - then)
117 .count();
118 auto period = std::chrono::seconds(timeout).count();
119 /*
120 * TODO(venture): We should check when these were last read.
121 * However, these are the fans, so if I'm not getting updated values
122 * for them... what should I do?
123 */
124 if constexpr (fanSensorLogging)
125 {
126 if (loggingEnabled)
127 {
128 const auto& v = _cachedValuesByName[sensorInput];
129 _log << "," << v.scaled << "," << v.unscaled;
130 const auto& p = _cachedFanOutputs[sensorInput];
131 _log << "," << p.scaled << "," << p.unscaled;
132 }
133 }
134
135 if (debugEnabled)
136 {
137 std::cerr << sensorInput << " sensor reading: " << r.value
138 << "\n";
139 }
140
141 // check if fan fail.
142 if (sensor->getFailed())
143 {
144 _failSafeSensors.insert(sensorInput);
145 if (debugEnabled)
146 {
147 std::cerr << sensorInput << " sensor get failed\n";
148 }
149 }
150 else if (timeout != 0 && duration >= period)
151 {
152 _failSafeSensors.insert(sensorInput);
153 if (debugEnabled)
154 {
155 std::cerr << sensorInput << " sensor timeout\n";
156 }
157 }
158 else
159 {
160 // Check if it's in there: remove it.
161 auto kt = _failSafeSensors.find(sensorInput);
162 if (kt != _failSafeSensors.end())
163 {
164 if (debugEnabled)
165 {
166 std::cerr << sensorInput
167 << " is erased from failsafe sensor set\n";
168 }
169 _failSafeSensors.erase(kt);
170 }
171 }
172 }
173 }
174
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700175 std::ofstream _log;
Patrick Ventured8012182018-03-08 08:21:38 -0800176
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700177 const int64_t _zoneId;
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700178 double _maximumSetPoint = 0;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800179 std::string _maximumSetPointName;
180 std::string _maximumSetPointNamePrev;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700181 bool _manualMode = false;
Josh Lehana4146eb2020-10-01 11:49:09 -0700182 bool _redundantWrite = false;
James Feist3484bed2019-02-25 13:28:18 -0800183 const double _minThermalOutputSetPt;
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800184 const double _failSafePercent;
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800185 const conf::CycleTime _cycleTime;
Patrick Ventured8012182018-03-08 08:21:38 -0800186
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700187 std::set<std::string> _failSafeSensors;
Patrick Ventured8012182018-03-08 08:21:38 -0800188
Patrick Venture9bbf3332019-07-16 10:50:37 -0700189 std::vector<double> _SetPoints;
James Feist608304d2019-02-25 10:01:42 -0800190 std::vector<double> _RPMCeilings;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700191 std::vector<std::string> _fanInputs;
192 std::vector<std::string> _thermalInputs;
Josh Lehanb3005752022-02-22 20:48:07 -0800193 std::map<std::string, ValueCacheEntry> _cachedValuesByName;
194 std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
Patrick Ventureda4a5dd2018-08-31 09:42:48 -0700195 const SensorManager& _mgr;
Patrick Ventured8012182018-03-08 08:21:38 -0800196
James Feist22c257a2018-08-31 14:07:12 -0700197 std::vector<std::unique_ptr<Controller>> _fans;
198 std::vector<std::unique_ptr<Controller>> _thermals;
Patrick Ventured8012182018-03-08 08:21:38 -0800199};
Patrick Venturea0764872020-08-08 07:48:43 -0700200
201} // namespace pid_control