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