1 /**
2  * Copyright 2017 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "pidloop.hpp"
18 
19 #include "pid/pidcontroller.hpp"
20 #include "pid/tuning.hpp"
21 #include "sensors/sensor.hpp"
22 
23 #include <boost/asio/steady_timer.hpp>
24 
25 #include <chrono>
26 #include <map>
27 #include <memory>
28 #include <thread>
29 #include <vector>
30 
31 static void processThermals(PIDZone* zone)
32 {
33     // Get the latest margins.
34     zone->updateSensors();
35     // Zero out the set point goals.
36     zone->clearSetPoints();
37     zone->clearRPMCeilings();
38     // Run the margin PIDs.
39     zone->processThermals();
40     // Get the maximum RPM setpoint.
41     zone->determineMaxSetPointRequest();
42 }
43 
44 void pidControlLoop(PIDZone* zone, boost::asio::steady_timer& timer, bool first,
45                     int ms100cnt)
46 {
47     if (first)
48     {
49         if (loggingEnabled)
50         {
51             zone->initializeLog();
52         }
53 
54         zone->initializeCache();
55         processThermals(zone);
56     }
57 
58     timer.expires_after(std::chrono::milliseconds(100));
59     timer.async_wait(
60         [zone, &timer, ms100cnt](const boost::system::error_code& ec) mutable {
61             if (ec == boost::asio::error::operation_aborted)
62             {
63                 return; // timer being canceled, stop loop
64             }
65 
66             /*
67              * This should sleep on the conditional wait for the listen thread
68              * to tell us it's in sync.  But then we also need a timeout option
69              * in case phosphor-hwmon is down, we can go into some weird failure
70              * more.
71              *
72              * Another approach would be to start all sensors in worst-case
73              * values, and fail-safe mode and then clear out of fail-safe mode
74              * once we start getting values.  Which I think it is a solid
75              * approach.
76              *
77              * For now this runs before it necessarily has any sensor values.
78              * For the host sensors they start out in fail-safe mode.  For the
79              * fans, they start out as 0 as input and then are adjusted once
80              * they have values.
81              *
82              * If a fan has failed, it's value will be whatever we're told or
83              * however we retrieve it.  This program disregards fan values of 0,
84              * so any code providing a fan speed can set to 0 on failure and
85              * that fan value will be effectively ignored.  The PID algorithm
86              * will be unhappy but nothing bad will happen.
87              *
88              * TODO(venture): If the fan value is 0 should that loop just be
89              * skipped? Right now, a 0 value is ignored in
90              * FanController::inputProc()
91              */
92 
93             // Check if we should just go back to sleep.
94             if (zone->getManualMode())
95             {
96                 pidControlLoop(zone, timer, false, ms100cnt);
97                 return;
98             }
99 
100             // Get the latest fan speeds.
101             zone->updateFanTelemetry();
102 
103             if (10 <= ms100cnt)
104             {
105                 ms100cnt = 0;
106 
107                 processThermals(zone);
108             }
109 
110             // Run the fan PIDs every iteration.
111             zone->processFans();
112 
113             if (loggingEnabled)
114             {
115                 zone->getLogHandle() << "," << zone->getFailSafeMode();
116                 zone->getLogHandle() << std::endl;
117             }
118 
119             ms100cnt += 1;
120 
121             pidControlLoop(zone, timer, false, ms100cnt);
122         });
123 }
124