xref: /openbmc/phosphor-pid-control/pid/zone.hpp (revision efda1ced)
1 #pragma once
2 
3 #include "conf.hpp"
4 #include "controller.hpp"
5 #include "pidcontroller.hpp"
6 #include "sensors/manager.hpp"
7 #include "sensors/sensor.hpp"
8 #include "tuning.hpp"
9 #include "zone_interface.hpp"
10 
11 #include <sdbusplus/bus.hpp>
12 #include <sdbusplus/server.hpp>
13 #include <xyz/openbmc_project/Control/Mode/server.hpp>
14 #include <xyz/openbmc_project/Debug/Pid/ThermalPower/server.hpp>
15 #include <xyz/openbmc_project/Debug/Pid/Zone/server.hpp>
16 #include <xyz/openbmc_project/Object/Enable/server.hpp>
17 
18 #include <fstream>
19 #include <iostream>
20 #include <map>
21 #include <memory>
22 #include <set>
23 #include <string>
24 #include <vector>
25 
26 template <typename... T>
27 using ServerObject = typename sdbusplus::server::object_t<T...>;
28 using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
29 using DebugZoneInterface =
30     sdbusplus::xyz::openbmc_project::Debug::Pid::server::Zone;
31 using ModeObject = ServerObject<ModeInterface, DebugZoneInterface>;
32 using ProcessInterface =
33     sdbusplus::xyz::openbmc_project::Object::server::Enable;
34 using DebugThermalPowerInterface =
35     sdbusplus::xyz::openbmc_project::Debug::Pid::server::ThermalPower;
36 using ProcessObject =
37     ServerObject<ProcessInterface, DebugThermalPowerInterface>;
38 
39 namespace pid_control
40 {
41 
42 /*
43  * 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.
46  */
47 class DbusPidZone : public ZoneInterface, public ModeObject
48 {
49   public:
50     DbusPidZone(int64_t zone, double minThermalOutput, double failSafePercent,
51                 conf::CycleTime cycleTime, const SensorManager& mgr,
52                 sdbusplus::bus_t& bus, const char* objPath, bool defer) :
53         ModeObject(bus, objPath,
54                    defer ? ModeObject::action::defer_emit
55                          : ModeObject::action::emit_object_added),
56         _zoneId(zone), _maximumSetPoint(),
57         _minThermalOutputSetPt(minThermalOutput),
58         _zoneFailSafePercent(failSafePercent), _cycleTime(cycleTime), _mgr(mgr)
59     {
60         if (loggingEnabled)
61         {
62             _log.open(loggingPath + "/zone_" + std::to_string(zone) + ".log");
63         }
64     }
65 
66     bool getManualMode(void) const override;
67     /* Could put lock around this since it's accessed from two threads, but
68      * only one reader/one writer.
69      */
70 
71     bool getRedundantWrite(void) const override;
72     void setManualMode(bool mode);
73     bool getFailSafeMode(void) const override;
74     void markSensorMissing(const std::string& name);
75 
76     int64_t getZoneID(void) const override;
77     void addSetPoint(double setPoint, const std::string& name) override;
78     double getMaxSetPointRequest(void) const override;
79     void addRPMCeiling(double ceiling) override;
80     void clearSetPoints(void) override;
81     void clearRPMCeilings(void) override;
82     double getFailSafePercent(void) const override;
83     double getMinThermalSetPoint(void) const;
84     uint64_t getCycleIntervalTime(void) const override;
85     uint64_t getUpdateThermalsCycle(void) const override;
86 
87     Sensor* getSensor(const std::string& name) override;
88     void determineMaxSetPointRequest(void) override;
89     void updateFanTelemetry(void) override;
90     void updateSensors(void) override;
91     void initializeCache(void) override;
92     void setOutputCache(std::string_view, const ValueCacheEntry&) override;
93     void dumpCache(void);
94 
95     void processFans(void) override;
96     void processThermals(void) override;
97 
98     void addFanPID(std::unique_ptr<Controller> pid);
99     void addThermalPID(std::unique_ptr<Controller> pid);
100     double getCachedValue(const std::string& name) override;
101     ValueCacheEntry getCachedValues(const std::string& name) override;
102 
103     void addFanInput(const std::string& fan, bool missingAcceptable);
104     void addThermalInput(const std::string& therm, bool missingAcceptable);
105 
106     void initializeLog(void) override;
107     void writeLog(const std::string& value) override;
108 
109     /* Method for setting the manual mode over dbus */
110     bool manual(bool value) override;
111     /* Method for reading whether in fail-safe mode over dbus */
112     bool failSafe() const override;
113     /* Method for recording the maximum SetPoint PID config name */
114     std::string leader() const override;
115     /* Method for control process for each loop at runtime */
116     void addPidControlProcess(std::string name, std::string type,
117                               double setpoint, sdbusplus::bus_t& bus,
118                               std::string objPath, bool defer);
119     bool isPidProcessEnabled(std::string name);
120 
121     void initPidFailSafePercent(void);
122     void addPidFailSafePercent(std::string name, double percent);
123 
124     void updateThermalPowerDebugInterface(std::string pidName,
125                                           std::string leader, double input,
126                                           double output) override;
127 
128   private:
129     template <bool fanSensorLogging>
130     void processSensorInputs(const std::vector<std::string>& sensorInputs,
131                              std::chrono::high_resolution_clock::time_point now)
132     {
133         for (const auto& sensorInput : sensorInputs)
134         {
135             auto sensor = _mgr.getSensor(sensorInput);
136             ReadReturn r = sensor->read();
137             _cachedValuesByName[sensorInput] = {r.value, r.unscaled};
138             int64_t timeout = sensor->getTimeout();
139             std::chrono::high_resolution_clock::time_point then = r.updated;
140 
141             auto duration =
142                 std::chrono::duration_cast<std::chrono::seconds>(now - then)
143                     .count();
144             auto period = std::chrono::seconds(timeout).count();
145             /*
146              * TODO(venture): We should check when these were last read.
147              * However, these are the fans, so if I'm not getting updated values
148              * for them... what should I do?
149              */
150             if constexpr (fanSensorLogging)
151             {
152                 if (loggingEnabled)
153                 {
154                     const auto& v = _cachedValuesByName[sensorInput];
155                     _log << "," << v.scaled << "," << v.unscaled;
156                     const auto& p = _cachedFanOutputs[sensorInput];
157                     _log << "," << p.scaled << "," << p.unscaled;
158                 }
159             }
160 
161             if (debugEnabled)
162             {
163                 std::cerr << sensorInput << " sensor reading: " << r.value
164                           << "\n";
165             }
166 
167             // check if fan fail.
168             if (sensor->getFailed())
169             {
170                 markSensorMissing(sensorInput);
171 
172                 if (debugEnabled)
173                 {
174                     std::cerr << sensorInput << " sensor get failed\n";
175                 }
176             }
177             else if (timeout != 0 && duration >= period)
178             {
179                 markSensorMissing(sensorInput);
180 
181                 if (debugEnabled)
182                 {
183                     std::cerr << sensorInput << " sensor timeout\n";
184                 }
185             }
186             else
187             {
188                 // Check if it's in there: remove it.
189                 auto kt = _failSafeSensors.find(sensorInput);
190                 if (kt != _failSafeSensors.end())
191                 {
192                     if (debugEnabled)
193                     {
194                         std::cerr << sensorInput
195                                   << " is erased from failsafe sensor set\n";
196                     }
197 
198                     _failSafeSensors.erase(kt);
199                 }
200             }
201         }
202     }
203 
204     std::ofstream _log;
205 
206     const int64_t _zoneId;
207     double _maximumSetPoint = 0;
208     std::string _maximumSetPointName;
209     std::string _maximumSetPointNamePrev;
210     bool _manualMode = false;
211     bool _redundantWrite = false;
212     const double _minThermalOutputSetPt;
213     // Current fail safe Percent.
214     double _failSafePercent;
215     // Zone fail safe Percent setting by configuration.
216     const double _zoneFailSafePercent;
217     const conf::CycleTime _cycleTime;
218 
219     std::set<std::string> _failSafeSensors;
220     std::set<std::string> _missingAcceptable;
221 
222     std::vector<double> _SetPoints;
223     std::vector<double> _RPMCeilings;
224     std::vector<std::string> _fanInputs;
225     std::vector<std::string> _thermalInputs;
226     std::map<std::string, ValueCacheEntry> _cachedValuesByName;
227     std::map<std::string, ValueCacheEntry> _cachedFanOutputs;
228     const SensorManager& _mgr;
229 
230     std::vector<std::unique_ptr<Controller>> _fans;
231     std::vector<std::unique_ptr<Controller>> _thermals;
232 
233     std::map<std::string, std::unique_ptr<ProcessObject>> _pidsControlProcess;
234     /*
235      * <key = pidname, value = pid failsafe percent>
236      * Pid fail safe Percent setting by each pid controller configuration.
237      */
238     std::map<std::string, double> _pidsFailSafePercent;
239 };
240 
241 } // namespace pid_control
242