blob: db15b8d3ccb6c5433131dbe02a5bd27f016d961e [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
Patrick Ventureba8ffa72019-02-11 12:03:56 -080019#include "build/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>
35#include <boost/asio/steady_timer.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070036#include <sdbusplus/asio/connection.hpp>
37#include <sdbusplus/bus.hpp>
Bruce Leecb4c1a22021-04-20 16:06:38 +080038#include <sdbusplus/server/manager.hpp>
Patrick Venturea83a3ec2020-08-04 09:52:05 -070039
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070040#include <chrono>
Patrick Venture7f9d6902020-09-10 12:09:57 -070041#include <filesystem>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070042#include <iostream>
James Feistce6a3f32019-03-12 11:20:16 -070043#include <list>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070044#include <map>
45#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070046#include <thread>
47#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080048#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070049#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080050
Patrick Venturea0764872020-08-08 07:48:43 -070051namespace pid_control
52{
53
James Feist1fe08952019-05-07 09:17:16 -070054/* The configuration converted sensor list. */
Patrick Venture1df9e872020-10-08 15:35:01 -070055std::map<std::string, conf::SensorConfig> sensorConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070056/* The configuration converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080057std::map<int64_t, conf::PIDConf> zoneConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070058/* The configuration converted Zone configuration. */
Patrick Venture1df9e872020-10-08 15:35:01 -070059std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080060
Patrick Venturea0764872020-08-08 07:48:43 -070061} // namespace pid_control
62
Patrick Venturec19f5d42019-02-14 10:59:47 -080063/** the swampd daemon will check for the existence of this file. */
64constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
James Feist1fe08952019-05-07 09:17:16 -070065std::string configPath = "";
Patrick Venturec19f5d42019-02-14 10:59:47 -080066
James Feist1fe08952019-05-07 09:17:16 -070067/* async io context for operation */
68boost::asio::io_context io;
69
70/* buses for system control */
71static sdbusplus::asio::connection modeControlBus(io);
72static sdbusplus::asio::connection
73 hostBus(io, sdbusplus::bus::new_system().release());
74static sdbusplus::asio::connection
75 passiveBus(io, sdbusplus::bus::new_system().release());
76
Patrick Venturea0764872020-08-08 07:48:43 -070077namespace pid_control
78{
79
James Feist1fe08952019-05-07 09:17:16 -070080void restartControlLoops()
Patrick Venturee6206562018-03-08 15:36:53 -080081{
James Feist1fe08952019-05-07 09:17:16 -070082 static SensorManager mgmr;
Johnathan Mantey5301aae2020-09-28 11:06:58 -070083 static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
84 static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
Hao Jiangb6a0b892021-02-21 18:00:45 -080085 static bool isCanceling = false;
Patrick Venturee6206562018-03-08 15:36:53 -080086
William A. Kennington III18d5bb12021-05-15 20:38:41 -070087 for (const auto& timer : timers)
Johnathan Mantey5301aae2020-09-28 11:06:58 -070088 {
89 timer->cancel();
90 }
Hao Jiangb6a0b892021-02-21 18:00:45 -080091 isCanceling = true;
James Feist1fe08952019-05-07 09:17:16 -070092 timers.clear();
Hao Jiangb228cea2021-02-20 11:53:09 -080093
94 if (zones.size() > 0 && zones.begin()->second.use_count() > 1)
95 {
96 throw std::runtime_error("wait for count back to 1");
97 }
Johnathan Mantey5301aae2020-09-28 11:06:58 -070098 zones.clear();
Hao Jiangb6a0b892021-02-21 18:00:45 -080099 isCanceling = false;
Patrick Venture4cb7c052019-02-14 11:11:33 -0800100
Patrick Venture18b13112019-02-14 11:43:59 -0800101 const std::string& path =
102 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -0800103
Patrick Venture7f9d6902020-09-10 12:09:57 -0700104 if (std::filesystem::exists(path))
Patrick Venturee6206562018-03-08 15:36:53 -0800105 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700106 /*
107 * When building the sensors, if any of the dbus passive ones aren't on
108 * the bus, it'll fail immediately.
109 */
110 try
111 {
112 auto jsonData = parseValidateJson(path);
113 sensorConfig = buildSensorsFromJson(jsonData);
114 std::tie(zoneConfig, zoneDetailsConfig) =
115 buildPIDsFromJson(jsonData);
116 }
117 catch (const std::exception& e)
118 {
119 std::cerr << "Failed during building: " << e.what() << "\n";
120 exit(EXIT_FAILURE); /* fatal error. */
121 }
Patrick Venturee6206562018-03-08 15:36:53 -0800122 }
Patrick Venture7f9d6902020-09-10 12:09:57 -0700123 else
Patrick Venture18b13112019-02-14 11:43:59 -0800124 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700125 static boost::asio::steady_timer reloadTimer(io);
Patrick Venture73823182020-10-08 15:12:51 -0700126 if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
127 zoneConfig, zoneDetailsConfig))
Patrick Venture7f9d6902020-09-10 12:09:57 -0700128 {
129 return; // configuration not ready
130 }
Patrick Venture18b13112019-02-14 11:43:59 -0800131 }
Patrick Venture4cb7c052019-02-14 11:11:33 -0800132
James Feist1fe08952019-05-07 09:17:16 -0700133 mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
134 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800135
136 if (0 == zones.size())
137 {
138 std::cerr << "No zones defined, exiting.\n";
James Feist1fe08952019-05-07 09:17:16 -0700139 std::exit(EXIT_FAILURE);
Patrick Venturee6206562018-03-08 15:36:53 -0800140 }
141
James Feist1fe08952019-05-07 09:17:16 -0700142 for (const auto& i : zones)
143 {
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700144 std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back(
145 std::make_shared<boost::asio::steady_timer>(io));
James Feist1fe08952019-05-07 09:17:16 -0700146 std::cerr << "pushing zone " << i.first << "\n";
Hao Jiangb6a0b892021-02-21 18:00:45 -0800147 pidControlLoop(i.second, timer, &isCanceling);
James Feist1fe08952019-05-07 09:17:16 -0700148 }
149}
150
Hao Jiangb228cea2021-02-20 11:53:09 -0800151void tryRestartControlLoops(bool first)
Yong Li298a95c2020-04-07 15:11:02 +0800152{
Hao Jiangb228cea2021-02-20 11:53:09 -0800153 static int count = 0;
Hao Jiangd11a7322021-03-24 11:25:57 -0700154 static const auto delayTime = std::chrono::seconds(10);
Hao Jiangb228cea2021-02-20 11:53:09 -0800155 static boost::asio::steady_timer timer(io);
156 // try to start a control loop while the loop has been scheduled.
157 if (first && count != 0)
Yong Li298a95c2020-04-07 15:11:02 +0800158 {
Hao Jiangb228cea2021-02-20 11:53:09 -0800159 std::cerr
160 << "ControlLoops has been scheduled, refresh the loop count\n";
161 count = 1;
162 return;
Yong Li298a95c2020-04-07 15:11:02 +0800163 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700164
165 auto restartLbd = [](const boost::system::error_code& error) {
Hao Jiang232ffe42021-02-21 20:11:49 -0800166 if (error == boost::asio::error::operation_aborted)
167 {
168 return;
169 }
Hao Jiangb228cea2021-02-20 11:53:09 -0800170
Hao Jiang232ffe42021-02-21 20:11:49 -0800171 // for the last loop, don't elminate the failure of restartControlLoops.
172 if (count >= 5)
173 {
174 restartControlLoops();
175 // reset count after succesful restartControlLoops()
176 count = 0;
177 return;
178 }
179
180 // retry when restartControlLoops() has some failure.
181 try
182 {
183 restartControlLoops();
184 // reset count after succesful restartControlLoops()
185 count = 0;
186 }
187 catch (const std::exception& e)
188 {
189 std::cerr << count
190 << " Failed during restartControlLoops, try again: "
191 << e.what() << "\n";
192 tryRestartControlLoops(false);
193 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700194 };
195 count++;
196 // first time of trying to restart the control loop without a delay
197 if (first)
198 {
199 boost::asio::post(io,
200 std::bind(restartLbd, boost::system::error_code()));
201 }
202 // re-try control loop, set up a delay.
203 else
204 {
205 timer.expires_after(delayTime);
206 timer.async_wait(restartLbd);
207 }
Yong Li298a95c2020-04-07 15:11:02 +0800208
209 return;
210}
211
Patrick Venturea0764872020-08-08 07:48:43 -0700212} // namespace pid_control
213
James Feist1fe08952019-05-07 09:17:16 -0700214int main(int argc, char* argv[])
215{
216 loggingPath = "";
217 loggingEnabled = false;
218 tuningEnabled = false;
219
220 CLI::App app{"OpenBMC Fan Control Daemon"};
221
222 app.add_option("-c,--conf", configPath,
223 "Optional parameter to specify configuration at run-time")
224 ->check(CLI::ExistingFile);
225 app.add_option("-l,--log", loggingPath,
226 "Optional parameter to specify logging folder")
227 ->check(CLI::ExistingDirectory);
228 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
229
James Feist1fe08952019-05-07 09:17:16 -0700230 CLI11_PARSE(app, argc, argv);
231
Josh Lehan811f31d2020-09-20 23:31:55 -0700232 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
233 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
234
Josh Lehanf54b2602021-04-09 12:17:34 -0700235 // Set up default logging path, preferring command line if it was given
236 std::string defLoggingPath(loggingPath);
237 if (defLoggingPath.empty())
238 {
239 defLoggingPath = std::filesystem::temp_directory_path();
240 }
241 else
242 {
243 // Enable logging, if user explicitly gave path on command line
244 loggingEnabled = true;
245 }
246
Josh Lehan811f31d2020-09-20 23:31:55 -0700247 // If this file exists, enable logging at runtime
248 std::ifstream fsLogging(loggingEnablePath);
249 if (fsLogging)
250 {
Josh Lehanf54b2602021-04-09 12:17:34 -0700251 // The first line of file might be a valid directory path
Josh Lehan811f31d2020-09-20 23:31:55 -0700252 std::getline(fsLogging, loggingPath);
Josh Lehan811f31d2020-09-20 23:31:55 -0700253 fsLogging.close();
254
Josh Lehanf54b2602021-04-09 12:17:34 -0700255 // If so, use it, otherwise use default logging path instead
256 if (!(std::filesystem::exists(loggingPath)))
257 {
258 loggingPath = defLoggingPath;
259 }
260
Josh Lehan811f31d2020-09-20 23:31:55 -0700261 loggingEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700262 }
263
264 if (loggingEnabled)
265 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700266 std::cerr << "Logging enabled: " << loggingPath << "\n";
267 }
268
269 // If this file exists, enable tuning at runtime
270 if (std::filesystem::exists(tuningEnablePath))
271 {
272 tuningEnabled = true;
Josh Lehanf54b2602021-04-09 12:17:34 -0700273 }
274
275 // This can also be enabled from the command line
276 if (tuningEnabled)
277 {
Josh Lehan811f31d2020-09-20 23:31:55 -0700278 std::cerr << "Tuning enabled\n";
279 }
Kun Yid4695592019-05-17 10:42:18 -0700280
James Feist1fe08952019-05-07 09:17:16 -0700281 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
282 // Create a manager for the ModeBus because we own it.
283 sdbusplus::server::manager::manager(
284 static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot);
285 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
286 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
Bruce Leecb4c1a22021-04-20 16:06:38 +0800287 sdbusplus::server::manager::manager objManager(modeControlBus, modeRoot);
James Feist1fe08952019-05-07 09:17:16 -0700288
Patrick Venturee6206562018-03-08 15:36:53 -0800289 /*
290 * All sensors are managed by one manager, but each zone has a pointer to
291 * it.
292 */
293
Patrick Venturea0764872020-08-08 07:48:43 -0700294 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800295
James Feistce6a3f32019-03-12 11:20:16 -0700296 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700297 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800298}