xref: /openbmc/phosphor-pid-control/pid/zone.hpp (revision 3484beda)
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 
9 #include <fstream>
10 #include <map>
11 #include <memory>
12 #include <sdbusplus/bus.hpp>
13 #include <sdbusplus/server.hpp>
14 #include <set>
15 #include <string>
16 #include <vector>
17 #include <xyz/openbmc_project/Control/Mode/server.hpp>
18 
19 template <typename... T>
20 using ServerObject = typename sdbusplus::server::object::object<T...>;
21 using ModeInterface = sdbusplus::xyz::openbmc_project::Control::server::Mode;
22 using ModeObject = ServerObject<ModeInterface>;
23 
24 class ZoneInterface
25 {
26   public:
27     virtual ~ZoneInterface() = default;
28 
29     virtual double getCachedValue(const std::string& name) = 0;
30     virtual void addRPMSetPoint(double setpoint) = 0;
31     virtual void addRPMCeiling(double ceiling) = 0;
32     virtual double getMaxRPMRequest() const = 0;
33     virtual bool getFailSafeMode() const = 0;
34     virtual double getFailSafePercent() const = 0;
35     virtual Sensor* getSensor(const std::string& name) = 0;
36 };
37 
38 /*
39  * The PIDZone inherits from the Mode object so that it can listen for control
40  * mode changes.  It primarily holds all PID loops and holds the sensor value
41  * cache that's used per iteration of the PID loops.
42  */
43 class PIDZone : public ZoneInterface, public ModeObject
44 {
45   public:
46     PIDZone(int64_t zone, double minThermalOutput, double failSafePercent,
47             const SensorManager& mgr, sdbusplus::bus::bus& bus,
48             const char* objPath, bool defer) :
49         ModeObject(bus, objPath, defer),
50         _zoneId(zone), _maximumRPMSetPt(),
51         _minThermalOutputSetPt(minThermalOutput),
52         _failSafePercent(failSafePercent), _mgr(mgr)
53     {
54 #ifdef __TUNING_LOGGING__
55         _log.open("/tmp/swampd.log");
56 #endif
57     }
58 
59     double getMaxRPMRequest(void) const override;
60     bool getManualMode(void) const;
61 
62     /* Could put lock around this since it's accessed from two threads, but
63      * only one reader/one writer.
64      */
65     void setManualMode(bool mode);
66     bool getFailSafeMode(void) const override;
67     int64_t getZoneID(void) const;
68     void addRPMSetPoint(double setpoint) override;
69     void addRPMCeiling(double ceiling) override;
70     void clearRPMSetPoints(void);
71     void clearRPMCeilings(void);
72     double getFailSafePercent(void) const override;
73     double getMinThermalRPMSetpoint(void) const;
74 
75     Sensor* getSensor(const std::string& name) override;
76     void determineMaxRPMRequest(void);
77     void updateFanTelemetry(void);
78     void updateSensors(void);
79     void initializeCache(void);
80     void dumpCache(void);
81     void processFans(void);
82     void processThermals(void);
83 
84     void addFanPID(std::unique_ptr<Controller> pid);
85     void addThermalPID(std::unique_ptr<Controller> pid);
86     double getCachedValue(const std::string& name) override;
87     void addFanInput(const std::string& fan);
88     void addThermalInput(const std::string& therm);
89 
90 #ifdef __TUNING_LOGGING__
91     void initializeLog(void);
92     std::ofstream& getLogHandle(void);
93 #endif
94 
95     /* Method for setting the manual mode over dbus */
96     bool manual(bool value) override;
97     /* Method for reading whether in fail-safe mode over dbus */
98     bool failSafe() const override;
99 
100   private:
101 #ifdef __TUNING_LOGGING__
102     std::ofstream _log;
103 #endif
104 
105     const int64_t _zoneId;
106     double _maximumRPMSetPt = 0;
107     bool _manualMode = false;
108     const double _minThermalOutputSetPt;
109     const double _failSafePercent;
110 
111     std::set<std::string> _failSafeSensors;
112 
113     std::vector<double> _RPMSetPoints;
114     std::vector<double> _RPMCeilings;
115     std::vector<std::string> _fanInputs;
116     std::vector<std::string> _thermalInputs;
117     std::map<std::string, double> _cachedValuesByName;
118     const SensorManager& _mgr;
119 
120     std::vector<std::unique_ptr<Controller>> _fans;
121     std::vector<std::unique_ptr<Controller>> _thermals;
122 };
123