blob: fc09d8b1221423fe221361846c7a78af805b32cb [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"
Patrick Venturee6206562018-03-08 15:36:53 -080022#include "interfaces.hpp"
Patrick Venture5c7cc542018-06-11 14:29:38 -070023#include "pid/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080024#include "pid/buildjson.hpp"
James Feistce6a3f32019-03-12 11:20:16 -070025#include "pid/pidloop.hpp"
Patrick Venturec32e3fc2019-02-28 10:01:11 -080026#include "pid/tuning.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080027#include "pid/zone.hpp"
Patrick Venture5e929092018-06-08 10:55:23 -070028#include "sensors/builder.hpp"
Patrick Ventureba8ffa72019-02-11 12:03:56 -080029#include "sensors/buildjson.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080030#include "sensors/manager.hpp"
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070031#include "util.hpp"
Patrick Venturee6206562018-03-08 15:36:53 -080032
Patrick Ventureb5cc37c2019-03-11 09:11:55 -070033#include <CLI/CLI.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070034#include <boost/asio/io_context.hpp>
Potin Laie2ec69a2022-09-30 17:09:34 +080035#include <boost/asio/signal_set.hpp>
James Feistce6a3f32019-03-12 11:20:16 -070036#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070037#include <sdbusplus/asio/connection.hpp>
38#include <sdbusplus/bus.hpp>
Bruce Leecb4c1a22021-04-20 16:06:38 +080039#include <sdbusplus/server/manager.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070040
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041#include <chrono>
Patrick Venture7f9d6902020-09-10 12:09:57 -070042#include <filesystem>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043#include <iostream>
James Feistce6a3f32019-03-12 11:20:16 -070044#include <list>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045#include <map>
46#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070047#include <thread>
48#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080049#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070050#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080051
Patrick Venturea0764872020-08-08 07:48:43 -070052namespace pid_control
53{
54
James Feist1fe08952019-05-07 09:17:16 -070055/* The configuration converted sensor list. */
Patrick Venture1df9e872020-10-08 15:35:01 -070056std::map<std::string, conf::SensorConfig> sensorConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070057/* The configuration converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080058std::map<int64_t, conf::PIDConf> zoneConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070059/* The configuration converted Zone configuration. */
Patrick Venture1df9e872020-10-08 15:35:01 -070060std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080061
Patrick Venturea0764872020-08-08 07:48:43 -070062} // namespace pid_control
63
Potin Lai79cde002023-02-16 13:53:10 +080064std::filesystem::path configPath = "";
Patrick Venturec19f5d42019-02-14 10:59:47 -080065
James Feist1fe08952019-05-07 09:17:16 -070066/* async io context for operation */
67boost::asio::io_context io;
Potin Laie2ec69a2022-09-30 17:09:34 +080068/* async signal_set for signal handling */
69boost::asio::signal_set signals(io, SIGHUP);
James Feist1fe08952019-05-07 09:17:16 -070070
71/* buses for system control */
72static sdbusplus::asio::connection modeControlBus(io);
73static sdbusplus::asio::connection
74 hostBus(io, sdbusplus::bus::new_system().release());
75static sdbusplus::asio::connection
76 passiveBus(io, sdbusplus::bus::new_system().release());
77
Patrick Venturea0764872020-08-08 07:48:43 -070078namespace pid_control
79{
80
Potin Lai79cde002023-02-16 13:53:10 +080081std::filesystem::path searchConfigurationPath()
82{
83 static constexpr auto name = "config.json";
84
85 for (auto pathSeg : {std::filesystem::current_path(),
86 std::filesystem::path{"/var/lib/swampd"},
87 std::filesystem::path{"/usr/share/swampd"}})
88 {
89 auto file = pathSeg / name;
90 if (std::filesystem::exists(file))
91 {
92 return file;
93 }
94 }
95
96 return name;
97}
98
James Feist1fe08952019-05-07 09:17:16 -070099void restartControlLoops()
Patrick Venturee6206562018-03-08 15:36:53 -0800100{
James Feist1fe08952019-05-07 09:17:16 -0700101 static SensorManager mgmr;
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700102 static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
103 static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
Hao Jiangb6a0b892021-02-21 18:00:45 -0800104 static bool isCanceling = false;
Patrick Venturee6206562018-03-08 15:36:53 -0800105
William A. Kennington III18d5bb12021-05-15 20:38:41 -0700106 for (const auto& timer : timers)
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700107 {
108 timer->cancel();
109 }
Hao Jiangb6a0b892021-02-21 18:00:45 -0800110 isCanceling = true;
James Feist1fe08952019-05-07 09:17:16 -0700111 timers.clear();
Hao Jiangb228cea2021-02-20 11:53:09 -0800112
113 if (zones.size() > 0 && zones.begin()->second.use_count() > 1)
114 {
115 throw std::runtime_error("wait for count back to 1");
116 }
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700117 zones.clear();
Hao Jiangb6a0b892021-02-21 18:00:45 -0800118 isCanceling = false;
Patrick Venture4cb7c052019-02-14 11:11:33 -0800119
Potin Lai79cde002023-02-16 13:53:10 +0800120 const std::filesystem::path path =
121 (!configPath.empty()) ? configPath : searchConfigurationPath();
Patrick Venturee6206562018-03-08 15:36:53 -0800122
Patrick Venture7f9d6902020-09-10 12:09:57 -0700123 if (std::filesystem::exists(path))
Patrick Venturee6206562018-03-08 15:36:53 -0800124 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700125 /*
126 * When building the sensors, if any of the dbus passive ones aren't on
127 * the bus, it'll fail immediately.
128 */
129 try
130 {
131 auto jsonData = parseValidateJson(path);
132 sensorConfig = buildSensorsFromJson(jsonData);
Patrick Williams8c051122023-05-10 07:50:59 -0500133 std::tie(zoneConfig,
134 zoneDetailsConfig) = buildPIDsFromJson(jsonData);
Patrick Venture7f9d6902020-09-10 12:09:57 -0700135 }
136 catch (const std::exception& e)
137 {
138 std::cerr << "Failed during building: " << e.what() << "\n";
139 exit(EXIT_FAILURE); /* fatal error. */
140 }
Patrick Venturee6206562018-03-08 15:36:53 -0800141 }
Patrick Venture7f9d6902020-09-10 12:09:57 -0700142 else
Patrick Venture18b13112019-02-14 11:43:59 -0800143 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700144 static boost::asio::steady_timer reloadTimer(io);
Patrick Venture73823182020-10-08 15:12:51 -0700145 if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
146 zoneConfig, zoneDetailsConfig))
Patrick Venture7f9d6902020-09-10 12:09:57 -0700147 {
148 return; // configuration not ready
149 }
Patrick Venture18b13112019-02-14 11:43:59 -0800150 }
Patrick Venture4cb7c052019-02-14 11:11:33 -0800151
James Feist1fe08952019-05-07 09:17:16 -0700152 mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
153 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800154
155 if (0 == zones.size())
156 {
157 std::cerr << "No zones defined, exiting.\n";
James Feist1fe08952019-05-07 09:17:16 -0700158 std::exit(EXIT_FAILURE);
Patrick Venturee6206562018-03-08 15:36:53 -0800159 }
160
James Feist1fe08952019-05-07 09:17:16 -0700161 for (const auto& i : zones)
162 {
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700163 std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back(
164 std::make_shared<boost::asio::steady_timer>(io));
James Feist1fe08952019-05-07 09:17:16 -0700165 std::cerr << "pushing zone " << i.first << "\n";
Hao Jiangb6a0b892021-02-21 18:00:45 -0800166 pidControlLoop(i.second, timer, &isCanceling);
James Feist1fe08952019-05-07 09:17:16 -0700167 }
168}
169
Hao Jiangb228cea2021-02-20 11:53:09 -0800170void tryRestartControlLoops(bool first)
Yong Li298a95c2020-04-07 15:11:02 +0800171{
Hao Jiangd11a7322021-03-24 11:25:57 -0700172 static const auto delayTime = std::chrono::seconds(10);
Hao Jiangb228cea2021-02-20 11:53:09 -0800173 static boost::asio::steady_timer timer(io);
Hao Jiangd11a7322021-03-24 11:25:57 -0700174
175 auto restartLbd = [](const boost::system::error_code& error) {
Hao Jiang232ffe42021-02-21 20:11:49 -0800176 if (error == boost::asio::error::operation_aborted)
177 {
178 return;
179 }
Hao Jiangb228cea2021-02-20 11:53:09 -0800180
Hao Jiang232ffe42021-02-21 20:11:49 -0800181 // retry when restartControlLoops() has some failure.
182 try
183 {
184 restartControlLoops();
Hao Jiang232ffe42021-02-21 20:11:49 -0800185 }
186 catch (const std::exception& e)
187 {
Zev Weiss5293ec22023-03-06 18:58:36 -0800188 std::cerr << "Failed during restartControlLoops, try again: "
Hao Jiang232ffe42021-02-21 20:11:49 -0800189 << e.what() << "\n";
190 tryRestartControlLoops(false);
191 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700192 };
Zev Weiss5293ec22023-03-06 18:58:36 -0800193
Hao Jiangd11a7322021-03-24 11:25:57 -0700194 // first time of trying to restart the control loop without a delay
195 if (first)
196 {
197 boost::asio::post(io,
198 std::bind(restartLbd, boost::system::error_code()));
199 }
200 // re-try control loop, set up a delay.
201 else
202 {
203 timer.expires_after(delayTime);
204 timer.async_wait(restartLbd);
205 }
Yong Li298a95c2020-04-07 15:11:02 +0800206
207 return;
208}
209
Patrick Venturea0764872020-08-08 07:48:43 -0700210} // namespace pid_control
211
Potin Laie2ec69a2022-09-30 17:09:34 +0800212void sighupHandler(const boost::system::error_code& error, int signal_number)
213{
214 static boost::asio::steady_timer timer(io);
215
216 if (error)
217 {
218 std::cout << "Signal " << signal_number
219 << " handler error: " << error.message() << "\n";
220 return;
221 }
222
223 timer.expires_after(std::chrono::seconds(1));
224 timer.async_wait([](const boost::system::error_code ec) {
225 if (ec)
226 {
227 std::cout << "Signal timer error: " << ec.message() << "\n";
228 return;
229 }
230
231 std::cout << "reloading configuration\n";
232 pid_control::tryRestartControlLoops();
233 });
234 signals.async_wait(sighupHandler);
235}
236
James Feist1fe08952019-05-07 09:17:16 -0700237int main(int argc, char* argv[])
238{
239 loggingPath = "";
240 loggingEnabled = false;
241 tuningEnabled = false;
Bonnie Loc51ba912022-10-12 14:07:22 +0800242 debugEnabled = false;
Josh Lehande745422020-11-07 02:14:09 -0800243 coreLoggingEnabled = false;
James Feist1fe08952019-05-07 09:17:16 -0700244
245 CLI::App app{"OpenBMC Fan Control Daemon"};
246
247 app.add_option("-c,--conf", configPath,
248 "Optional parameter to specify configuration at run-time")
249 ->check(CLI::ExistingFile);
250 app.add_option("-l,--log", loggingPath,
251 "Optional parameter to specify logging folder")
252 ->check(CLI::ExistingDirectory);
253 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
Bonnie Loc51ba912022-10-12 14:07:22 +0800254 app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode");
Josh Lehande745422020-11-07 02:14:09 -0800255 app.add_flag("-g,--corelogging", coreLoggingEnabled,
256 "Enable or disable logging of core PID loop computations");
James Feist1fe08952019-05-07 09:17:16 -0700257
James Feist1fe08952019-05-07 09:17:16 -0700258 CLI11_PARSE(app, argc, argv);
259
Josh Lehan811f31d2020-09-20 23:31:55 -0700260 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
261 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
Bonnie Loc51ba912022-10-12 14:07:22 +0800262 static constexpr auto debugEnablePath = "/etc/thermal.d/debugging";
Josh Lehande745422020-11-07 02:14:09 -0800263 static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging";
Josh Lehan811f31d2020-09-20 23:31:55 -0700264
Josh Lehanf54b2602021-04-09 12:17:34 -0700265 // Set up default logging path, preferring command line if it was given
266 std::string defLoggingPath(loggingPath);
267 if (defLoggingPath.empty())
268 {
269 defLoggingPath = std::filesystem::temp_directory_path();
270 }
271 else
272 {
273 // Enable logging, if user explicitly gave path on command line
274 loggingEnabled = true;
275 }
276
Josh Lehan811f31d2020-09-20 23:31:55 -0700277 // If this file exists, enable logging at runtime
278 std::ifstream fsLogging(loggingEnablePath);
279 if (fsLogging)
280 {
Josh Lehande745422020-11-07 02:14:09 -0800281 // Allow logging path to be changed by file content
282 std::string altPath;
283 std::getline(fsLogging, altPath);
Josh Lehan811f31d2020-09-20 23:31:55 -0700284 fsLogging.close();
285
Josh Lehande745422020-11-07 02:14:09 -0800286 if (std::filesystem::exists(altPath))
Josh Lehanf54b2602021-04-09 12:17:34 -0700287 {
Josh Lehande745422020-11-07 02:14:09 -0800288 loggingPath = altPath;
Josh Lehanf54b2602021-04-09 12:17:34 -0700289 }
Jinliang Wangcdc8dca2023-04-20 14:36:22 -0700290 else
291 {
292 loggingPath = defLoggingPath;
293 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700294
Josh Lehan811f31d2020-09-20 23:31:55 -0700295 loggingEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700296 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700297 if (loggingEnabled)
298 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700299 std::cerr << "Logging enabled: " << loggingPath << "\n";
300 }
301
302 // If this file exists, enable tuning at runtime
303 if (std::filesystem::exists(tuningEnablePath))
304 {
305 tuningEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700306 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700307 if (tuningEnabled)
308 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700309 std::cerr << "Tuning enabled\n";
310 }
Kun Yid4695592019-05-17 10:42:18 -0700311
Bonnie Loc51ba912022-10-12 14:07:22 +0800312 // If this file exists, enable debug mode at runtime
313 if (std::filesystem::exists(debugEnablePath))
314 {
315 debugEnabled = true;
316 }
317
318 if (debugEnabled)
319 {
320 std::cerr << "Debug mode enabled\n";
321 }
322
Josh Lehande745422020-11-07 02:14:09 -0800323 // If this file exists, enable core logging at runtime
324 if (std::filesystem::exists(coreLoggingEnablePath))
325 {
326 coreLoggingEnabled = true;
327 }
328 if (coreLoggingEnabled)
329 {
330 std::cerr << "Core logging enabled\n";
331 }
332
James Feist1fe08952019-05-07 09:17:16 -0700333 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
334 // Create a manager for the ModeBus because we own it.
Patrick Williamsb228bc32022-07-22 19:26:56 -0500335 sdbusplus::server::manager_t(static_cast<sdbusplus::bus_t&>(modeControlBus),
336 modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700337 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
338 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Williamsb228bc32022-07-22 19:26:56 -0500339 sdbusplus::server::manager_t objManager(modeControlBus, modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700340
Potin Laie2ec69a2022-09-30 17:09:34 +0800341 // Enable SIGHUP handling to reload JSON config
342 signals.async_wait(sighupHandler);
343
Patrick Venturee6206562018-03-08 15:36:53 -0800344 /*
345 * All sensors are managed by one manager, but each zone has a pointer to
346 * it.
347 */
348
Patrick Venturea0764872020-08-08 07:48:43 -0700349 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800350
James Feistce6a3f32019-03-12 11:20:16 -0700351 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700352 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800353}