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> |
| 38 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 39 | #include <chrono> |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 40 | #include <filesystem> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 41 | #include <iostream> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 42 | #include <list> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 43 | #include <map> |
| 44 | #include <memory> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 45 | #include <thread> |
| 46 | #include <unordered_map> |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 47 | #include <utility> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 48 | #include <vector> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 49 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 50 | namespace pid_control |
| 51 | { |
| 52 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 53 | /* The configuration converted sensor list. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 54 | std::map<std::string, conf::SensorConfig> sensorConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 55 | /* The configuration converted PID list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 56 | std::map<int64_t, conf::PIDConf> zoneConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 57 | /* The configuration converted Zone configuration. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 58 | std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 59 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 60 | } // namespace pid_control |
| 61 | |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 62 | /** the swampd daemon will check for the existence of this file. */ |
| 63 | constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 64 | std::string configPath = ""; |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 65 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 66 | /* async io context for operation */ |
| 67 | boost::asio::io_context io; |
| 68 | |
| 69 | /* buses for system control */ |
| 70 | static sdbusplus::asio::connection modeControlBus(io); |
| 71 | static sdbusplus::asio::connection |
| 72 | hostBus(io, sdbusplus::bus::new_system().release()); |
| 73 | static sdbusplus::asio::connection |
| 74 | passiveBus(io, sdbusplus::bus::new_system().release()); |
| 75 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 76 | namespace pid_control |
| 77 | { |
| 78 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 79 | void restartControlLoops() |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 80 | { |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 81 | static SensorManager mgmr; |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 82 | static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; |
| 83 | static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 84 | |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 85 | for (const auto timer : timers) |
| 86 | { |
| 87 | timer->cancel(); |
| 88 | } |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 89 | timers.clear(); |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame^] | 90 | |
| 91 | if (zones.size() > 0 && zones.begin()->second.use_count() > 1) |
| 92 | { |
| 93 | throw std::runtime_error("wait for count back to 1"); |
| 94 | } |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 95 | zones.clear(); |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 96 | |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 97 | const std::string& path = |
| 98 | (configPath.length() > 0) ? configPath : jsonConfigurationPath; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 99 | |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 100 | if (std::filesystem::exists(path)) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 101 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 102 | /* |
| 103 | * When building the sensors, if any of the dbus passive ones aren't on |
| 104 | * the bus, it'll fail immediately. |
| 105 | */ |
| 106 | try |
| 107 | { |
| 108 | auto jsonData = parseValidateJson(path); |
| 109 | sensorConfig = buildSensorsFromJson(jsonData); |
| 110 | std::tie(zoneConfig, zoneDetailsConfig) = |
| 111 | buildPIDsFromJson(jsonData); |
| 112 | } |
| 113 | catch (const std::exception& e) |
| 114 | { |
| 115 | std::cerr << "Failed during building: " << e.what() << "\n"; |
| 116 | exit(EXIT_FAILURE); /* fatal error. */ |
| 117 | } |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 118 | } |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 119 | else |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 120 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 121 | static boost::asio::steady_timer reloadTimer(io); |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 122 | if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, |
| 123 | zoneConfig, zoneDetailsConfig)) |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 124 | { |
| 125 | return; // configuration not ready |
| 126 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 127 | } |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 128 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 129 | mgmr = buildSensors(sensorConfig, passiveBus, hostBus); |
| 130 | zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 131 | |
| 132 | if (0 == zones.size()) |
| 133 | { |
| 134 | std::cerr << "No zones defined, exiting.\n"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 135 | std::exit(EXIT_FAILURE); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 136 | } |
| 137 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 138 | for (const auto& i : zones) |
| 139 | { |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 140 | std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( |
| 141 | std::make_shared<boost::asio::steady_timer>(io)); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 142 | std::cerr << "pushing zone " << i.first << "\n"; |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 143 | pidControlLoop(i.second, timer); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame^] | 147 | void tryRestartControlLoops(bool first) |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 148 | { |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame^] | 149 | static int count = 0; |
| 150 | static const auto initialStartTime = std::chrono::milliseconds(10); |
| 151 | static const auto delayTeime = std::chrono::seconds(10); |
| 152 | static boost::asio::steady_timer timer(io); |
| 153 | // try to start a control loop while the loop has been scheduled. |
| 154 | if (first && count != 0) |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 155 | { |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame^] | 156 | std::cerr |
| 157 | << "ControlLoops has been scheduled, refresh the loop count\n"; |
| 158 | count = 1; |
| 159 | return; |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 160 | } |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame^] | 161 | // first time of trying to restart the control loop, delay for a small |
| 162 | // amount of time. |
| 163 | else if (first) |
| 164 | { |
| 165 | timer.expires_after(initialStartTime); |
| 166 | } |
| 167 | // re-try control loop, set up a delay. |
| 168 | else |
| 169 | { |
| 170 | timer.expires_after(delayTeime); |
| 171 | } |
| 172 | count++; |
| 173 | timer.async_wait( |
| 174 | [](const boost::system::error_code& error) { |
| 175 | if (error == boost::asio::error::operation_aborted) |
| 176 | { |
| 177 | return; |
| 178 | } |
| 179 | try |
| 180 | { |
| 181 | restartControlLoops(); |
| 182 | // reset count after succesful restartControlLoops() |
| 183 | count = 0; |
| 184 | } |
| 185 | catch (const std::exception& e) |
| 186 | { |
| 187 | if (count >= 5) |
| 188 | { |
| 189 | std::cerr |
| 190 | << "Failed to restartControlLoops after multiple tries" |
| 191 | << std::endl; |
| 192 | throw std::runtime_error(e.what()); |
| 193 | } |
| 194 | |
| 195 | std::cerr << count |
| 196 | << " Failed during restartControlLoops, try again: " |
| 197 | << e.what() << "\n"; |
| 198 | tryRestartControlLoops(false); |
| 199 | } |
| 200 | }); |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 201 | |
| 202 | return; |
| 203 | } |
| 204 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 205 | } // namespace pid_control |
| 206 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 207 | int main(int argc, char* argv[]) |
| 208 | { |
| 209 | loggingPath = ""; |
| 210 | loggingEnabled = false; |
| 211 | tuningEnabled = false; |
| 212 | |
| 213 | CLI::App app{"OpenBMC Fan Control Daemon"}; |
| 214 | |
| 215 | app.add_option("-c,--conf", configPath, |
| 216 | "Optional parameter to specify configuration at run-time") |
| 217 | ->check(CLI::ExistingFile); |
| 218 | app.add_option("-l,--log", loggingPath, |
| 219 | "Optional parameter to specify logging folder") |
| 220 | ->check(CLI::ExistingDirectory); |
| 221 | app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); |
| 222 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 223 | CLI11_PARSE(app, argc, argv); |
| 224 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 225 | static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; |
| 226 | static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; |
| 227 | |
| 228 | // If this file exists, enable logging at runtime |
| 229 | std::ifstream fsLogging(loggingEnablePath); |
| 230 | if (fsLogging) |
| 231 | { |
| 232 | // Unless file contents are a valid directory path, use system default |
| 233 | std::getline(fsLogging, loggingPath); |
| 234 | if (!(std::filesystem::exists(loggingPath))) |
| 235 | { |
| 236 | loggingPath = std::filesystem::temp_directory_path(); |
| 237 | } |
| 238 | fsLogging.close(); |
| 239 | |
| 240 | loggingEnabled = true; |
| 241 | std::cerr << "Logging enabled: " << loggingPath << "\n"; |
| 242 | } |
| 243 | |
| 244 | // If this file exists, enable tuning at runtime |
| 245 | if (std::filesystem::exists(tuningEnablePath)) |
| 246 | { |
| 247 | tuningEnabled = true; |
| 248 | std::cerr << "Tuning enabled\n"; |
| 249 | } |
Kun Yi | d469559 | 2019-05-17 10:42:18 -0700 | [diff] [blame] | 250 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 251 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
| 252 | // Create a manager for the ModeBus because we own it. |
| 253 | sdbusplus::server::manager::manager( |
| 254 | static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot); |
| 255 | hostBus.request_name("xyz.openbmc_project.Hwmon.external"); |
| 256 | modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); |
| 257 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 258 | /* |
| 259 | * All sensors are managed by one manager, but each zone has a pointer to |
| 260 | * it. |
| 261 | */ |
| 262 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 263 | pid_control::tryRestartControlLoops(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 264 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 265 | io.run(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 266 | return 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 267 | } |