1e620656cSPatrick Venture /** 2e620656cSPatrick Venture * Copyright 2017 Google Inc. 3e620656cSPatrick Venture * 4e620656cSPatrick Venture * Licensed under the Apache License, Version 2.0 (the "License"); 5e620656cSPatrick Venture * you may not use this file except in compliance with the License. 6e620656cSPatrick Venture * You may obtain a copy of the License at 7e620656cSPatrick Venture * 8e620656cSPatrick Venture * http://www.apache.org/licenses/LICENSE-2.0 9e620656cSPatrick Venture * 10e620656cSPatrick Venture * Unless required by applicable law or agreed to in writing, software 11e620656cSPatrick Venture * distributed under the License is distributed on an "AS IS" BASIS, 12e620656cSPatrick Venture * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13e620656cSPatrick Venture * See the License for the specific language governing permissions and 14e620656cSPatrick Venture * limitations under the License. 15e620656cSPatrick Venture */ 16e620656cSPatrick Venture 177136a5aeSJames Feist #include "config.h" 18e620656cSPatrick Venture 19ba8ffa73SPatrick Venture #include "build/buildjson.hpp" 20da4a5dd1SPatrick Venture #include "conf.hpp" 217f9d690dSPatrick Venture #include "dbus/dbusconfiguration.hpp" 22e620656cSPatrick Venture #include "interfaces.hpp" 235c7cc545SPatrick Venture #include "pid/builder.hpp" 24ba8ffa73SPatrick Venture #include "pid/buildjson.hpp" 25ce6a3f36SJames Feist #include "pid/pidloop.hpp" 26c32e3fc5SPatrick Venture #include "pid/tuning.hpp" 27e620656cSPatrick Venture #include "pid/zone.hpp" 285e929093SPatrick Venture #include "sensors/builder.hpp" 29ba8ffa73SPatrick Venture #include "sensors/buildjson.hpp" 30e620656cSPatrick Venture #include "sensors/manager.hpp" 31da4a5dd1SPatrick Venture #include "util.hpp" 32e620656cSPatrick Venture 33b5cc37ceSPatrick Venture #include <CLI/CLI.hpp> 34ce6a3f36SJames Feist #include <boost/asio/io_context.hpp> 35ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp> 36a83a3eccSPatrick Venture #include <sdbusplus/asio/connection.hpp> 37a83a3eccSPatrick Venture #include <sdbusplus/bus.hpp> 38a83a3eccSPatrick Venture 39da4a5dd1SPatrick Venture #include <chrono> 407f9d690dSPatrick Venture #include <filesystem> 41da4a5dd1SPatrick Venture #include <iostream> 42ce6a3f36SJames Feist #include <list> 43da4a5dd1SPatrick Venture #include <map> 44da4a5dd1SPatrick Venture #include <memory> 45da4a5dd1SPatrick Venture #include <thread> 46da4a5dd1SPatrick Venture #include <unordered_map> 47ba8ffa73SPatrick Venture #include <utility> 48da4a5dd1SPatrick Venture #include <vector> 49e620656cSPatrick Venture 50a076487aSPatrick Venture namespace pid_control 51a076487aSPatrick Venture { 52a076487aSPatrick Venture 531fe08952SJames Feist /* The configuration converted sensor list. */ 541df9e879SPatrick Venture std::map<std::string, conf::SensorConfig> sensorConfig = {}; 551fe08952SJames Feist /* The configuration converted PID list. */ 56f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {}; 571fe08952SJames Feist /* The configuration converted Zone configuration. */ 581df9e879SPatrick Venture std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; 59e620656cSPatrick Venture 60a076487aSPatrick Venture } // namespace pid_control 61a076487aSPatrick Venture 62c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */ 63c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 64e620656cSPatrick Venture std::string configPath = ""; 65e620656cSPatrick Venture 661fe08952SJames Feist /* async io context for operation */ 671fe08952SJames Feist boost::asio::io_context io; 68e620656cSPatrick Venture 691fe08952SJames Feist /* buses for system control */ 701fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io); 711fe08952SJames Feist static sdbusplus::asio::connection 721fe08952SJames Feist hostBus(io, sdbusplus::bus::new_system().release()); 731fe08952SJames Feist static sdbusplus::asio::connection 741fe08952SJames Feist passiveBus(io, sdbusplus::bus::new_system().release()); 75de79ee05SPatrick Venture 76a076487aSPatrick Venture namespace pid_control 77a076487aSPatrick Venture { 78a076487aSPatrick Venture 791fe08952SJames Feist void restartControlLoops() 801fe08952SJames Feist { 811fe08952SJames Feist static SensorManager mgmr; 825301aae3SJohnathan Mantey static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; 835301aae3SJohnathan Mantey static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; 84e620656cSPatrick Venture 855301aae3SJohnathan Mantey for (const auto timer : timers) 865301aae3SJohnathan Mantey { 875301aae3SJohnathan Mantey timer->cancel(); 885301aae3SJohnathan Mantey } 891fe08952SJames Feist timers.clear(); 90b228cea2SHao Jiang 91b228cea2SHao Jiang if (zones.size() > 0 && zones.begin()->second.use_count() > 1) 92b228cea2SHao Jiang { 93b228cea2SHao Jiang throw std::runtime_error("wait for count back to 1"); 94b228cea2SHao Jiang } 955301aae3SJohnathan Mantey zones.clear(); 964cb7c058SPatrick Venture 9718b1311eSPatrick Venture const std::string& path = 9818b1311eSPatrick Venture (configPath.length() > 0) ? configPath : jsonConfigurationPath; 99e620656cSPatrick Venture 1007f9d690dSPatrick Venture if (std::filesystem::exists(path)) 1017f9d690dSPatrick Venture { 102e620656cSPatrick Venture /* 1037f9d690dSPatrick Venture * When building the sensors, if any of the dbus passive ones aren't on 1047f9d690dSPatrick Venture * the bus, it'll fail immediately. 105e620656cSPatrick Venture */ 106e620656cSPatrick Venture try 107e620656cSPatrick Venture { 10818b1311eSPatrick Venture auto jsonData = parseValidateJson(path); 1094cb7c058SPatrick Venture sensorConfig = buildSensorsFromJson(jsonData); 1107f9d690dSPatrick Venture std::tie(zoneConfig, zoneDetailsConfig) = 1117f9d690dSPatrick Venture buildPIDsFromJson(jsonData); 112e620656cSPatrick Venture } 113e620656cSPatrick Venture catch (const std::exception& e) 114e620656cSPatrick Venture { 115e620656cSPatrick Venture std::cerr << "Failed during building: " << e.what() << "\n"; 116e620656cSPatrick Venture exit(EXIT_FAILURE); /* fatal error. */ 117e620656cSPatrick Venture } 1187f9d690dSPatrick Venture } 1197f9d690dSPatrick Venture else 1207f9d690dSPatrick Venture { 1217f9d690dSPatrick Venture static boost::asio::steady_timer reloadTimer(io); 1227382318fSPatrick Venture if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, 1237382318fSPatrick Venture zoneConfig, zoneDetailsConfig)) 1247f9d690dSPatrick Venture { 1257f9d690dSPatrick Venture return; // configuration not ready 1267f9d690dSPatrick Venture } 1277f9d690dSPatrick Venture } 1284cb7c058SPatrick Venture 1291fe08952SJames Feist mgmr = buildSensors(sensorConfig, passiveBus, hostBus); 1301fe08952SJames Feist zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 131e620656cSPatrick Venture 132e620656cSPatrick Venture if (0 == zones.size()) 133e620656cSPatrick Venture { 134e620656cSPatrick Venture std::cerr << "No zones defined, exiting.\n"; 1351fe08952SJames Feist std::exit(EXIT_FAILURE); 136e620656cSPatrick Venture } 137e620656cSPatrick Venture 1381fe08952SJames Feist for (const auto& i : zones) 1391fe08952SJames Feist { 1405301aae3SJohnathan Mantey std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( 1415301aae3SJohnathan Mantey std::make_shared<boost::asio::steady_timer>(io)); 1421fe08952SJames Feist std::cerr << "pushing zone " << i.first << "\n"; 1435301aae3SJohnathan Mantey pidControlLoop(i.second, timer); 1441fe08952SJames Feist } 1451fe08952SJames Feist } 1461fe08952SJames Feist 147b228cea2SHao Jiang void tryRestartControlLoops(bool first) 148298a95cbSYong Li { 149b228cea2SHao Jiang static int count = 0; 150b228cea2SHao Jiang static const auto initialStartTime = std::chrono::milliseconds(10); 151b228cea2SHao Jiang static const auto delayTeime = std::chrono::seconds(10); 152b228cea2SHao Jiang static boost::asio::steady_timer timer(io); 153b228cea2SHao Jiang // try to start a control loop while the loop has been scheduled. 154b228cea2SHao Jiang if (first && count != 0) 155298a95cbSYong Li { 156b228cea2SHao Jiang std::cerr 157b228cea2SHao Jiang << "ControlLoops has been scheduled, refresh the loop count\n"; 158b228cea2SHao Jiang count = 1; 159b228cea2SHao Jiang return; 160b228cea2SHao Jiang } 161b228cea2SHao Jiang // first time of trying to restart the control loop, delay for a small 162b228cea2SHao Jiang // amount of time. 163b228cea2SHao Jiang else if (first) 164b228cea2SHao Jiang { 165b228cea2SHao Jiang timer.expires_after(initialStartTime); 166b228cea2SHao Jiang } 167b228cea2SHao Jiang // re-try control loop, set up a delay. 168b228cea2SHao Jiang else 169b228cea2SHao Jiang { 170b228cea2SHao Jiang timer.expires_after(delayTeime); 171b228cea2SHao Jiang } 172b228cea2SHao Jiang count++; 173*232ffe4eSHao Jiang timer.async_wait([](const boost::system::error_code& error) { 174b228cea2SHao Jiang if (error == boost::asio::error::operation_aborted) 175b228cea2SHao Jiang { 176b228cea2SHao Jiang return; 177b228cea2SHao Jiang } 178*232ffe4eSHao Jiang 179*232ffe4eSHao Jiang // for the last loop, don't elminate the failure of restartControlLoops. 180*232ffe4eSHao Jiang if (count >= 5) 181*232ffe4eSHao Jiang { 182*232ffe4eSHao Jiang restartControlLoops(); 183*232ffe4eSHao Jiang // reset count after succesful restartControlLoops() 184*232ffe4eSHao Jiang count = 0; 185*232ffe4eSHao Jiang return; 186*232ffe4eSHao Jiang } 187*232ffe4eSHao Jiang 188*232ffe4eSHao Jiang // retry when restartControlLoops() has some failure. 189298a95cbSYong Li try 190298a95cbSYong Li { 191298a95cbSYong Li restartControlLoops(); 192b228cea2SHao Jiang // reset count after succesful restartControlLoops() 193b228cea2SHao Jiang count = 0; 194298a95cbSYong Li } 195298a95cbSYong Li catch (const std::exception& e) 196298a95cbSYong Li { 197298a95cbSYong Li std::cerr << count 198298a95cbSYong Li << " Failed during restartControlLoops, try again: " 199298a95cbSYong Li << e.what() << "\n"; 200b228cea2SHao Jiang tryRestartControlLoops(false); 201298a95cbSYong Li } 202b228cea2SHao Jiang }); 203298a95cbSYong Li 204298a95cbSYong Li return; 205298a95cbSYong Li } 206298a95cbSYong Li 207a076487aSPatrick Venture } // namespace pid_control 208a076487aSPatrick Venture 2091fe08952SJames Feist int main(int argc, char* argv[]) 2101fe08952SJames Feist { 2111fe08952SJames Feist loggingPath = ""; 2121fe08952SJames Feist loggingEnabled = false; 2131fe08952SJames Feist tuningEnabled = false; 2141fe08952SJames Feist 2151fe08952SJames Feist CLI::App app{"OpenBMC Fan Control Daemon"}; 2161fe08952SJames Feist 2171fe08952SJames Feist app.add_option("-c,--conf", configPath, 2181fe08952SJames Feist "Optional parameter to specify configuration at run-time") 2191fe08952SJames Feist ->check(CLI::ExistingFile); 2201fe08952SJames Feist app.add_option("-l,--log", loggingPath, 2211fe08952SJames Feist "Optional parameter to specify logging folder") 2221fe08952SJames Feist ->check(CLI::ExistingDirectory); 2231fe08952SJames Feist app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); 2241fe08952SJames Feist 2251fe08952SJames Feist CLI11_PARSE(app, argc, argv); 2261fe08952SJames Feist 227811f31d6SJosh Lehan static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; 228811f31d6SJosh Lehan static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; 229811f31d6SJosh Lehan 230811f31d6SJosh Lehan // If this file exists, enable logging at runtime 231811f31d6SJosh Lehan std::ifstream fsLogging(loggingEnablePath); 232811f31d6SJosh Lehan if (fsLogging) 233811f31d6SJosh Lehan { 234811f31d6SJosh Lehan // Unless file contents are a valid directory path, use system default 235811f31d6SJosh Lehan std::getline(fsLogging, loggingPath); 236811f31d6SJosh Lehan if (!(std::filesystem::exists(loggingPath))) 237811f31d6SJosh Lehan { 238811f31d6SJosh Lehan loggingPath = std::filesystem::temp_directory_path(); 239811f31d6SJosh Lehan } 240811f31d6SJosh Lehan fsLogging.close(); 241811f31d6SJosh Lehan 242811f31d6SJosh Lehan loggingEnabled = true; 243811f31d6SJosh Lehan std::cerr << "Logging enabled: " << loggingPath << "\n"; 244811f31d6SJosh Lehan } 245811f31d6SJosh Lehan 246811f31d6SJosh Lehan // If this file exists, enable tuning at runtime 247811f31d6SJosh Lehan if (std::filesystem::exists(tuningEnablePath)) 248811f31d6SJosh Lehan { 249811f31d6SJosh Lehan tuningEnabled = true; 250811f31d6SJosh Lehan std::cerr << "Tuning enabled\n"; 251811f31d6SJosh Lehan } 252d4695590SKun Yi 2531fe08952SJames Feist static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 2541fe08952SJames Feist // Create a manager for the ModeBus because we own it. 2551fe08952SJames Feist sdbusplus::server::manager::manager( 2561fe08952SJames Feist static_cast<sdbusplus::bus::bus&>(modeControlBus), modeRoot); 2571fe08952SJames Feist hostBus.request_name("xyz.openbmc_project.Hwmon.external"); 2581fe08952SJames Feist modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); 2591fe08952SJames Feist 260e620656cSPatrick Venture /* 261e620656cSPatrick Venture * All sensors are managed by one manager, but each zone has a pointer to 262e620656cSPatrick Venture * it. 263e620656cSPatrick Venture */ 264e620656cSPatrick Venture 265a076487aSPatrick Venture pid_control::tryRestartControlLoops(); 266e620656cSPatrick Venture 267ce6a3f36SJames Feist io.run(); 2681fe08952SJames Feist return 0; 269e620656cSPatrick Venture } 270