Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 1 | /** |
| 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 Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 17 | #include "config.h" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 18 | |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 19 | #include "build/buildjson.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 20 | #include "conf.hpp" |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 21 | #include "dbus/dbusconfiguration.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 22 | #include "interfaces.hpp" |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 23 | #include "pid/builder.hpp" |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 24 | #include "pid/buildjson.hpp" |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 25 | #include "pid/pidloop.hpp" |
Patrick Venture | c32e3fc | 2019-02-28 10:01:11 -0800 | [diff] [blame] | 26 | #include "pid/tuning.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 27 | #include "pid/zone.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 28 | #include "sensors/builder.hpp" |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 29 | #include "sensors/buildjson.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 30 | #include "sensors/manager.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 31 | #include "util.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 32 | |
Patrick Venture | b5cc37c | 2019-03-11 09:11:55 -0700 | [diff] [blame] | 33 | #include <CLI/CLI.hpp> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 34 | #include <boost/asio/io_context.hpp> |
| 35 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 36 | #include <sdbusplus/asio/connection.hpp> |
| 37 | #include <sdbusplus/bus.hpp> |
Bruce Lee | cb4c1a2 | 2021-04-20 16:06:38 +0800 | [diff] [blame] | 38 | #include <sdbusplus/server/manager.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 39 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 40 | #include <chrono> |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 41 | #include <filesystem> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 42 | #include <iostream> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 43 | #include <list> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 44 | #include <map> |
| 45 | #include <memory> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 46 | #include <thread> |
| 47 | #include <unordered_map> |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 48 | #include <utility> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 49 | #include <vector> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 50 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 51 | namespace pid_control |
| 52 | { |
| 53 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 54 | /* The configuration converted sensor list. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 55 | std::map<std::string, conf::SensorConfig> sensorConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 56 | /* The configuration converted PID list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 57 | std::map<int64_t, conf::PIDConf> zoneConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 58 | /* The configuration converted Zone configuration. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 59 | std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 60 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 61 | } // namespace pid_control |
| 62 | |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 63 | /** the swampd daemon will check for the existence of this file. */ |
| 64 | constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 65 | std::string configPath = ""; |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 66 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 67 | /* async io context for operation */ |
| 68 | boost::asio::io_context io; |
| 69 | |
| 70 | /* buses for system control */ |
| 71 | static sdbusplus::asio::connection modeControlBus(io); |
| 72 | static sdbusplus::asio::connection |
| 73 | hostBus(io, sdbusplus::bus::new_system().release()); |
| 74 | static sdbusplus::asio::connection |
| 75 | passiveBus(io, sdbusplus::bus::new_system().release()); |
| 76 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 77 | namespace pid_control |
| 78 | { |
| 79 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 80 | void restartControlLoops() |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 81 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 82 | static SensorManager mgmr; |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 83 | static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; |
| 84 | static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 85 | static bool isCanceling = false; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 86 | |
William A. Kennington III | 18d5bb1 | 2021-05-15 20:38:41 -0700 | [diff] [blame] | 87 | for (const auto& timer : timers) |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 88 | { |
| 89 | timer->cancel(); |
| 90 | } |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 91 | isCanceling = true; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 92 | timers.clear(); |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 93 | |
| 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 Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 98 | zones.clear(); |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 99 | isCanceling = false; |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 100 | |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 101 | const std::string& path = |
| 102 | (configPath.length() > 0) ? configPath : jsonConfigurationPath; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 103 | |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 104 | if (std::filesystem::exists(path)) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 105 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 106 | /* |
| 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 Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 122 | } |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 123 | else |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 124 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 125 | static boost::asio::steady_timer reloadTimer(io); |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 126 | if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, |
| 127 | zoneConfig, zoneDetailsConfig)) |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 128 | { |
| 129 | return; // configuration not ready |
| 130 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 131 | } |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 132 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 133 | mgmr = buildSensors(sensorConfig, passiveBus, hostBus); |
| 134 | zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 135 | |
| 136 | if (0 == zones.size()) |
| 137 | { |
| 138 | std::cerr << "No zones defined, exiting.\n"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 139 | std::exit(EXIT_FAILURE); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 140 | } |
| 141 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 142 | for (const auto& i : zones) |
| 143 | { |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 144 | std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( |
| 145 | std::make_shared<boost::asio::steady_timer>(io)); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 146 | std::cerr << "pushing zone " << i.first << "\n"; |
Hao Jiang | b6a0b89 | 2021-02-21 18:00:45 -0800 | [diff] [blame] | 147 | pidControlLoop(i.second, timer, &isCanceling); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 148 | } |
| 149 | } |
| 150 | |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 151 | void tryRestartControlLoops(bool first) |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 152 | { |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 153 | static int count = 0; |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 154 | static const auto delayTime = std::chrono::seconds(10); |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 155 | 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 Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 158 | { |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 159 | std::cerr |
| 160 | << "ControlLoops has been scheduled, refresh the loop count\n"; |
| 161 | count = 1; |
| 162 | return; |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 163 | } |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 164 | |
| 165 | auto restartLbd = [](const boost::system::error_code& error) { |
Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 166 | if (error == boost::asio::error::operation_aborted) |
| 167 | { |
| 168 | return; |
| 169 | } |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 170 | |
Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 171 | // 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 Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 194 | }; |
| 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 Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 208 | |
| 209 | return; |
| 210 | } |
| 211 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 212 | } // namespace pid_control |
| 213 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 214 | int 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 Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 230 | CLI11_PARSE(app, argc, argv); |
| 231 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 232 | static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; |
| 233 | static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; |
| 234 | |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 235 | // 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 Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 247 | // If this file exists, enable logging at runtime |
| 248 | std::ifstream fsLogging(loggingEnablePath); |
| 249 | if (fsLogging) |
| 250 | { |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 251 | // The first line of file might be a valid directory path |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 252 | std::getline(fsLogging, loggingPath); |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 253 | fsLogging.close(); |
| 254 | |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 255 | // If so, use it, otherwise use default logging path instead |
| 256 | if (!(std::filesystem::exists(loggingPath))) |
| 257 | { |
| 258 | loggingPath = defLoggingPath; |
| 259 | } |
| 260 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 261 | loggingEnabled = true; |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 262 | } |
| 263 | |
| 264 | if (loggingEnabled) |
| 265 | { |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 266 | 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 Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | // This can also be enabled from the command line |
| 276 | if (tuningEnabled) |
| 277 | { |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 278 | std::cerr << "Tuning enabled\n"; |
| 279 | } |
Kun Yi | d469559 | 2019-05-17 10:42:18 -0700 | [diff] [blame] | 280 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 281 | 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 Lee | cb4c1a2 | 2021-04-20 16:06:38 +0800 | [diff] [blame] | 287 | sdbusplus::server::manager::manager objManager(modeControlBus, modeRoot); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 288 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 289 | /* |
| 290 | * All sensors are managed by one manager, but each zone has a pointer to |
| 291 | * it. |
| 292 | */ |
| 293 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 294 | pid_control::tryRestartControlLoops(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 295 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 296 | io.run(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 297 | return 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 298 | } |