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 | */ |
Pete O_o | 765a6d8 | 2025-07-23 21:44:14 -0700 | [diff] [blame] | 16 | #include "config.h" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 17 | |
Harvey Wu | 3dcfd54 | 2022-10-31 17:11:02 +0800 | [diff] [blame] | 18 | #include "buildjson/buildjson.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 19 | #include "conf.hpp" |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 20 | #include "dbus/dbusconfiguration.hpp" |
James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 21 | #include "failsafeloggers/builder.hpp" |
Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame^] | 22 | #include "hoststatemonitor.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 | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 27 | #include "sensors/builder.hpp" |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 28 | #include "sensors/buildjson.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 29 | #include "sensors/manager.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 30 | #include "util.hpp" |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 31 | #include "zone_interface.hpp" |
| 32 | |
| 33 | #include <signal.h> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 34 | |
Patrick Venture | b5cc37c | 2019-03-11 09:11:55 -0700 | [diff] [blame] | 35 | #include <CLI/CLI.hpp> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 36 | #include <boost/asio/error.hpp> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 37 | #include <boost/asio/io_context.hpp> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 38 | #include <boost/asio/post.hpp> |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 39 | #include <boost/asio/signal_set.hpp> |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 40 | #include <boost/asio/steady_timer.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 41 | #include <sdbusplus/asio/connection.hpp> |
| 42 | #include <sdbusplus/bus.hpp> |
Bruce Lee | cb4c1a2 | 2021-04-20 16:06:38 +0800 | [diff] [blame] | 43 | #include <sdbusplus/server/manager.hpp> |
Patrick Venture | a83a3ec | 2020-08-04 09:52:05 -0700 | [diff] [blame] | 44 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 45 | #include <chrono> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 46 | #include <csignal> |
| 47 | #include <cstdint> |
| 48 | #include <cstdlib> |
| 49 | #include <exception> |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 50 | #include <filesystem> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 51 | #include <fstream> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 52 | #include <iostream> |
| 53 | #include <map> |
| 54 | #include <memory> |
Patrick Williams | e3c6077 | 2025-04-07 17:53:42 -0400 | [diff] [blame] | 55 | #include <optional> |
Ed Tanous | f8b6e55 | 2025-06-27 13:27:50 -0700 | [diff] [blame] | 56 | #include <stdexcept> |
| 57 | #include <string> |
| 58 | #include <tuple> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 59 | #include <unordered_map> |
Patrick Venture | ba8ffa7 | 2019-02-11 12:03:56 -0800 | [diff] [blame] | 60 | #include <utility> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 61 | #include <vector> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 62 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 63 | namespace pid_control |
| 64 | { |
| 65 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 66 | /* The configuration converted sensor list. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 67 | std::map<std::string, conf::SensorConfig> sensorConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 68 | /* The configuration converted PID list. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 69 | std::map<int64_t, conf::PIDConf> zoneConfig = {}; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 70 | /* The configuration converted Zone configuration. */ |
Patrick Venture | 1df9e87 | 2020-10-08 15:35:01 -0700 | [diff] [blame] | 71 | std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 72 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 73 | namespace state |
| 74 | { |
| 75 | /* Set to true while canceling is in progress */ |
| 76 | static bool isCanceling = false; |
| 77 | /* The zones build from configuration */ |
| 78 | static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; |
| 79 | /* The timers used by the PID loop */ |
| 80 | static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; |
| 81 | /* The sensors build from configuration */ |
Patrick Williams | e3c6077 | 2025-04-07 17:53:42 -0400 | [diff] [blame] | 82 | static std::optional<SensorManager> mgmr; |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 83 | } // namespace state |
| 84 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 85 | } // namespace pid_control |
| 86 | |
Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 87 | std::filesystem::path configPath = ""; |
Patrick Venture | c19f5d4 | 2019-02-14 10:59:47 -0800 | [diff] [blame] | 88 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 89 | /* async io context for operation */ |
| 90 | boost::asio::io_context io; |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 91 | /* async signal_set for signal handling */ |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 92 | boost::asio::signal_set signals(io, SIGHUP, SIGTERM); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 93 | |
| 94 | /* buses for system control */ |
| 95 | static sdbusplus::asio::connection modeControlBus(io); |
Patrick Williams | 7dea66c | 2025-04-07 14:23:50 -0400 | [diff] [blame] | 96 | static sdbusplus::asio::connection hostBus(io, sdbusplus::bus::new_bus()); |
| 97 | static sdbusplus::asio::connection passiveBus(io, sdbusplus::bus::new_bus()); |
Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame^] | 98 | static sdbusplus::asio::connection hostMatchBus(io, sdbusplus::bus::new_bus()); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 99 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 100 | namespace pid_control |
| 101 | { |
| 102 | |
Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 103 | std::filesystem::path searchConfigurationPath() |
| 104 | { |
| 105 | static constexpr auto name = "config.json"; |
| 106 | |
Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 107 | for (const auto& pathSeg : {std::filesystem::current_path(), |
| 108 | std::filesystem::path{"/var/lib/swampd"}, |
| 109 | std::filesystem::path{"/usr/share/swampd"}}) |
Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 110 | { |
| 111 | auto file = pathSeg / name; |
| 112 | if (std::filesystem::exists(file)) |
| 113 | { |
| 114 | return file; |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | return name; |
| 119 | } |
| 120 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 121 | void stopControlLoops() |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 122 | { |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 123 | for (const auto& timer : state::timers) |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 124 | { |
| 125 | timer->cancel(); |
| 126 | } |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 127 | state::isCanceling = true; |
| 128 | state::timers.clear(); |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 129 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 130 | if (state::zones.size() > 0 && state::zones.begin()->second.use_count() > 1) |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 131 | { |
| 132 | throw std::runtime_error("wait for count back to 1"); |
| 133 | } |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 134 | |
| 135 | state::zones.clear(); |
| 136 | state::isCanceling = false; |
| 137 | } |
| 138 | |
| 139 | void restartControlLoops() |
| 140 | { |
| 141 | stopControlLoops(); |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 142 | |
Potin Lai | 79cde00 | 2023-02-16 13:53:10 +0800 | [diff] [blame] | 143 | const std::filesystem::path path = |
| 144 | (!configPath.empty()) ? configPath : searchConfigurationPath(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 145 | |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 146 | if (std::filesystem::exists(path)) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 147 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 148 | /* |
| 149 | * When building the sensors, if any of the dbus passive ones aren't on |
| 150 | * the bus, it'll fail immediately. |
| 151 | */ |
| 152 | try |
| 153 | { |
| 154 | auto jsonData = parseValidateJson(path); |
| 155 | sensorConfig = buildSensorsFromJson(jsonData); |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 156 | std::tie(zoneConfig, zoneDetailsConfig) = |
| 157 | buildPIDsFromJson(jsonData); |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 158 | } |
| 159 | catch (const std::exception& e) |
| 160 | { |
| 161 | std::cerr << "Failed during building: " << e.what() << "\n"; |
| 162 | exit(EXIT_FAILURE); /* fatal error. */ |
| 163 | } |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 164 | } |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 165 | else |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 166 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 167 | static boost::asio::steady_timer reloadTimer(io); |
Patrick Venture | 7382318 | 2020-10-08 15:12:51 -0700 | [diff] [blame] | 168 | if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, |
| 169 | zoneConfig, zoneDetailsConfig)) |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 170 | { |
| 171 | return; // configuration not ready |
| 172 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 173 | } |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 174 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 175 | state::mgmr = buildSensors(sensorConfig, passiveBus, hostBus); |
Patrick Williams | bd63bca | 2024-08-16 15:21:10 -0400 | [diff] [blame] | 176 | state::zones = |
Patrick Williams | e3c6077 | 2025-04-07 17:53:42 -0400 | [diff] [blame] | 177 | buildZones(zoneConfig, zoneDetailsConfig, *state::mgmr, modeControlBus); |
James Zheng | 6df8bb5 | 2024-11-27 23:38:47 +0000 | [diff] [blame] | 178 | // Set `logMaxCountPerSecond` to 20 will limit the number of logs output per |
| 179 | // second in each zone. Using 20 here would limit the output rate to be no |
| 180 | // larger than 100 per sec for most platforms as the number of zones are |
| 181 | // usually <=3. This will effectively avoid resource exhaustion. |
| 182 | buildFailsafeLoggers(state::zones, /* logMaxCountPerSecond = */ 20); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 183 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 184 | if (0 == state::zones.size()) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 185 | { |
| 186 | std::cerr << "No zones defined, exiting.\n"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 187 | std::exit(EXIT_FAILURE); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 190 | for (const auto& i : state::zones) |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 191 | { |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 192 | std::shared_ptr<boost::asio::steady_timer> timer = |
| 193 | state::timers.emplace_back( |
| 194 | std::make_shared<boost::asio::steady_timer>(io)); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 195 | std::cerr << "pushing zone " << i.first << "\n"; |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 196 | pidControlLoop(i.second, timer, &state::isCanceling); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 200 | void tryRestartControlLoops(bool first) |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 201 | { |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 202 | static const auto delayTime = std::chrono::seconds(10); |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 203 | static boost::asio::steady_timer timer(io); |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 204 | |
| 205 | auto restartLbd = [](const boost::system::error_code& error) { |
Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 206 | if (error == boost::asio::error::operation_aborted) |
| 207 | { |
| 208 | return; |
| 209 | } |
Hao Jiang | b228cea | 2021-02-20 11:53:09 -0800 | [diff] [blame] | 210 | |
Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 211 | // retry when restartControlLoops() has some failure. |
| 212 | try |
| 213 | { |
| 214 | restartControlLoops(); |
Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 215 | } |
| 216 | catch (const std::exception& e) |
| 217 | { |
Zev Weiss | 5293ec2 | 2023-03-06 18:58:36 -0800 | [diff] [blame] | 218 | std::cerr << "Failed during restartControlLoops, try again: " |
Hao Jiang | 232ffe4 | 2021-02-21 20:11:49 -0800 | [diff] [blame] | 219 | << e.what() << "\n"; |
| 220 | tryRestartControlLoops(false); |
| 221 | } |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 222 | }; |
Zev Weiss | 5293ec2 | 2023-03-06 18:58:36 -0800 | [diff] [blame] | 223 | |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 224 | // first time of trying to restart the control loop without a delay |
| 225 | if (first) |
| 226 | { |
Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 227 | boost::asio::post(io, [restartLbd] { |
| 228 | restartLbd(boost::system::error_code()); |
| 229 | }); |
Hao Jiang | d11a732 | 2021-03-24 11:25:57 -0700 | [diff] [blame] | 230 | } |
| 231 | // re-try control loop, set up a delay. |
| 232 | else |
| 233 | { |
| 234 | timer.expires_after(delayTime); |
| 235 | timer.async_wait(restartLbd); |
| 236 | } |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 237 | |
| 238 | return; |
| 239 | } |
| 240 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 241 | void tryTerminateControlLoops(bool first) |
| 242 | { |
| 243 | static const auto delayTime = std::chrono::milliseconds(50); |
| 244 | static boost::asio::steady_timer timer(io); |
| 245 | |
| 246 | auto stopLbd = [](const boost::system::error_code& error) { |
| 247 | if (error == boost::asio::error::operation_aborted) |
| 248 | { |
| 249 | return; |
| 250 | } |
| 251 | |
| 252 | // retry when stopControlLoops() has some failure. |
| 253 | try |
| 254 | { |
| 255 | stopControlLoops(); |
| 256 | } |
| 257 | catch (const std::exception& e) |
| 258 | { |
| 259 | std::cerr << "Failed during stopControlLoops, try again: " |
| 260 | << e.what() << "\n"; |
| 261 | tryTerminateControlLoops(false); |
| 262 | return; |
| 263 | } |
| 264 | io.stop(); |
| 265 | }; |
| 266 | |
| 267 | // first time of trying to stop the control loop without a delay |
| 268 | if (first) |
| 269 | { |
Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 270 | boost::asio::post(io, |
| 271 | [stopLbd] { stopLbd(boost::system::error_code()); }); |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 272 | } |
| 273 | // re-try control loop, set up a delay. |
| 274 | else |
| 275 | { |
| 276 | timer.expires_after(delayTime); |
| 277 | timer.async_wait(stopLbd); |
| 278 | } |
| 279 | |
| 280 | return; |
| 281 | } |
| 282 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 283 | } // namespace pid_control |
| 284 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 285 | void signalHandler(const boost::system::error_code& error, int signal_number) |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 286 | { |
| 287 | static boost::asio::steady_timer timer(io); |
| 288 | |
| 289 | if (error) |
| 290 | { |
| 291 | std::cout << "Signal " << signal_number |
| 292 | << " handler error: " << error.message() << "\n"; |
| 293 | return; |
| 294 | } |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 295 | if (signal_number == SIGTERM) |
| 296 | { |
| 297 | pid_control::tryTerminateControlLoops(true); |
| 298 | } |
| 299 | else |
| 300 | { |
| 301 | timer.expires_after(std::chrono::seconds(1)); |
| 302 | timer.async_wait([](const boost::system::error_code ec) { |
| 303 | if (ec) |
| 304 | { |
| 305 | std::cout << "Signal timer error: " << ec.message() << "\n"; |
| 306 | return; |
| 307 | } |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 308 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 309 | std::cout << "reloading configuration\n"; |
| 310 | pid_control::tryRestartControlLoops(); |
| 311 | }); |
| 312 | } |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 313 | |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 314 | signals.async_wait(signalHandler); |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 315 | } |
| 316 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 317 | int main(int argc, char* argv[]) |
| 318 | { |
| 319 | loggingPath = ""; |
| 320 | loggingEnabled = false; |
| 321 | tuningEnabled = false; |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 322 | debugEnabled = false; |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 323 | coreLoggingEnabled = false; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 324 | |
| 325 | CLI::App app{"OpenBMC Fan Control Daemon"}; |
| 326 | |
| 327 | app.add_option("-c,--conf", configPath, |
| 328 | "Optional parameter to specify configuration at run-time") |
| 329 | ->check(CLI::ExistingFile); |
| 330 | app.add_option("-l,--log", loggingPath, |
| 331 | "Optional parameter to specify logging folder") |
| 332 | ->check(CLI::ExistingDirectory); |
| 333 | app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 334 | app.add_flag("-d,--debug", debugEnabled, "Enable or disable debug mode"); |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 335 | app.add_flag("-g,--corelogging", coreLoggingEnabled, |
| 336 | "Enable or disable logging of core PID loop computations"); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 337 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 338 | CLI11_PARSE(app, argc, argv); |
| 339 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 340 | static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; |
| 341 | static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 342 | static constexpr auto debugEnablePath = "/etc/thermal.d/debugging"; |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 343 | static constexpr auto coreLoggingEnablePath = "/etc/thermal.d/corelogging"; |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 344 | |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 345 | // Set up default logging path, preferring command line if it was given |
| 346 | std::string defLoggingPath(loggingPath); |
| 347 | if (defLoggingPath.empty()) |
| 348 | { |
| 349 | defLoggingPath = std::filesystem::temp_directory_path(); |
| 350 | } |
| 351 | else |
| 352 | { |
| 353 | // Enable logging, if user explicitly gave path on command line |
| 354 | loggingEnabled = true; |
| 355 | } |
| 356 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 357 | // If this file exists, enable logging at runtime |
| 358 | std::ifstream fsLogging(loggingEnablePath); |
| 359 | if (fsLogging) |
| 360 | { |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 361 | // Allow logging path to be changed by file content |
| 362 | std::string altPath; |
| 363 | std::getline(fsLogging, altPath); |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 364 | fsLogging.close(); |
| 365 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 366 | if (std::filesystem::exists(altPath)) |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 367 | { |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 368 | loggingPath = altPath; |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 369 | } |
Jinliang Wang | cdc8dca | 2023-04-20 14:36:22 -0700 | [diff] [blame] | 370 | else |
| 371 | { |
| 372 | loggingPath = defLoggingPath; |
| 373 | } |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 374 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 375 | loggingEnabled = true; |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 376 | } |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 377 | if (loggingEnabled) |
| 378 | { |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 379 | std::cerr << "Logging enabled: " << loggingPath << "\n"; |
| 380 | } |
| 381 | |
| 382 | // If this file exists, enable tuning at runtime |
| 383 | if (std::filesystem::exists(tuningEnablePath)) |
| 384 | { |
| 385 | tuningEnabled = true; |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 386 | } |
Josh Lehan | f54b260 | 2021-04-09 12:17:34 -0700 | [diff] [blame] | 387 | if (tuningEnabled) |
| 388 | { |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame] | 389 | std::cerr << "Tuning enabled\n"; |
| 390 | } |
Kun Yi | d469559 | 2019-05-17 10:42:18 -0700 | [diff] [blame] | 391 | |
Bonnie Lo | c51ba91 | 2022-10-12 14:07:22 +0800 | [diff] [blame] | 392 | // If this file exists, enable debug mode at runtime |
| 393 | if (std::filesystem::exists(debugEnablePath)) |
| 394 | { |
| 395 | debugEnabled = true; |
| 396 | } |
| 397 | |
| 398 | if (debugEnabled) |
| 399 | { |
| 400 | std::cerr << "Debug mode enabled\n"; |
| 401 | } |
| 402 | |
Josh Lehan | de74542 | 2020-11-07 02:14:09 -0800 | [diff] [blame] | 403 | // If this file exists, enable core logging at runtime |
| 404 | if (std::filesystem::exists(coreLoggingEnablePath)) |
| 405 | { |
| 406 | coreLoggingEnabled = true; |
| 407 | } |
| 408 | if (coreLoggingEnabled) |
| 409 | { |
| 410 | std::cerr << "Core logging enabled\n"; |
| 411 | } |
| 412 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 413 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
| 414 | // Create a manager for the ModeBus because we own it. |
Ed Tanous | d2768c5 | 2025-06-26 11:42:57 -0700 | [diff] [blame] | 415 | sdbusplus::server::manager_t manager( |
| 416 | static_cast<sdbusplus::bus_t&>(modeControlBus), modeRoot); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 417 | hostBus.request_name("xyz.openbmc_project.Hwmon.external"); |
| 418 | modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); |
Patrick Williams | b228bc3 | 2022-07-22 19:26:56 -0500 | [diff] [blame] | 419 | sdbusplus::server::manager_t objManager(modeControlBus, modeRoot); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 420 | |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 421 | // Enable SIGHUP handling to reload JSON config |
Patrick Rudolph | c7b2be3 | 2023-10-13 12:34:58 +0200 | [diff] [blame] | 422 | signals.async_wait(signalHandler); |
Potin Lai | e2ec69a | 2022-09-30 17:09:34 +0800 | [diff] [blame] | 423 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 424 | /* |
| 425 | * All sensors are managed by one manager, but each zone has a pointer to |
| 426 | * it. |
| 427 | */ |
| 428 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 429 | pid_control::tryRestartControlLoops(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 430 | |
Potin Lai | e1fa859 | 2025-08-29 15:27:08 +0800 | [diff] [blame^] | 431 | /* setup host state monitor */ |
| 432 | auto& monitor = HostStateMonitor::getInstance(hostMatchBus); |
| 433 | monitor.startMonitoring(); |
| 434 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 435 | io.run(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 436 | return 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 437 | } |