xref: /openbmc/phosphor-pid-control/main.cpp (revision c32e3fc5d920967ef7fd58a7120096ba2224d41b)
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"
21e620656cSPatrick Venture #include "interfaces.hpp"
225c7cc545SPatrick Venture #include "pid/builder.hpp"
23ba8ffa73SPatrick Venture #include "pid/buildjson.hpp"
24da4a5dd1SPatrick Venture #include "pid/pidthread.hpp"
25*c32e3fc5SPatrick Venture #include "pid/tuning.hpp"
26e620656cSPatrick Venture #include "pid/zone.hpp"
275e929093SPatrick Venture #include "sensors/builder.hpp"
28ba8ffa73SPatrick Venture #include "sensors/buildjson.hpp"
29e620656cSPatrick Venture #include "sensors/manager.hpp"
30e620656cSPatrick Venture #include "threads/busthread.hpp"
31da4a5dd1SPatrick Venture #include "util.hpp"
32e620656cSPatrick Venture 
33da4a5dd1SPatrick Venture #include <getopt.h>
34da4a5dd1SPatrick Venture 
35da4a5dd1SPatrick Venture #include <chrono>
36da4a5dd1SPatrick Venture #include <iostream>
37da4a5dd1SPatrick Venture #include <map>
38da4a5dd1SPatrick Venture #include <memory>
39da4a5dd1SPatrick Venture #include <sdbusplus/bus.hpp>
40da4a5dd1SPatrick Venture #include <thread>
41da4a5dd1SPatrick Venture #include <unordered_map>
42ba8ffa73SPatrick Venture #include <utility>
43da4a5dd1SPatrick Venture #include <vector>
44e620656cSPatrick Venture 
459f044415SPatrick Venture #if CONFIGURE_DBUS
469f044415SPatrick Venture #include "dbus/dbusconfiguration.hpp"
479f044415SPatrick Venture #endif
489f044415SPatrick Venture 
49e620656cSPatrick Venture /* The YAML converted sensor list. */
50f81f2886SJames Feist std::map<std::string, struct conf::SensorConfig> sensorConfig = {};
51e620656cSPatrick Venture /* The YAML converted PID list. */
52f81f2886SJames Feist std::map<int64_t, conf::PIDConf> zoneConfig = {};
53e620656cSPatrick Venture /* The YAML converted Zone configuration. */
54f81f2886SJames Feist std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {};
55e620656cSPatrick Venture 
56c19f5d4dSPatrick Venture /** the swampd daemon will check for the existence of this file. */
57c19f5d4dSPatrick Venture constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json";
58c19f5d4dSPatrick Venture 
59e620656cSPatrick Venture int main(int argc, char* argv[])
60e620656cSPatrick Venture {
61e620656cSPatrick Venture     int rc = 0;
62e620656cSPatrick Venture     std::string configPath = "";
63e620656cSPatrick Venture 
64e620656cSPatrick Venture     while (1)
65e620656cSPatrick Venture     {
66da4a5dd1SPatrick Venture         // clang-format off
67da4a5dd1SPatrick Venture         static struct option long_options[] = {
68e620656cSPatrick Venture             {"conf", required_argument, 0, 'c'},
69*c32e3fc5SPatrick Venture             {"tuning", required_argument, 0, 't'},
70e620656cSPatrick Venture             {0, 0, 0, 0}
71e620656cSPatrick Venture         };
72da4a5dd1SPatrick Venture         // clang-format on
73e620656cSPatrick Venture 
74e620656cSPatrick Venture         int option_index = 0;
75*c32e3fc5SPatrick Venture         int c = getopt_long(argc, argv, "t:c:", long_options, &option_index);
76e620656cSPatrick Venture 
77e620656cSPatrick Venture         if (c == -1)
78e620656cSPatrick Venture         {
79e620656cSPatrick Venture             break;
80e620656cSPatrick Venture         }
81e620656cSPatrick Venture 
82e620656cSPatrick Venture         switch (c)
83e620656cSPatrick Venture         {
84e620656cSPatrick Venture             case 'c':
85e620656cSPatrick Venture                 configPath = std::string{optarg};
86e620656cSPatrick Venture                 break;
87*c32e3fc5SPatrick Venture             case 't':
88*c32e3fc5SPatrick Venture                 tuningLoggingEnabled = true;
89*c32e3fc5SPatrick Venture                 tuningLoggingPath = std::string{optarg};
90*c32e3fc5SPatrick Venture                 break;
91e620656cSPatrick Venture             default:
92e620656cSPatrick Venture                 /* skip garbage. */
93e620656cSPatrick Venture                 continue;
94e620656cSPatrick Venture         }
95e620656cSPatrick Venture     }
96e620656cSPatrick Venture 
979fa90c19SJames Feist     auto modeControlBus = sdbusplus::bus::new_system();
984cb7c058SPatrick Venture     static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl";
994cb7c058SPatrick Venture     // Create a manager for the ModeBus because we own it.
1004cb7c058SPatrick Venture     sdbusplus::server::manager::manager(modeControlBus, modeRoot);
1014cb7c058SPatrick Venture 
1029f044415SPatrick Venture #if CONFIGURE_DBUS
1037136a5aeSJames Feist     {
104c179d406SPatrick Venture         dbus_configuration::init(modeControlBus);
1057136a5aeSJames Feist     }
10618b1311eSPatrick Venture #else
10718b1311eSPatrick Venture     const std::string& path =
10818b1311eSPatrick Venture         (configPath.length() > 0) ? configPath : jsonConfigurationPath;
109e620656cSPatrick Venture 
110e620656cSPatrick Venture     /*
111e620656cSPatrick Venture      * When building the sensors, if any of the dbus passive ones aren't on the
112e620656cSPatrick Venture      * bus, it'll fail immediately.
113e620656cSPatrick Venture      */
114e620656cSPatrick Venture     try
115e620656cSPatrick Venture     {
11618b1311eSPatrick Venture         auto jsonData = parseValidateJson(path);
1174cb7c058SPatrick Venture         sensorConfig = buildSensorsFromJson(jsonData);
11818b1311eSPatrick Venture         std::tie(zoneConfig, zoneDetailsConfig) = 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     }
12518b1311eSPatrick Venture #endif
1264cb7c058SPatrick Venture 
1274cb7c058SPatrick Venture     SensorManager mgmr = buildSensors(sensorConfig);
1284cb7c058SPatrick Venture     std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones =
1294cb7c058SPatrick Venture         buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus);
130e620656cSPatrick Venture 
131e620656cSPatrick Venture     if (0 == zones.size())
132e620656cSPatrick Venture     {
133e620656cSPatrick Venture         std::cerr << "No zones defined, exiting.\n";
134e620656cSPatrick Venture         return rc;
135e620656cSPatrick Venture     }
136e620656cSPatrick Venture 
137e620656cSPatrick Venture     /*
138e620656cSPatrick Venture      * All sensors are managed by one manager, but each zone has a pointer to
139e620656cSPatrick Venture      * it.
140e620656cSPatrick Venture      */
141e620656cSPatrick Venture 
142c179d406SPatrick Venture     auto& hostSensorBus = mgmr.getHostBus();
143c179d406SPatrick Venture     auto& passiveListeningBus = mgmr.getPassiveBus();
144e620656cSPatrick Venture 
145e620656cSPatrick Venture     std::cerr << "Starting threads\n";
146e620656cSPatrick Venture 
147e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any passive sensors. */
148c179d406SPatrick Venture     struct ThreadParams p = {std::ref(passiveListeningBus), ""};
149c179d406SPatrick Venture     std::thread l(busThread, std::ref(p));
150e620656cSPatrick Venture 
151e620656cSPatrick Venture     /* TODO(venture): Ask SensorManager if we have any host sensors. */
152e620656cSPatrick Venture     static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external";
153c179d406SPatrick Venture     struct ThreadParams e = {std::ref(hostSensorBus), hostBus};
154c179d406SPatrick Venture     std::thread te(busThread, std::ref(e));
155e620656cSPatrick Venture 
156e620656cSPatrick Venture     static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl";
157c179d406SPatrick Venture     struct ThreadParams m = {std::ref(modeControlBus), modeBus};
158c179d406SPatrick Venture     std::thread tm(busThread, std::ref(m));
159e620656cSPatrick Venture 
160e620656cSPatrick Venture     std::vector<std::thread> zoneThreads;
161e620656cSPatrick Venture 
162e620656cSPatrick Venture     /* TODO(venture): This was designed to have one thread per zone, but really
163e620656cSPatrick Venture      * it could have one thread for all the zones and iterate through each
164e620656cSPatrick Venture      * sequentially as it goes -- and it'd probably be fast enough to do that,
165e620656cSPatrick Venture      * however, a system isn't likely going to have more than a couple zones.
166e620656cSPatrick Venture      * If it only has a couple zones, then this is fine.
167e620656cSPatrick Venture      */
1684a2dc4d8SPatrick Venture     for (const auto& i : zones)
169e620656cSPatrick Venture     {
170e620656cSPatrick Venture         std::cerr << "pushing zone" << std::endl;
1717af157b1SPatrick Venture         zoneThreads.push_back(std::thread(pidControlThread, i.second.get()));
172e620656cSPatrick Venture     }
173e620656cSPatrick Venture 
174e620656cSPatrick Venture     l.join();
175e620656cSPatrick Venture     te.join();
176e620656cSPatrick Venture     tm.join();
177e620656cSPatrick Venture     for (auto& t : zoneThreads)
178e620656cSPatrick Venture     {
179e620656cSPatrick Venture         t.join();
180e620656cSPatrick Venture     }
181e620656cSPatrick Venture 
182e620656cSPatrick Venture     return rc;
183e620656cSPatrick Venture }
184