blob: 835b79ebc03c92072a502912c78d83bc7dad4f95 [file] [log] [blame]
Patrick Ventured8012182018-03-08 08:21:38 -08001/**
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. */
Patrick Ventured8012182018-03-08 08:21:38 -080018#include "zone.hpp"
19
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020#include "conf.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +000021#include "failsafeloggers/failsafe_logger_utility.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070022#include "interfaces.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070023#include "pid/controller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080024#include "pid/tuning.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025
Ed Tanousf8b6e552025-06-27 13:27:50 -070026#include <sdbusplus/bus.hpp>
27
Patrick Ventured8012182018-03-08 08:21:38 -080028#include <algorithm>
29#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -070030#include <cstdint>
Patrick Ventured8012182018-03-08 08:21:38 -080031#include <cstring>
Ed Tanousf8b6e552025-06-27 13:27:50 -070032#include <exception>
Patrick Ventured8012182018-03-08 08:21:38 -080033#include <fstream>
34#include <iostream>
Ed Tanousf8b6e552025-06-27 13:27:50 -070035#include <limits>
Patrick Ventured8012182018-03-08 08:21:38 -080036#include <memory>
Josh Lehan55ccad62020-09-20 23:57:49 -070037#include <sstream>
Patrick Venture7a98c192020-08-12 08:35:16 -070038#include <string>
Ed Tanousf8b6e552025-06-27 13:27:50 -070039#include <string_view>
40#include <utility>
41#include <vector>
Patrick Ventured8012182018-03-08 08:21:38 -080042
Patrick Ventured8012182018-03-08 08:21:38 -080043using tstamp = std::chrono::high_resolution_clock::time_point;
44using namespace std::literals::chrono_literals;
45
Josh Lehan55ccad62020-09-20 23:57:49 -070046// Enforces minimum duration between events
47// Rreturns true if event should be allowed, false if disallowed
48bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace)
49{
50 static tstamp then;
51 static bool first = true;
52
53 if (first)
54 {
55 // Special case initialization
56 then = now;
57 first = false;
58
59 // Initialization, always allow
60 return true;
61 }
62
63 auto elapsed = now - then;
64 if (elapsed < pace)
65 {
66 // Too soon since last time, disallow
67 return false;
68 }
69
70 // It has been long enough, allow
71 then = now;
72 return true;
73}
74
75namespace pid_control
76{
77
Patrick Venture597ebd62020-08-11 08:48:19 -070078double DbusPidZone::getMaxSetPointRequest(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080079{
Patrick Venturef7a2dd52019-07-16 14:31:13 -070080 return _maximumSetPoint;
Patrick Ventured8012182018-03-08 08:21:38 -080081}
82
Patrick Venture597ebd62020-08-11 08:48:19 -070083bool DbusPidZone::getManualMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080084{
85 return _manualMode;
86}
87
Patrick Venture597ebd62020-08-11 08:48:19 -070088void DbusPidZone::setManualMode(bool mode)
Patrick Ventured8012182018-03-08 08:21:38 -080089{
90 _manualMode = mode;
Josh Lehana4146eb2020-10-01 11:49:09 -070091
92 // If returning to automatic mode, need to restore PWM from PID loop
93 if (!mode)
94 {
95 _redundantWrite = true;
96 }
Patrick Ventured8012182018-03-08 08:21:38 -080097}
98
Patrick Venture597ebd62020-08-11 08:48:19 -070099bool DbusPidZone::getFailSafeMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800100{
101 // If any keys are present at least one sensor is in fail safe mode.
102 return !_failSafeSensors.empty();
103}
104
Harvey Wua4270072024-05-29 16:11:13 +0800105FailSafeSensorsMap DbusPidZone::getFailSafeSensors(void) const
106{
107 return _failSafeSensors;
108}
109
110void DbusPidZone::markSensorMissing(const std::string& name,
111 const std::string& failReason)
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800112{
113 if (_missingAcceptable.find(name) != _missingAcceptable.end())
114 {
115 // Disallow sensors in MissingIsAcceptable list from causing failsafe
James Zheng6df8bb52024-11-27 23:38:47 +0000116 outputFailsafeLogWithZone(_zoneId, this->getFailSafeMode(), name,
117 "The sensor is missing but is acceptable.");
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800118 return;
119 }
120
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800121 if (_sensorFailSafePercent[name] == 0)
122 {
Harvey Wua4270072024-05-29 16:11:13 +0800123 _failSafeSensors[name] = std::pair(failReason, _zoneFailSafePercent);
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800124 }
125 else
126 {
Harvey Wua4270072024-05-29 16:11:13 +0800127 _failSafeSensors[name] =
128 std::pair(failReason, _sensorFailSafePercent[name]);
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800129 }
130
131 if (debugEnabled)
132 {
133 std::cerr << "Sensor " << name << " marked missing\n";
134 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800135}
136
Patrick Venture597ebd62020-08-11 08:48:19 -0700137int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800138{
139 return _zoneId;
140}
141
Nirav Shahccc8bb62022-02-17 21:06:51 -0800142void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800143{
ykchiu7c6d35d2023-05-10 17:01:46 +0800144 /* exclude disabled pidloop from _maximumSetPoint calculation*/
145 if (!isPidProcessEnabled(name))
146 {
147 return;
148 }
149
Delphine CC Chiu97889632023-11-06 11:32:46 +0800150 auto profileName = name;
151 if (getAccSetPoint())
152 {
153 /*
154 * If the name of controller is Linear_Temp_CPU0.
155 * The profile name will be Temp_CPU0.
156 */
Ed Tanousd2768c52025-06-26 11:42:57 -0700157 profileName = name.substr(name.find('_') + 1);
158 setPoints[profileName] += setPoint;
Delphine CC Chiu97889632023-11-06 11:32:46 +0800159 }
160 else
161 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700162 if (setPoints[profileName] < setPoint)
Delphine CC Chiu97889632023-11-06 11:32:46 +0800163 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700164 setPoints[profileName] = setPoint;
Delphine CC Chiu97889632023-11-06 11:32:46 +0800165 }
166 }
167
Nirav Shahccc8bb62022-02-17 21:06:51 -0800168 /*
169 * if there are multiple thermal controllers with the same
170 * value, pick the first one in the iterator
171 */
Ed Tanousd2768c52025-06-26 11:42:57 -0700172 if (_maximumSetPoint < setPoints[profileName])
Nirav Shahccc8bb62022-02-17 21:06:51 -0800173 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700174 _maximumSetPoint = setPoints[profileName];
Delphine CC Chiu97889632023-11-06 11:32:46 +0800175 _maximumSetPointName = profileName;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800176 }
Patrick Ventured8012182018-03-08 08:21:38 -0800177}
178
Patrick Venture597ebd62020-08-11 08:48:19 -0700179void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800180{
Ed Tanousd2768c52025-06-26 11:42:57 -0700181 rpmCeilings.push_back(ceiling);
James Feist608304d2019-02-25 10:01:42 -0800182}
183
Patrick Venture597ebd62020-08-11 08:48:19 -0700184void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800185{
Ed Tanousd2768c52025-06-26 11:42:57 -0700186 rpmCeilings.clear();
James Feist608304d2019-02-25 10:01:42 -0800187}
188
Patrick Venture597ebd62020-08-11 08:48:19 -0700189void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800190{
Ed Tanousd2768c52025-06-26 11:42:57 -0700191 setPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800192 _maximumSetPoint = 0;
ykchiu7c6d35d2023-05-10 17:01:46 +0800193 _maximumSetPointName.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800194}
195
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800196double DbusPidZone::getFailSafePercent(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800197{
Eric Yangd59ccac2025-06-05 19:18:03 +0800198 if (_failSafeSensors.empty())
199 {
200 return _zoneFailSafePercent;
201 }
202
Harvey Wua4270072024-05-29 16:11:13 +0800203 FailSafeSensorsMap::iterator maxData = std::max_element(
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800204 _failSafeSensors.begin(), _failSafeSensors.end(),
Ed Tanousd2768c52025-06-26 11:42:57 -0700205 [](const FailSafeSensorPair& firstData,
206 const FailSafeSensorPair& secondData) {
Harvey Wua4270072024-05-29 16:11:13 +0800207 return firstData.second.second < secondData.second.second;
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800208 });
209
210 // In dbus/dbusconfiguration.cpp, the default sensor failsafepercent is 0 if
211 // there is no setting in json.
212 // Therfore, if the max failsafe duty in _failSafeSensors is 0, set final
213 // failsafe duty to _zoneFailSafePercent.
Harvey Wua4270072024-05-29 16:11:13 +0800214 if ((*maxData).second.second == 0)
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800215 {
216 return _zoneFailSafePercent;
217 }
Ed Tanousd2768c52025-06-26 11:42:57 -0700218
219 return (*maxData).second.second;
Patrick Ventured8012182018-03-08 08:21:38 -0800220}
221
Nirav Shahccc8bb62022-02-17 21:06:51 -0800222double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800223{
James Feist3484bed2019-02-25 13:28:18 -0800224 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800225}
226
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800227uint64_t DbusPidZone::getCycleIntervalTime(void) const
228{
229 return _cycleTime.cycleIntervalTimeMS;
230}
231
232uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
233{
234 return _cycleTime.updateThermalsTimeMS;
235}
236
Patrick Venture597ebd62020-08-11 08:48:19 -0700237void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800238{
239 _fans.push_back(std::move(pid));
240}
241
Patrick Venture597ebd62020-08-11 08:48:19 -0700242void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800243{
244 _thermals.push_back(std::move(pid));
245}
246
Patrick Venture597ebd62020-08-11 08:48:19 -0700247double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800248{
Josh Lehanb3005752022-02-22 20:48:07 -0800249 return _cachedValuesByName.at(name).scaled;
250}
251
252ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
253{
Patrick Ventured8012182018-03-08 08:21:38 -0800254 return _cachedValuesByName.at(name);
255}
256
Josh Lehanb3005752022-02-22 20:48:07 -0800257void DbusPidZone::setOutputCache(std::string_view name,
258 const ValueCacheEntry& values)
259{
260 _cachedFanOutputs[std::string{name}] = values;
261}
262
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800263void DbusPidZone::addFanInput(const std::string& fan, bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800264{
265 _fanInputs.push_back(fan);
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800266
267 if (missingAcceptable)
268 {
269 _missingAcceptable.emplace(fan);
270 }
Patrick Ventured8012182018-03-08 08:21:38 -0800271}
272
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800273void DbusPidZone::addThermalInput(const std::string& therm,
274 bool missingAcceptable)
Patrick Ventured8012182018-03-08 08:21:38 -0800275{
Delphine CC Chiu97889632023-11-06 11:32:46 +0800276 /*
277 * One sensor may have stepwise and PID at the same time.
278 * Searching the sensor name before inserting it to avoid duplicated sensor
279 * names.
280 */
281 if (std::find(_thermalInputs.begin(), _thermalInputs.end(), therm) ==
282 _thermalInputs.end())
283 {
284 _thermalInputs.push_back(therm);
285 }
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800286
287 if (missingAcceptable)
288 {
289 _missingAcceptable.emplace(therm);
290 }
Patrick Ventured8012182018-03-08 08:21:38 -0800291}
292
Josh Lehan55ccad62020-09-20 23:57:49 -0700293// Updates desired RPM setpoint from optional text file
294// Returns true if rpmValue updated, false if left unchanged
295static bool fileParseRpm(const std::string& fileName, double& rpmValue)
296{
297 static constexpr std::chrono::seconds throttlePace{3};
298
299 std::string errText;
300
301 try
302 {
303 std::ifstream ifs;
304 ifs.open(fileName);
305 if (ifs)
306 {
307 int value;
308 ifs >> value;
309
310 if (value <= 0)
311 {
312 errText = "File content could not be parsed to a number";
313 }
314 else if (value <= 100)
315 {
316 errText = "File must contain RPM value, not PWM value";
317 }
318 else
319 {
320 rpmValue = static_cast<double>(value);
321 return true;
322 }
323 }
324 }
325 catch (const std::exception& e)
326 {
327 errText = "Exception: ";
328 errText += e.what();
329 }
330
331 // The file is optional, intentionally not an error if file not found
332 if (!(errText.empty()))
333 {
334 tstamp now = std::chrono::high_resolution_clock::now();
335 if (allowThrottle(now, throttlePace))
336 {
337 std::cerr << "Unable to read from '" << fileName << "': " << errText
338 << "\n";
339 }
340 }
341
342 return false;
343}
344
Patrick Venture597ebd62020-08-11 08:48:19 -0700345void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800346{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800347 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800348 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800349
Ed Tanousd2768c52025-06-26 11:42:57 -0700350 if (rpmCeilings.size() > 0)
James Feist608304d2019-02-25 10:01:42 -0800351 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700352 result = std::min_element(rpmCeilings.begin(), rpmCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800353 // if Max set point is larger than the lowest ceiling, reset to lowest
354 // ceiling.
355 if (*result < _maximumSetPoint)
356 {
357 _maximumSetPoint = *result;
358 // When using lowest ceiling, controller name is ceiling.
359 _maximumSetPointName = "Ceiling";
360 }
James Feist608304d2019-02-25 10:01:42 -0800361 }
362
Patrick Ventured8012182018-03-08 08:21:38 -0800363 /*
Delphine CC Chiu97889632023-11-06 11:32:46 +0800364 * Combine the maximum SetPoint Name if the controllers have same profile
365 * name. e.g., PID_BB_INLET_TEMP_C + Stepwise_BB_INLET_TEMP_C.
366 */
367 if (getAccSetPoint())
368 {
369 auto profileName = _maximumSetPointName;
370 _maximumSetPointName = "";
371
372 for (auto& p : _thermals)
373 {
374 auto controllerID = p->getID();
375 auto found = controllerID.find(profileName);
376 if (found != std::string::npos)
377 {
378 if (_maximumSetPointName.empty())
379 {
380 _maximumSetPointName = controllerID;
381 }
382 else
383 {
384 _maximumSetPointName += " + " + controllerID;
385 }
386 }
387 }
388 }
389
390 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800391 * If the maximum RPM setpoint output is below the minimum RPM
392 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800393 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800394 if (minThermalThreshold >= _maximumSetPoint)
395 {
396 _maximumSetPoint = minThermalThreshold;
ykchiu7c6d35d2023-05-10 17:01:46 +0800397 _maximumSetPointName = "Minimum";
Nirav Shahccc8bb62022-02-17 21:06:51 -0800398 }
399 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
400 {
401 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
402 << _maximumSetPoint << " requested by "
403 << _maximumSetPointName;
404 for (const auto& sensor : _failSafeSensors)
405 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800406 if (sensor.first.find("Fan") == std::string::npos)
Nirav Shahccc8bb62022-02-17 21:06:51 -0800407 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800408 std::cerr << " " << sensor.first;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800409 }
410 }
411 std::cerr << "\n";
412 _maximumSetPointNamePrev.assign(_maximumSetPointName);
413 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700414 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800415 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800416 /*
417 * We received no setpoints from thermal sensors.
418 * This is a case experienced during tuning where they only specify
419 * fan sensors and one large fan PID for all the fans.
420 */
421 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700422
Nirav Shahccc8bb62022-02-17 21:06:51 -0800423 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700424
425 // Allow per-zone setpoint files to override overall setpoint file
426 std::ostringstream zoneSuffix;
427 zoneSuffix << ".zone" << _zoneId;
428 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
429
Nirav Shahccc8bb62022-02-17 21:06:51 -0800430 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800431 }
Patrick Ventured8012182018-03-08 08:21:38 -0800432 return;
433}
434
Patrick Venture597ebd62020-08-11 08:48:19 -0700435void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800436{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700437 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800438 * 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 -0700439 */
Patrick Ventured8012182018-03-08 08:21:38 -0800440
Nirav Shahccc8bb62022-02-17 21:06:51 -0800441 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800442
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700443 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800444 {
Josh Lehanb3005752022-02-22 20:48:07 -0800445 _log << "," << f << "," << f << "_raw";
446 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800447 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700448 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700449 {
Josh Lehanb3005752022-02-22 20:48:07 -0800450 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700451 }
Josh Lehanb3005752022-02-22 20:48:07 -0800452
Patrick Venture5f02ad22018-04-24 10:18:40 -0700453 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800454 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800455}
456
Patrick Venture7a98c192020-08-12 08:35:16 -0700457void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800458{
Patrick Venture7a98c192020-08-12 08:35:16 -0700459 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800460}
Patrick Ventured8012182018-03-08 08:21:38 -0800461
462/*
463 * TODO(venture) This is effectively updating the cache and should check if the
464 * values they're using to update it are new or old, or whatnot. For instance,
465 * if we haven't heard from the host in X time we need to detect this failure.
466 *
467 * I haven't decided if the Sensor should have a lastUpdated method or whether
468 * that should be for the ReadInterface or etc...
469 */
470
471/**
472 * We want the PID loop to run with values cached, so this will get all the
473 * fan tachs for the loop.
474 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700475void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800476{
477 /* TODO(venture): Should I just make _log point to /dev/null when logging
478 * is disabled? I think it's a waste to try and log things even if the
479 * data is just being dropped though.
480 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800481 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700482 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800483 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800484 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
485 now.time_since_epoch())
486 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700487 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800488 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800489 }
Patrick Ventured8012182018-03-08 08:21:38 -0800490
Tom Tungdf1f1832022-11-14 19:26:52 +0800491 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800492
Patrick Venturede79ee02019-05-08 14:50:00 -0700493 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700494 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800495 for (const auto& t : _thermalInputs)
496 {
Josh Lehanb3005752022-02-22 20:48:07 -0800497 const auto& v = _cachedValuesByName[t];
498 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800499 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700500 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700501
Patrick Ventured8012182018-03-08 08:21:38 -0800502 return;
503}
504
Patrick Venture597ebd62020-08-11 08:48:19 -0700505void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800506{
Tom Tungdf1f1832022-11-14 19:26:52 +0800507 processSensorInputs</* fanSensorLogging */ false>(
508 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800509
510 return;
511}
512
Patrick Venture597ebd62020-08-11 08:48:19 -0700513void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800514{
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800515 auto nan = std::numeric_limits<double>::quiet_NaN();
516
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700517 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800518 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800519 _cachedValuesByName[f] = {nan, nan};
520 _cachedFanOutputs[f] = {nan, nan};
Will Liangded0ab52019-05-15 17:10:06 +0800521
522 // Start all fans in fail-safe mode.
Harvey Wua4270072024-05-29 16:11:13 +0800523 markSensorMissing(f, "");
Patrick Ventured8012182018-03-08 08:21:38 -0800524 }
525
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700526 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800527 {
Josh Lehan3f0f7bc2023-02-13 01:45:29 -0800528 _cachedValuesByName[t] = {nan, nan};
Patrick Ventured8012182018-03-08 08:21:38 -0800529
530 // Start all sensors in fail-safe mode.
Harvey Wua4270072024-05-29 16:11:13 +0800531 markSensorMissing(t, "");
Patrick Ventured8012182018-03-08 08:21:38 -0800532 }
533}
534
Patrick Venture597ebd62020-08-11 08:48:19 -0700535void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800536{
537 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700538 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800539 {
Josh Lehanb3005752022-02-22 20:48:07 -0800540 std::cerr << name << ": " << value.scaled << " " << value.unscaled
541 << "\n";
542 }
543
544 std::cerr << "Fan outputs now: \n";
545 for (const auto& [name, value] : _cachedFanOutputs)
546 {
547 std::cerr << name << ": " << value.scaled << " " << value.unscaled
548 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800549 }
550}
551
Patrick Venture597ebd62020-08-11 08:48:19 -0700552void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800553{
554 for (auto& p : _fans)
555 {
James Feist22c257a2018-08-31 14:07:12 -0700556 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800557 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700558
559 if (_redundantWrite)
560 {
561 // This is only needed once
562 _redundantWrite = false;
563 }
Patrick Ventured8012182018-03-08 08:21:38 -0800564}
565
Patrick Venture597ebd62020-08-11 08:48:19 -0700566void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800567{
568 for (auto& p : _thermals)
569 {
James Feist22c257a2018-08-31 14:07:12 -0700570 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800571 }
572}
573
Patrick Venture597ebd62020-08-11 08:48:19 -0700574Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800575{
Patrick Venturefe75b192018-06-08 11:19:43 -0700576 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800577}
578
James Zheng6df8bb52024-11-27 23:38:47 +0000579std::vector<std::string> DbusPidZone::getSensorNames(void)
580{
581 return _thermalInputs;
582}
583
Josh Lehana4146eb2020-10-01 11:49:09 -0700584bool DbusPidZone::getRedundantWrite(void) const
585{
586 return _redundantWrite;
587}
588
Patrick Venture597ebd62020-08-11 08:48:19 -0700589bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800590{
591 std::cerr << "manual: " << value << std::endl;
592 setManualMode(value);
593 return ModeObject::manual(value);
594}
595
Patrick Venture597ebd62020-08-11 08:48:19 -0700596bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800597{
598 return getFailSafeMode();
599}
Patrick Venturea0764872020-08-08 07:48:43 -0700600
Ed Tanousd2768c52025-06-26 11:42:57 -0700601void DbusPidZone::addPidControlProcess(
602 const std::string& name, const std::string& type, double setpoint,
603 sdbusplus::bus_t& bus, const std::string& objPath, bool defer)
ykchiu7c6d35d2023-05-10 17:01:46 +0800604{
605 _pidsControlProcess[name] = std::make_unique<ProcessObject>(
606 bus, objPath.c_str(),
607 defer ? ProcessObject::action::defer_emit
608 : ProcessObject::action::emit_object_added);
609 // Default enable setting = true
610 _pidsControlProcess[name]->enabled(true);
Harvey Wu37180062023-10-02 09:42:50 +0800611 _pidsControlProcess[name]->setpoint(setpoint);
612
613 if (type == "temp")
614 {
615 _pidsControlProcess[name]->classType("Temperature");
616 }
617 else if (type == "margin")
618 {
619 _pidsControlProcess[name]->classType("Margin");
620 }
621 else if (type == "power")
622 {
623 _pidsControlProcess[name]->classType("Power");
624 }
625 else if (type == "powersum")
626 {
627 _pidsControlProcess[name]->classType("PowerSum");
628 }
ykchiu7c6d35d2023-05-10 17:01:46 +0800629}
630
Ed Tanousd2768c52025-06-26 11:42:57 -0700631bool DbusPidZone::isPidProcessEnabled(const std::string& name)
ykchiu7c6d35d2023-05-10 17:01:46 +0800632{
633 return _pidsControlProcess[name]->enabled();
634}
635
Ed Tanousd2768c52025-06-26 11:42:57 -0700636void DbusPidZone::addPidFailSafePercent(const std::vector<std::string>& inputs,
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800637 double percent)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800638{
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800639 for (const auto& sensorName : inputs)
ykchiu9fe3a3c2023-05-11 13:43:54 +0800640 {
Harvey Wu92f9f3c2023-11-07 09:23:35 +0800641 if (_sensorFailSafePercent.find(sensorName) !=
642 _sensorFailSafePercent.end())
643 {
644 _sensorFailSafePercent[sensorName] =
645 std::max(_sensorFailSafePercent[sensorName], percent);
646 if (debugEnabled)
647 {
648 std::cerr << "Sensor " << sensorName
649 << " failsafe percent updated to "
650 << _sensorFailSafePercent[sensorName] << "\n";
651 }
652 }
653 else
654 {
655 _sensorFailSafePercent[sensorName] = percent;
656 if (debugEnabled)
657 {
658 std::cerr << "Sensor " << sensorName
659 << " failsafe percent set to " << percent << "\n";
660 }
661 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800662 }
ykchiu9fe3a3c2023-05-11 13:43:54 +0800663}
664
Harvey Wucc0232a2023-02-09 14:58:55 +0800665std::string DbusPidZone::leader() const
666{
667 return _maximumSetPointName;
668}
669
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400670void DbusPidZone::updateThermalPowerDebugInterface(
671 std::string pidName, std::string leader, double input, double output)
Harvey Wu37180062023-10-02 09:42:50 +0800672{
673 if (leader.empty())
674 {
675 _pidsControlProcess[pidName]->output(output);
676 }
677 else
678 {
679 _pidsControlProcess[pidName]->leader(leader);
680 _pidsControlProcess[pidName]->input(input);
681 }
682}
683
Delphine CC Chiu97889632023-11-06 11:32:46 +0800684bool DbusPidZone::getAccSetPoint(void) const
685{
686 return _accumulateSetPoint;
687}
688
Patrick Venturea0764872020-08-08 07:48:43 -0700689} // namespace pid_control