blob: 749bf777a6b9efaa4082d6e45cf121a0aea355de [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>
38
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070039#include <chrono>
Patrick Venture7f9d6902020-09-10 12:09:57 -070040#include <filesystem>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070041#include <iostream>
James Feistce6a3f32019-03-12 11:20:16 -070042#include <list>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070043#include <map>
44#include <memory>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070045#include <thread>
46#include <unordered_map>
Patrick Ventureba8ffa72019-02-11 12:03:56 -080047#include <utility>
Patrick Ventureda4a5dd2018-08-31 09:42:48 -070048#include <vector>
Patrick Venturee6206562018-03-08 15:36:53 -080049
Patrick Venturea0764872020-08-08 07:48:43 -070050namespace pid_control
51{
52
James Feist1fe08952019-05-07 09:17:16 -070053/* The configuration converted sensor list. */
Patrick Venture1df9e872020-10-08 15:35:01 -070054std::map<std::string, conf::SensorConfig> sensorConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070055/* The configuration converted PID list. */
James Feistf81f2882019-02-26 11:26:36 -080056std::map<int64_t, conf::PIDConf> zoneConfig = {};
James Feist1fe08952019-05-07 09:17:16 -070057/* The configuration converted Zone configuration. */
Patrick Venture1df9e872020-10-08 15:35:01 -070058std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {};
Patrick Venturee6206562018-03-08 15:36:53 -080059
Patrick Venturea0764872020-08-08 07:48:43 -070060} // namespace pid_control
61
Patrick Venturec19f5d42019-02-14 10:59:47 -080062/** the swampd daemon will check for the existence of this file. */
63constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
James Feist1fe08952019-05-07 09:17:16 -070064std::string 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;
68
69/* buses for system control */
70static sdbusplus::asio::connection modeControlBus(io);
71static sdbusplus::asio::connection
72 hostBus(io, sdbusplus::bus::new_system().release());
73static sdbusplus::asio::connection
74 passiveBus(io, sdbusplus::bus::new_system().release());
75
Patrick Venturea0764872020-08-08 07:48:43 -070076namespace pid_control
77{
78
James Feist1fe08952019-05-07 09:17:16 -070079void restartControlLoops()
Patrick Venturee6206562018-03-08 15:36:53 -080080{
James Feist1fe08952019-05-07 09:17:16 -070081 static SensorManager mgmr;
Johnathan Mantey5301aae2020-09-28 11:06:58 -070082 static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones;
83 static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers;
Hao Jiangb6a0b892021-02-21 18:00:45 -080084 static bool isCanceling = false;
Patrick Venturee6206562018-03-08 15:36:53 -080085
Johnathan Mantey5301aae2020-09-28 11:06:58 -070086 for (const auto timer : timers)
87 {
88 timer->cancel();
89 }
Hao Jiangb6a0b892021-02-21 18:00:45 -080090 isCanceling = true;
James Feist1fe08952019-05-07 09:17:16 -070091 timers.clear();
Hao Jiangb228cea2021-02-20 11:53:09 -080092
93 if (zones.size() > 0 && zones.begin()->second.use_count() > 1)
94 {
95 throw std::runtime_error("wait for count back to 1");
96 }
Johnathan Mantey5301aae2020-09-28 11:06:58 -070097 zones.clear();
Hao Jiangb6a0b892021-02-21 18:00:45 -080098 isCanceling = false;
Patrick Venture4cb7c052019-02-14 11:11:33 -080099
Patrick Venture18b13112019-02-14 11:43:59 -0800100 const std::string& path =
101 (configPath.length() > 0) ? configPath : jsonConfigurationPath;
Patrick Venturee6206562018-03-08 15:36:53 -0800102
Patrick Venture7f9d6902020-09-10 12:09:57 -0700103 if (std::filesystem::exists(path))
Patrick Venturee6206562018-03-08 15:36:53 -0800104 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700105 /*
106 * When building the sensors, if any of the dbus passive ones aren't on
107 * the bus, it'll fail immediately.
108 */
109 try
110 {
111 auto jsonData = parseValidateJson(path);
112 sensorConfig = buildSensorsFromJson(jsonData);
113 std::tie(zoneConfig, zoneDetailsConfig) =
114 buildPIDsFromJson(jsonData);
115 }
116 catch (const std::exception& e)
117 {
118 std::cerr << "Failed during building: " << e.what() << "\n";
119 exit(EXIT_FAILURE); /* fatal error. */
120 }
Patrick Venturee6206562018-03-08 15:36:53 -0800121 }
Patrick Venture7f9d6902020-09-10 12:09:57 -0700122 else
Patrick Venture18b13112019-02-14 11:43:59 -0800123 {
Patrick Venture7f9d6902020-09-10 12:09:57 -0700124 static boost::asio::steady_timer reloadTimer(io);
Patrick Venture73823182020-10-08 15:12:51 -0700125 if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig,
126 zoneConfig, zoneDetailsConfig))
Patrick Venture7f9d6902020-09-10 12:09:57 -0700127 {
128 return; // configuration not ready
129 }
Patrick Venture18b13112019-02-14 11:43:59 -0800130 }
Patrick Venture4cb7c052019-02-14 11:11:33 -0800131
James Feist1fe08952019-05-07 09:17:16 -0700132 mgmr = buildSensors(sensorConfig, passiveBus, hostBus);
133 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
Patrick Venturee6206562018-03-08 15:36:53 -0800134
135 if (0 == zones.size())
136 {
137 std::cerr << "No zones defined, exiting.\n";
James Feist1fe08952019-05-07 09:17:16 -0700138 std::exit(EXIT_FAILURE);
Patrick Venturee6206562018-03-08 15:36:53 -0800139 }
140
James Feist1fe08952019-05-07 09:17:16 -0700141 for (const auto& i : zones)
142 {
Johnathan Mantey5301aae2020-09-28 11:06:58 -0700143 std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back(
144 std::make_shared<boost::asio::steady_timer>(io));
James Feist1fe08952019-05-07 09:17:16 -0700145 std::cerr << "pushing zone " << i.first << "\n";
Hao Jiangb6a0b892021-02-21 18:00:45 -0800146 pidControlLoop(i.second, timer, &isCanceling);
James Feist1fe08952019-05-07 09:17:16 -0700147 }
148}
149
Hao Jiangb228cea2021-02-20 11:53:09 -0800150void tryRestartControlLoops(bool first)
Yong Li298a95c2020-04-07 15:11:02 +0800151{
Hao Jiangb228cea2021-02-20 11:53:09 -0800152 static int count = 0;
Hao Jiangd11a7322021-03-24 11:25:57 -0700153 static const auto delayTime = std::chrono::seconds(10);
Hao Jiangb228cea2021-02-20 11:53:09 -0800154 static boost::asio::steady_timer timer(io);
155 // try to start a control loop while the loop has been scheduled.
156 if (first && count != 0)
Yong Li298a95c2020-04-07 15:11:02 +0800157 {
Hao Jiangb228cea2021-02-20 11:53:09 -0800158 std::cerr
159 << "ControlLoops has been scheduled, refresh the loop count\n";
160 count = 1;
161 return;
Yong Li298a95c2020-04-07 15:11:02 +0800162 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700163
164 auto restartLbd = [](const boost::system::error_code& error) {
Hao Jiang232ffe42021-02-21 20:11:49 -0800165 if (error == boost::asio::error::operation_aborted)
166 {
167 return;
168 }
Hao Jiangb228cea2021-02-20 11:53:09 -0800169
Hao Jiang232ffe42021-02-21 20:11:49 -0800170 // for the last loop, don't elminate the failure of restartControlLoops.
171 if (count >= 5)
172 {
173 restartControlLoops();
174 // reset count after succesful restartControlLoops()
175 count = 0;
176 return;
177 }
178
179 // retry when restartControlLoops() has some failure.
180 try
181 {
182 restartControlLoops();
183 // reset count after succesful restartControlLoops()
184 count = 0;
185 }
186 catch (const std::exception& e)
187 {
188 std::cerr << count
189 << " Failed during restartControlLoops, try again: "
190 << e.what() << "\n";
191 tryRestartControlLoops(false);
192 }
Hao Jiangd11a7322021-03-24 11:25:57 -0700193 };
194 count++;
195 // first time of trying to restart the control loop without a delay
196 if (first)
197 {
198 boost::asio::post(io,
199 std::bind(restartLbd, boost::system::error_code()));
200 }
201 // re-try control loop, set up a delay.
202 else
203 {
204 timer.expires_after(delayTime);
205 timer.async_wait(restartLbd);
206 }
Yong Li298a95c2020-04-07 15:11:02 +0800207
208 return;
209}
210
Patrick Venturea0764872020-08-08 07:48:43 -0700211} // namespace pid_control
212
James Feist1fe08952019-05-07 09:17:16 -0700213int main(int argc, char* argv[])
214{
215 loggingPath = "";
216 loggingEnabled = false;
217 tuningEnabled = false;
218
219 CLI::App app{"OpenBMC Fan Control Daemon"};
220
221 app.add_option("-c,--conf", configPath,
222 "Optional parameter to specify configuration at run-time")
223 ->check(CLI::ExistingFile);
224 app.add_option("-l,--log", loggingPath,
225 "Optional parameter to specify logging folder")
226 ->check(CLI::ExistingDirectory);
227 app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning");
228
James Feist1fe08952019-05-07 09:17:16 -0700229 CLI11_PARSE(app, argc, argv);
230
Josh Lehan811f31d2020-09-20 23:31:55 -0700231 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging";
232 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning";
233
234 // If this file exists, enable logging at runtime
235 std::ifstream fsLogging(loggingEnablePath);
236 if (fsLogging)
237 {
238 // Unless file contents are a valid directory path, use system default
239 std::getline(fsLogging, loggingPath);
240 if (!(std::filesystem::exists(loggingPath)))
241 {
242 loggingPath = std::filesystem::temp_directory_path();
243 }
244 fsLogging.close();
245
246 loggingEnabled = true;
247 std::cerr << "Logging enabled: " << loggingPath << "\n";
248 }
249
250 // If this file exists, enable tuning at runtime
251 if (std::filesystem::exists(tuningEnablePath))
252 {
253 tuningEnabled = true;
254 std::cerr << "Tuning enabled\n";
255 }
Kun Yid4695592019-05-17 10:42:18 -0700256
James Feist1fe08952019-05-07 09:17:16 -0700257 static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
258 // Create a manager for the ModeBus because we own it.
259 sdbusplus::server::manager::manager(
260 static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot);
261 hostBus.request_name("xyz.openbmc_project.Hwmon.external");
262 modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl");
263
Patrick Venturee6206562018-03-08 15:36:53 -0800264 /*
265 * All sensors are managed by one manager, but each zone has a pointer to
266 * it.
267 */
268
Patrick Venturea0764872020-08-08 07:48:43 -0700269 pid_control::tryRestartControlLoops();
Patrick Venturee6206562018-03-08 15:36:53 -0800270
James Feistce6a3f32019-03-12 11:20:16 -0700271 io.run();
James Feist1fe08952019-05-07 09:17:16 -0700272 return 0;
Patrick Venturee6206562018-03-08 15:36:53 -0800273}