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 17 #include "config.h" 18 19 #include "build/buildjson.hpp" 20 #include "conf.hpp" 21 #include "dbus/dbusconfiguration.hpp" 22 #include "interfaces.hpp" 23 #include "pid/builder.hpp" 24 #include "pid/buildjson.hpp" 25 #include "pid/pidloop.hpp" 26 #include "pid/tuning.hpp" 27 #include "pid/zone.hpp" 28 #include "sensors/builder.hpp" 29 #include "sensors/buildjson.hpp" 30 #include "sensors/manager.hpp" 31 #include "util.hpp" 32 33 #include <CLI/CLI.hpp> 34 #include <boost/asio/io_context.hpp> 35 #include <boost/asio/steady_timer.hpp> 36 #include <sdbusplus/asio/connection.hpp> 37 #include <sdbusplus/bus.hpp> 38 #include <sdbusplus/server/manager.hpp> 39 40 #include <chrono> 41 #include <filesystem> 42 #include <iostream> 43 #include <list> 44 #include <map> 45 #include <memory> 46 #include <thread> 47 #include <unordered_map> 48 #include <utility> 49 #include <vector> 50 51 namespace pid_control 52 { 53 54 /* The configuration converted sensor list. */ 55 std::map<std::string, conf::SensorConfig> sensorConfig = {}; 56 /* The configuration converted PID list. */ 57 std::map<int64_t, conf::PIDConf> zoneConfig = {}; 58 /* The configuration converted Zone configuration. */ 59 std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; 60 61 } // namespace pid_control 62 63 /** the swampd daemon will check for the existence of this file. */ 64 constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 65 std::string configPath = ""; 66 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 77 namespace pid_control 78 { 79 80 void restartControlLoops() 81 { 82 static SensorManager mgmr; 83 static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; 84 static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; 85 static bool isCanceling = false; 86 87 for (const auto& timer : timers) 88 { 89 timer->cancel(); 90 } 91 isCanceling = true; 92 timers.clear(); 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 } 98 zones.clear(); 99 isCanceling = false; 100 101 const std::string& path = 102 (configPath.length() > 0) ? configPath : jsonConfigurationPath; 103 104 if (std::filesystem::exists(path)) 105 { 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 } 122 } 123 else 124 { 125 static boost::asio::steady_timer reloadTimer(io); 126 if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, 127 zoneConfig, zoneDetailsConfig)) 128 { 129 return; // configuration not ready 130 } 131 } 132 133 mgmr = buildSensors(sensorConfig, passiveBus, hostBus); 134 zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 135 136 if (0 == zones.size()) 137 { 138 std::cerr << "No zones defined, exiting.\n"; 139 std::exit(EXIT_FAILURE); 140 } 141 142 for (const auto& i : zones) 143 { 144 std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( 145 std::make_shared<boost::asio::steady_timer>(io)); 146 std::cerr << "pushing zone " << i.first << "\n"; 147 pidControlLoop(i.second, timer, &isCanceling); 148 } 149 } 150 151 void tryRestartControlLoops(bool first) 152 { 153 static int count = 0; 154 static const auto delayTime = std::chrono::seconds(10); 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) 158 { 159 std::cerr 160 << "ControlLoops has been scheduled, refresh the loop count\n"; 161 count = 1; 162 return; 163 } 164 165 auto restartLbd = [](const boost::system::error_code& error) { 166 if (error == boost::asio::error::operation_aborted) 167 { 168 return; 169 } 170 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 } 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 } 208 209 return; 210 } 211 212 } // namespace pid_control 213 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 230 CLI11_PARSE(app, argc, argv); 231 232 static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; 233 static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; 234 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 247 // If this file exists, enable logging at runtime 248 std::ifstream fsLogging(loggingEnablePath); 249 if (fsLogging) 250 { 251 // The first line of file might be a valid directory path 252 std::getline(fsLogging, loggingPath); 253 fsLogging.close(); 254 255 // If so, use it, otherwise use default logging path instead 256 if (!(std::filesystem::exists(loggingPath))) 257 { 258 loggingPath = defLoggingPath; 259 } 260 261 loggingEnabled = true; 262 } 263 264 if (loggingEnabled) 265 { 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; 273 } 274 275 // This can also be enabled from the command line 276 if (tuningEnabled) 277 { 278 std::cerr << "Tuning enabled\n"; 279 } 280 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"); 287 sdbusplus::server::manager::manager objManager(modeControlBus, modeRoot); 288 289 /* 290 * All sensors are managed by one manager, but each zone has a pointer to 291 * it. 292 */ 293 294 pid_control::tryRestartControlLoops(); 295 296 io.run(); 297 return 0; 298 } 299