blob: 37c16607e8d03cfc74cd8cc54651050bc81ba0cd [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
Harvey Wu3dcfd542022-10-31 17:11:02 +080017#include "buildjson/buildjson.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070018#include "conf.hpp"
Patrick Venture7f9d6902020-09-10 12:09:57 -070019#include "dbus/dbusconfiguration.hpp"
James Zheng6df8bb52024-11-27 23:38:47 +000020#include "failsafeloggers/builder.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070021#include "pid/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080022#include "pid/buildjson.hpp"
James Feistce6a3f32019-03-12 11:20:16 -070023#include "pid/pidloop.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080024#include "pid/tuning.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070025#include "sensors/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080026#include "sensors/buildjson.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080027#include "sensors/manager.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070028#include "util.hpp"
Ed Tanousf8b6e552025-06-27 13:27:50 -070029#include "zone_interface.hpp"
30
31#include <signal.h>
Patrick Venturee6206562018-03-08 15:36:53 -080032
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070033#include <CLI/CLI.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070034#include <boost/asio/error.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070035#include <boost/asio/io_context.hpp>
Ed Tanousf8b6e552025-06-27 13:27:50 -070036#include <boost/asio/post.hpp>
Potin Laie2ec69a2022-09-30 17:09:34 +080037#include <boost/asio/signal_set.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070038#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070039#include <sdbusplus/asio/connection.hpp>
40#include <sdbusplus/bus.hpp>
Bruce Leecb4c1a22021-04-20 16:06:38 +080041#include <sdbusplus/server/manager.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070042
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043#include <chrono>
Ed Tanousf8b6e552025-06-27 13:27:50 -070044#include <csignal>
45#include <cstdint>
46#include <cstdlib>
47#include <exception>
Patrick Venture7f9d6902020-09-10 12:09:57 -070048#include <filesystem>
Ed Tanousf8b6e552025-06-27 13:27:50 -070049#include <fstream>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070050#include <iostream>
51#include <map>
52#include <memory>
Patrick Williamse3c60772025-04-07 17:53:42 -040053#include <optional>
Ed Tanousf8b6e552025-06-27 13:27:50 -070054#include <stdexcept>
55#include <string>
56#include <tuple>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070057#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080058#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070059#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080060
Patrick Venturea0764872020-08-08 07:48:43 -070061namespace pid_control
62{
63
James Feist1fe08952019-05-07 09:17:16 -070064/* The configuration converted sensor list. */
Patrick Venture1df9e872020-10-08 15:35:01 -070065std::map<std::string, conf::SensorConfig> sensorConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070066/* The configuration converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080067std::map<int64_t, conf::PIDConf> zoneConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070068/* The configuration converted Zone configuration. */
Patrick Venture1df9e872020-10-08 15:35:01 -070069std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080070
Patrick Rudolphc7b2be32023-10-13 12:34:58 +020071namespace state
72{
73/* Set to true while canceling is in progress */
74static bool isCanceling = false;
75/* The zones build from configuration */
76static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
77/* The timers used by the PID loop */
78static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
79/* The sensors build from configuration */
Patrick Williamse3c60772025-04-07 17:53:42 -040080static std::optional<SensorManager> mgmr;
Patrick Rudolphc7b2be32023-10-13 12:34:58 +020081} // namespace state
82
Patrick Venturea0764872020-08-08 07:48:43 -070083} // namespace pid_control
84
Potin Lai79cde002023-02-16 13:53:10 +080085std::filesystem::path configPath = "";
Patrick Venturec19f5d42019-02-14 10:59:47 -080086
James Feist1fe08952019-05-07 09:17:16 -070087/* async io context for operation */
88boost::asio::io_context io;
Potin Laie2ec69a2022-09-30 17:09:34 +080089/* async signal_set for signal handling */
Patrick Rudolphc7b2be32023-10-13 12:34:58 +020090boost::asio::signal_set signals(io, SIGHUP, SIGTERM);
James Feist1fe08952019-05-07 09:17:16 -070091
92/* buses for system control */
93static sdbusplus::asio::connection modeControlBus(io);
Patrick Williams7dea66c2025-04-07 14:23:50 -040094static sdbusplus::asio::connection hostBus(io, sdbusplus::bus::new_bus());
95static sdbusplus::asio::connection passiveBus(io, sdbusplus::bus::new_bus());
James Feist1fe08952019-05-07 09:17:16 -070096
Patrick Venturea0764872020-08-08 07:48:43 -070097namespace pid_control
98{
99
Potin Lai79cde002023-02-16 13:53:10 +0800100std::filesystem::path searchConfigurationPath()
101{
102 static constexpr auto name = "config.json";
103
Ed Tanousd2768c52025-06-26 11:42:57 -0700104 for (const auto& pathSeg : {std::filesystem::current_path(),
105 std::filesystem::path{"/var/lib/swampd"},
106 std::filesystem::path{"/usr/share/swampd"}})
Potin Lai79cde002023-02-16 13:53:10 +0800107 {
108 auto file = pathSeg / name;
109 if (std::filesystem::exists(file))
110 {
111 return file;
112 }
113 }
114
115 return name;
116}
117
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200118void stopControlLoops()
Patrick Venturee6206562018-03-08 15:36:53 -0800119{
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200120 for (const auto& timer : state::timers)
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700121 {
122 timer->cancel();
123 }
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200124 state::isCanceling = true;
125 state::timers.clear();
Hao Jiangb228cea2021-02-20 11:53:09 -0800126
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200127 if (state::zones.size() > 0 && state::zones.begin()->second.use_count() > 1)
Hao Jiangb228cea2021-02-20 11:53:09 -0800128 {
129 throw std::runtime_error("wait for count back to 1");
130 }
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200131
132 state::zones.clear();
133 state::isCanceling = false;
134}
135
136void restartControlLoops()
137{
138 stopControlLoops();
Patrick Venture4cb7c052019-02-14 11:11:33 -0800139
Potin Lai79cde002023-02-16 13:53:10 +0800140 const std::filesystem::path path =
141 (!configPath.empty()) ? configPath : searchConfigurationPath();
Patrick Venturee6206562018-03-08 15:36:53 -0800142
Patrick Venture7f9d6902020-09-10 12:09:57 -0700143 if (std::filesystem::exists(path))
Patrick Venturee6206562018-03-08 15:36:53 -0800144 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700145 /*
146 * When building the sensors, if any of the dbus passive ones aren't on
147 * the bus, it'll fail immediately.
148 */
149 try
150 {
151 auto jsonData = parseValidateJson(path);
152 sensorConfig = buildSensorsFromJson(jsonData);
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400153 std::tie(zoneConfig, zoneDetailsConfig) =
154 buildPIDsFromJson(jsonData);
Patrick Venture7f9d6902020-09-10 12:09:57 -0700155 }
156 catch (const std::exception& e)
157 {
158 std::cerr << "Failed during building: " << e.what() << "\n";
159 exit(EXIT_FAILURE); /* fatal error. */
160 }
Patrick Venturee6206562018-03-08 15:36:53 -0800161 }
Patrick Venture7f9d6902020-09-10 12:09:57 -0700162 else
Patrick Venture18b13112019-02-14 11:43:59 -0800163 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700164 static boost::asio::steady_timer reloadTimer(io);
Patrick Venture73823182020-10-08 15:12:51 -0700165 if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
166 zoneConfig, zoneDetailsConfig))
Patrick Venture7f9d6902020-09-10 12:09:57 -0700167 {
168 return; // configuration not ready
169 }
Patrick Venture18b13112019-02-14 11:43:59 -0800170 }
Patrick Venture4cb7c052019-02-14 11:11:33 -0800171
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200172 state::mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
Patrick Williamsbd63bca2024-08-16 15:21:10 -0400173 state::zones =
Patrick Williamse3c60772025-04-07 17:53:42 -0400174 buildZones(zoneConfig, zoneDetailsConfig, *state::mgmr, modeControlBus);
James Zheng6df8bb52024-11-27 23:38:47 +0000175 // Set `logMaxCountPerSecond` to 20 will limit the number of logs output per
176 // second in each zone. Using 20 here would limit the output rate to be no
177 // larger than 100 per sec for most platforms as the number of zones are
178 // usually <=3. This will effectively avoid resource exhaustion.
179 buildFailsafeLoggers(state::zones, /* logMaxCountPerSecond = */ 20);
Patrick Venturee6206562018-03-08 15:36:53 -0800180
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200181 if (0 == state::zones.size())
Patrick Venturee6206562018-03-08 15:36:53 -0800182 {
183 std::cerr << "No zones defined, exiting.\n";
James Feist1fe08952019-05-07 09:17:16 -0700184 std::exit(EXIT_FAILURE);
Patrick Venturee6206562018-03-08 15:36:53 -0800185 }
186
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200187 for (const auto& i : state::zones)
James Feist1fe08952019-05-07 09:17:16 -0700188 {
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200189 std::shared_ptr<boost::asio::steady_timer> timer =
190 state::timers.emplace_back(
191 std::make_shared<boost::asio::steady_timer>(io));
James Feist1fe08952019-05-07 09:17:16 -0700192 std::cerr << "pushing zone " << i.first << "\n";
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200193 pidControlLoop(i.second, timer, &state::isCanceling);
James Feist1fe08952019-05-07 09:17:16 -0700194 }
195}
196
Hao Jiangb228cea2021-02-20 11:53:09 -0800197void tryRestartControlLoops(bool first)
Yong Li298a95c2020-04-07 15:11:02 +0800198{
Hao Jiangd11a7322021-03-24 11:25:57 -0700199 static const auto delayTime = std::chrono::seconds(10);
Hao Jiangb228cea2021-02-20 11:53:09 -0800200 static boost::asio::steady_timer timer(io);
Hao Jiangd11a7322021-03-24 11:25:57 -0700201
202 auto restartLbd = [](const boost::system::error_code& error) {
Hao Jiang232ffe42021-02-21 20:11:49 -0800203 if (error == boost::asio::error::operation_aborted)
204 {
205 return;
206 }
Hao Jiangb228cea2021-02-20 11:53:09 -0800207
Hao Jiang232ffe42021-02-21 20:11:49 -0800208 // retry when restartControlLoops() has some failure.
209 try
210 {
211 restartControlLoops();
Hao Jiang232ffe42021-02-21 20:11:49 -0800212 }
213 catch (const std::exception& e)
214 {
Zev Weiss5293ec22023-03-06 18:58:36 -0800215 std::cerr << "Failed during restartControlLoops, try again: "
Hao Jiang232ffe42021-02-21 20:11:49 -0800216 << e.what() << "\n";
217 tryRestartControlLoops(false);
218 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700219 };
Zev Weiss5293ec22023-03-06 18:58:36 -0800220
Hao Jiangd11a7322021-03-24 11:25:57 -0700221 // first time of trying to restart the control loop without a delay
222 if (first)
223 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700224 boost::asio::post(io, [restartLbd] {
225 restartLbd(boost::system::error_code());
226 });
Hao Jiangd11a7322021-03-24 11:25:57 -0700227 }
228 // re-try control loop, set up a delay.
229 else
230 {
231 timer.expires_after(delayTime);
232 timer.async_wait(restartLbd);
233 }
Yong Li298a95c2020-04-07 15:11:02 +0800234
235 return;
236}
237
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200238void tryTerminateControlLoops(bool first)
239{
240 static const auto delayTime = std::chrono::milliseconds(50);
241 static boost::asio::steady_timer timer(io);
242
243 auto stopLbd = [](const boost::system::error_code& error) {
244 if (error == boost::asio::error::operation_aborted)
245 {
246 return;
247 }
248
249 // retry when stopControlLoops() has some failure.
250 try
251 {
252 stopControlLoops();
253 }
254 catch (const std::exception& e)
255 {
256 std::cerr << "Failed during stopControlLoops, try again: "
257 << e.what() << "\n";
258 tryTerminateControlLoops(false);
259 return;
260 }
261 io.stop();
262 };
263
264 // first time of trying to stop the control loop without a delay
265 if (first)
266 {
Ed Tanousd2768c52025-06-26 11:42:57 -0700267 boost::asio::post(io,
268 [stopLbd] { stopLbd(boost::system::error_code()); });
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200269 }
270 // re-try control loop, set up a delay.
271 else
272 {
273 timer.expires_after(delayTime);
274 timer.async_wait(stopLbd);
275 }
276
277 return;
278}
279
Patrick Venturea0764872020-08-08 07:48:43 -0700280} // namespace pid_control
281
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200282void signalHandler(const boost::system::error_code& error, int signal_number)
Potin Laie2ec69a2022-09-30 17:09:34 +0800283{
284 static boost::asio::steady_timer timer(io);
285
286 if (error)
287 {
288 std::cout << "Signal " << signal_number
289 << " handler error: " << error.message() << "\n";
290 return;
291 }
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200292 if (signal_number == SIGTERM)
293 {
294 pid_control::tryTerminateControlLoops(true);
295 }
296 else
297 {
298 timer.expires_after(std::chrono::seconds(1));
299 timer.async_wait([](const boost::system::error_code ec) {
300 if (ec)
301 {
302 std::cout << "Signal timer error: " << ec.message() << "\n";
303 return;
304 }
Potin Laie2ec69a2022-09-30 17:09:34 +0800305
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200306 std::cout << "reloading configuration\n";
307 pid_control::tryRestartControlLoops();
308 });
309 }
Potin Laie2ec69a2022-09-30 17:09:34 +0800310
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200311 signals.async_wait(signalHandler);
Potin Laie2ec69a2022-09-30 17:09:34 +0800312}
313
James Feist1fe08952019-05-07 09:17:16 -0700314int main(int argc, char* argv[])
315{
316 loggingPath = "";
317 loggingEnabled = false;
318 tuningEnabled = false;
Bonnie Loc51ba912022-10-12 14:07:22 +0800319 debugEnabled = false;
Josh Lehande745422020-11-07 02:14:09 -0800320 coreLoggingEnabled = false;
James Feist1fe08952019-05-07 09:17:16 -0700321
322 CLI::App app{"OpenBMC Fan Control Daemon"};
323
324 app.add_option("-c,--conf", configPath,
325 "Optional parameter to specify configuration at run-time")
326 ->check(CLI::ExistingFile);
327 app.add_option("-l,--log", loggingPath,
328 "Optional parameter to specify logging folder")
329 ->check(CLI::ExistingDirectory);
330 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
Bonnie Loc51ba912022-10-12 14:07:22 +0800331 app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode");
Josh Lehande745422020-11-07 02:14:09 -0800332 app.add_flag("-g,--corelogging", coreLoggingEnabled,
333 "Enable or disable logging of core PID loop computations");
James Feist1fe08952019-05-07 09:17:16 -0700334
James Feist1fe08952019-05-07 09:17:16 -0700335 CLI11_PARSE(app, argc, argv);
336
Josh Lehan811f31d2020-09-20 23:31:55 -0700337 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
338 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
Bonnie Loc51ba912022-10-12 14:07:22 +0800339 static constexpr auto debugEnablePath = "/etc/thermal.d/debugging";
Josh Lehande745422020-11-07 02:14:09 -0800340 static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging";
Josh Lehan811f31d2020-09-20 23:31:55 -0700341
Josh Lehanf54b2602021-04-09 12:17:34 -0700342 // Set up default logging path, preferring command line if it was given
343 std::string defLoggingPath(loggingPath);
344 if (defLoggingPath.empty())
345 {
346 defLoggingPath = std::filesystem::temp_directory_path();
347 }
348 else
349 {
350 // Enable logging, if user explicitly gave path on command line
351 loggingEnabled = true;
352 }
353
Josh Lehan811f31d2020-09-20 23:31:55 -0700354 // If this file exists, enable logging at runtime
355 std::ifstream fsLogging(loggingEnablePath);
356 if (fsLogging)
357 {
Josh Lehande745422020-11-07 02:14:09 -0800358 // Allow logging path to be changed by file content
359 std::string altPath;
360 std::getline(fsLogging, altPath);
Josh Lehan811f31d2020-09-20 23:31:55 -0700361 fsLogging.close();
362
Josh Lehande745422020-11-07 02:14:09 -0800363 if (std::filesystem::exists(altPath))
Josh Lehanf54b2602021-04-09 12:17:34 -0700364 {
Josh Lehande745422020-11-07 02:14:09 -0800365 loggingPath = altPath;
Josh Lehanf54b2602021-04-09 12:17:34 -0700366 }
Jinliang Wangcdc8dca2023-04-20 14:36:22 -0700367 else
368 {
369 loggingPath = defLoggingPath;
370 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700371
Josh Lehan811f31d2020-09-20 23:31:55 -0700372 loggingEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700373 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700374 if (loggingEnabled)
375 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700376 std::cerr << "Logging enabled: " << loggingPath << "\n";
377 }
378
379 // If this file exists, enable tuning at runtime
380 if (std::filesystem::exists(tuningEnablePath))
381 {
382 tuningEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700383 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700384 if (tuningEnabled)
385 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700386 std::cerr << "Tuning enabled\n";
387 }
Kun Yid4695592019-05-17 10:42:18 -0700388
Bonnie Loc51ba912022-10-12 14:07:22 +0800389 // If this file exists, enable debug mode at runtime
390 if (std::filesystem::exists(debugEnablePath))
391 {
392 debugEnabled = true;
393 }
394
395 if (debugEnabled)
396 {
397 std::cerr << "Debug mode enabled\n";
398 }
399
Josh Lehande745422020-11-07 02:14:09 -0800400 // If this file exists, enable core logging at runtime
401 if (std::filesystem::exists(coreLoggingEnablePath))
402 {
403 coreLoggingEnabled = true;
404 }
405 if (coreLoggingEnabled)
406 {
407 std::cerr << "Core logging enabled\n";
408 }
409
James Feist1fe08952019-05-07 09:17:16 -0700410 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
411 // Create a manager for the ModeBus because we own it.
Ed Tanousd2768c52025-06-26 11:42:57 -0700412 sdbusplus::server::manager_t manager(
413 static_cast<sdbusplus::bus_t&>(modeControlBus), modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700414 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
415 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Williamsb228bc32022-07-22 19:26:56 -0500416 sdbusplus::server::manager_t objManager(modeControlBus, modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700417
Potin Laie2ec69a2022-09-30 17:09:34 +0800418 // Enable SIGHUP handling to reload JSON config
Patrick Rudolphc7b2be32023-10-13 12:34:58 +0200419 signals.async_wait(signalHandler);
Potin Laie2ec69a2022-09-30 17:09:34 +0800420
Patrick Venturee6206562018-03-08 15:36:53 -0800421 /*
422 * All sensors are managed by one manager, but each zone has a pointer to
423 * it.
424 */
425
Patrick Venturea0764872020-08-08 07:48:43 -0700426 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800427
James Feistce6a3f32019-03-12 11:20:16 -0700428 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700429 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800430}