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. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 54 | std::map<std::string, struct 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. */ |
James Feist | f81f288 | 2019-02-26 11:26:36 -0800 | [diff] [blame] | 58 | std::map<int64_t, struct 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(); |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 90 | zones.clear(); |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 91 | |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 92 | const std::string& path = |
| 93 | (configPath.length() > 0) ? configPath : jsonConfigurationPath; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 94 | |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 95 | if (std::filesystem::exists(path)) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 96 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 97 | /* |
| 98 | * When building the sensors, if any of the dbus passive ones aren't on |
| 99 | * the bus, it'll fail immediately. |
| 100 | */ |
| 101 | try |
| 102 | { |
| 103 | auto jsonData = parseValidateJson(path); |
| 104 | sensorConfig = buildSensorsFromJson(jsonData); |
| 105 | std::tie(zoneConfig, zoneDetailsConfig) = |
| 106 | buildPIDsFromJson(jsonData); |
| 107 | } |
| 108 | catch (const std::exception& e) |
| 109 | { |
| 110 | std::cerr << "Failed during building: " << e.what() << "\n"; |
| 111 | exit(EXIT_FAILURE); /* fatal error. */ |
| 112 | } |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 113 | } |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 114 | else |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 115 | { |
Patrick Venture | 7f9d690 | 2020-09-10 12:09:57 -0700 | [diff] [blame] | 116 | static boost::asio::steady_timer reloadTimer(io); |
| 117 | if (!dbus_configuration::init(modeControlBus, reloadTimer)) |
| 118 | { |
| 119 | return; // configuration not ready |
| 120 | } |
Patrick Venture | 18b1311 | 2019-02-14 11:43:59 -0800 | [diff] [blame] | 121 | } |
Patrick Venture | 4cb7c05 | 2019-02-14 11:11:33 -0800 | [diff] [blame] | 122 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 123 | mgmr = buildSensors(sensorConfig, passiveBus, hostBus); |
| 124 | zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 125 | |
| 126 | if (0 == zones.size()) |
| 127 | { |
| 128 | std::cerr << "No zones defined, exiting.\n"; |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 129 | std::exit(EXIT_FAILURE); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 130 | } |
| 131 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 132 | for (const auto& i : zones) |
| 133 | { |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 134 | std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( |
| 135 | std::make_shared<boost::asio::steady_timer>(io)); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 136 | std::cerr << "pushing zone " << i.first << "\n"; |
Johnathan Mantey | 5301aae | 2020-09-28 11:06:58 -0700 | [diff] [blame] | 137 | pidControlLoop(i.second, timer); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Yong Li | 298a95c | 2020-04-07 15:11:02 +0800 | [diff] [blame] | 141 | void tryRestartControlLoops() |
| 142 | { |
| 143 | int count = 0; |
| 144 | for (count = 0; count <= 5; count++) |
| 145 | { |
| 146 | try |
| 147 | { |
| 148 | restartControlLoops(); |
| 149 | break; |
| 150 | } |
| 151 | catch (const std::exception& e) |
| 152 | { |
| 153 | std::cerr << count |
| 154 | << " Failed during restartControlLoops, try again: " |
| 155 | << e.what() << "\n"; |
| 156 | if (count >= 5) |
| 157 | { |
| 158 | throw std::runtime_error(e.what()); |
| 159 | } |
| 160 | } |
| 161 | std::this_thread::sleep_for(std::chrono::seconds(10)); |
| 162 | } |
| 163 | |
| 164 | return; |
| 165 | } |
| 166 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 167 | } // namespace pid_control |
| 168 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 169 | int main(int argc, char* argv[]) |
| 170 | { |
| 171 | loggingPath = ""; |
| 172 | loggingEnabled = false; |
| 173 | tuningEnabled = false; |
| 174 | |
| 175 | CLI::App app{"OpenBMC Fan Control Daemon"}; |
| 176 | |
| 177 | app.add_option("-c,--conf", configPath, |
| 178 | "Optional parameter to specify configuration at run-time") |
| 179 | ->check(CLI::ExistingFile); |
| 180 | app.add_option("-l,--log", loggingPath, |
| 181 | "Optional parameter to specify logging folder") |
| 182 | ->check(CLI::ExistingDirectory); |
| 183 | app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); |
| 184 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 185 | CLI11_PARSE(app, argc, argv); |
| 186 | |
Josh Lehan | 811f31d | 2020-09-20 23:31:55 -0700 | [diff] [blame^] | 187 | static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; |
| 188 | static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; |
| 189 | |
| 190 | // If this file exists, enable logging at runtime |
| 191 | std::ifstream fsLogging(loggingEnablePath); |
| 192 | if (fsLogging) |
| 193 | { |
| 194 | // Unless file contents are a valid directory path, use system default |
| 195 | std::getline(fsLogging, loggingPath); |
| 196 | if (!(std::filesystem::exists(loggingPath))) |
| 197 | { |
| 198 | loggingPath = std::filesystem::temp_directory_path(); |
| 199 | } |
| 200 | fsLogging.close(); |
| 201 | |
| 202 | loggingEnabled = true; |
| 203 | std::cerr << "Logging enabled: " << loggingPath << "\n"; |
| 204 | } |
| 205 | |
| 206 | // If this file exists, enable tuning at runtime |
| 207 | if (std::filesystem::exists(tuningEnablePath)) |
| 208 | { |
| 209 | tuningEnabled = true; |
| 210 | std::cerr << "Tuning enabled\n"; |
| 211 | } |
Kun Yi | d469559 | 2019-05-17 10:42:18 -0700 | [diff] [blame] | 212 | |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 213 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
| 214 | // Create a manager for the ModeBus because we own it. |
| 215 | sdbusplus::server::manager::manager( |
| 216 | static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot); |
| 217 | hostBus.request_name("xyz.openbmc_project.Hwmon.external"); |
| 218 | modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); |
| 219 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 220 | /* |
| 221 | * All sensors are managed by one manager, but each zone has a pointer to |
| 222 | * it. |
| 223 | */ |
| 224 | |
Patrick Venture | a076487 | 2020-08-08 07:48:43 -0700 | [diff] [blame] | 225 | pid_control::tryRestartControlLoops(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 226 | |
James Feist | ce6a3f3 | 2019-03-12 11:20:16 -0700 | [diff] [blame] | 227 | io.run(); |
James Feist | 1fe0895 | 2019-05-07 09:17:16 -0700 | [diff] [blame] | 228 | return 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 229 | } |