blob: 98e61a1d42d5c33d2d9ac85785fdb5cb534e01df [file] [log] [blame]
Alexander Hansen46a755f2025-10-27 16:31:08 +01001// SPDX-License-Identifier: Apache-2.0
2// SPDX-FileCopyrightText: Copyright 2017 Google Inc
Patrick Ventured8012182018-03-08 08:21:38 -08003
4/* Configuration. */
Patrick Ventured8012182018-03-08 08:21:38 -08005#include "zone.hpp"
6
Patrick Ventureda4a5dd2018-08-31 09:42:48 -07007#include "conf.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +00008#include "failsafeloggers/failsafe_logger_utility.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -07009#include "interfaces.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070010#include "pid/controller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080011#include "pid/tuning.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070012
Ed Tanousf8b6e552025-06-27 13:27:50 -070013#include <sdbusplus/bus.hpp>
14
Patrick Ventured8012182018-03-08 08:21:38 -080015#include <algorithm>
16#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -070017#include <cstdint>
Patrick Ventured8012182018-03-08 08:21:38 -080018#include <cstring>
Ed Tanousf8b6e552025-06-27 13:27:50 -070019#include <exception>
Patrick Ventured8012182018-03-08 08:21:38 -080020#include <fstream>
21#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070022#include <limits>
Patrick Ventured8012182018-03-08 08:21:38 -080023#include <memory>
Josh Lehan55ccad62020-09-20 23:57:49 -070024#include <sstream>
Patrick Venture7a98c192020-08-12 08:35:16 -070025#include <string>
Ed Tanousf8b6e552025-06-27 13:27:50 -070026#include <string_view>
27#include <utility>
28#include <vector>
Patrick Ventured8012182018-03-08 08:21:38 -080029
Patrick Ventured8012182018-03-08 08:21:38 -080030using tstamp = std::chrono::high_resolution_clock::time_point;
31using namespace std::literals::chrono_literals;
32
Josh Lehan55ccad62020-09-20 23:57:49 -070033// Enforces minimum duration between events
34// Rreturns true if event should be allowed, false if disallowed
35bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace)
36{
37 static tstamp then;
38 static bool first = true;
39
40 if (first)
41 {
42 // Special case initialization
43 then = now;
44 first = false;
45
46 // Initialization, always allow
47 return true;
48 }
49
50 auto elapsed = now - then;
51 if (elapsed < pace)
52 {
53 // Too soon since last time, disallow
54 return false;
55 }
56
57 // It has been long enough, allow
58 then = now;
59 return true;
60}
61
62namespace pid_control
63{
64
Patrick Venture597ebd62020-08-11 08:48:19 -070065double DbusPidZone::getMaxSetPointRequest(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080066{
Patrick Venturef7a2dd52019-07-16 14:31:13 -070067 return _maximumSetPoint;
Patrick Ventured8012182018-03-08 08:21:38 -080068}
69
Patrick Venture597ebd62020-08-11 08:48:19 -070070bool DbusPidZone::getManualMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080071{
72 return _manualMode;
73}
74
Patrick Venture597ebd62020-08-11 08:48:19 -070075void DbusPidZone::setManualMode(bool mode)
Patrick Ventured8012182018-03-08 08:21:38 -080076{
77 _manualMode = mode;
Josh Lehana4146eb2020-10-01 11:49:09 -070078
79 // If returning to automatic mode, need to restore PWM from PID loop
80 if (!mode)
81 {
82 _redundantWrite = true;
83 }
Patrick Ventured8012182018-03-08 08:21:38 -080084}
85
Patrick Venture597ebd62020-08-11 08:48:19 -070086bool DbusPidZone::getFailSafeMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080087{
88 // If any keys are present at least one sensor is in fail safe mode.
89 return !_failSafeSensors.empty();
90}
91
Harvey Wua4270072024-05-29 16:11:13 +080092FailSafeSensorsMap DbusPidZone::getFailSafeSensors(void) const
93{
94 return _failSafeSensors;
95}
96
97void DbusPidZone::markSensorMissing(const std::string& name,
98 const std::string& failReason)
Josh Lehan3f0f7bc2023-02-13 01:45:29 -080099{
100 if (_missingAcceptable.find(name) != _missingAcceptable.end())
101 {
102 // Disallow sensors in MissingIsAcceptable list from causing failsafe
James Zheng6df8bb52024-11-27 23:38:47 +0000103 outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(), name,
104 "The sensor is missing but is acceptable.");
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800105 return;
106 }
107
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800108 if (_sensorFailSafePercent[name] == 0)
109 {
Harvey Wua4270072024-05-29 16:11:13 +0800110 _failSafeSensors[name] = std::pair(failReason, _zoneFailSafePercent);
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800111 }
112 else
113 {
Harvey Wua4270072024-05-29 16:11:13 +0800114 _failSafeSensors[name] =
115 std::pair(failReason, _sensorFailSafePercent[name]);
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800116 }
117
118 if (debugEnabled)
119 {
120 std::cerr << "Sensor " << name << " marked missing\n";
121 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800122}
123
Patrick Venture597ebd62020-08-11 08:48:19 -0700124int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800125{
126 return _zoneId;
127}
128
Nirav Shahccc8bb62022-02-17 21:06:51 -0800129void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800130{
ykchiu7c6d35d2023-05-10 17:01:46 +0800131 /* exclude disabled pidloop from _maximumSetPoint calculation*/
132 if (!isPidProcessEnabled(name))
133 {
134 return;
135 }
136
Delphine CC Chiu97889632023-11-06 11:32:46 +0800137 auto profileName = name;
138 if (getAccSetPoint())
139 {
140 /*
141 * If the name of controller is Linear_Temp_CPU0.
142 * The profile name will be Temp_CPU0.
143 */
Ed Tanousd2768c52025-06-26 11:42:57 -0700144 profileName = name.substr(name.find('_') + 1);
145 setPoints[profileName] += setPoint;
Delphine CC Chiu97889632023-11-06 11:32:46 +0800146 }
147 else
148 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700149 if (setPoints[profileName] < setPoint)
Delphine CC Chiu97889632023-11-06 11:32:46 +0800150 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700151 setPoints[profileName] = setPoint;
Delphine CC Chiu97889632023-11-06 11:32:46 +0800152 }
153 }
154
Nirav Shahccc8bb62022-02-17 21:06:51 -0800155 /*
156 * if there are multiple thermal controllers with the same
157 * value, pick the first one in the iterator
158 */
Ed Tanousd2768c52025-06-26 11:42:57 -0700159 if (_maximumSetPoint < setPoints[profileName])
Nirav Shahccc8bb62022-02-17 21:06:51 -0800160 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700161 _maximumSetPoint = setPoints[profileName];
Delphine CC Chiu97889632023-11-06 11:32:46 +0800162 _maximumSetPointName = profileName;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800163 }
Patrick Ventured8012182018-03-08 08:21:38 -0800164}
165
Patrick Venture597ebd62020-08-11 08:48:19 -0700166void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800167{
Ed Tanousd2768c52025-06-26 11:42:57 -0700168 rpmCeilings.push_back(ceiling);
James Feist608304d2019-02-25 10:01:42 -0800169}
170
Patrick Venture597ebd62020-08-11 08:48:19 -0700171void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800172{
Ed Tanousd2768c52025-06-26 11:42:57 -0700173 rpmCeilings.clear();
James Feist608304d2019-02-25 10:01:42 -0800174}
175
Patrick Venture597ebd62020-08-11 08:48:19 -0700176void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800177{
Ed Tanousd2768c52025-06-26 11:42:57 -0700178 setPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800179 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800180 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800181}
182
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800183double DbusPidZone::getFailSafePercent(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800184{
Eric Yangd59ccac2025-06-05 19:18:03 +0800185 if (_failSafeSensors.empty())
186 {
187 return _zoneFailSafePercent;
188 }
189
Harvey Wua4270072024-05-29 16:11:13 +0800190 FailSafeSensorsMap::iterator maxData = std::max_element(
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800191 _failSafeSensors.begin(), _failSafeSensors.end(),
Ed Tanousd2768c52025-06-26 11:42:57 -0700192 [](const FailSafeSensorPair& firstData,
193 const FailSafeSensorPair& secondData) {
Harvey Wua4270072024-05-29 16:11:13 +0800194 return firstData.second.second < secondData.second.second;
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800195 });
196
197 // In dbus/dbusconfiguration.cpp, the default sensor failsafepercent is 0 if
198 // there is no setting in json.
199 // Therfore, if the max failsafe duty in _failSafeSensors is 0, set final
200 // failsafe duty to _zoneFailSafePercent.
Harvey Wua4270072024-05-29 16:11:13 +0800201 if ((*maxData).second.second == 0)
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800202 {
203 return _zoneFailSafePercent;
204 }
Ed Tanousd2768c52025-06-26 11:42:57 -0700205
206 return (*maxData).second.second;
Patrick Ventured8012182018-03-08 08:21:38 -0800207}
208
Nirav Shahccc8bb62022-02-17 21:06:51 -0800209double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800210{
James Feist3484bed2019-02-25 13:28:18 -0800211 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800212}
213
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800214uint64_t DbusPidZone::getCycleIntervalTime(void) const
215{
216 return _cycleTime.cycleIntervalTimeMS;
217}
218
219uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
220{
221 return _cycleTime.updateThermalsTimeMS;
222}
223
Patrick Venture597ebd62020-08-11 08:48:19 -0700224void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800225{
226 _fans.push_back(std::move(pid));
227}
228
Patrick Venture597ebd62020-08-11 08:48:19 -0700229void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800230{
231 _thermals.push_back(std::move(pid));
232}
233
Patrick Venture597ebd62020-08-11 08:48:19 -0700234double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800235{
Josh Lehanb3005752022-02-22 20:48:07 -0800236 return _cachedValuesByName.at(name).scaled;
237}
238
239ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
240{
Patrick Ventured8012182018-03-08 08:21:38 -0800241 return _cachedValuesByName.at(name);
242}
243
Josh Lehanb3005752022-02-22 20:48:07 -0800244void DbusPidZone::setOutputCache(std::string_view name,
245 const ValueCacheEntry& values)
246{
247 _cachedFanOutputs[std::string{name}] = values;
248}
249
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800250void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800251{
252 _fanInputs.push_back(fan);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800253
254 if (missingAcceptable)
255 {
256 _missingAcceptable.emplace(fan);
257 }
Patrick Ventured8012182018-03-08 08:21:38 -0800258}
259
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800260void DbusPidZone::addThermalInput(const std::string& therm,
261 bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800262{
Delphine CC Chiu97889632023-11-06 11:32:46 +0800263 /*
264 * One sensor may have stepwise and PID at the same time.
265 * Searching the sensor name before inserting it to avoid duplicated sensor
266 * names.
267 */
268 if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) ==
269 _thermalInputs.end())
270 {
271 _thermalInputs.push_back(therm);
272 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800273
274 if (missingAcceptable)
275 {
276 _missingAcceptable.emplace(therm);
277 }
Patrick Ventured8012182018-03-08 08:21:38 -0800278}
279
Josh Lehan55ccad62020-09-20 23:57:49 -0700280// Updates desired RPM setpoint from optional text file
281// Returns true if rpmValue updated, false if left unchanged
282static bool fileParseRpm(const std::string& fileName, double& rpmValue)
283{
284 static constexpr std::chrono::seconds throttlePace{3};
285
286 std::string errText;
287
288 try
289 {
290 std::ifstream ifs;
291 ifs.open(fileName);
292 if (ifs)
293 {
294 int value;
295 ifs >> value;
296
297 if (value <= 0)
298 {
299 errText = "File content could not be parsed to a number";
300 }
301 else if (value <= 100)
302 {
303 errText = "File must contain RPM value, not PWM value";
304 }
305 else
306 {
307 rpmValue = static_cast<double>(value);
308 return true;
309 }
310 }
311 }
312 catch (const std::exception& e)
313 {
314 errText = "Exception: ";
315 errText += e.what();
316 }
317
318 // The file is optional, intentionally not an error if file not found
319 if (!(errText.empty()))
320 {
321 tstamp now = std::chrono::high_resolution_clock::now();
322 if (allowThrottle(now, throttlePace))
323 {
324 std::cerr << "Unable to read from '" << fileName << "': " << errText
325 << "\n";
326 }
327 }
328
329 return false;
330}
331
Patrick Venture597ebd62020-08-11 08:48:19 -0700332void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800333{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800334 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800335 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800336
Ed Tanousd2768c52025-06-26 11:42:57 -0700337 if (rpmCeilings.size() > 0)
James Feist608304d2019-02-25 10:01:42 -0800338 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700339 result = std::min_element(rpmCeilings.begin(), rpmCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800340 // if Max set point is larger than the lowest ceiling, reset to lowest
341 // ceiling.
342 if (*result < _maximumSetPoint)
343 {
344 _maximumSetPoint = *result;
345 // When using lowest ceiling, controller name is ceiling.
346 _maximumSetPointName = "Ceiling";
347 }
James Feist608304d2019-02-25 10:01:42 -0800348 }
349
Patrick Ventured8012182018-03-08 08:21:38 -0800350 /*
Delphine CC Chiu97889632023-11-06 11:32:46 +0800351 * Combine the maximum SetPoint Name if the controllers have same profile
352 * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C.
353 */
354 if (getAccSetPoint())
355 {
356 auto profileName = _maximumSetPointName;
357 _maximumSetPointName = "";
358
359 for (auto& p : _thermals)
360 {
361 auto controllerID = p->getID();
362 auto found = controllerID.find(profileName);
363 if (found != std::string::npos)
364 {
365 if (_maximumSetPointName.empty())
366 {
367 _maximumSetPointName = controllerID;
368 }
369 else
370 {
371 _maximumSetPointName += " + " + controllerID;
372 }
373 }
374 }
375 }
376
377 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800378 * If the maximum RPM setpoint output is below the minimum RPM
379 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800380 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800381 if (minThermalThreshold >= _maximumSetPoint)
382 {
383 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800384 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800385 }
386 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
387 {
388 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
389 << _maximumSetPoint << " requested by "
390 << _maximumSetPointName;
391 for (const auto& sensor : _failSafeSensors)
392 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800393 if (sensor.first.find("Fan") == std::string::npos)
Nirav Shahccc8bb62022-02-17 21:06:51 -0800394 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800395 std::cerr << " " << sensor.first;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800396 }
397 }
398 std::cerr << "\n";
399 _maximumSetPointNamePrev.assign(_maximumSetPointName);
400 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700401 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800402 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800403 /*
404 * We received no setpoints from thermal sensors.
405 * This is a case experienced during tuning where they only specify
406 * fan sensors and one large fan PID for all the fans.
407 */
408 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700409
Nirav Shahccc8bb62022-02-17 21:06:51 -0800410 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700411
412 // Allow per-zone setpoint files to override overall setpoint file
413 std::ostringstream zoneSuffix;
414 zoneSuffix << ".zone" << _zoneId;
415 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
416
Nirav Shahccc8bb62022-02-17 21:06:51 -0800417 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800418 }
Patrick Ventured8012182018-03-08 08:21:38 -0800419 return;
420}
421
Patrick Venture597ebd62020-08-11 08:48:19 -0700422void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800423{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700424 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800425 * 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
Patrick Venture5f02ad22018-04-24 10:18:40 -0700426 */
Patrick Ventured8012182018-03-08 08:21:38 -0800427
Nirav Shahccc8bb62022-02-17 21:06:51 -0800428 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800429
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700430 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800431 {
Josh Lehanb3005752022-02-22 20:48:07 -0800432 _log << "," << f << "," << f << "_raw";
433 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800434 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700435 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700436 {
Josh Lehanb3005752022-02-22 20:48:07 -0800437 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700438 }
Josh Lehanb3005752022-02-22 20:48:07 -0800439
Patrick Venture5f02ad22018-04-24 10:18:40 -0700440 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800441 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800442}
443
Patrick Venture7a98c192020-08-12 08:35:16 -0700444void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800445{
Patrick Venture7a98c192020-08-12 08:35:16 -0700446 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800447}
Patrick Ventured8012182018-03-08 08:21:38 -0800448
449/*
450 * TODO(venture) This is effectively updating the cache and should check if the
451 * values they're using to update it are new or old, or whatnot. For instance,
452 * if we haven't heard from the host in X time we need to detect this failure.
453 *
454 * I haven't decided if the Sensor should have a lastUpdated method or whether
455 * that should be for the ReadInterface or etc...
456 */
457
458/**
459 * We want the PID loop to run with values cached, so this will get all the
460 * fan tachs for the loop.
461 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700462void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800463{
464 /* TODO(venture): Should I just make _log point to /dev/null when logging
465 * is disabled? I think it's a waste to try and log things even if the
466 * data is just being dropped though.
467 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800468 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700469 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800470 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800471 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
472 now.time_since_epoch())
473 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700474 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800475 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800476 }
Patrick Ventured8012182018-03-08 08:21:38 -0800477
Tom Tungdf1f1832022-11-14 19:26:52 +0800478 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800479
Patrick Venturede79ee02019-05-08 14:50:00 -0700480 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700481 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800482 for (const auto& t : _thermalInputs)
483 {
Josh Lehanb3005752022-02-22 20:48:07 -0800484 const auto& v = _cachedValuesByName[t];
485 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800486 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700487 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700488
Patrick Ventured8012182018-03-08 08:21:38 -0800489 return;
490}
491
Patrick Venture597ebd62020-08-11 08:48:19 -0700492void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800493{
Tom Tungdf1f1832022-11-14 19:26:52 +0800494 processSensorInputs</* fanSensorLogging */ false>(
495 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800496
497 return;
498}
499
Patrick Venture597ebd62020-08-11 08:48:19 -0700500void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800501{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800502 auto nan = std::numeric_limits<double>::quiet_NaN();
503
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700504 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800505 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800506 _cachedValuesByName[f] = {nan, nan};
507 _cachedFanOutputs[f] = {nan, nan};
Will Liangded0ab52019-05-15 17:10:06 +0800508
509 // Start all fans in fail-safe mode.
Harvey Wua4270072024-05-29 16:11:13 +0800510 markSensorMissing(f, "");
Patrick Ventured8012182018-03-08 08:21:38 -0800511 }
512
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700513 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800514 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800515 _cachedValuesByName[t] = {nan, nan};
Patrick Ventured8012182018-03-08 08:21:38 -0800516
517 // Start all sensors in fail-safe mode.
Harvey Wua4270072024-05-29 16:11:13 +0800518 markSensorMissing(t, "");
Patrick Ventured8012182018-03-08 08:21:38 -0800519 }
520}
521
Patrick Venture597ebd62020-08-11 08:48:19 -0700522void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800523{
524 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700525 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800526 {
Josh Lehanb3005752022-02-22 20:48:07 -0800527 std::cerr << name << ": " << value.scaled << " " << value.unscaled
528 << "\n";
529 }
530
531 std::cerr << "Fan outputs now: \n";
532 for (const auto& [name, value] : _cachedFanOutputs)
533 {
534 std::cerr << name << ": " << value.scaled << " " << value.unscaled
535 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800536 }
537}
538
Patrick Venture597ebd62020-08-11 08:48:19 -0700539void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800540{
541 for (auto& p : _fans)
542 {
James Feist22c257a2018-08-31 14:07:12 -0700543 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800544 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700545
546 if (_redundantWrite)
547 {
548 // This is only needed once
549 _redundantWrite = false;
550 }
Patrick Ventured8012182018-03-08 08:21:38 -0800551}
552
Patrick Venture597ebd62020-08-11 08:48:19 -0700553void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800554{
555 for (auto& p : _thermals)
556 {
James Feist22c257a2018-08-31 14:07:12 -0700557 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800558 }
559}
560
Patrick Venture597ebd62020-08-11 08:48:19 -0700561Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800562{
Patrick Venturefe75b192018-06-08 11:19:43 -0700563 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800564}
565
James Zheng6df8bb52024-11-27 23:38:47 +0000566std::vector<std::string> DbusPidZone::getSensorNames(void)
567{
568 return _thermalInputs;
569}
570
Josh Lehana4146eb2020-10-01 11:49:09 -0700571bool DbusPidZone::getRedundantWrite(void) const
572{
573 return _redundantWrite;
574}
575
Patrick Venture597ebd62020-08-11 08:48:19 -0700576bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800577{
578 std::cerr << "manual: " << value << std::endl;
579 setManualMode(value);
580 return ModeObject::manual(value);
581}
582
Patrick Venture597ebd62020-08-11 08:48:19 -0700583bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800584{
585 return getFailSafeMode();
586}
Patrick Venturea0764872020-08-08 07:48:43 -0700587
Ed Tanousd2768c52025-06-26 11:42:57 -0700588void DbusPidZone::addPidControlProcess(
589 const std::string& name, const std::string& type, double setpoint,
590 sdbusplus::bus_t& bus, const std::string& objPath, bool defer)
ykchiu7c6d35d2023-05-10 17:01:46 +0800591{
592 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
593 bus, objPath.c_str(),
594 defer ? ProcessObject::action::defer_emit
595 : ProcessObject::action::emit_object_added);
596 // Default enable setting = true
597 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800598 _pidsControlProcess[name]->setpoint(setpoint);
599
600 if (type == "temp")
601 {
602 _pidsControlProcess[name]->classType("Temperature");
603 }
604 else if (type == "margin")
605 {
606 _pidsControlProcess[name]->classType("Margin");
607 }
608 else if (type == "power")
609 {
610 _pidsControlProcess[name]->classType("Power");
611 }
612 else if (type == "powersum")
613 {
614 _pidsControlProcess[name]->classType("PowerSum");
615 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800616}
617
Ed Tanousd2768c52025-06-26 11:42:57 -0700618bool DbusPidZone::isPidProcessEnabled(const std::string& name)
ykchiu7c6d35d2023-05-10 17:01:46 +0800619{
620 return _pidsControlProcess[name]->enabled();
621}
622
Ed Tanousd2768c52025-06-26 11:42:57 -0700623void DbusPidZone::addPidFailSafePercent(const std::vector<std::string>& inputs,
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800624 double percent)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800625{
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800626 for (const auto& sensorName : inputs)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800627 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800628 if (_sensorFailSafePercent.find(sensorName) !=
629 _sensorFailSafePercent.end())
630 {
631 _sensorFailSafePercent[sensorName] =
632 std::max(_sensorFailSafePercent[sensorName], percent);
633 if (debugEnabled)
634 {
635 std::cerr << "Sensor " << sensorName
636 << " failsafe percent updated to "
637 << _sensorFailSafePercent[sensorName] << "\n";
638 }
639 }
640 else
641 {
642 _sensorFailSafePercent[sensorName] = percent;
643 if (debugEnabled)
644 {
645 std::cerr << "Sensor " << sensorName
646 << " failsafe percent set to " << percent << "\n";
647 }
648 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800649 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800650}
651
Harvey Wucc0232a2023-02-09 14:58:55 +0800652std::string DbusPidZone::leader() const
653{
654 return _maximumSetPointName;
655}
656
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400657void DbusPidZone::updateThermalPowerDebugInterface(
658 std::string pidName, std::string leader, double input, double output)
Harvey Wu37180062023-10-02 09:42:50 +0800659{
660 if (leader.empty())
661 {
662 _pidsControlProcess[pidName]->output(output);
663 }
664 else
665 {
666 _pidsControlProcess[pidName]->leader(leader);
667 _pidsControlProcess[pidName]->input(input);
668 }
669}
670
Delphine CC Chiu97889632023-11-06 11:32:46 +0800671bool DbusPidZone::getAccSetPoint(void) const
672{
673 return _accumulateSetPoint;
674}
675
Patrick Venturea0764872020-08-08 07:48:43 -0700676} // namespace pid_control