xref: /openbmc/phosphor-pid-control/main.cpp (revision cdc8dca44b5a60af4bb86598043b54f955b81bcd)
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 
193dcfd546SHarvey Wu #include "buildjson/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>
35e2ec69adSPotin Lai #include <boost/asio/signal_set.hpp>
36ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp>
37a83a3eccSPatrick Venture #include <sdbusplus/asio/connection.hpp>
38a83a3eccSPatrick Venture #include <sdbusplus/bus.hpp>
39cb4c1a27SBruce Lee #include <sdbusplus/server/manager.hpp>
40a83a3eccSPatrick Venture 
41da4a5dd1SPatrick Venture #include <chrono>
427f9d690dSPatrick Venture #include <filesystem>
43da4a5dd1SPatrick Venture #include <iostream>
44ce6a3f36SJames Feist #include <list>
45da4a5dd1SPatrick Venture #include <map>
46da4a5dd1SPatrick Venture #include <memory>
47da4a5dd1SPatrick Venture #include <thread>
48da4a5dd1SPatrick Venture #include <unordered_map>
49ba8ffa73SPatrick Venture #include <utility>
50da4a5dd1SPatrick Venture #include <vector>
51e620656cSPatrick Venture 
52a076487aSPatrick Venture namespace pid_control
53a076487aSPatrick Venture {
54a076487aSPatrick Venture 
551fe08952SJames Feist /* The configuration converted sensor list. */
561df9e879SPatrick Venture std::map<std::string, conf::SensorConfig> sensorConfig = {};
571fe08952SJames Feist /* The configuration converted PID list. */
58f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
591fe08952SJames Feist /* The configuration converted Zone configuration. */
601df9e879SPatrick Venture std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
61e620656cSPatrick Venture 
62a076487aSPatrick Venture } // namespace pid_control
63a076487aSPatrick Venture 
6479cde00aSPotin Lai std::filesystem::path configPath = "";
65e620656cSPatrick Venture 
661fe08952SJames Feist /* async io context for operation */
671fe08952SJames Feist boost::asio::io_context io;
68e2ec69adSPotin Lai /* async signal_set for signal handling */
69e2ec69adSPotin Lai boost::asio::signal_set signals(io, SIGHUP);
70e620656cSPatrick Venture 
711fe08952SJames Feist /* buses for system control */
721fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io);
731fe08952SJames Feist static sdbusplus::asio::connection
741fe08952SJames Feist     hostBus(io, sdbusplus::bus::new_system().release());
751fe08952SJames Feist static sdbusplus::asio::connection
761fe08952SJames Feist     passiveBus(io, sdbusplus::bus::new_system().release());
77de79ee05SPatrick Venture 
78a076487aSPatrick Venture namespace pid_control
79a076487aSPatrick Venture {
80a076487aSPatrick Venture 
8179cde00aSPotin Lai std::filesystem::path searchConfigurationPath()
8279cde00aSPotin Lai {
8379cde00aSPotin Lai     static constexpr auto name = "config.json";
8479cde00aSPotin Lai 
8579cde00aSPotin Lai     for (auto pathSeg : {std::filesystem::current_path(),
8679cde00aSPotin Lai                          std::filesystem::path{"/var/lib/swampd"},
8779cde00aSPotin Lai                          std::filesystem::path{"/usr/share/swampd"}})
8879cde00aSPotin Lai     {
8979cde00aSPotin Lai         auto file = pathSeg / name;
9079cde00aSPotin Lai         if (std::filesystem::exists(file))
9179cde00aSPotin Lai         {
9279cde00aSPotin Lai             return file;
9379cde00aSPotin Lai         }
9479cde00aSPotin Lai     }
9579cde00aSPotin Lai 
9679cde00aSPotin Lai     return name;
9779cde00aSPotin Lai }
9879cde00aSPotin Lai 
991fe08952SJames Feist void restartControlLoops()
1001fe08952SJames Feist {
1011fe08952SJames Feist     static SensorManager mgmr;
1025301aae3SJohnathan Mantey     static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
1035301aae3SJohnathan Mantey     static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
104b6a0b89eSHao Jiang     static bool isCanceling = false;
105e620656cSPatrick Venture 
10618d5bb18SWilliam A. Kennington III     for (const auto& timer : timers)
1075301aae3SJohnathan Mantey     {
1085301aae3SJohnathan Mantey         timer->cancel();
1095301aae3SJohnathan Mantey     }
110b6a0b89eSHao Jiang     isCanceling = true;
1111fe08952SJames Feist     timers.clear();
112b228cea2SHao Jiang 
113b228cea2SHao Jiang     if (zones.size() > 0 && zones.begin()->second.use_count() > 1)
114b228cea2SHao Jiang     {
115b228cea2SHao Jiang         throw std::runtime_error("wait for count back to 1");
116b228cea2SHao Jiang     }
1175301aae3SJohnathan Mantey     zones.clear();
118b6a0b89eSHao Jiang     isCanceling = false;
1194cb7c058SPatrick Venture 
12079cde00aSPotin Lai     const std::filesystem::path path =
12179cde00aSPotin Lai         (!configPath.empty()) ? configPath : searchConfigurationPath();
122e620656cSPatrick Venture 
1237f9d690dSPatrick Venture     if (std::filesystem::exists(path))
1247f9d690dSPatrick Venture     {
125e620656cSPatrick Venture         /*
1267f9d690dSPatrick Venture          * When building the sensors, if any of the dbus passive ones aren't on
1277f9d690dSPatrick Venture          * the bus, it'll fail immediately.
128e620656cSPatrick Venture          */
129e620656cSPatrick Venture         try
130e620656cSPatrick Venture         {
13118b1311eSPatrick Venture             auto jsonData = parseValidateJson(path);
1324cb7c058SPatrick Venture             sensorConfig = buildSensorsFromJson(jsonData);
1337f9d690dSPatrick Venture             std::tie(zoneConfig, zoneDetailsConfig) =
1347f9d690dSPatrick Venture                 buildPIDsFromJson(jsonData);
135e620656cSPatrick Venture         }
136e620656cSPatrick Venture         catch (const std::exception& e)
137e620656cSPatrick Venture         {
138e620656cSPatrick Venture             std::cerr << "Failed during building: " << e.what() << "\n";
139e620656cSPatrick Venture             exit(EXIT_FAILURE); /* fatal error. */
140e620656cSPatrick Venture         }
1417f9d690dSPatrick Venture     }
1427f9d690dSPatrick Venture     else
1437f9d690dSPatrick Venture     {
1447f9d690dSPatrick Venture         static boost::asio::steady_timer reloadTimer(io);
1457382318fSPatrick Venture         if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
1467382318fSPatrick Venture                                       zoneConfig, zoneDetailsConfig))
1477f9d690dSPatrick Venture         {
1487f9d690dSPatrick Venture             return; // configuration not ready
1497f9d690dSPatrick Venture         }
1507f9d690dSPatrick Venture     }
1514cb7c058SPatrick Venture 
1521fe08952SJames Feist     mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
1531fe08952SJames Feist     zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
154e620656cSPatrick Venture 
155e620656cSPatrick Venture     if (0 == zones.size())
156e620656cSPatrick Venture     {
157e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
1581fe08952SJames Feist         std::exit(EXIT_FAILURE);
159e620656cSPatrick Venture     }
160e620656cSPatrick Venture 
1611fe08952SJames Feist     for (const auto& i : zones)
1621fe08952SJames Feist     {
1635301aae3SJohnathan Mantey         std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back(
1645301aae3SJohnathan Mantey             std::make_shared<boost::asio::steady_timer>(io));
1651fe08952SJames Feist         std::cerr << "pushing zone " << i.first << "\n";
166b6a0b89eSHao Jiang         pidControlLoop(i.second, timer, &isCanceling);
1671fe08952SJames Feist     }
1681fe08952SJames Feist }
1691fe08952SJames Feist 
170b228cea2SHao Jiang void tryRestartControlLoops(bool first)
171298a95cbSYong Li {
172d11a732aSHao Jiang     static const auto delayTime = std::chrono::seconds(10);
173b228cea2SHao Jiang     static boost::asio::steady_timer timer(io);
174d11a732aSHao Jiang 
175d11a732aSHao Jiang     auto restartLbd = [](const boost::system::error_code& error) {
176b228cea2SHao Jiang         if (error == boost::asio::error::operation_aborted)
177b228cea2SHao Jiang         {
178b228cea2SHao Jiang             return;
179b228cea2SHao Jiang         }
180232ffe4eSHao Jiang 
181232ffe4eSHao Jiang         // retry when restartControlLoops() has some failure.
182298a95cbSYong Li         try
183298a95cbSYong Li         {
184298a95cbSYong Li             restartControlLoops();
185298a95cbSYong Li         }
186298a95cbSYong Li         catch (const std::exception& e)
187298a95cbSYong Li         {
1885293ec2eSZev Weiss             std::cerr << "Failed during restartControlLoops, try again: "
189298a95cbSYong Li                       << e.what() << "\n";
190b228cea2SHao Jiang             tryRestartControlLoops(false);
191298a95cbSYong Li         }
192d11a732aSHao Jiang     };
1935293ec2eSZev Weiss 
194d11a732aSHao Jiang     // first time of trying to restart the control loop without a delay
195d11a732aSHao Jiang     if (first)
196d11a732aSHao Jiang     {
197d11a732aSHao Jiang         boost::asio::post(io,
198d11a732aSHao Jiang                           std::bind(restartLbd, boost::system::error_code()));
199d11a732aSHao Jiang     }
200d11a732aSHao Jiang     // re-try control loop, set up a delay.
201d11a732aSHao Jiang     else
202d11a732aSHao Jiang     {
203d11a732aSHao Jiang         timer.expires_after(delayTime);
204d11a732aSHao Jiang         timer.async_wait(restartLbd);
205d11a732aSHao Jiang     }
206298a95cbSYong Li 
207298a95cbSYong Li     return;
208298a95cbSYong Li }
209298a95cbSYong Li 
210a076487aSPatrick Venture } // namespace pid_control
211a076487aSPatrick Venture 
212e2ec69adSPotin Lai void sighupHandler(const boost::system::error_code& error, int signal_number)
213e2ec69adSPotin Lai {
214e2ec69adSPotin Lai     static boost::asio::steady_timer timer(io);
215e2ec69adSPotin Lai 
216e2ec69adSPotin Lai     if (error)
217e2ec69adSPotin Lai     {
218e2ec69adSPotin Lai         std::cout << "Signal " << signal_number
219e2ec69adSPotin Lai                   << " handler error: " << error.message() << "\n";
220e2ec69adSPotin Lai         return;
221e2ec69adSPotin Lai     }
222e2ec69adSPotin Lai 
223e2ec69adSPotin Lai     timer.expires_after(std::chrono::seconds(1));
224e2ec69adSPotin Lai     timer.async_wait([](const boost::system::error_code ec) {
225e2ec69adSPotin Lai         if (ec)
226e2ec69adSPotin Lai         {
227e2ec69adSPotin Lai             std::cout << "Signal timer error: " << ec.message() << "\n";
228e2ec69adSPotin Lai             return;
229e2ec69adSPotin Lai         }
230e2ec69adSPotin Lai 
231e2ec69adSPotin Lai         std::cout << "reloading configuration\n";
232e2ec69adSPotin Lai         pid_control::tryRestartControlLoops();
233e2ec69adSPotin Lai     });
234e2ec69adSPotin Lai     signals.async_wait(sighupHandler);
235e2ec69adSPotin Lai }
236e2ec69adSPotin Lai 
2371fe08952SJames Feist int main(int argc, char* argv[])
2381fe08952SJames Feist {
2391fe08952SJames Feist     loggingPath = "";
2401fe08952SJames Feist     loggingEnabled = false;
2411fe08952SJames Feist     tuningEnabled = false;
242c51ba919SBonnie Lo     debugEnabled = false;
243de74542cSJosh Lehan     coreLoggingEnabled = false;
2441fe08952SJames Feist 
2451fe08952SJames Feist     CLI::App app{"OpenBMC Fan Control Daemon"};
2461fe08952SJames Feist 
2471fe08952SJames Feist     app.add_option("-c,--conf", configPath,
2481fe08952SJames Feist                    "Optional parameter to specify configuration at run-time")
2491fe08952SJames Feist         ->check(CLI::ExistingFile);
2501fe08952SJames Feist     app.add_option("-l,--log", loggingPath,
2511fe08952SJames Feist                    "Optional parameter to specify logging folder")
2521fe08952SJames Feist         ->check(CLI::ExistingDirectory);
2531fe08952SJames Feist     app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
254c51ba919SBonnie Lo     app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode");
255de74542cSJosh Lehan     app.add_flag("-g,--corelogging", coreLoggingEnabled,
256de74542cSJosh Lehan                  "Enable or disable logging of core PID loop computations");
2571fe08952SJames Feist 
2581fe08952SJames Feist     CLI11_PARSE(app, argc, argv);
2591fe08952SJames Feist 
260811f31d6SJosh Lehan     static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
261811f31d6SJosh Lehan     static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
262c51ba919SBonnie Lo     static constexpr auto debugEnablePath = "/etc/thermal.d/debugging";
263de74542cSJosh Lehan     static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging";
264811f31d6SJosh Lehan 
265f54b260bSJosh Lehan     // Set up default logging path, preferring command line if it was given
266f54b260bSJosh Lehan     std::string defLoggingPath(loggingPath);
267f54b260bSJosh Lehan     if (defLoggingPath.empty())
268f54b260bSJosh Lehan     {
269f54b260bSJosh Lehan         defLoggingPath = std::filesystem::temp_directory_path();
270f54b260bSJosh Lehan     }
271f54b260bSJosh Lehan     else
272f54b260bSJosh Lehan     {
273f54b260bSJosh Lehan         // Enable logging, if user explicitly gave path on command line
274f54b260bSJosh Lehan         loggingEnabled = true;
275f54b260bSJosh Lehan     }
276f54b260bSJosh Lehan 
277811f31d6SJosh Lehan     // If this file exists, enable logging at runtime
278811f31d6SJosh Lehan     std::ifstream fsLogging(loggingEnablePath);
279811f31d6SJosh Lehan     if (fsLogging)
280811f31d6SJosh Lehan     {
281de74542cSJosh Lehan         // Allow logging path to be changed by file content
282de74542cSJosh Lehan         std::string altPath;
283de74542cSJosh Lehan         std::getline(fsLogging, altPath);
284811f31d6SJosh Lehan         fsLogging.close();
285811f31d6SJosh Lehan 
286de74542cSJosh Lehan         if (std::filesystem::exists(altPath))
287f54b260bSJosh Lehan         {
288de74542cSJosh Lehan             loggingPath = altPath;
289f54b260bSJosh Lehan         }
290*cdc8dca4SJinliang Wang         else
291*cdc8dca4SJinliang Wang         {
292*cdc8dca4SJinliang Wang             loggingPath = defLoggingPath;
293*cdc8dca4SJinliang Wang         }
294f54b260bSJosh Lehan 
295811f31d6SJosh Lehan         loggingEnabled = true;
296f54b260bSJosh Lehan     }
297f54b260bSJosh Lehan     if (loggingEnabled)
298f54b260bSJosh Lehan     {
299811f31d6SJosh Lehan         std::cerr << "Logging enabled: " << loggingPath << "\n";
300811f31d6SJosh Lehan     }
301811f31d6SJosh Lehan 
302811f31d6SJosh Lehan     // If this file exists, enable tuning at runtime
303811f31d6SJosh Lehan     if (std::filesystem::exists(tuningEnablePath))
304811f31d6SJosh Lehan     {
305811f31d6SJosh Lehan         tuningEnabled = true;
306f54b260bSJosh Lehan     }
307f54b260bSJosh Lehan     if (tuningEnabled)
308f54b260bSJosh Lehan     {
309811f31d6SJosh Lehan         std::cerr << "Tuning enabled\n";
310811f31d6SJosh Lehan     }
311d4695590SKun Yi 
312c51ba919SBonnie Lo     // If this file exists, enable debug mode at runtime
313c51ba919SBonnie Lo     if (std::filesystem::exists(debugEnablePath))
314c51ba919SBonnie Lo     {
315c51ba919SBonnie Lo         debugEnabled = true;
316c51ba919SBonnie Lo     }
317c51ba919SBonnie Lo 
318c51ba919SBonnie Lo     if (debugEnabled)
319c51ba919SBonnie Lo     {
320c51ba919SBonnie Lo         std::cerr << "Debug mode enabled\n";
321c51ba919SBonnie Lo     }
322c51ba919SBonnie Lo 
323de74542cSJosh Lehan     // If this file exists, enable core logging at runtime
324de74542cSJosh Lehan     if (std::filesystem::exists(coreLoggingEnablePath))
325de74542cSJosh Lehan     {
326de74542cSJosh Lehan         coreLoggingEnabled = true;
327de74542cSJosh Lehan     }
328de74542cSJosh Lehan     if (coreLoggingEnabled)
329de74542cSJosh Lehan     {
330de74542cSJosh Lehan         std::cerr << "Core logging enabled\n";
331de74542cSJosh Lehan     }
332de74542cSJosh Lehan 
3331fe08952SJames Feist     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
3341fe08952SJames Feist     // Create a manager for the ModeBus because we own it.
335b228bc30SPatrick Williams     sdbusplus::server::manager_t(static_cast<sdbusplus::bus_t&>(modeControlBus),
336b228bc30SPatrick Williams                                  modeRoot);
3371fe08952SJames Feist     hostBus.request_name("xyz.openbmc_project.Hwmon.external");
3381fe08952SJames Feist     modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
339b228bc30SPatrick Williams     sdbusplus::server::manager_t objManager(modeControlBus, modeRoot);
3401fe08952SJames Feist 
341e2ec69adSPotin Lai     // Enable SIGHUP handling to reload JSON config
342e2ec69adSPotin Lai     signals.async_wait(sighupHandler);
343e2ec69adSPotin Lai 
344e620656cSPatrick Venture     /*
345e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
346e620656cSPatrick Venture      * it.
347e620656cSPatrick Venture      */
348e620656cSPatrick Venture 
349a076487aSPatrick Venture     pid_control::tryRestartControlLoops();
350e620656cSPatrick Venture 
351ce6a3f36SJames Feist     io.run();
3521fe08952SJames Feist     return 0;
353e620656cSPatrick Venture }
354