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