xref: /openbmc/phosphor-pid-control/pid/zone.cpp (revision d59ccac3052dfa739c7814090f53ee613ee7ddc3)
1 /**
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 /* Configuration. */
18 #include "zone.hpp"
19 
20 #include "conf.hpp"
21 #include "failsafeloggers/failsafe_logger_utility.hpp"
22 #include "pid/controller.hpp"
23 #include "pid/ec/pid.hpp"
24 #include "pid/fancontroller.hpp"
25 #include "pid/stepwisecontroller.hpp"
26 #include "pid/thermalcontroller.hpp"
27 #include "pid/tuning.hpp"
28 
29 #include <algorithm>
30 #include <chrono>
31 #include <cstring>
32 #include <fstream>
33 #include <iostream>
34 #include <memory>
35 #include <sstream>
36 #include <string>
37 
38 using tstamp = std::chrono::high_resolution_clock::time_point;
39 using namespace std::literals::chrono_literals;
40 
41 // Enforces minimum duration between events
42 // Rreturns true if event should be allowed, false if disallowed
allowThrottle(const tstamp & now,const std::chrono::seconds & pace)43 bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace)
44 {
45     static tstamp then;
46     static bool first = true;
47 
48     if (first)
49     {
50         // Special case initialization
51         then = now;
52         first = false;
53 
54         // Initialization, always allow
55         return true;
56     }
57 
58     auto elapsed = now - then;
59     if (elapsed < pace)
60     {
61         // Too soon since last time, disallow
62         return false;
63     }
64 
65     // It has been long enough, allow
66     then = now;
67     return true;
68 }
69 
70 namespace pid_control
71 {
72 
getMaxSetPointRequest(void) const73 double DbusPidZone::getMaxSetPointRequest(void) const
74 {
75     return _maximumSetPoint;
76 }
77 
getManualMode(void) const78 bool DbusPidZone::getManualMode(void) const
79 {
80     return _manualMode;
81 }
82 
setManualMode(bool mode)83 void DbusPidZone::setManualMode(bool mode)
84 {
85     _manualMode = mode;
86 
87     // If returning to automatic mode, need to restore PWM from PID loop
88     if (!mode)
89     {
90         _redundantWrite = true;
91     }
92 }
93 
getFailSafeMode(void) const94 bool DbusPidZone::getFailSafeMode(void) const
95 {
96     // If any keys are present at least one sensor is in fail safe mode.
97     return !_failSafeSensors.empty();
98 }
99 
getFailSafeSensors(void) const100 FailSafeSensorsMap DbusPidZone::getFailSafeSensors(void) const
101 {
102     return _failSafeSensors;
103 }
104 
markSensorMissing(const std::string & name,const std::string & failReason)105 void DbusPidZone::markSensorMissing(const std::string& name,
106                                     const std::string& failReason)
107 {
108     if (_missingAcceptable.find(name) != _missingAcceptable.end())
109     {
110         // Disallow sensors in MissingIsAcceptable list from causing failsafe
111         outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(), name,
112                                   "The sensor is missing but is acceptable.");
113         return;
114     }
115 
116     if (_sensorFailSafePercent[name] == 0)
117     {
118         _failSafeSensors[name] = std::pair(failReason, _zoneFailSafePercent);
119     }
120     else
121     {
122         _failSafeSensors[name] =
123             std::pair(failReason, _sensorFailSafePercent[name]);
124     }
125 
126     if (debugEnabled)
127     {
128         std::cerr << "Sensor " << name << " marked missing\n";
129     }
130 }
131 
getZoneID(void) const132 int64_t DbusPidZone::getZoneID(void) const
133 {
134     return _zoneId;
135 }
136 
addSetPoint(double setPoint,const std::string & name)137 void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
138 {
139     /* exclude disabled pidloop from _maximumSetPoint calculation*/
140     if (!isPidProcessEnabled(name))
141     {
142         return;
143     }
144 
145     auto profileName = name;
146     if (getAccSetPoint())
147     {
148         /*
149          * If the name of controller is Linear_Temp_CPU0.
150          * The profile name will be Temp_CPU0.
151          */
152         profileName = name.substr(name.find("_") + 1);
153         _SetPoints[profileName] += setPoint;
154     }
155     else
156     {
157         if (_SetPoints[profileName] < setPoint)
158         {
159             _SetPoints[profileName] = setPoint;
160         }
161     }
162 
163     /*
164      * if there are multiple thermal controllers with the same
165      * value, pick the first one in the iterator
166      */
167     if (_maximumSetPoint < _SetPoints[profileName])
168     {
169         _maximumSetPoint = _SetPoints[profileName];
170         _maximumSetPointName = profileName;
171     }
172 }
173 
addRPMCeiling(double ceiling)174 void DbusPidZone::addRPMCeiling(double ceiling)
175 {
176     _RPMCeilings.push_back(ceiling);
177 }
178 
clearRPMCeilings(void)179 void DbusPidZone::clearRPMCeilings(void)
180 {
181     _RPMCeilings.clear();
182 }
183 
clearSetPoints(void)184 void DbusPidZone::clearSetPoints(void)
185 {
186     _SetPoints.clear();
187     _maximumSetPoint = 0;
188     _maximumSetPointName.clear();
189 }
190 
getFailSafePercent(void)191 double DbusPidZone::getFailSafePercent(void)
192 {
193     if (_failSafeSensors.empty())
194     {
195         return _zoneFailSafePercent;
196     }
197 
198     FailSafeSensorsMap::iterator maxData = std::max_element(
199         _failSafeSensors.begin(), _failSafeSensors.end(),
200         [](const FailSafeSensorPair firstData,
201            const FailSafeSensorPair secondData) {
202             return firstData.second.second < secondData.second.second;
203         });
204 
205     // In dbus/dbusconfiguration.cpp, the default sensor failsafepercent is 0 if
206     // there is no setting in json.
207     // Therfore, if the max failsafe duty in _failSafeSensors is 0, set final
208     // failsafe duty to _zoneFailSafePercent.
209     if ((*maxData).second.second == 0)
210     {
211         return _zoneFailSafePercent;
212     }
213     else
214     {
215         return (*maxData).second.second;
216     }
217 }
218 
getMinThermalSetPoint(void) const219 double DbusPidZone::getMinThermalSetPoint(void) const
220 {
221     return _minThermalOutputSetPt;
222 }
223 
getCycleIntervalTime(void) const224 uint64_t DbusPidZone::getCycleIntervalTime(void) const
225 {
226     return _cycleTime.cycleIntervalTimeMS;
227 }
228 
getUpdateThermalsCycle(void) const229 uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
230 {
231     return _cycleTime.updateThermalsTimeMS;
232 }
233 
addFanPID(std::unique_ptr<Controller> pid)234 void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
235 {
236     _fans.push_back(std::move(pid));
237 }
238 
addThermalPID(std::unique_ptr<Controller> pid)239 void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
240 {
241     _thermals.push_back(std::move(pid));
242 }
243 
getCachedValue(const std::string & name)244 double DbusPidZone::getCachedValue(const std::string& name)
245 {
246     return _cachedValuesByName.at(name).scaled;
247 }
248 
getCachedValues(const std::string & name)249 ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
250 {
251     return _cachedValuesByName.at(name);
252 }
253 
setOutputCache(std::string_view name,const ValueCacheEntry & values)254 void DbusPidZone::setOutputCache(std::string_view name,
255                                  const ValueCacheEntry& values)
256 {
257     _cachedFanOutputs[std::string{name}] = values;
258 }
259 
addFanInput(const std::string & fan,bool missingAcceptable)260 void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
261 {
262     _fanInputs.push_back(fan);
263 
264     if (missingAcceptable)
265     {
266         _missingAcceptable.emplace(fan);
267     }
268 }
269 
addThermalInput(const std::string & therm,bool missingAcceptable)270 void DbusPidZone::addThermalInput(const std::string& therm,
271                                   bool missingAcceptable)
272 {
273     /*
274      * One sensor may have stepwise and PID at the same time.
275      * Searching the sensor name before inserting it to avoid duplicated sensor
276      * names.
277      */
278     if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) ==
279         _thermalInputs.end())
280     {
281         _thermalInputs.push_back(therm);
282     }
283 
284     if (missingAcceptable)
285     {
286         _missingAcceptable.emplace(therm);
287     }
288 }
289 
290 // Updates desired RPM setpoint from optional text file
291 // Returns true if rpmValue updated, false if left unchanged
fileParseRpm(const std::string & fileName,double & rpmValue)292 static bool fileParseRpm(const std::string& fileName, double& rpmValue)
293 {
294     static constexpr std::chrono::seconds throttlePace{3};
295 
296     std::string errText;
297 
298     try
299     {
300         std::ifstream ifs;
301         ifs.open(fileName);
302         if (ifs)
303         {
304             int value;
305             ifs >> value;
306 
307             if (value <= 0)
308             {
309                 errText = "File content could not be parsed to a number";
310             }
311             else if (value <= 100)
312             {
313                 errText = "File must contain RPM value, not PWM value";
314             }
315             else
316             {
317                 rpmValue = static_cast<double>(value);
318                 return true;
319             }
320         }
321     }
322     catch (const std::exception& e)
323     {
324         errText = "Exception: ";
325         errText += e.what();
326     }
327 
328     // The file is optional, intentionally not an error if file not found
329     if (!(errText.empty()))
330     {
331         tstamp now = std::chrono::high_resolution_clock::now();
332         if (allowThrottle(now, throttlePace))
333         {
334             std::cerr << "Unable to read from '" << fileName << "': " << errText
335                       << "\n";
336         }
337     }
338 
339     return false;
340 }
341 
determineMaxSetPointRequest(void)342 void DbusPidZone::determineMaxSetPointRequest(void)
343 {
344     std::vector<double>::iterator result;
345     double minThermalThreshold = getMinThermalSetPoint();
346 
347     if (_RPMCeilings.size() > 0)
348     {
349         result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
350         // if Max set point is larger than the lowest ceiling, reset to lowest
351         // ceiling.
352         if (*result < _maximumSetPoint)
353         {
354             _maximumSetPoint = *result;
355             // When using lowest ceiling, controller name is ceiling.
356             _maximumSetPointName = "Ceiling";
357         }
358     }
359 
360     /*
361      * Combine the maximum SetPoint Name if the controllers have same profile
362      * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C.
363      */
364     if (getAccSetPoint())
365     {
366         auto profileName = _maximumSetPointName;
367         _maximumSetPointName = "";
368 
369         for (auto& p : _thermals)
370         {
371             auto controllerID = p->getID();
372             auto found = controllerID.find(profileName);
373             if (found != std::string::npos)
374             {
375                 if (_maximumSetPointName.empty())
376                 {
377                     _maximumSetPointName = controllerID;
378                 }
379                 else
380                 {
381                     _maximumSetPointName += " + " + controllerID;
382                 }
383             }
384         }
385     }
386 
387     /*
388      * If the maximum RPM setpoint output is below the minimum RPM
389      * setpoint, set it to the minimum.
390      */
391     if (minThermalThreshold >= _maximumSetPoint)
392     {
393         _maximumSetPoint = minThermalThreshold;
394         _maximumSetPointName = "Minimum";
395     }
396     else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
397     {
398         std::cerr << "PID Zone " << _zoneId << " max SetPoint "
399                   << _maximumSetPoint << " requested by "
400                   << _maximumSetPointName;
401         for (const auto& sensor : _failSafeSensors)
402         {
403             if (sensor.first.find("Fan") == std::string::npos)
404             {
405                 std::cerr << " " << sensor.first;
406             }
407         }
408         std::cerr << "\n";
409         _maximumSetPointNamePrev.assign(_maximumSetPointName);
410     }
411     if (tuningEnabled)
412     {
413         /*
414          * We received no setpoints from thermal sensors.
415          * This is a case experienced during tuning where they only specify
416          * fan sensors and one large fan PID for all the fans.
417          */
418         static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
419 
420         fileParseRpm(setpointpath, _maximumSetPoint);
421 
422         // Allow per-zone setpoint files to override overall setpoint file
423         std::ostringstream zoneSuffix;
424         zoneSuffix << ".zone" << _zoneId;
425         std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
426 
427         fileParseRpm(zoneSetpointPath, _maximumSetPoint);
428     }
429     return;
430 }
431 
initializeLog(void)432 void DbusPidZone::initializeLog(void)
433 {
434     /* Print header for log file:
435      * epoch_ms,setpt,fan1,fan1_raw,fan1_pwm,fan1_pwm_raw,fan2,fan2_raw,fan2_pwm,fan2_pwm_raw,fanN,fanN_raw,fanN_pwm,fanN_pwm_raw,sensor1,sensor1_raw,sensor2,sensor2_raw,sensorN,sensorN_raw,failsafe
436      */
437 
438     _log << "epoch_ms,setpt,requester";
439 
440     for (const auto& f : _fanInputs)
441     {
442         _log << "," << f << "," << f << "_raw";
443         _log << "," << f << "_pwm," << f << "_pwm_raw";
444     }
445     for (const auto& t : _thermalInputs)
446     {
447         _log << "," << t << "," << t << "_raw";
448     }
449 
450     _log << ",failsafe";
451     _log << std::endl;
452 }
453 
writeLog(const std::string & value)454 void DbusPidZone::writeLog(const std::string& value)
455 {
456     _log << value;
457 }
458 
459 /*
460  * TODO(venture) This is effectively updating the cache and should check if the
461  * values they're using to update it are new or old, or whatnot.  For instance,
462  * if we haven't heard from the host in X time we need to detect this failure.
463  *
464  * I haven't decided if the Sensor should have a lastUpdated method or whether
465  * that should be for the ReadInterface or etc...
466  */
467 
468 /**
469  * We want the PID loop to run with values cached, so this will get all the
470  * fan tachs for the loop.
471  */
updateFanTelemetry(void)472 void DbusPidZone::updateFanTelemetry(void)
473 {
474     /* TODO(venture): Should I just make _log point to /dev/null when logging
475      * is disabled?  I think it's a waste to try and log things even if the
476      * data is just being dropped though.
477      */
478     const auto now = std::chrono::high_resolution_clock::now();
479     if (loggingEnabled)
480     {
481         _log << std::chrono::duration_cast<std::chrono::milliseconds>(
482                     now.time_since_epoch())
483                     .count();
484         _log << "," << _maximumSetPoint;
485         _log << "," << _maximumSetPointName;
486     }
487 
488     processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
489 
490     if (loggingEnabled)
491     {
492         for (const auto& t : _thermalInputs)
493         {
494             const auto& v = _cachedValuesByName[t];
495             _log << "," << v.scaled << "," << v.unscaled;
496         }
497     }
498 
499     return;
500 }
501 
updateSensors(void)502 void DbusPidZone::updateSensors(void)
503 {
504     processSensorInputs</* fanSensorLogging */ false>(
505         _thermalInputs, std::chrono::high_resolution_clock::now());
506 
507     return;
508 }
509 
initializeCache(void)510 void DbusPidZone::initializeCache(void)
511 {
512     auto nan = std::numeric_limits<double>::quiet_NaN();
513 
514     for (const auto& f : _fanInputs)
515     {
516         _cachedValuesByName[f] = {nan, nan};
517         _cachedFanOutputs[f] = {nan, nan};
518 
519         // Start all fans in fail-safe mode.
520         markSensorMissing(f, "");
521     }
522 
523     for (const auto& t : _thermalInputs)
524     {
525         _cachedValuesByName[t] = {nan, nan};
526 
527         // Start all sensors in fail-safe mode.
528         markSensorMissing(t, "");
529     }
530 }
531 
dumpCache(void)532 void DbusPidZone::dumpCache(void)
533 {
534     std::cerr << "Cache values now: \n";
535     for (const auto& [name, value] : _cachedValuesByName)
536     {
537         std::cerr << name << ": " << value.scaled << " " << value.unscaled
538                   << "\n";
539     }
540 
541     std::cerr << "Fan outputs now: \n";
542     for (const auto& [name, value] : _cachedFanOutputs)
543     {
544         std::cerr << name << ": " << value.scaled << " " << value.unscaled
545                   << "\n";
546     }
547 }
548 
processFans(void)549 void DbusPidZone::processFans(void)
550 {
551     for (auto& p : _fans)
552     {
553         p->process();
554     }
555 
556     if (_redundantWrite)
557     {
558         // This is only needed once
559         _redundantWrite = false;
560     }
561 }
562 
processThermals(void)563 void DbusPidZone::processThermals(void)
564 {
565     for (auto& p : _thermals)
566     {
567         p->process();
568     }
569 }
570 
getSensor(const std::string & name)571 Sensor* DbusPidZone::getSensor(const std::string& name)
572 {
573     return _mgr.getSensor(name);
574 }
575 
getSensorNames(void)576 std::vector<std::string> DbusPidZone::getSensorNames(void)
577 {
578     return _thermalInputs;
579 }
580 
getRedundantWrite(void) const581 bool DbusPidZone::getRedundantWrite(void) const
582 {
583     return _redundantWrite;
584 }
585 
manual(bool value)586 bool DbusPidZone::manual(bool value)
587 {
588     std::cerr << "manual: " << value << std::endl;
589     setManualMode(value);
590     return ModeObject::manual(value);
591 }
592 
failSafe() const593 bool DbusPidZone::failSafe() const
594 {
595     return getFailSafeMode();
596 }
597 
addPidControlProcess(std::string name,std::string type,double setpoint,sdbusplus::bus_t & bus,std::string objPath,bool defer)598 void DbusPidZone::addPidControlProcess(std::string name, std::string type,
599                                        double setpoint, sdbusplus::bus_t& bus,
600                                        std::string objPath, bool defer)
601 {
602     _pidsControlProcess[name] = std::make_unique<ProcessObject>(
603         bus, objPath.c_str(),
604         defer ? ProcessObject::action::defer_emit
605               : ProcessObject::action::emit_object_added);
606     // Default enable setting = true
607     _pidsControlProcess[name]->enabled(true);
608     _pidsControlProcess[name]->setpoint(setpoint);
609 
610     if (type == "temp")
611     {
612         _pidsControlProcess[name]->classType("Temperature");
613     }
614     else if (type == "margin")
615     {
616         _pidsControlProcess[name]->classType("Margin");
617     }
618     else if (type == "power")
619     {
620         _pidsControlProcess[name]->classType("Power");
621     }
622     else if (type == "powersum")
623     {
624         _pidsControlProcess[name]->classType("PowerSum");
625     }
626 }
627 
isPidProcessEnabled(std::string name)628 bool DbusPidZone::isPidProcessEnabled(std::string name)
629 {
630     return _pidsControlProcess[name]->enabled();
631 }
632 
addPidFailSafePercent(std::vector<std::string> inputs,double percent)633 void DbusPidZone::addPidFailSafePercent(std::vector<std::string> inputs,
634                                         double percent)
635 {
636     for (const auto& sensorName : inputs)
637     {
638         if (_sensorFailSafePercent.find(sensorName) !=
639             _sensorFailSafePercent.end())
640         {
641             _sensorFailSafePercent[sensorName] =
642                 std::max(_sensorFailSafePercent[sensorName], percent);
643             if (debugEnabled)
644             {
645                 std::cerr << "Sensor " << sensorName
646                           << " failsafe percent updated to "
647                           << _sensorFailSafePercent[sensorName] << "\n";
648             }
649         }
650         else
651         {
652             _sensorFailSafePercent[sensorName] = percent;
653             if (debugEnabled)
654             {
655                 std::cerr << "Sensor " << sensorName
656                           << " failsafe percent set to " << percent << "\n";
657             }
658         }
659     }
660 }
661 
leader() const662 std::string DbusPidZone::leader() const
663 {
664     return _maximumSetPointName;
665 }
666 
updateThermalPowerDebugInterface(std::string pidName,std::string leader,double input,double output)667 void DbusPidZone::updateThermalPowerDebugInterface(
668     std::string pidName, std::string leader, double input, double output)
669 {
670     if (leader.empty())
671     {
672         _pidsControlProcess[pidName]->output(output);
673     }
674     else
675     {
676         _pidsControlProcess[pidName]->leader(leader);
677         _pidsControlProcess[pidName]->input(input);
678     }
679 }
680 
getAccSetPoint(void) const681 bool DbusPidZone::getAccSetPoint(void) const
682 {
683     return _accumulateSetPoint;
684 }
685 
686 } // namespace pid_control
687