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