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> 35*e2ec69adSPotin Lai #include <boost/asio/signal_set.hpp> 36ce6a3f36SJames Feist #include <boost/asio/steady_timer.hpp> 37a83a3eccSPatrick Venture #include <sdbusplus/asio/connection.hpp> 38a83a3eccSPatrick Venture #include <sdbusplus/bus.hpp> 39cb4c1a27SBruce Lee #include <sdbusplus/server/manager.hpp> 40a83a3eccSPatrick Venture 41da4a5dd1SPatrick Venture #include <chrono> 427f9d690dSPatrick Venture #include <filesystem> 43da4a5dd1SPatrick Venture #include <iostream> 44ce6a3f36SJames Feist #include <list> 45da4a5dd1SPatrick Venture #include <map> 46da4a5dd1SPatrick Venture #include <memory> 47da4a5dd1SPatrick Venture #include <thread> 48da4a5dd1SPatrick Venture #include <unordered_map> 49ba8ffa73SPatrick Venture #include <utility> 50da4a5dd1SPatrick Venture #include <vector> 51e620656cSPatrick Venture 52a076487aSPatrick Venture namespace pid_control 53a076487aSPatrick Venture { 54a076487aSPatrick Venture 551fe08952SJames Feist /* The configuration converted sensor list. */ 561df9e879SPatrick Venture std::map<std::string, conf::SensorConfig> sensorConfig = {}; 571fe08952SJames Feist /* The configuration converted PID list. */ 58f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {}; 591fe08952SJames Feist /* The configuration converted Zone configuration. */ 601df9e879SPatrick Venture std::map<int64_t, conf::ZoneConfig> zoneDetailsConfig = {}; 61e620656cSPatrick Venture 62a076487aSPatrick Venture } // namespace pid_control 63a076487aSPatrick Venture 64c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */ 65c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 66e620656cSPatrick Venture std::string configPath = ""; 67e620656cSPatrick Venture 681fe08952SJames Feist /* async io context for operation */ 691fe08952SJames Feist boost::asio::io_context io; 70*e2ec69adSPotin Lai /* async signal_set for signal handling */ 71*e2ec69adSPotin Lai boost::asio::signal_set signals(io, SIGHUP); 72e620656cSPatrick Venture 731fe08952SJames Feist /* buses for system control */ 741fe08952SJames Feist static sdbusplus::asio::connection modeControlBus(io); 751fe08952SJames Feist static sdbusplus::asio::connection 761fe08952SJames Feist hostBus(io, sdbusplus::bus::new_system().release()); 771fe08952SJames Feist static sdbusplus::asio::connection 781fe08952SJames Feist passiveBus(io, sdbusplus::bus::new_system().release()); 79de79ee05SPatrick Venture 80a076487aSPatrick Venture namespace pid_control 81a076487aSPatrick Venture { 82a076487aSPatrick Venture 831fe08952SJames Feist void restartControlLoops() 841fe08952SJames Feist { 851fe08952SJames Feist static SensorManager mgmr; 865301aae3SJohnathan Mantey static std::unordered_map<int64_t, std::shared_ptr<ZoneInterface>> zones; 875301aae3SJohnathan Mantey static std::vector<std::shared_ptr<boost::asio::steady_timer>> timers; 88b6a0b89eSHao Jiang static bool isCanceling = false; 89e620656cSPatrick Venture 9018d5bb18SWilliam A. Kennington III for (const auto& timer : timers) 915301aae3SJohnathan Mantey { 925301aae3SJohnathan Mantey timer->cancel(); 935301aae3SJohnathan Mantey } 94b6a0b89eSHao Jiang isCanceling = true; 951fe08952SJames Feist timers.clear(); 96b228cea2SHao Jiang 97b228cea2SHao Jiang if (zones.size() > 0 && zones.begin()->second.use_count() > 1) 98b228cea2SHao Jiang { 99b228cea2SHao Jiang throw std::runtime_error("wait for count back to 1"); 100b228cea2SHao Jiang } 1015301aae3SJohnathan Mantey zones.clear(); 102b6a0b89eSHao Jiang isCanceling = false; 1034cb7c058SPatrick Venture 10418b1311eSPatrick Venture const std::string& path = 10518b1311eSPatrick Venture (configPath.length() > 0) ? configPath : jsonConfigurationPath; 106e620656cSPatrick Venture 1077f9d690dSPatrick Venture if (std::filesystem::exists(path)) 1087f9d690dSPatrick Venture { 109e620656cSPatrick Venture /* 1107f9d690dSPatrick Venture * When building the sensors, if any of the dbus passive ones aren't on 1117f9d690dSPatrick Venture * the bus, it'll fail immediately. 112e620656cSPatrick Venture */ 113e620656cSPatrick Venture try 114e620656cSPatrick Venture { 11518b1311eSPatrick Venture auto jsonData = parseValidateJson(path); 1164cb7c058SPatrick Venture sensorConfig = buildSensorsFromJson(jsonData); 1177f9d690dSPatrick Venture std::tie(zoneConfig, zoneDetailsConfig) = 1187f9d690dSPatrick Venture buildPIDsFromJson(jsonData); 119e620656cSPatrick Venture } 120e620656cSPatrick Venture catch (const std::exception& e) 121e620656cSPatrick Venture { 122e620656cSPatrick Venture std::cerr << "Failed during building: " << e.what() << "\n"; 123e620656cSPatrick Venture exit(EXIT_FAILURE); /* fatal error. */ 124e620656cSPatrick Venture } 1257f9d690dSPatrick Venture } 1267f9d690dSPatrick Venture else 1277f9d690dSPatrick Venture { 1287f9d690dSPatrick Venture static boost::asio::steady_timer reloadTimer(io); 1297382318fSPatrick Venture if (!dbus_configuration::init(modeControlBus, reloadTimer, sensorConfig, 1307382318fSPatrick Venture zoneConfig, zoneDetailsConfig)) 1317f9d690dSPatrick Venture { 1327f9d690dSPatrick Venture return; // configuration not ready 1337f9d690dSPatrick Venture } 1347f9d690dSPatrick Venture } 1354cb7c058SPatrick Venture 1361fe08952SJames Feist mgmr = buildSensors(sensorConfig, passiveBus, hostBus); 1371fe08952SJames Feist zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 138e620656cSPatrick Venture 139e620656cSPatrick Venture if (0 == zones.size()) 140e620656cSPatrick Venture { 141e620656cSPatrick Venture std::cerr << "No zones defined, exiting.\n"; 1421fe08952SJames Feist std::exit(EXIT_FAILURE); 143e620656cSPatrick Venture } 144e620656cSPatrick Venture 1451fe08952SJames Feist for (const auto& i : zones) 1461fe08952SJames Feist { 1475301aae3SJohnathan Mantey std::shared_ptr<boost::asio::steady_timer> timer = timers.emplace_back( 1485301aae3SJohnathan Mantey std::make_shared<boost::asio::steady_timer>(io)); 1491fe08952SJames Feist std::cerr << "pushing zone " << i.first << "\n"; 150b6a0b89eSHao Jiang pidControlLoop(i.second, timer, &isCanceling); 1511fe08952SJames Feist } 1521fe08952SJames Feist } 1531fe08952SJames Feist 154b228cea2SHao Jiang void tryRestartControlLoops(bool first) 155298a95cbSYong Li { 156b228cea2SHao Jiang static int count = 0; 157d11a732aSHao Jiang static const auto delayTime = std::chrono::seconds(10); 158b228cea2SHao Jiang static boost::asio::steady_timer timer(io); 159b228cea2SHao Jiang // try to start a control loop while the loop has been scheduled. 160b228cea2SHao Jiang if (first && count != 0) 161298a95cbSYong Li { 162b228cea2SHao Jiang std::cerr 163b228cea2SHao Jiang << "ControlLoops has been scheduled, refresh the loop count\n"; 164b228cea2SHao Jiang count = 1; 165b228cea2SHao Jiang return; 166b228cea2SHao Jiang } 167d11a732aSHao Jiang 168d11a732aSHao Jiang auto restartLbd = [](const boost::system::error_code& error) { 169b228cea2SHao Jiang if (error == boost::asio::error::operation_aborted) 170b228cea2SHao Jiang { 171b228cea2SHao Jiang return; 172b228cea2SHao Jiang } 173232ffe4eSHao Jiang 174232ffe4eSHao Jiang // for the last loop, don't elminate the failure of restartControlLoops. 175232ffe4eSHao Jiang if (count >= 5) 176232ffe4eSHao Jiang { 177232ffe4eSHao Jiang restartControlLoops(); 178232ffe4eSHao Jiang // reset count after succesful restartControlLoops() 179232ffe4eSHao Jiang count = 0; 180232ffe4eSHao Jiang return; 181232ffe4eSHao Jiang } 182232ffe4eSHao Jiang 183232ffe4eSHao Jiang // retry when restartControlLoops() has some failure. 184298a95cbSYong Li try 185298a95cbSYong Li { 186298a95cbSYong Li restartControlLoops(); 187b228cea2SHao Jiang // reset count after succesful restartControlLoops() 188b228cea2SHao Jiang count = 0; 189298a95cbSYong Li } 190298a95cbSYong Li catch (const std::exception& e) 191298a95cbSYong Li { 192298a95cbSYong Li std::cerr << count 193298a95cbSYong Li << " Failed during restartControlLoops, try again: " 194298a95cbSYong Li << e.what() << "\n"; 195b228cea2SHao Jiang tryRestartControlLoops(false); 196298a95cbSYong Li } 197d11a732aSHao Jiang }; 198d11a732aSHao Jiang count++; 199d11a732aSHao Jiang // first time of trying to restart the control loop without a delay 200d11a732aSHao Jiang if (first) 201d11a732aSHao Jiang { 202d11a732aSHao Jiang boost::asio::post(io, 203d11a732aSHao Jiang std::bind(restartLbd, boost::system::error_code())); 204d11a732aSHao Jiang } 205d11a732aSHao Jiang // re-try control loop, set up a delay. 206d11a732aSHao Jiang else 207d11a732aSHao Jiang { 208d11a732aSHao Jiang timer.expires_after(delayTime); 209d11a732aSHao Jiang timer.async_wait(restartLbd); 210d11a732aSHao Jiang } 211298a95cbSYong Li 212298a95cbSYong Li return; 213298a95cbSYong Li } 214298a95cbSYong Li 215a076487aSPatrick Venture } // namespace pid_control 216a076487aSPatrick Venture 217*e2ec69adSPotin Lai void sighupHandler(const boost::system::error_code& error, int signal_number) 218*e2ec69adSPotin Lai { 219*e2ec69adSPotin Lai static boost::asio::steady_timer timer(io); 220*e2ec69adSPotin Lai 221*e2ec69adSPotin Lai if (error) 222*e2ec69adSPotin Lai { 223*e2ec69adSPotin Lai std::cout << "Signal " << signal_number 224*e2ec69adSPotin Lai << " handler error: " << error.message() << "\n"; 225*e2ec69adSPotin Lai return; 226*e2ec69adSPotin Lai } 227*e2ec69adSPotin Lai 228*e2ec69adSPotin Lai timer.expires_after(std::chrono::seconds(1)); 229*e2ec69adSPotin Lai timer.async_wait([](const boost::system::error_code ec) { 230*e2ec69adSPotin Lai if (ec) 231*e2ec69adSPotin Lai { 232*e2ec69adSPotin Lai std::cout << "Signal timer error: " << ec.message() << "\n"; 233*e2ec69adSPotin Lai return; 234*e2ec69adSPotin Lai } 235*e2ec69adSPotin Lai 236*e2ec69adSPotin Lai std::cout << "reloading configuration\n"; 237*e2ec69adSPotin Lai pid_control::tryRestartControlLoops(); 238*e2ec69adSPotin Lai }); 239*e2ec69adSPotin Lai signals.async_wait(sighupHandler); 240*e2ec69adSPotin Lai } 241*e2ec69adSPotin Lai 2421fe08952SJames Feist int main(int argc, char* argv[]) 2431fe08952SJames Feist { 2441fe08952SJames Feist loggingPath = ""; 2451fe08952SJames Feist loggingEnabled = false; 2461fe08952SJames Feist tuningEnabled = false; 2471fe08952SJames Feist 2481fe08952SJames Feist CLI::App app{"OpenBMC Fan Control Daemon"}; 2491fe08952SJames Feist 2501fe08952SJames Feist app.add_option("-c,--conf", configPath, 2511fe08952SJames Feist "Optional parameter to specify configuration at run-time") 2521fe08952SJames Feist ->check(CLI::ExistingFile); 2531fe08952SJames Feist app.add_option("-l,--log", loggingPath, 2541fe08952SJames Feist "Optional parameter to specify logging folder") 2551fe08952SJames Feist ->check(CLI::ExistingDirectory); 2561fe08952SJames Feist app.add_flag("-t,--tuning", tuningEnabled, "Enable or disable tuning"); 2571fe08952SJames Feist 2581fe08952SJames Feist CLI11_PARSE(app, argc, argv); 2591fe08952SJames Feist 260811f31d6SJosh Lehan static constexpr auto loggingEnablePath = "/etc/thermal.d/logging"; 261811f31d6SJosh Lehan static constexpr auto tuningEnablePath = "/etc/thermal.d/tuning"; 262811f31d6SJosh Lehan 263f54b260bSJosh Lehan // Set up default logging path, preferring command line if it was given 264f54b260bSJosh Lehan std::string defLoggingPath(loggingPath); 265f54b260bSJosh Lehan if (defLoggingPath.empty()) 266f54b260bSJosh Lehan { 267f54b260bSJosh Lehan defLoggingPath = std::filesystem::temp_directory_path(); 268f54b260bSJosh Lehan } 269f54b260bSJosh Lehan else 270f54b260bSJosh Lehan { 271f54b260bSJosh Lehan // Enable logging, if user explicitly gave path on command line 272f54b260bSJosh Lehan loggingEnabled = true; 273f54b260bSJosh Lehan } 274f54b260bSJosh Lehan 275811f31d6SJosh Lehan // If this file exists, enable logging at runtime 276811f31d6SJosh Lehan std::ifstream fsLogging(loggingEnablePath); 277811f31d6SJosh Lehan if (fsLogging) 278811f31d6SJosh Lehan { 279f54b260bSJosh Lehan // The first line of file might be a valid directory path 280811f31d6SJosh Lehan std::getline(fsLogging, loggingPath); 281811f31d6SJosh Lehan fsLogging.close(); 282811f31d6SJosh Lehan 283f54b260bSJosh Lehan // If so, use it, otherwise use default logging path instead 284f54b260bSJosh Lehan if (!(std::filesystem::exists(loggingPath))) 285f54b260bSJosh Lehan { 286f54b260bSJosh Lehan loggingPath = defLoggingPath; 287f54b260bSJosh Lehan } 288f54b260bSJosh Lehan 289811f31d6SJosh Lehan loggingEnabled = true; 290f54b260bSJosh Lehan } 291f54b260bSJosh Lehan 292f54b260bSJosh Lehan if (loggingEnabled) 293f54b260bSJosh Lehan { 294811f31d6SJosh Lehan std::cerr << "Logging enabled: " << loggingPath << "\n"; 295811f31d6SJosh Lehan } 296811f31d6SJosh Lehan 297811f31d6SJosh Lehan // If this file exists, enable tuning at runtime 298811f31d6SJosh Lehan if (std::filesystem::exists(tuningEnablePath)) 299811f31d6SJosh Lehan { 300811f31d6SJosh Lehan tuningEnabled = true; 301f54b260bSJosh Lehan } 302f54b260bSJosh Lehan 303f54b260bSJosh Lehan // This can also be enabled from the command line 304f54b260bSJosh Lehan if (tuningEnabled) 305f54b260bSJosh Lehan { 306811f31d6SJosh Lehan std::cerr << "Tuning enabled\n"; 307811f31d6SJosh Lehan } 308d4695590SKun Yi 3091fe08952SJames Feist static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; 3101fe08952SJames Feist // Create a manager for the ModeBus because we own it. 311b228bc30SPatrick Williams sdbusplus::server::manager_t(static_cast<sdbusplus::bus_t&>(modeControlBus), 312b228bc30SPatrick Williams modeRoot); 3131fe08952SJames Feist hostBus.request_name("xyz.openbmc_project.Hwmon.external"); 3141fe08952SJames Feist modeControlBus.request_name("xyz.openbmc_project.State.FanCtrl"); 315b228bc30SPatrick Williams sdbusplus::server::manager_t objManager(modeControlBus, modeRoot); 3161fe08952SJames Feist 317*e2ec69adSPotin Lai // Enable SIGHUP handling to reload JSON config 318*e2ec69adSPotin Lai signals.async_wait(sighupHandler); 319*e2ec69adSPotin Lai 320e620656cSPatrick Venture /* 321e620656cSPatrick Venture * All sensors are managed by one manager, but each zone has a pointer to 322e620656cSPatrick Venture * it. 323e620656cSPatrick Venture */ 324e620656cSPatrick Venture 325a076487aSPatrick Venture pid_control::tryRestartControlLoops(); 326e620656cSPatrick Venture 327ce6a3f36SJames Feist io.run(); 3281fe08952SJames Feist return 0; 329e620656cSPatrick Venture } 330