blob: 441031a78f1989c46a30f4ee2846fe3acc1b32e2 [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;
85}
86
Patrick Venture597ebd62020-08-11 08:48:19 -070087bool DbusPidZone::getFailSafeMode(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080088{
89 // If any keys are present at least one sensor is in fail safe mode.
90 return !_failSafeSensors.empty();
91}
92
Patrick Venture597ebd62020-08-11 08:48:19 -070093int64_t DbusPidZone::getZoneID(void) const
Patrick Ventured8012182018-03-08 08:21:38 -080094{
95 return _zoneId;
96}
97
Patrick Venture597ebd62020-08-11 08:48:19 -070098void DbusPidZone::addSetPoint(double setpoint)
Patrick Ventured8012182018-03-08 08:21:38 -080099{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700100 _SetPoints.push_back(setpoint);
Patrick Ventured8012182018-03-08 08:21:38 -0800101}
102
Patrick Venture597ebd62020-08-11 08:48:19 -0700103void DbusPidZone::addRPMCeiling(double ceiling)
James Feist608304d2019-02-25 10:01:42 -0800104{
105 _RPMCeilings.push_back(ceiling);
106}
107
Patrick Venture597ebd62020-08-11 08:48:19 -0700108void DbusPidZone::clearRPMCeilings(void)
James Feist608304d2019-02-25 10:01:42 -0800109{
110 _RPMCeilings.clear();
111}
112
Patrick Venture597ebd62020-08-11 08:48:19 -0700113void DbusPidZone::clearSetPoints(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800114{
Patrick Venture9bbf3332019-07-16 10:50:37 -0700115 _SetPoints.clear();
Patrick Ventured8012182018-03-08 08:21:38 -0800116}
117
Patrick Venture597ebd62020-08-11 08:48:19 -0700118double DbusPidZone::getFailSafePercent(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800119{
120 return _failSafePercent;
121}
122
Patrick Venture597ebd62020-08-11 08:48:19 -0700123double DbusPidZone::getMinThermalSetpoint(void) const
Patrick Ventured8012182018-03-08 08:21:38 -0800124{
James Feist3484bed2019-02-25 13:28:18 -0800125 return _minThermalOutputSetPt;
Patrick Ventured8012182018-03-08 08:21:38 -0800126}
127
Patrick Venture597ebd62020-08-11 08:48:19 -0700128void DbusPidZone::addFanPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800129{
130 _fans.push_back(std::move(pid));
131}
132
Patrick Venture597ebd62020-08-11 08:48:19 -0700133void DbusPidZone::addThermalPID(std::unique_ptr<Controller> pid)
Patrick Ventured8012182018-03-08 08:21:38 -0800134{
135 _thermals.push_back(std::move(pid));
136}
137
Patrick Venture597ebd62020-08-11 08:48:19 -0700138double DbusPidZone::getCachedValue(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800139{
140 return _cachedValuesByName.at(name);
141}
142
Patrick Venture597ebd62020-08-11 08:48:19 -0700143void DbusPidZone::addFanInput(const std::string& fan)
Patrick Ventured8012182018-03-08 08:21:38 -0800144{
145 _fanInputs.push_back(fan);
146}
147
Patrick Venture597ebd62020-08-11 08:48:19 -0700148void DbusPidZone::addThermalInput(const std::string& therm)
Patrick Ventured8012182018-03-08 08:21:38 -0800149{
150 _thermalInputs.push_back(therm);
151}
152
Josh Lehan55ccad62020-09-20 23:57:49 -0700153// Updates desired RPM setpoint from optional text file
154// Returns true if rpmValue updated, false if left unchanged
155static bool fileParseRpm(const std::string& fileName, double& rpmValue)
156{
157 static constexpr std::chrono::seconds throttlePace{3};
158
159 std::string errText;
160
161 try
162 {
163 std::ifstream ifs;
164 ifs.open(fileName);
165 if (ifs)
166 {
167 int value;
168 ifs >> value;
169
170 if (value <= 0)
171 {
172 errText = "File content could not be parsed to a number";
173 }
174 else if (value <= 100)
175 {
176 errText = "File must contain RPM value, not PWM value";
177 }
178 else
179 {
180 rpmValue = static_cast<double>(value);
181 return true;
182 }
183 }
184 }
185 catch (const std::exception& e)
186 {
187 errText = "Exception: ";
188 errText += e.what();
189 }
190
191 // The file is optional, intentionally not an error if file not found
192 if (!(errText.empty()))
193 {
194 tstamp now = std::chrono::high_resolution_clock::now();
195 if (allowThrottle(now, throttlePace))
196 {
197 std::cerr << "Unable to read from '" << fileName << "': " << errText
198 << "\n";
199 }
200 }
201
202 return false;
203}
204
Patrick Venture597ebd62020-08-11 08:48:19 -0700205void DbusPidZone::determineMaxSetPointRequest(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800206{
Patrick Venture5f59c0f2018-11-11 12:55:14 -0800207 double max = 0;
208 std::vector<double>::iterator result;
Patrick Ventured8012182018-03-08 08:21:38 -0800209
Patrick Venture9bbf3332019-07-16 10:50:37 -0700210 if (_SetPoints.size() > 0)
Patrick Ventured8012182018-03-08 08:21:38 -0800211 {
Patrick Venture9bbf3332019-07-16 10:50:37 -0700212 result = std::max_element(_SetPoints.begin(), _SetPoints.end());
Patrick Ventured8012182018-03-08 08:21:38 -0800213 max = *result;
214 }
215
James Feist608304d2019-02-25 10:01:42 -0800216 if (_RPMCeilings.size() > 0)
217 {
218 result = std::min_element(_RPMCeilings.begin(), _RPMCeilings.end());
219 max = std::min(max, *result);
220 }
221
Patrick Ventured8012182018-03-08 08:21:38 -0800222 /*
Patrick Venture7280e272019-02-11 10:45:32 -0800223 * If the maximum RPM setpoint output is below the minimum RPM
224 * setpoint, set it to the minimum.
Patrick Ventured8012182018-03-08 08:21:38 -0800225 */
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700226 max = std::max(getMinThermalSetpoint(), max);
Patrick Ventured8012182018-03-08 08:21:38 -0800227
Patrick Venturede79ee02019-05-08 14:50:00 -0700228 if (tuningEnabled)
Patrick Ventured8012182018-03-08 08:21:38 -0800229 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800230 /*
231 * We received no setpoints from thermal sensors.
232 * This is a case experienced during tuning where they only specify
233 * fan sensors and one large fan PID for all the fans.
234 */
235 static constexpr auto setpointpath = "/etc/thermal.d/setpoint";
Patrick Venturedf766f22018-10-13 09:30:58 -0700236
Josh Lehan55ccad62020-09-20 23:57:49 -0700237 fileParseRpm(setpointpath, max);
238
239 // Allow per-zone setpoint files to override overall setpoint file
240 std::ostringstream zoneSuffix;
241 zoneSuffix << ".zone" << _zoneId;
242 std::string zoneSetpointPath = setpointpath + zoneSuffix.str();
243
244 fileParseRpm(zoneSetpointPath, max);
Patrick Ventured8012182018-03-08 08:21:38 -0800245 }
Patrick Ventured8012182018-03-08 08:21:38 -0800246
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700247 _maximumSetPoint = max;
Patrick Ventured8012182018-03-08 08:21:38 -0800248 return;
249}
250
Patrick Venture597ebd62020-08-11 08:48:19 -0700251void DbusPidZone::initializeLog(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800252{
Patrick Venture5f02ad22018-04-24 10:18:40 -0700253 /* Print header for log file:
254 * epoch_ms,setpt,fan1,fan2,fanN,sensor1,sensor2,sensorN,failsafe
255 */
Patrick Ventured8012182018-03-08 08:21:38 -0800256
257 _log << "epoch_ms,setpt";
258
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700259 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800260 {
261 _log << "," << f;
262 }
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700263 for (const auto& t : _thermalInputs)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700264 {
265 _log << "," << t;
266 }
267 _log << ",failsafe";
Patrick Ventured8012182018-03-08 08:21:38 -0800268 _log << std::endl;
269
270 return;
271}
272
Patrick Venture7a98c192020-08-12 08:35:16 -0700273void DbusPidZone::writeLog(const std::string& value)
Patrick Ventured8012182018-03-08 08:21:38 -0800274{
Patrick Venture7a98c192020-08-12 08:35:16 -0700275 _log << value;
276 return;
Patrick Ventured8012182018-03-08 08:21:38 -0800277}
Patrick Ventured8012182018-03-08 08:21:38 -0800278
279/*
280 * TODO(venture) This is effectively updating the cache and should check if the
281 * values they're using to update it are new or old, or whatnot. For instance,
282 * if we haven't heard from the host in X time we need to detect this failure.
283 *
284 * I haven't decided if the Sensor should have a lastUpdated method or whether
285 * that should be for the ReadInterface or etc...
286 */
287
288/**
289 * We want the PID loop to run with values cached, so this will get all the
290 * fan tachs for the loop.
291 */
Patrick Venture597ebd62020-08-11 08:48:19 -0700292void DbusPidZone::updateFanTelemetry(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800293{
294 /* TODO(venture): Should I just make _log point to /dev/null when logging
295 * is disabled? I think it's a waste to try and log things even if the
296 * data is just being dropped though.
297 */
Will Liangded0ab52019-05-15 17:10:06 +0800298 tstamp now = std::chrono::high_resolution_clock::now();
Patrick Venturede79ee02019-05-08 14:50:00 -0700299 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800300 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800301 _log << std::chrono::duration_cast<std::chrono::milliseconds>(
302 now.time_since_epoch())
303 .count();
Patrick Venturef7a2dd52019-07-16 14:31:13 -0700304 _log << "," << _maximumSetPoint;
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800305 }
Patrick Ventured8012182018-03-08 08:21:38 -0800306
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700307 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800308 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700309 auto sensor = _mgr.getSensor(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800310 ReadReturn r = sensor->read();
311 _cachedValuesByName[f] = r.value;
Will Liangded0ab52019-05-15 17:10:06 +0800312 int64_t timeout = sensor->getTimeout();
313 tstamp then = r.updated;
Patrick Ventured8012182018-03-08 08:21:38 -0800314
Will Liangded0ab52019-05-15 17:10:06 +0800315 auto duration =
316 std::chrono::duration_cast<std::chrono::seconds>(now - then)
317 .count();
318 auto period = std::chrono::seconds(timeout).count();
Patrick Ventured8012182018-03-08 08:21:38 -0800319 /*
320 * TODO(venture): We should check when these were last read.
321 * However, these are the fans, so if I'm not getting updated values
322 * for them... what should I do?
323 */
Patrick Venturede79ee02019-05-08 14:50:00 -0700324 if (loggingEnabled)
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800325 {
326 _log << "," << r.value;
327 }
Will Liangded0ab52019-05-15 17:10:06 +0800328
329 // check if fan fail.
330 if (sensor->getFailed())
331 {
332 _failSafeSensors.insert(f);
333 }
334 else if (timeout != 0 && duration >= period)
335 {
336 _failSafeSensors.insert(f);
337 }
338 else
339 {
340 // Check if it's in there: remove it.
341 auto kt = _failSafeSensors.find(f);
342 if (kt != _failSafeSensors.end())
343 {
344 _failSafeSensors.erase(kt);
345 }
346 }
Patrick Ventured8012182018-03-08 08:21:38 -0800347 }
348
Patrick Venturede79ee02019-05-08 14:50:00 -0700349 if (loggingEnabled)
Patrick Venture5f02ad22018-04-24 10:18:40 -0700350 {
Patrick Venturec32e3fc2019-02-28 10:01:11 -0800351 for (const auto& t : _thermalInputs)
352 {
353 _log << "," << _cachedValuesByName[t];
354 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700355 }
Patrick Venture5f02ad22018-04-24 10:18:40 -0700356
Patrick Ventured8012182018-03-08 08:21:38 -0800357 return;
358}
359
Patrick Venture597ebd62020-08-11 08:48:19 -0700360void DbusPidZone::updateSensors(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800361{
362 using namespace std::chrono;
363 /* margin and temp are stored as temp */
364 tstamp now = high_resolution_clock::now();
365
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700366 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800367 {
Patrick Venturea58197c2018-06-11 15:29:45 -0700368 auto sensor = _mgr.getSensor(t);
Patrick Ventured8012182018-03-08 08:21:38 -0800369 ReadReturn r = sensor->read();
Patrick Venture563a3562018-10-30 09:31:26 -0700370 int64_t timeout = sensor->getTimeout();
Patrick Ventured8012182018-03-08 08:21:38 -0800371
372 _cachedValuesByName[t] = r.value;
373 tstamp then = r.updated;
374
James Feist473d68d2019-02-25 09:19:13 -0800375 auto duration = duration_cast<std::chrono::seconds>(now - then).count();
376 auto period = std::chrono::seconds(timeout).count();
377
James Feist36b7d8e2018-10-05 15:39:01 -0700378 if (sensor->getFailed())
379 {
380 _failSafeSensors.insert(t);
381 }
James Feist473d68d2019-02-25 09:19:13 -0800382 else if (timeout != 0 && duration >= period)
Patrick Ventured8012182018-03-08 08:21:38 -0800383 {
James Feist473d68d2019-02-25 09:19:13 -0800384 // std::cerr << "Entering fail safe mode.\n";
385 _failSafeSensors.insert(t);
386 }
387 else
388 {
389 // Check if it's in there: remove it.
390 auto kt = _failSafeSensors.find(t);
391 if (kt != _failSafeSensors.end())
Patrick Ventured8012182018-03-08 08:21:38 -0800392 {
James Feist473d68d2019-02-25 09:19:13 -0800393 _failSafeSensors.erase(kt);
Patrick Ventured8012182018-03-08 08:21:38 -0800394 }
395 }
396 }
397
398 return;
399}
400
Patrick Venture597ebd62020-08-11 08:48:19 -0700401void DbusPidZone::initializeCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800402{
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700403 for (const auto& f : _fanInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800404 {
405 _cachedValuesByName[f] = 0;
Will Liangded0ab52019-05-15 17:10:06 +0800406
407 // Start all fans in fail-safe mode.
408 _failSafeSensors.insert(f);
Patrick Ventured8012182018-03-08 08:21:38 -0800409 }
410
Patrick Venture4a2dc4d2018-10-23 09:02:55 -0700411 for (const auto& t : _thermalInputs)
Patrick Ventured8012182018-03-08 08:21:38 -0800412 {
413 _cachedValuesByName[t] = 0;
414
415 // Start all sensors in fail-safe mode.
416 _failSafeSensors.insert(t);
417 }
418}
419
Patrick Venture597ebd62020-08-11 08:48:19 -0700420void DbusPidZone::dumpCache(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800421{
422 std::cerr << "Cache values now: \n";
Patrick Venture2a50eda2020-08-16 08:26:35 -0700423 for (const auto& [name, value] : _cachedValuesByName)
Patrick Ventured8012182018-03-08 08:21:38 -0800424 {
Patrick Venture2a50eda2020-08-16 08:26:35 -0700425 std::cerr << name << ": " << value << "\n";
Patrick Ventured8012182018-03-08 08:21:38 -0800426 }
427}
428
Patrick Venture597ebd62020-08-11 08:48:19 -0700429void DbusPidZone::processFans(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800430{
431 for (auto& p : _fans)
432 {
James Feist22c257a2018-08-31 14:07:12 -0700433 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800434 }
435}
436
Patrick Venture597ebd62020-08-11 08:48:19 -0700437void DbusPidZone::processThermals(void)
Patrick Ventured8012182018-03-08 08:21:38 -0800438{
439 for (auto& p : _thermals)
440 {
James Feist22c257a2018-08-31 14:07:12 -0700441 p->process();
Patrick Ventured8012182018-03-08 08:21:38 -0800442 }
443}
444
Patrick Venture597ebd62020-08-11 08:48:19 -0700445Sensor* DbusPidZone::getSensor(const std::string& name)
Patrick Ventured8012182018-03-08 08:21:38 -0800446{
Patrick Venturefe75b192018-06-08 11:19:43 -0700447 return _mgr.getSensor(name);
Patrick Ventured8012182018-03-08 08:21:38 -0800448}
449
Patrick Venture597ebd62020-08-11 08:48:19 -0700450bool DbusPidZone::manual(bool value)
Patrick Ventured8012182018-03-08 08:21:38 -0800451{
452 std::cerr << "manual: " << value << std::endl;
453 setManualMode(value);
454 return ModeObject::manual(value);
455}
456
Patrick Venture597ebd62020-08-11 08:48:19 -0700457bool DbusPidZone::failSafe() const
Patrick Ventured8012182018-03-08 08:21:38 -0800458{
459 return getFailSafeMode();
460}
Patrick Venturea0764872020-08-08 07:48:43 -0700461
462} // namespace pid_control