xref: /openbmc/phosphor-pid-control/main.cpp (revision e6e6f626)
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 "interfaces.hpp"
22 #include "pid/builder.hpp"
23 #include "pid/buildjson.hpp"
24 #include "pid/pidthread.hpp"
25 #include "pid/tuning.hpp"
26 #include "pid/zone.hpp"
27 #include "sensors/builder.hpp"
28 #include "sensors/buildjson.hpp"
29 #include "sensors/manager.hpp"
30 #include "threads/busthread.hpp"
31 #include "util.hpp"
32 
33 #include <CLI/CLI.hpp>
34 #include <chrono>
35 #include <iostream>
36 #include <map>
37 #include <memory>
38 #include <sdbusplus/bus.hpp>
39 #include <thread>
40 #include <unordered_map>
41 #include <utility>
42 #include <vector>
43 
44 #if CONFIGURE_DBUS
45 #include "dbus/dbusconfiguration.hpp"
46 #endif
47 
48 /* The YAML converted sensor list. */
49 std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
50 /* The YAML converted PID list. */
51 std::map<int64_t, conf::PIDConf> zoneConfig = {};
52 /* The YAML converted Zone configuration. */
53 std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
54 
55 /** the swampd daemon will check for the existence of this file. */
56 constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
57 
58 int main(int argc, char* argv[])
59 {
60     int rc = 0;
61     std::string configPath = "";
62     tuningLoggingPath = "";
63 
64     CLI::App app{"OpenBMC Fan Control Daemon"};
65 
66     app.add_option("-c,--conf", configPath,
67                    "Optional parameter to specify configuration at run-time")
68         ->check(CLI::ExistingFile);
69     app.add_option("-t,--tuning", tuningLoggingPath,
70                    "Optional parameter to specify tuning logging path, and "
71                    "enable tuning")
72         ->check(CLI::ExistingFile);
73 
74     CLI11_PARSE(app, argc, argv);
75 
76     tuningLoggingEnabled = (tuningLoggingPath.length() > 0);
77 
78     auto modeControlBus = sdbusplus::bus::new_system();
79     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
80     // Create a manager for the ModeBus because we own it.
81     sdbusplus::server::manager::manager(modeControlBus, modeRoot);
82 
83 #if CONFIGURE_DBUS
84     {
85         dbus_configuration::init(modeControlBus);
86     }
87 #else
88     const std::string& path =
89         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
90 
91     /*
92      * When building the sensors, if any of the dbus passive ones aren't on the
93      * bus, it'll fail immediately.
94      */
95     try
96     {
97         auto jsonData = parseValidateJson(path);
98         sensorConfig = buildSensorsFromJson(jsonData);
99         std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData);
100     }
101     catch (const std::exception& e)
102     {
103         std::cerr << "Failed during building: " << e.what() << "\n";
104         exit(EXIT_FAILURE); /* fatal error. */
105     }
106 #endif
107 
108     SensorManager mgmr = buildSensors(sensorConfig);
109     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
110         buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
111 
112     if (0 == zones.size())
113     {
114         std::cerr << "No zones defined, exiting.\n";
115         return rc;
116     }
117 
118     /*
119      * All sensors are managed by one manager, but each zone has a pointer to
120      * it.
121      */
122 
123     auto& hostSensorBus = mgmr.getHostBus();
124     auto& passiveListeningBus = mgmr.getPassiveBus();
125 
126     std::cerr << "Starting threads\n";
127 
128     /* TODO(venture): Ask SensorManager if we have any passive sensors. */
129     struct ThreadParams p = {std::ref(passiveListeningBus), ""};
130     std::thread l(busThread, std::ref(p));
131 
132     /* TODO(venture): Ask SensorManager if we have any host sensors. */
133     static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
134     struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
135     std::thread te(busThread, std::ref(e));
136 
137     static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
138     struct ThreadParams m = {std::ref(modeControlBus), modeBus};
139     std::thread tm(busThread, std::ref(m));
140 
141     std::vector<std::thread> zoneThreads;
142 
143     /* TODO(venture): This was designed to have one thread per zone, but really
144      * it could have one thread for all the zones and iterate through each
145      * sequentially as it goes -- and it'd probably be fast enough to do that,
146      * however, a system isn't likely going to have more than a couple zones.
147      * If it only has a couple zones, then this is fine.
148      */
149     for (const auto& i : zones)
150     {
151         std::cerr << "pushing zone" << std::endl;
152         zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
153     }
154 
155     l.join();
156     te.join();
157     tm.join();
158     for (auto& t : zoneThreads)
159     {
160         t.join();
161     }
162 
163     return rc;
164 }
165