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