xref: /openbmc/phosphor-pid-control/main.cpp (revision 18d5bb18dcb4ebf7340b0b7a0b39daa887d530ce)
1e620656cSPatrick Venture /**
2e620656cSPatrick Venture  * Copyright 2017 Google Inc.
3e620656cSPatrick Venture  *
4e620656cSPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
5e620656cSPatrick Venture  * you may not use this file except in compliance with the License.
6e620656cSPatrick Venture  * You may obtain a copy of the License at
7e620656cSPatrick Venture  *
8e620656cSPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
9e620656cSPatrick Venture  *
10e620656cSPatrick Venture  * Unless required by applicable law or agreed to in writing, software
11e620656cSPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
12e620656cSPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13e620656cSPatrick Venture  * See the License for the specific language governing permissions and
14e620656cSPatrick Venture  * limitations under the License.
15e620656cSPatrick Venture  */
16e620656cSPatrick Venture 
177136a5aeSJames Feist #include "config.h"
18e620656cSPatrick Venture 
19ba8ffa73SPatrick Venture #include "build/buildjson.hpp"
20da4a5dd1SPatrick Venture #include "conf.hpp"
217f9d690dSPatrick Venture #include "dbus/dbusconfiguration.hpp"
22e620656cSPatrick Venture #include "interfaces.hpp"
235c7cc545SPatrick Venture #include "pid/builder.hpp"
24ba8ffa73SPatrick Venture #include "pid/buildjson.hpp"
25ce6a3f36SJames Feist #include "pid/pidloop.hpp"
26c32e3fc5SPatrick Venture #include "pid/tuning.hpp"
27e620656cSPatrick Venture #include "pid/zone.hpp"
285e929093SPatrick Venture #include "sensors/builder.hpp"
29ba8ffa73SPatrick Venture #include "sensors/buildjson.hpp"
30e620656cSPatrick Venture #include "sensors/manager.hpp"
31da4a5dd1SPatrick Venture #include "util.hpp"
32e620656cSPatrick Venture 
33b5cc37ceSPatrick Venture #include <CLI/CLI.hpp>
34ce6a3f36SJames Feist #include <boost/asio/io_context.hpp>
35ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp>
36a83a3eccSPatrick Venture #include <sdbusplus/asio/connection.hpp>
37a83a3eccSPatrick Venture #include <sdbusplus/bus.hpp>
38cb4c1a27SBruce Lee #include <sdbusplus/server/manager.hpp>
39a83a3eccSPatrick Venture 
40da4a5dd1SPatrick Venture #include <chrono>
417f9d690dSPatrick Venture #include <filesystem>
42da4a5dd1SPatrick Venture #include <iostream>
43ce6a3f36SJames Feist #include <list>
44da4a5dd1SPatrick Venture #include <map>
45da4a5dd1SPatrick Venture #include <memory>
46da4a5dd1SPatrick Venture #include <thread>
47da4a5dd1SPatrick Venture #include <unordered_map>
48ba8ffa73SPatrick Venture #include <utility>
49da4a5dd1SPatrick Venture #include <vector>
50e620656cSPatrick Venture 
51a076487aSPatrick Venture namespace pid_control
52a076487aSPatrick Venture {
53a076487aSPatrick Venture 
541fe08952SJames Feist /* The configuration converted sensor list. */
551df9e879SPatrick Venture std::map<std::string, conf::SensorConfig> sensorConfig = {};
561fe08952SJames Feist /* The configuration converted PID list. */
57f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
581fe08952SJames Feist /* The configuration converted Zone configuration. */
591df9e879SPatrick Venture std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
60e620656cSPatrick Venture 
61a076487aSPatrick Venture } // namespace pid_control
62a076487aSPatrick Venture 
63c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */
64c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
65e620656cSPatrick Venture std::string configPath = "";
66e620656cSPatrick Venture 
671fe08952SJames Feist /* async io context for operation */
681fe08952SJames Feist boost::asio::io_context io;
69e620656cSPatrick Venture 
701fe08952SJames Feist /* buses for system control */
711fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io);
721fe08952SJames Feist static sdbusplus::asio::connection
731fe08952SJames Feist     hostBus(io, sdbusplus::bus::new_system().release());
741fe08952SJames Feist static sdbusplus::asio::connection
751fe08952SJames Feist     passiveBus(io, sdbusplus::bus::new_system().release());
76de79ee05SPatrick Venture 
77a076487aSPatrick Venture namespace pid_control
78a076487aSPatrick Venture {
79a076487aSPatrick Venture 
801fe08952SJames Feist void restartControlLoops()
811fe08952SJames Feist {
821fe08952SJames Feist     static SensorManager mgmr;
835301aae3SJohnathan Mantey     static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
845301aae3SJohnathan Mantey     static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
85b6a0b89eSHao Jiang     static bool isCanceling = false;
86e620656cSPatrick Venture 
87*18d5bb18SWilliam A. Kennington III     for (const auto& timer : timers)
885301aae3SJohnathan Mantey     {
895301aae3SJohnathan Mantey         timer->cancel();
905301aae3SJohnathan Mantey     }
91b6a0b89eSHao Jiang     isCanceling = true;
921fe08952SJames Feist     timers.clear();
93b228cea2SHao Jiang 
94b228cea2SHao Jiang     if (zones.size() > 0 && zones.begin()->second.use_count() > 1)
95b228cea2SHao Jiang     {
96b228cea2SHao Jiang         throw std::runtime_error("wait for count back to 1");
97b228cea2SHao Jiang     }
985301aae3SJohnathan Mantey     zones.clear();
99b6a0b89eSHao Jiang     isCanceling = false;
1004cb7c058SPatrick Venture 
10118b1311eSPatrick Venture     const std::string& path =
10218b1311eSPatrick Venture         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
103e620656cSPatrick Venture 
1047f9d690dSPatrick Venture     if (std::filesystem::exists(path))
1057f9d690dSPatrick Venture     {
106e620656cSPatrick Venture         /*
1077f9d690dSPatrick Venture          * When building the sensors, if any of the dbus passive ones aren't on
1087f9d690dSPatrick Venture          * the bus, it'll fail immediately.
109e620656cSPatrick Venture          */
110e620656cSPatrick Venture         try
111e620656cSPatrick Venture         {
11218b1311eSPatrick Venture             auto jsonData = parseValidateJson(path);
1134cb7c058SPatrick Venture             sensorConfig = buildSensorsFromJson(jsonData);
1147f9d690dSPatrick Venture             std::tie(zoneConfig, zoneDetailsConfig) =
1157f9d690dSPatrick Venture                 buildPIDsFromJson(jsonData);
116e620656cSPatrick Venture         }
117e620656cSPatrick Venture         catch (const std::exception& e)
118e620656cSPatrick Venture         {
119e620656cSPatrick Venture             std::cerr << "Failed during building: " << e.what() << "\n";
120e620656cSPatrick Venture             exit(EXIT_FAILURE); /* fatal error. */
121e620656cSPatrick Venture         }
1227f9d690dSPatrick Venture     }
1237f9d690dSPatrick Venture     else
1247f9d690dSPatrick Venture     {
1257f9d690dSPatrick Venture         static boost::asio::steady_timer reloadTimer(io);
1267382318fSPatrick Venture         if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
1277382318fSPatrick Venture                                       zoneConfig, zoneDetailsConfig))
1287f9d690dSPatrick Venture         {
1297f9d690dSPatrick Venture             return; // configuration not ready
1307f9d690dSPatrick Venture         }
1317f9d690dSPatrick Venture     }
1324cb7c058SPatrick Venture 
1331fe08952SJames Feist     mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
1341fe08952SJames Feist     zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
135e620656cSPatrick Venture 
136e620656cSPatrick Venture     if (0 == zones.size())
137e620656cSPatrick Venture     {
138e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
1391fe08952SJames Feist         std::exit(EXIT_FAILURE);
140e620656cSPatrick Venture     }
141e620656cSPatrick Venture 
1421fe08952SJames Feist     for (const auto& i : zones)
1431fe08952SJames Feist     {
1445301aae3SJohnathan Mantey         std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back(
1455301aae3SJohnathan Mantey             std::make_shared<boost::asio::steady_timer>(io));
1461fe08952SJames Feist         std::cerr << "pushing zone " << i.first << "\n";
147b6a0b89eSHao Jiang         pidControlLoop(i.second, timer, &isCanceling);
1481fe08952SJames Feist     }
1491fe08952SJames Feist }
1501fe08952SJames Feist 
151b228cea2SHao Jiang void tryRestartControlLoops(bool first)
152298a95cbSYong Li {
153b228cea2SHao Jiang     static int count = 0;
154d11a732aSHao Jiang     static const auto delayTime = std::chrono::seconds(10);
155b228cea2SHao Jiang     static boost::asio::steady_timer timer(io);
156b228cea2SHao Jiang     // try to start a control loop while the loop has been scheduled.
157b228cea2SHao Jiang     if (first && count != 0)
158298a95cbSYong Li     {
159b228cea2SHao Jiang         std::cerr
160b228cea2SHao Jiang             << "ControlLoops has been scheduled, refresh the loop count\n";
161b228cea2SHao Jiang         count = 1;
162b228cea2SHao Jiang         return;
163b228cea2SHao Jiang     }
164d11a732aSHao Jiang 
165d11a732aSHao Jiang     auto restartLbd = [](const boost::system::error_code& error) {
166b228cea2SHao Jiang         if (error == boost::asio::error::operation_aborted)
167b228cea2SHao Jiang         {
168b228cea2SHao Jiang             return;
169b228cea2SHao Jiang         }
170232ffe4eSHao Jiang 
171232ffe4eSHao Jiang         // for the last loop, don't elminate the failure of restartControlLoops.
172232ffe4eSHao Jiang         if (count >= 5)
173232ffe4eSHao Jiang         {
174232ffe4eSHao Jiang             restartControlLoops();
175232ffe4eSHao Jiang             // reset count after succesful restartControlLoops()
176232ffe4eSHao Jiang             count = 0;
177232ffe4eSHao Jiang             return;
178232ffe4eSHao Jiang         }
179232ffe4eSHao Jiang 
180232ffe4eSHao Jiang         // retry when restartControlLoops() has some failure.
181298a95cbSYong Li         try
182298a95cbSYong Li         {
183298a95cbSYong Li             restartControlLoops();
184b228cea2SHao Jiang             // reset count after succesful restartControlLoops()
185b228cea2SHao Jiang             count = 0;
186298a95cbSYong Li         }
187298a95cbSYong Li         catch (const std::exception& e)
188298a95cbSYong Li         {
189298a95cbSYong Li             std::cerr << count
190298a95cbSYong Li                       << " Failed during restartControlLoops, try again: "
191298a95cbSYong Li                       << e.what() << "\n";
192b228cea2SHao Jiang             tryRestartControlLoops(false);
193298a95cbSYong Li         }
194d11a732aSHao Jiang     };
195d11a732aSHao Jiang     count++;
196d11a732aSHao Jiang     // first time of trying to restart the control loop without a delay
197d11a732aSHao Jiang     if (first)
198d11a732aSHao Jiang     {
199d11a732aSHao Jiang         boost::asio::post(io,
200d11a732aSHao Jiang                           std::bind(restartLbd, boost::system::error_code()));
201d11a732aSHao Jiang     }
202d11a732aSHao Jiang     // re-try control loop, set up a delay.
203d11a732aSHao Jiang     else
204d11a732aSHao Jiang     {
205d11a732aSHao Jiang         timer.expires_after(delayTime);
206d11a732aSHao Jiang         timer.async_wait(restartLbd);
207d11a732aSHao Jiang     }
208298a95cbSYong Li 
209298a95cbSYong Li     return;
210298a95cbSYong Li }
211298a95cbSYong Li 
212a076487aSPatrick Venture } // namespace pid_control
213a076487aSPatrick Venture 
2141fe08952SJames Feist int main(int argc, char* argv[])
2151fe08952SJames Feist {
2161fe08952SJames Feist     loggingPath = "";
2171fe08952SJames Feist     loggingEnabled = false;
2181fe08952SJames Feist     tuningEnabled = false;
2191fe08952SJames Feist 
2201fe08952SJames Feist     CLI::App app{"OpenBMC Fan Control Daemon"};
2211fe08952SJames Feist 
2221fe08952SJames Feist     app.add_option("-c,--conf", configPath,
2231fe08952SJames Feist                    "Optional parameter to specify configuration at run-time")
2241fe08952SJames Feist         ->check(CLI::ExistingFile);
2251fe08952SJames Feist     app.add_option("-l,--log", loggingPath,
2261fe08952SJames Feist                    "Optional parameter to specify logging folder")
2271fe08952SJames Feist         ->check(CLI::ExistingDirectory);
2281fe08952SJames Feist     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
2291fe08952SJames Feist 
2301fe08952SJames Feist     CLI11_PARSE(app, argc, argv);
2311fe08952SJames Feist 
232811f31d6SJosh Lehan     static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
233811f31d6SJosh Lehan     static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
234811f31d6SJosh Lehan 
235f54b260bSJosh Lehan     // Set up default logging path, preferring command line if it was given
236f54b260bSJosh Lehan     std::string defLoggingPath(loggingPath);
237f54b260bSJosh Lehan     if (defLoggingPath.empty())
238f54b260bSJosh Lehan     {
239f54b260bSJosh Lehan         defLoggingPath = std::filesystem::temp_directory_path();
240f54b260bSJosh Lehan     }
241f54b260bSJosh Lehan     else
242f54b260bSJosh Lehan     {
243f54b260bSJosh Lehan         // Enable logging, if user explicitly gave path on command line
244f54b260bSJosh Lehan         loggingEnabled = true;
245f54b260bSJosh Lehan     }
246f54b260bSJosh Lehan 
247811f31d6SJosh Lehan     // If this file exists, enable logging at runtime
248811f31d6SJosh Lehan     std::ifstream fsLogging(loggingEnablePath);
249811f31d6SJosh Lehan     if (fsLogging)
250811f31d6SJosh Lehan     {
251f54b260bSJosh Lehan         // The first line of file might be a valid directory path
252811f31d6SJosh Lehan         std::getline(fsLogging, loggingPath);
253811f31d6SJosh Lehan         fsLogging.close();
254811f31d6SJosh Lehan 
255f54b260bSJosh Lehan         // If so, use it, otherwise use default logging path instead
256f54b260bSJosh Lehan         if (!(std::filesystem::exists(loggingPath)))
257f54b260bSJosh Lehan         {
258f54b260bSJosh Lehan             loggingPath = defLoggingPath;
259f54b260bSJosh Lehan         }
260f54b260bSJosh Lehan 
261811f31d6SJosh Lehan         loggingEnabled = true;
262f54b260bSJosh Lehan     }
263f54b260bSJosh Lehan 
264f54b260bSJosh Lehan     if (loggingEnabled)
265f54b260bSJosh Lehan     {
266811f31d6SJosh Lehan         std::cerr << "Logging enabled: " << loggingPath << "\n";
267811f31d6SJosh Lehan     }
268811f31d6SJosh Lehan 
269811f31d6SJosh Lehan     // If this file exists, enable tuning at runtime
270811f31d6SJosh Lehan     if (std::filesystem::exists(tuningEnablePath))
271811f31d6SJosh Lehan     {
272811f31d6SJosh Lehan         tuningEnabled = true;
273f54b260bSJosh Lehan     }
274f54b260bSJosh Lehan 
275f54b260bSJosh Lehan     // This can also be enabled from the command line
276f54b260bSJosh Lehan     if (tuningEnabled)
277f54b260bSJosh Lehan     {
278811f31d6SJosh Lehan         std::cerr << "Tuning enabled\n";
279811f31d6SJosh Lehan     }
280d4695590SKun Yi 
2811fe08952SJames Feist     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
2821fe08952SJames Feist     // Create a manager for the ModeBus because we own it.
2831fe08952SJames Feist     sdbusplus::server::manager::manager(
2841fe08952SJames Feist         static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot);
2851fe08952SJames Feist     hostBus.request_name("xyz.openbmc_project.Hwmon.external");
2861fe08952SJames Feist     modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
287cb4c1a27SBruce Lee     sdbusplus::server::manager::manager objManager(modeControlBus, modeRoot);
2881fe08952SJames Feist 
289e620656cSPatrick Venture     /*
290e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
291e620656cSPatrick Venture      * it.
292e620656cSPatrick Venture      */
293e620656cSPatrick Venture 
294a076487aSPatrick Venture     pid_control::tryRestartControlLoops();
295e620656cSPatrick Venture 
296ce6a3f36SJames Feist     io.run();
2971fe08952SJames Feist     return 0;
298e620656cSPatrick Venture }
299