blob: bace238f7c71e9ecaf8c23d752622cc12004a416 [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);
133 std::tie(zoneConfig, zoneDetailsConfig) =
134 buildPIDsFromJson(jsonData);
135 }
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 Jiangb228cea2021-02-20 11:53:09 -0800172 static int count = 0;
Hao Jiangd11a7322021-03-24 11:25:57 -0700173 static const auto delayTime = std::chrono::seconds(10);
Hao Jiangb228cea2021-02-20 11:53:09 -0800174 static boost::asio::steady_timer timer(io);
175 // try to start a control loop while the loop has been scheduled.
176 if (first && count != 0)
Yong Li298a95c2020-04-07 15:11:02 +0800177 {
Hao Jiangb228cea2021-02-20 11:53:09 -0800178 std::cerr
179 << "ControlLoops has been scheduled, refresh the loop count\n";
180 count = 1;
181 return;
Yong Li298a95c2020-04-07 15:11:02 +0800182 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700183
184 auto restartLbd = [](const boost::system::error_code& error) {
Hao Jiang232ffe42021-02-21 20:11:49 -0800185 if (error == boost::asio::error::operation_aborted)
186 {
187 return;
188 }
Hao Jiangb228cea2021-02-20 11:53:09 -0800189
Hao Jiang232ffe42021-02-21 20:11:49 -0800190 // for the last loop, don't elminate the failure of restartControlLoops.
191 if (count >= 5)
192 {
193 restartControlLoops();
194 // reset count after succesful restartControlLoops()
195 count = 0;
196 return;
197 }
198
199 // retry when restartControlLoops() has some failure.
200 try
201 {
202 restartControlLoops();
203 // reset count after succesful restartControlLoops()
204 count = 0;
205 }
206 catch (const std::exception& e)
207 {
208 std::cerr << count
209 << " Failed during restartControlLoops, try again: "
210 << e.what() << "\n";
211 tryRestartControlLoops(false);
212 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700213 };
214 count++;
215 // first time of trying to restart the control loop without a delay
216 if (first)
217 {
218 boost::asio::post(io,
219 std::bind(restartLbd, boost::system::error_code()));
220 }
221 // re-try control loop, set up a delay.
222 else
223 {
224 timer.expires_after(delayTime);
225 timer.async_wait(restartLbd);
226 }
Yong Li298a95c2020-04-07 15:11:02 +0800227
228 return;
229}
230
Patrick Venturea0764872020-08-08 07:48:43 -0700231} // namespace pid_control
232
Potin Laie2ec69a2022-09-30 17:09:34 +0800233void sighupHandler(const boost::system::error_code& error, int signal_number)
234{
235 static boost::asio::steady_timer timer(io);
236
237 if (error)
238 {
239 std::cout << "Signal " << signal_number
240 << " handler error: " << error.message() << "\n";
241 return;
242 }
243
244 timer.expires_after(std::chrono::seconds(1));
245 timer.async_wait([](const boost::system::error_code ec) {
246 if (ec)
247 {
248 std::cout << "Signal timer error: " << ec.message() << "\n";
249 return;
250 }
251
252 std::cout << "reloading configuration\n";
253 pid_control::tryRestartControlLoops();
254 });
255 signals.async_wait(sighupHandler);
256}
257
James Feist1fe08952019-05-07 09:17:16 -0700258int main(int argc, char* argv[])
259{
260 loggingPath = "";
261 loggingEnabled = false;
262 tuningEnabled = false;
Bonnie Loc51ba912022-10-12 14:07:22 +0800263 debugEnabled = false;
Josh Lehande745422020-11-07 02:14:09 -0800264 coreLoggingEnabled = false;
James Feist1fe08952019-05-07 09:17:16 -0700265
266 CLI::App app{"OpenBMC Fan Control Daemon"};
267
268 app.add_option("-c,--conf", configPath,
269 "Optional parameter to specify configuration at run-time")
270 ->check(CLI::ExistingFile);
271 app.add_option("-l,--log", loggingPath,
272 "Optional parameter to specify logging folder")
273 ->check(CLI::ExistingDirectory);
274 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
Bonnie Loc51ba912022-10-12 14:07:22 +0800275 app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode");
Josh Lehande745422020-11-07 02:14:09 -0800276 app.add_flag("-g,--corelogging", coreLoggingEnabled,
277 "Enable or disable logging of core PID loop computations");
James Feist1fe08952019-05-07 09:17:16 -0700278
James Feist1fe08952019-05-07 09:17:16 -0700279 CLI11_PARSE(app, argc, argv);
280
Josh Lehan811f31d2020-09-20 23:31:55 -0700281 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
282 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
Bonnie Loc51ba912022-10-12 14:07:22 +0800283 static constexpr auto debugEnablePath = "/etc/thermal.d/debugging";
Josh Lehande745422020-11-07 02:14:09 -0800284 static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging";
Josh Lehan811f31d2020-09-20 23:31:55 -0700285
Josh Lehanf54b2602021-04-09 12:17:34 -0700286 // Set up default logging path, preferring command line if it was given
287 std::string defLoggingPath(loggingPath);
288 if (defLoggingPath.empty())
289 {
290 defLoggingPath = std::filesystem::temp_directory_path();
291 }
292 else
293 {
294 // Enable logging, if user explicitly gave path on command line
295 loggingEnabled = true;
296 }
297
Josh Lehan811f31d2020-09-20 23:31:55 -0700298 // If this file exists, enable logging at runtime
299 std::ifstream fsLogging(loggingEnablePath);
300 if (fsLogging)
301 {
Josh Lehande745422020-11-07 02:14:09 -0800302 // Allow logging path to be changed by file content
303 std::string altPath;
304 std::getline(fsLogging, altPath);
Josh Lehan811f31d2020-09-20 23:31:55 -0700305 fsLogging.close();
306
Josh Lehande745422020-11-07 02:14:09 -0800307 if (std::filesystem::exists(altPath))
Josh Lehanf54b2602021-04-09 12:17:34 -0700308 {
Josh Lehande745422020-11-07 02:14:09 -0800309 loggingPath = altPath;
Josh Lehanf54b2602021-04-09 12:17:34 -0700310 }
311
Josh Lehan811f31d2020-09-20 23:31:55 -0700312 loggingEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700313 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700314 if (loggingEnabled)
315 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700316 std::cerr << "Logging enabled: " << loggingPath << "\n";
317 }
318
319 // If this file exists, enable tuning at runtime
320 if (std::filesystem::exists(tuningEnablePath))
321 {
322 tuningEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700323 }
Josh Lehanf54b2602021-04-09 12:17:34 -0700324 if (tuningEnabled)
325 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700326 std::cerr << "Tuning enabled\n";
327 }
Kun Yid4695592019-05-17 10:42:18 -0700328
Bonnie Loc51ba912022-10-12 14:07:22 +0800329 // If this file exists, enable debug mode at runtime
330 if (std::filesystem::exists(debugEnablePath))
331 {
332 debugEnabled = true;
333 }
334
335 if (debugEnabled)
336 {
337 std::cerr << "Debug mode enabled\n";
338 }
339
Josh Lehande745422020-11-07 02:14:09 -0800340 // If this file exists, enable core logging at runtime
341 if (std::filesystem::exists(coreLoggingEnablePath))
342 {
343 coreLoggingEnabled = true;
344 }
345 if (coreLoggingEnabled)
346 {
347 std::cerr << "Core logging enabled\n";
348 }
349
James Feist1fe08952019-05-07 09:17:16 -0700350 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
351 // Create a manager for the ModeBus because we own it.
Patrick Williamsb228bc32022-07-22 19:26:56 -0500352 sdbusplus::server::manager_t(static_cast<sdbusplus::bus_t&>(modeControlBus),
353 modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700354 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
355 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
Patrick Williamsb228bc32022-07-22 19:26:56 -0500356 sdbusplus::server::manager_t objManager(modeControlBus, modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700357
Potin Laie2ec69a2022-09-30 17:09:34 +0800358 // Enable SIGHUP handling to reload JSON config
359 signals.async_wait(sighupHandler);
360
Patrick Venturee6206562018-03-08 15:36:53 -0800361 /*
362 * All sensors are managed by one manager, but each zone has a pointer to
363 * it.
364 */
365
Patrick Venturea0764872020-08-08 07:48:43 -0700366 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800367
James Feistce6a3f32019-03-12 11:20:16 -0700368 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700369 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800370}