Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 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 | |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 17 | #include "config.h" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 18 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 19 | #include "conf.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 20 | #include "interfaces.hpp" |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 21 | #include "pid/builder.hpp" |
| 22 | #include "pid/builderconfig.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 23 | #include "pid/pidthread.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 24 | #include "pid/zone.hpp" |
Patrick Venture | 5e92909 | 2018-06-08 10:55:23 -0700 | [diff] [blame] | 25 | #include "sensors/builder.hpp" |
| 26 | #include "sensors/builderconfig.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 27 | #include "sensors/manager.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 28 | #include "threads/busthread.hpp" |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 29 | #include "util.hpp" |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 30 | |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 31 | #include <getopt.h> |
| 32 | |
| 33 | #include <chrono> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 34 | #include <iostream> |
| 35 | #include <map> |
| 36 | #include <memory> |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 37 | #include <sdbusplus/bus.hpp> |
| 38 | #include <thread> |
| 39 | #include <unordered_map> |
| 40 | #include <vector> |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 41 | |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 42 | #if CONFIGURE_DBUS |
| 43 | #include "dbus/dbusconfiguration.hpp" |
| 44 | #endif |
| 45 | |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 46 | /* The YAML converted sensor list. */ |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 47 | extern std::map<std::string, struct SensorConfig> sensorConfig; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 48 | /* The YAML converted PID list. */ |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 49 | extern std::map<int64_t, PIDConf> zoneConfig; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 50 | /* The YAML converted Zone configuration. */ |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 51 | extern std::map<int64_t, struct ZoneConfig> zoneDetailsConfig; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 52 | |
| 53 | int main(int argc, char* argv[]) |
| 54 | { |
| 55 | int rc = 0; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 56 | std::string configPath = ""; |
| 57 | |
| 58 | while (1) |
| 59 | { |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 60 | // clang-format off |
| 61 | static struct option long_options[] = { |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 62 | {"conf", required_argument, 0, 'c'}, |
| 63 | {0, 0, 0, 0} |
| 64 | }; |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 65 | // clang-format on |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 66 | |
| 67 | int option_index = 0; |
Patrick Venture | df766f2 | 2018-10-13 09:30:58 -0700 | [diff] [blame] | 68 | int c = getopt_long(argc, argv, "c:", long_options, &option_index); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 69 | |
| 70 | if (c == -1) |
| 71 | { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | switch (c) |
| 76 | { |
| 77 | case 'c': |
Patrick Venture | da4a5dd | 2018-08-31 09:42:48 -0700 | [diff] [blame] | 78 | configPath = std::string{optarg}; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 79 | break; |
| 80 | default: |
| 81 | /* skip garbage. */ |
| 82 | continue; |
| 83 | } |
| 84 | } |
| 85 | |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 86 | auto modeControlBus = sdbusplus::bus::new_default(); |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 87 | #if CONFIGURE_DBUS |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 88 | { |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 89 | dbus_configuration::init(modeControlBus); |
James Feist | 7136a5a | 2018-07-19 09:52:05 -0700 | [diff] [blame] | 90 | } |
Patrick Venture | 9f04441 | 2018-09-20 19:49:55 -0700 | [diff] [blame] | 91 | #endif |
Patrick Venture | fe75b19 | 2018-06-08 11:19:43 -0700 | [diff] [blame] | 92 | SensorManager mgmr; |
Patrick Venture | 5c7cc54 | 2018-06-11 14:29:38 -0700 | [diff] [blame] | 93 | std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones; |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 94 | |
Gunnar Mills | 08afbb2 | 2018-06-14 08:50:53 -0500 | [diff] [blame] | 95 | // Create a manager for the ModeBus because we own it. |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 96 | static constexpr auto modeRoot = "/xyz/openbmc_project/settings/fanctrl"; |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 97 | sdbusplus::server::manager::manager(modeControlBus, modeRoot); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 98 | |
| 99 | /* |
| 100 | * When building the sensors, if any of the dbus passive ones aren't on the |
| 101 | * bus, it'll fail immediately. |
| 102 | */ |
| 103 | if (configPath.length() > 0) |
| 104 | { |
| 105 | try |
| 106 | { |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 107 | mgmr = buildSensorsFromConfig(configPath); |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 108 | zones = buildZonesFromConfig(configPath, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 109 | } |
| 110 | catch (const std::exception& e) |
| 111 | { |
| 112 | std::cerr << "Failed during building: " << e.what() << "\n"; |
| 113 | exit(EXIT_FAILURE); /* fatal error. */ |
| 114 | } |
| 115 | } |
| 116 | else |
| 117 | { |
Patrick Venture | c54fbd8 | 2018-10-30 19:40:05 -0700 | [diff] [blame] | 118 | mgmr = buildSensors(sensorConfig); |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 119 | zones = buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | if (0 == zones.size()) |
| 123 | { |
| 124 | std::cerr << "No zones defined, exiting.\n"; |
| 125 | return rc; |
| 126 | } |
| 127 | |
| 128 | /* |
| 129 | * All sensors are managed by one manager, but each zone has a pointer to |
| 130 | * it. |
| 131 | */ |
| 132 | |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 133 | auto& hostSensorBus = mgmr.getHostBus(); |
| 134 | auto& passiveListeningBus = mgmr.getPassiveBus(); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 135 | |
| 136 | std::cerr << "Starting threads\n"; |
| 137 | |
| 138 | /* TODO(venture): Ask SensorManager if we have any passive sensors. */ |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 139 | struct ThreadParams p = {std::ref(passiveListeningBus), ""}; |
| 140 | std::thread l(busThread, std::ref(p)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 141 | |
| 142 | /* TODO(venture): Ask SensorManager if we have any host sensors. */ |
| 143 | static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external"; |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 144 | struct ThreadParams e = {std::ref(hostSensorBus), hostBus}; |
| 145 | std::thread te(busThread, std::ref(e)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 146 | |
| 147 | static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl"; |
Patrick Venture | c179d40 | 2018-10-30 19:51:55 -0700 | [diff] [blame] | 148 | struct ThreadParams m = {std::ref(modeControlBus), modeBus}; |
| 149 | std::thread tm(busThread, std::ref(m)); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 150 | |
| 151 | std::vector<std::thread> zoneThreads; |
| 152 | |
| 153 | /* TODO(venture): This was designed to have one thread per zone, but really |
| 154 | * it could have one thread for all the zones and iterate through each |
| 155 | * sequentially as it goes -- and it'd probably be fast enough to do that, |
| 156 | * however, a system isn't likely going to have more than a couple zones. |
| 157 | * If it only has a couple zones, then this is fine. |
| 158 | */ |
Patrick Venture | 4a2dc4d | 2018-10-23 09:02:55 -0700 | [diff] [blame] | 159 | for (const auto& i : zones) |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 160 | { |
| 161 | std::cerr << "pushing zone" << std::endl; |
Patrick Venture | 7af157b | 2018-10-30 11:24:40 -0700 | [diff] [blame] | 162 | zoneThreads.push_back(std::thread(pidControlThread, i.second.get())); |
Patrick Venture | e620656 | 2018-03-08 15:36:53 -0800 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | l.join(); |
| 166 | te.join(); |
| 167 | tm.join(); |
| 168 | for (auto& t : zoneThreads) |
| 169 | { |
| 170 | t.join(); |
| 171 | } |
| 172 | |
| 173 | return rc; |
| 174 | } |