blob: 823181a9416a4d184f3d00add0372e7419cedb7c [file] [log] [blame]
Patrick Venturee6206562018-03-08 15:36:53 -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
James Feist7136a5a2018-07-19 09:52:05 -070017#include "config.h"
Patrick Venturee6206562018-03-08 15:36:53 -080018
Harvey Wu3dcfd542022-10-31 17:11:02 +080019#include "buildjson/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070020#include "conf.hpp"
Patrick Venture7f9d6902020-09-10 12:09:57 -070021#include "dbus/dbusconfiguration.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +000022#include "failsafeloggers/builder.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080023#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070024#include "pid/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080025#include "pid/buildjson.hpp"
James Feistce6a3f32019-03-12 11:20:16 -070026#include "pid/pidloop.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080027#include "pid/tuning.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080028#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070029#include "sensors/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080030#include "sensors/buildjson.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080031#include "sensors/manager.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070032#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080033
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070034#include <CLI/CLI.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070035#include <boost/asio/io_context.hpp>
Potin Laie2ec69a2022-09-30 17:09:34 +080036#include <boost/asio/signal_set.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070037#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070038#include <sdbusplus/asio/connection.hpp>
39#include <sdbusplus/bus.hpp>
Bruce Leecb4c1a22021-04-20 16:06:38 +080040#include <sdbusplus/server/manager.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070041
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042#include <chrono>
Patrick Venture7f9d6902020-09-10 12:09:57 -070043#include <filesystem>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070044#include <iostream>
James Feistce6a3f32019-03-12 11:20:16 -070045#include <list>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070046#include <map>
47#include <memory>
Patrick Williamse3c60772025-04-07 17:53:42 -040048#include <optional>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080050#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070051#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080052
Patrick Venturea0764872020-08-08 07:48:43 -070053namespace pid_control
54{
55
James Feist1fe08952019-05-07 09:17:16 -070056/* The configuration converted sensor list. */
Patrick Venture1df9e872020-10-08 15:35:01 -070057std::map<std::string, conf::SensorConfig> sensorConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070058/* The configuration converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080059std::map<int64_t, conf::PIDConf> zoneConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070060/* The configuration converted Zone configuration. */
Patrick Venture1df9e872020-10-08 15:35:01 -070061std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080062
Patrick Rudolphc7b2be32023-10-13 12:34:58 +020063namespace state
64{
65/* Set to true while canceling is in progress */
66static bool isCanceling = false;
67/* The zones build from configuration */
68static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
69/* The timers used by the PID loop */
70static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
71/* The sensors build from configuration */
Patrick Williamse3c60772025-04-07 17:53:42 -040072static std::optional<SensorManager> mgmr;
Patrick Rudolphc7b2be32023-10-13 12:34:58 +020073} // namespace state
74
Patrick Venturea0764872020-08-08 07:48:43 -070075} // namespace pid_control
76
Potin Lai79cde002023-02-16 13:53:10 +080077std::filesystem::path configPath = "";
Patrick Venturec19f5d42019-02-14 10:59:47 -080078
James Feist1fe08952019-05-07 09:17:16 -070079/* async io context for operation */
80boost::asio::io_context io;
Potin Laie2ec69a2022-09-30 17:09:34 +080081/* async signal_set for signal handling */
Patrick Rudolphc7b2be32023-10-13 12:34:58 +020082boost::asio::signal_set signals(io, SIGHUP, SIGTERM);
James Feist1fe08952019-05-07 09:17:16 -070083
84/* buses for system control */
85static sdbusplus::asio::connection modeControlBus(io);
Patrick Williams7dea66c2025-04-07 14:23:50 -040086static sdbusplus::asio::connection hostBus(io, sdbusplus::bus::new_bus());
87static sdbusplus::asio::connection passiveBus(io, sdbusplus::bus::new_bus());
James Feist1fe08952019-05-07 09:17:16 -070088
Patrick Venturea0764872020-08-08 07:48:43 -070089namespace pid_control
90{
91
Potin Lai79cde002023-02-16 13:53:10 +080092std::filesystem::path searchConfigurationPath()
93{
94 static constexpr auto name = "config.json";
95
Ed Tanousd2768c52025-06-26 11:42:57 -070096 for (const auto& pathSeg : {std::filesystem::current_path(),
97 std::filesystem::path{"/var/lib/swampd"},
98 std::filesystem::path{"/usr/share/swampd"}})
Potin Lai79cde002023-02-16 13:53:10 +080099 {
100 auto file = pathSeg / name;
101 if (std::filesystem::exists(file))
102 {
103 return file;
104 }
105 }
106
107 return name;
108}
109
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200110void stopControlLoops()
Patrick Venturee6206562018-03-08 15:36:53 -0800111{
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200112 for (const auto& timer : state::timers)
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700113 {
114 timer->cancel();
115 }
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200116 state::isCanceling = true;
117 state::timers.clear();
Hao Jiangb228cea2021-02-20 11:53:09 -0800118
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200119 if (state::zones.size() > 0 && state::zones.begin()->second.use_count() > 1)
Hao Jiangb228cea2021-02-20 11:53:09 -0800120 {
121 throw std::runtime_error("wait for count back to 1");
122 }
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200123
124 state::zones.clear();
125 state::isCanceling = false;
126}
127
128void restartControlLoops()
129{
130 stopControlLoops();
Patrick Venture4cb7c052019-02-14 11:11:33 -0800131
Potin Lai79cde002023-02-16 13:53:10 +0800132 const std::filesystem::path path =
133 (!configPath.empty()) ? configPath : searchConfigurationPath();
Patrick Venturee6206562018-03-08 15:36:53 -0800134
Patrick Venture7f9d6902020-09-10 12:09:57 -0700135 if (std::filesystem::exists(path))
Patrick Venturee6206562018-03-08 15:36:53 -0800136 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700137 /*
138 * When building the sensors, if any of the dbus passive ones aren't on
139 * the bus, it'll fail immediately.
140 */
141 try
142 {
143 auto jsonData = parseValidateJson(path);
144 sensorConfig = buildSensorsFromJson(jsonData);
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400145 std::tie(zoneConfig, zoneDetailsConfig) =
146 buildPIDsFromJson(jsonData);
Patrick Venture7f9d6902020-09-10 12:09:57 -0700147 }
148 catch (const std::exception& e)
149 {
150 std::cerr << "Failed during building: " << e.what() << "\n";
151 exit(EXIT_FAILURE); /* fatal error. */
152 }
Patrick Venturee6206562018-03-08 15:36:53 -0800153 }
Patrick Venture7f9d6902020-09-10 12:09:57 -0700154 else
Patrick Venture18b13112019-02-14 11:43:59 -0800155 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700156 static boost::asio::steady_timer reloadTimer(io);
Patrick Venture73823182020-10-08 15:12:51 -0700157 if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
158 zoneConfig, zoneDetailsConfig))
Patrick Venture7f9d6902020-09-10 12:09:57 -0700159 {
160 return; // configuration not ready
161 }
Patrick Venture18b13112019-02-14 11:43:59 -0800162 }
Patrick Venture4cb7c052019-02-14 11:11:33 -0800163
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200164 state::mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400165 state::zones =
Patrick Williamse3c60772025-04-07 17:53:42 -0400166 buildZones(zoneConfig, zoneDetailsConfig, *state::mgmr, modeControlBus);
James Zheng6df8bb52024-11-27 23:38:47 +0000167 // Set `logMaxCountPerSecond` to 20 will limit the number of logs output per
168 // second in each zone. Using 20 here would limit the output rate to be no
169 // larger than 100 per sec for most platforms as the number of zones are
170 // usually <=3. This will effectively avoid resource exhaustion.
171 buildFailsafeLoggers(state::zones, /* logMaxCountPerSecond = */ 20);
Patrick Venturee6206562018-03-08 15:36:53 -0800172
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200173 if (0 == state::zones.size())
Patrick Venturee6206562018-03-08 15:36:53 -0800174 {
175 std::cerr << "No zones defined, exiting.\n";
James Feist1fe08952019-05-07 09:17:16 -0700176 std::exit(EXIT_FAILURE);
Patrick Venturee6206562018-03-08 15:36:53 -0800177 }
178
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200179 for (const auto& i : state::zones)
James Feist1fe08952019-05-07 09:17:16 -0700180 {
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200181 std::shared_ptr<boost::asio::steady_timer> timer =
182 state::timers.emplace_back(
183 std::make_shared<boost::asio::steady_timer>(io));
James Feist1fe08952019-05-07 09:17:16 -0700184 std::cerr << "pushing zone " << i.first << "\n";
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200185 pidControlLoop(i.second, timer, &state::isCanceling);
James Feist1fe08952019-05-07 09:17:16 -0700186 }
187}
188
Hao Jiangb228cea2021-02-20 11:53:09 -0800189void tryRestartControlLoops(bool first)
Yong Li298a95c2020-04-07 15:11:02 +0800190{
Hao Jiangd11a7322021-03-24 11:25:57 -0700191 static const auto delayTime = std::chrono::seconds(10);
Hao Jiangb228cea2021-02-20 11:53:09 -0800192 static boost::asio::steady_timer timer(io);
Hao Jiangd11a7322021-03-24 11:25:57 -0700193
194 auto restartLbd = [](const boost::system::error_code& error) {
Hao Jiang232ffe42021-02-21 20:11:49 -0800195 if (error == boost::asio::error::operation_aborted)
196 {
197 return;
198 }
Hao Jiangb228cea2021-02-20 11:53:09 -0800199
Hao Jiang232ffe42021-02-21 20:11:49 -0800200 // retry when restartControlLoops() has some failure.
201 try
202 {
203 restartControlLoops();
Hao Jiang232ffe42021-02-21 20:11:49 -0800204 }
205 catch (const std::exception& e)
206 {
Zev Weiss5293ec22023-03-06 18:58:36 -0800207 std::cerr << "Failed during restartControlLoops, try again: "
Hao Jiang232ffe42021-02-21 20:11:49 -0800208 << e.what() << "\n";
209 tryRestartControlLoops(false);
210 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700211 };
Zev Weiss5293ec22023-03-06 18:58:36 -0800212
Hao Jiangd11a7322021-03-24 11:25:57 -0700213 // first time of trying to restart the control loop without a delay
214 if (first)
215 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700216 boost::asio::post(io, [restartLbd] {
217 restartLbd(boost::system::error_code());
218 });
Hao Jiangd11a7322021-03-24 11:25:57 -0700219 }
220 // re-try control loop, set up a delay.
221 else
222 {
223 timer.expires_after(delayTime);
224 timer.async_wait(restartLbd);
225 }
Yong Li298a95c2020-04-07 15:11:02 +0800226
227 return;
228}
229
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200230void tryTerminateControlLoops(bool first)
231{
232 static const auto delayTime = std::chrono::milliseconds(50);
233 static boost::asio::steady_timer timer(io);
234
235 auto stopLbd = [](const boost::system::error_code& error) {
236 if (error == boost::asio::error::operation_aborted)
237 {
238 return;
239 }
240
241 // retry when stopControlLoops() has some failure.
242 try
243 {
244 stopControlLoops();
245 }
246 catch (const std::exception& e)
247 {
248 std::cerr << "Failed during stopControlLoops, try again: "
249 << e.what() << "\n";
250 tryTerminateControlLoops(false);
251 return;
252 }
253 io.stop();
254 };
255
256 // first time of trying to stop the control loop without a delay
257 if (first)
258 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700259 boost::asio::post(io,
260 [stopLbd] { stopLbd(boost::system::error_code()); });
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200261 }
262 // re-try control loop, set up a delay.
263 else
264 {
265 timer.expires_after(delayTime);
266 timer.async_wait(stopLbd);
267 }
268
269 return;
270}
271
Patrick Venturea0764872020-08-08 07:48:43 -0700272} // namespace pid_control
273
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200274void signalHandler(const boost::system::error_code& error, int signal_number)
Potin Laie2ec69a2022-09-30 17:09:34 +0800275{
276 static boost::asio::steady_timer timer(io);
277
278 if (error)
279 {
280 std::cout << "Signal " << signal_number
281 << " handler error: " << error.message() << "\n";
282 return;
283 }
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200284 if (signal_number == SIGTERM)
285 {
286 pid_control::tryTerminateControlLoops(true);
287 }
288 else
289 {
290 timer.expires_after(std::chrono::seconds(1));
291 timer.async_wait([](const boost::system::error_code ec) {
292 if (ec)
293 {
294 std::cout << "Signal timer error: " << ec.message() << "\n";
295 return;
296 }
Potin Laie2ec69a2022-09-30 17:09:34 +0800297
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200298 std::cout << "reloading configuration\n";
299 pid_control::tryRestartControlLoops();
300 });
301 }
Potin Laie2ec69a2022-09-30 17:09:34 +0800302
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200303 signals.async_wait(signalHandler);
Potin Laie2ec69a2022-09-30 17:09:34 +0800304}
305
James Feist1fe08952019-05-07 09:17:16 -0700306int main(int argc, char* argv[])
307{
308 loggingPath = "";
309 loggingEnabled = false;
310 tuningEnabled = false;
Bonnie Loc51ba912022-10-12 14:07:22 +0800311 debugEnabled = false;
Josh Lehande745422020-11-07 02:14:09 -0800312 coreLoggingEnabled = false;
James Feist1fe08952019-05-07 09:17:16 -0700313
314 CLI::App app{"OpenBMC Fan Control Daemon"};
315
316 app.add_option("-c,--conf", configPath,
317 "Optional parameter to specify configuration at run-time")
318 ->check(CLI::ExistingFile);
319 app.add_option("-l,--log", loggingPath,
320 "Optional parameter to specify logging folder")
321 ->check(CLI::ExistingDirectory);
322 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
Bonnie Loc51ba912022-10-12 14:07:22 +0800323 app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode");
Josh Lehande745422020-11-07 02:14:09 -0800324 app.add_flag("-g,--corelogging", coreLoggingEnabled,
325 "Enable or disable logging of core PID loop computations");
James Feist1fe08952019-05-07 09:17:16 -0700326
James Feist1fe08952019-05-07 09:17:16 -0700327 CLI11_PARSE(app, argc, argv);
328
Josh Lehan811f31d2020-09-20 23:31:55 -0700329 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
330 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
Bonnie Loc51ba912022-10-12 14:07:22 +0800331 static constexpr auto debugEnablePath = "/etc/thermal.d/debugging";
Josh Lehande745422020-11-07 02:14:09 -0800332 static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging";
Josh Lehan811f31d2020-09-20 23:31:55 -0700333
Josh Lehanf54b2602021-04-09 12:17:34 -0700334 // Set up default logging path, preferring command line if it was given
335 std::string defLoggingPath(loggingPath);
336 if (defLoggingPath.empty())
337 {
338 defLoggingPath = std::filesystem::temp_directory_path();
339 }
340 else
341 {
342 // Enable logging, if user explicitly gave path on command line
343 loggingEnabled = true;
344 }
345
Josh Lehan811f31d2020-09-20 23:31:55 -0700346 // If this file exists, enable logging at runtime
347 std::ifstream fsLogging(loggingEnablePath);
348 if (fsLogging)
349 {
Josh Lehande745422020-11-07 02:14:09 -0800350 // Allow logging path to be changed by file content
351 std::string altPath;
352 std::getline(fsLogging, altPath);
Josh Lehan811f31d2020-09-20 23:31:55 -0700353 fsLogging.close();
354
Josh Lehande745422020-11-07 02:14:09 -0800355 if (std::filesystem::exists(altPath))
Josh Lehanf54b2602021-04-09 12:17:34 -0700356 {
Josh Lehande745422020-11-07 02:14:09 -0800357 loggingPath = altPath;
Josh Lehanf54b2602021-04-09 12:17:34 -0700358 }
Jinliang Wangcdc8dca2023-04-20 14:36:22 -0700359 else
360 {
361 loggingPath = defLoggingPath;
362 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700363
Josh Lehan811f31d2020-09-20 23:31:55 -0700364 loggingEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700365 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700366 if (loggingEnabled)
367 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700368 std::cerr << "Logging enabled: " << loggingPath << "\n";
369 }
370
371 // If this file exists, enable tuning at runtime
372 if (std::filesystem::exists(tuningEnablePath))
373 {
374 tuningEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700375 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700376 if (tuningEnabled)
377 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700378 std::cerr << "Tuning enabled\n";
379 }
Kun Yid4695592019-05-17 10:42:18 -0700380
Bonnie Loc51ba912022-10-12 14:07:22 +0800381 // If this file exists, enable debug mode at runtime
382 if (std::filesystem::exists(debugEnablePath))
383 {
384 debugEnabled = true;
385 }
386
387 if (debugEnabled)
388 {
389 std::cerr << "Debug mode enabled\n";
390 }
391
Josh Lehande745422020-11-07 02:14:09 -0800392 // If this file exists, enable core logging at runtime
393 if (std::filesystem::exists(coreLoggingEnablePath))
394 {
395 coreLoggingEnabled = true;
396 }
397 if (coreLoggingEnabled)
398 {
399 std::cerr << "Core logging enabled\n";
400 }
401
James Feist1fe08952019-05-07 09:17:16 -0700402 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
403 // Create a manager for the ModeBus because we own it.
Ed Tanousd2768c52025-06-26 11:42:57 -0700404 sdbusplus::server::manager_t manager(
405 static_cast<sdbusplus::bus_t&>(modeControlBus), modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700406 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
407 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Williamsb228bc32022-07-22 19:26:56 -0500408 sdbusplus::server::manager_t objManager(modeControlBus, modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700409
Potin Laie2ec69a2022-09-30 17:09:34 +0800410 // Enable SIGHUP handling to reload JSON config
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200411 signals.async_wait(signalHandler);
Potin Laie2ec69a2022-09-30 17:09:34 +0800412
Patrick Venturee6206562018-03-08 15:36:53 -0800413 /*
414 * All sensors are managed by one manager, but each zone has a pointer to
415 * it.
416 */
417
Patrick Venturea0764872020-08-08 07:48:43 -0700418 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800419
James Feistce6a3f32019-03-12 11:20:16 -0700420 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700421 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800422}