blob: c88f41ba44f60d356f4dfabf27ae2575b8c3b1ff [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"
21#include "pid/controller.hpp"
22#include "pid/ec/pid.hpp"
23#include "pid/fancontroller.hpp"
James Feist22c257a2018-08-31 14:07:12 -070024#include "pid/stepwisecontroller.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070025#include "pid/thermalcontroller.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080026#include "pid/tuning.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070027
Patrick Ventured8012182018-03-08 08:21:38 -080028#include <algorithm>
29#include <chrono>
30#include <cstring>
31#include <fstream>
32#include <iostream>
Patrick Ventured8012182018-03-08 08:21:38 -080033#include <memory>
Josh Lehan55ccad62020-09-20 23:57:49 -070034#include <sstream>
Patrick Venture7a98c192020-08-12 08:35:16 -070035#include <string>
Patrick Ventured8012182018-03-08 08:21:38 -080036
Patrick Ventured8012182018-03-08 08:21:38 -080037using tstamp = std::chrono::high_resolution_clock::time_point;
38using namespace std::literals::chrono_literals;
39
Josh Lehan55ccad62020-09-20 23:57:49 -070040// Enforces minimum duration between events
41// Rreturns true if event should be allowed, false if disallowed
42bool allowThrottle(const tstamp& now, const std::chrono::seconds& pace)
43{
44 static tstamp then;
45 static bool first = true;
46
47 if (first)
48 {
49 // Special case initialization
50 then = now;
51 first = false;
52
53 // Initialization, always allow
54 return true;
55 }
56
57 auto elapsed = now - then;
58 if (elapsed < pace)
59 {
60 // Too soon since last time, disallow
61 return false;
62 }
63
64 // It has been long enough, allow
65 then = now;
66 return true;
67}
68
69namespace pid_control
70{
71
Patrick Venture597ebd62020-08-11 08:48:19 -070072double DbusPidZone::getMaxSetPointRequest(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080073{
Patrick Venturef7a2dd52019-07-16 14:31:13 -070074 return _maximumSetPoint;
Patrick Ventured8012182018-03-08 08:21:38 -080075}
76
Patrick Venture597ebd62020-08-11 08:48:19 -070077bool DbusPidZone::getManualMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080078{
79 return _manualMode;
80}
81
Patrick Venture597ebd62020-08-11 08:48:19 -070082void DbusPidZone::setManualMode(bool mode)
Patrick Ventured8012182018-03-08 08:21:38 -080083{
84 _manualMode = mode;
Josh Lehana4146eb2020-10-01 11:49:09 -070085
86 // If returning to automatic mode, need to restore PWM from PID loop
87 if (!mode)
88 {
89 _redundantWrite = true;
90 }
Patrick Ventured8012182018-03-08 08:21:38 -080091}
92
Patrick Venture597ebd62020-08-11 08:48:19 -070093bool DbusPidZone::getFailSafeMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080094{
95 // If any keys are present at least one sensor is in fail safe mode.
96 return !_failSafeSensors.empty();
97}
98
Patrick Venture597ebd62020-08-11 08:48:19 -070099int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800100{
101 return _zoneId;
102}
103
Nirav Shahccc8bb62022-02-17 21:06:51 -0800104void DbusPidZone::addSetPoint(double setPoint, const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800105{
Nirav Shahccc8bb62022-02-17 21:06:51 -0800106 _SetPoints.push_back(setPoint);
107 /*
108 * if there are multiple thermal controllers with the same
109 * value, pick the first one in the iterator
110 */
111 if (_maximumSetPoint < setPoint)
112 {
113 _maximumSetPoint = setPoint;
114 _maximumSetPointName = name;
115 }
Patrick Ventured8012182018-03-08 08:21:38 -0800116}
117
Patrick Venture597ebd62020-08-11 08:48:19 -0700118void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800119{
120 _RPMCeilings.push_back(ceiling);
121}
122
Patrick Venture597ebd62020-08-11 08:48:19 -0700123void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800124{
125 _RPMCeilings.clear();
126}
127
Patrick Venture597ebd62020-08-11 08:48:19 -0700128void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800129{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700130 _SetPoints.clear();
Nirav Shahccc8bb62022-02-17 21:06:51 -0800131 _maximumSetPoint = 0;
Patrick Ventured8012182018-03-08 08:21:38 -0800132}
133
Patrick Venture597ebd62020-08-11 08:48:19 -0700134double DbusPidZone::getFailSafePercent(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800135{
136 return _failSafePercent;
137}
138
Nirav Shahccc8bb62022-02-17 21:06:51 -0800139double DbusPidZone::getMinThermalSetPoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800140{
James Feist3484bed2019-02-25 13:28:18 -0800141 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800142}
143
Bonnie Lo0e8fc392022-10-05 10:20:55 +0800144uint64_t DbusPidZone::getCycleIntervalTime(void) const
145{
146 return _cycleTime.cycleIntervalTimeMS;
147}
148
149uint64_t DbusPidZone::getUpdateThermalsCycle(void) const
150{
151 return _cycleTime.updateThermalsTimeMS;
152}
153
Patrick Venture597ebd62020-08-11 08:48:19 -0700154void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800155{
156 _fans.push_back(std::move(pid));
157}
158
Patrick Venture597ebd62020-08-11 08:48:19 -0700159void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800160{
161 _thermals.push_back(std::move(pid));
162}
163
Patrick Venture597ebd62020-08-11 08:48:19 -0700164double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800165{
Josh Lehanb3005752022-02-22 20:48:07 -0800166 return _cachedValuesByName.at(name).scaled;
167}
168
169ValueCacheEntry DbusPidZone::getCachedValues(const std::string& name)
170{
Patrick Ventured8012182018-03-08 08:21:38 -0800171 return _cachedValuesByName.at(name);
172}
173
Josh Lehanb3005752022-02-22 20:48:07 -0800174void DbusPidZone::setOutputCache(std::string_view name,
175 const ValueCacheEntry& values)
176{
177 _cachedFanOutputs[std::string{name}] = values;
178}
179
Patrick Venture597ebd62020-08-11 08:48:19 -0700180void DbusPidZone::addFanInput(const std::string& fan)
Patrick Ventured8012182018-03-08 08:21:38 -0800181{
182 _fanInputs.push_back(fan);
183}
184
Patrick Venture597ebd62020-08-11 08:48:19 -0700185void DbusPidZone::addThermalInput(const std::string& therm)
Patrick Ventured8012182018-03-08 08:21:38 -0800186{
187 _thermalInputs.push_back(therm);
188}
189
Josh Lehan55ccad62020-09-20 23:57:49 -0700190// Updates desired RPM setpoint from optional text file
191// Returns true if rpmValue updated, false if left unchanged
192static bool fileParseRpm(const std::string& fileName, double& rpmValue)
193{
194 static constexpr std::chrono::seconds throttlePace{3};
195
196 std::string errText;
197
198 try
199 {
200 std::ifstream ifs;
201 ifs.open(fileName);
202 if (ifs)
203 {
204 int value;
205 ifs >> value;
206
207 if (value <= 0)
208 {
209 errText = "File content could not be parsed to a number";
210 }
211 else if (value <= 100)
212 {
213 errText = "File must contain RPM value, not PWM value";
214 }
215 else
216 {
217 rpmValue = static_cast<double>(value);
218 return true;
219 }
220 }
221 }
222 catch (const std::exception& e)
223 {
224 errText = "Exception: ";
225 errText += e.what();
226 }
227
228 // The file is optional, intentionally not an error if file not found
229 if (!(errText.empty()))
230 {
231 tstamp now = std::chrono::high_resolution_clock::now();
232 if (allowThrottle(now, throttlePace))
233 {
234 std::cerr << "Unable to read from '" << fileName << "': " << errText
235 << "\n";
236 }
237 }
238
239 return false;
240}
241
Patrick Venture597ebd62020-08-11 08:48:19 -0700242void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800243{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800244 std::vector<double>::iterator result;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800245 double minThermalThreshold = getMinThermalSetPoint();
Patrick Ventured8012182018-03-08 08:21:38 -0800246
James Feist608304d2019-02-25 10:01:42 -0800247 if (_RPMCeilings.size() > 0)
248 {
249 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
Nirav Shahccc8bb62022-02-17 21:06:51 -0800250 // if Max set point is larger than the lowest ceiling, reset to lowest
251 // ceiling.
252 if (*result < _maximumSetPoint)
253 {
254 _maximumSetPoint = *result;
255 // When using lowest ceiling, controller name is ceiling.
256 _maximumSetPointName = "Ceiling";
257 }
James Feist608304d2019-02-25 10:01:42 -0800258 }
259
Patrick Ventured8012182018-03-08 08:21:38 -0800260 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800261 * If the maximum RPM setpoint output is below the minimum RPM
262 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800263 */
Nirav Shahccc8bb62022-02-17 21:06:51 -0800264 if (minThermalThreshold >= _maximumSetPoint)
265 {
266 _maximumSetPoint = minThermalThreshold;
267 _maximumSetPointName = "";
268 }
269 else if (_maximumSetPointName.compare(_maximumSetPointNamePrev))
270 {
271 std::cerr << "PID Zone " << _zoneId << " max SetPoint "
272 << _maximumSetPoint << " requested by "
273 << _maximumSetPointName;
274 for (const auto& sensor : _failSafeSensors)
275 {
276 if (sensor.find("Fan") == std::string::npos)
277 {
278 std::cerr << " " << sensor;
279 }
280 }
281 std::cerr << "\n";
282 _maximumSetPointNamePrev.assign(_maximumSetPointName);
283 }
Patrick Venturede79ee02019-05-08 14:50:00 -0700284 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800285 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800286 /*
287 * We received no setpoints from thermal sensors.
288 * This is a case experienced during tuning where they only specify
289 * fan sensors and one large fan PID for all the fans.
290 */
291 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700292
Nirav Shahccc8bb62022-02-17 21:06:51 -0800293 fileParseRpm(setpointpath, _maximumSetPoint);
Josh Lehan55ccad62020-09-20 23:57:49 -0700294
295 // Allow per-zone setpoint files to override overall setpoint file
296 std::ostringstream zoneSuffix;
297 zoneSuffix << ".zone" << _zoneId;
298 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
299
Nirav Shahccc8bb62022-02-17 21:06:51 -0800300 fileParseRpm(zoneSetpointPath, _maximumSetPoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800301 }
Patrick Ventured8012182018-03-08 08:21:38 -0800302 return;
303}
304
Patrick Venture597ebd62020-08-11 08:48:19 -0700305void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800306{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700307 /* Print header for log file:
Josh Lehanb3005752022-02-22 20:48:07 -0800308 * 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 -0700309 */
Patrick Ventured8012182018-03-08 08:21:38 -0800310
Nirav Shahccc8bb62022-02-17 21:06:51 -0800311 _log << "epoch_ms,setpt,requester";
Patrick Ventured8012182018-03-08 08:21:38 -0800312
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700313 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800314 {
Josh Lehanb3005752022-02-22 20:48:07 -0800315 _log << "," << f << "," << f << "_raw";
316 _log << "," << f << "_pwm," << f << "_pwm_raw";
Patrick Ventured8012182018-03-08 08:21:38 -0800317 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700318 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700319 {
Josh Lehanb3005752022-02-22 20:48:07 -0800320 _log << "," << t << "," << t << "_raw";
Patrick Venture5f02ad22018-04-24 10:18:40 -0700321 }
Josh Lehanb3005752022-02-22 20:48:07 -0800322
Patrick Venture5f02ad22018-04-24 10:18:40 -0700323 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800324 _log << std::endl;
Patrick Ventured8012182018-03-08 08:21:38 -0800325}
326
Patrick Venture7a98c192020-08-12 08:35:16 -0700327void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800328{
Patrick Venture7a98c192020-08-12 08:35:16 -0700329 _log << value;
Patrick Ventured8012182018-03-08 08:21:38 -0800330}
Patrick Ventured8012182018-03-08 08:21:38 -0800331
332/*
333 * TODO(venture) This is effectively updating the cache and should check if the
334 * values they're using to update it are new or old, or whatnot. For instance,
335 * if we haven't heard from the host in X time we need to detect this failure.
336 *
337 * I haven't decided if the Sensor should have a lastUpdated method or whether
338 * that should be for the ReadInterface or etc...
339 */
340
341/**
342 * We want the PID loop to run with values cached, so this will get all the
343 * fan tachs for the loop.
344 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700345void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800346{
347 /* TODO(venture): Should I just make _log point to /dev/null when logging
348 * is disabled? I think it's a waste to try and log things even if the
349 * data is just being dropped though.
350 */
Tom Tungdf1f1832022-11-14 19:26:52 +0800351 const auto now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700352 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800353 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800354 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
355 now.time_since_epoch())
356 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700357 _log << "," << _maximumSetPoint;
Nirav Shahccc8bb62022-02-17 21:06:51 -0800358 _log << "," << _maximumSetPointName;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800359 }
Patrick Ventured8012182018-03-08 08:21:38 -0800360
Tom Tungdf1f1832022-11-14 19:26:52 +0800361 processSensorInputs</* fanSensorLogging */ true>(_fanInputs, now);
Patrick Ventured8012182018-03-08 08:21:38 -0800362
Patrick Venturede79ee02019-05-08 14:50:00 -0700363 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700364 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800365 for (const auto& t : _thermalInputs)
366 {
Josh Lehanb3005752022-02-22 20:48:07 -0800367 const auto& v = _cachedValuesByName[t];
368 _log << "," << v.scaled << "," << v.unscaled;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800369 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700370 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700371
Patrick Ventured8012182018-03-08 08:21:38 -0800372 return;
373}
374
Patrick Venture597ebd62020-08-11 08:48:19 -0700375void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800376{
Tom Tungdf1f1832022-11-14 19:26:52 +0800377 processSensorInputs</* fanSensorLogging */ false>(
378 _thermalInputs, std::chrono::high_resolution_clock::now());
Patrick Ventured8012182018-03-08 08:21:38 -0800379
380 return;
381}
382
Patrick Venture597ebd62020-08-11 08:48:19 -0700383void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800384{
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700385 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800386 {
Josh Lehand38ae272020-11-13 02:59:30 -0800387 _cachedValuesByName[f] = {0, 0};
388 _cachedFanOutputs[f] = {0, 0};
Will Liangded0ab52019-05-15 17:10:06 +0800389
390 // Start all fans in fail-safe mode.
391 _failSafeSensors.insert(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800392 }
393
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700394 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800395 {
Josh Lehand38ae272020-11-13 02:59:30 -0800396 _cachedValuesByName[t] = {0, 0};
Patrick Ventured8012182018-03-08 08:21:38 -0800397
398 // Start all sensors in fail-safe mode.
399 _failSafeSensors.insert(t);
400 }
401}
402
Patrick Venture597ebd62020-08-11 08:48:19 -0700403void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800404{
405 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700406 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800407 {
Josh Lehanb3005752022-02-22 20:48:07 -0800408 std::cerr << name << ": " << value.scaled << " " << value.unscaled
409 << "\n";
410 }
411
412 std::cerr << "Fan outputs now: \n";
413 for (const auto& [name, value] : _cachedFanOutputs)
414 {
415 std::cerr << name << ": " << value.scaled << " " << value.unscaled
416 << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800417 }
418}
419
Patrick Venture597ebd62020-08-11 08:48:19 -0700420void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800421{
422 for (auto& p : _fans)
423 {
James Feist22c257a2018-08-31 14:07:12 -0700424 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800425 }
Josh Lehana4146eb2020-10-01 11:49:09 -0700426
427 if (_redundantWrite)
428 {
429 // This is only needed once
430 _redundantWrite = false;
431 }
Patrick Ventured8012182018-03-08 08:21:38 -0800432}
433
Patrick Venture597ebd62020-08-11 08:48:19 -0700434void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800435{
436 for (auto& p : _thermals)
437 {
James Feist22c257a2018-08-31 14:07:12 -0700438 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800439 }
440}
441
Patrick Venture597ebd62020-08-11 08:48:19 -0700442Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800443{
Patrick Venturefe75b192018-06-08 11:19:43 -0700444 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800445}
446
Josh Lehana4146eb2020-10-01 11:49:09 -0700447bool DbusPidZone::getRedundantWrite(void) const
448{
449 return _redundantWrite;
450}
451
Patrick Venture597ebd62020-08-11 08:48:19 -0700452bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800453{
454 std::cerr << "manual: " << value << std::endl;
455 setManualMode(value);
456 return ModeObject::manual(value);
457}
458
Patrick Venture597ebd62020-08-11 08:48:19 -0700459bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800460{
461 return getFailSafeMode();
462}
Patrick Venturea0764872020-08-08 07:48:43 -0700463
464} // namespace pid_control