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 <getopt.h> 34 35 #include <chrono> 36 #include <iostream> 37 #include <map> 38 #include <memory> 39 #include <sdbusplus/bus.hpp> 40 #include <thread> 41 #include <unordered_map> 42 #include <utility> 43 #include <vector> 44 45 #if CONFIGURE_DBUS 46 #include "dbus/dbusconfiguration.hpp" 47 #endif 48 49 /* The YAML converted sensor list. */ 50 std::map<std::string, struct conf::SensorConfig> sensorConfig = {}; 51 /* The YAML converted PID list. */ 52 std::map<int64_t, conf::PIDConf> zoneConfig = {}; 53 /* The YAML converted Zone configuration. */ 54 std::map<int64_t, struct conf::ZoneConfig> zoneDetailsConfig = {}; 55 56 /** the swampd daemon will check for the existence of this file. */ 57 constexpr auto jsonConfigurationPath = "/usr/share/swampd/config.json"; 58 59 int main(int argc, char* argv[]) 60 { 61 int rc = 0; 62 std::string configPath = ""; 63 64 while (1) 65 { 66 // clang-format off 67 static struct option long_options[] = { 68 {"conf", required_argument, 0, 'c'}, 69 {"tuning", required_argument, 0, 't'}, 70 {0, 0, 0, 0} 71 }; 72 // clang-format on 73 74 int option_index = 0; 75 int c = getopt_long(argc, argv, "t:c:", long_options, &option_index); 76 77 if (c == -1) 78 { 79 break; 80 } 81 82 switch (c) 83 { 84 case 'c': 85 configPath = std::string{optarg}; 86 break; 87 case 't': 88 tuningLoggingEnabled = true; 89 tuningLoggingPath = std::string{optarg}; 90 break; 91 default: 92 /* skip garbage. */ 93 continue; 94 } 95 } 96 97 auto modeControlBus = sdbusplus::bus::new_system(); 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 102 #if CONFIGURE_DBUS 103 { 104 dbus_configuration::init(modeControlBus); 105 } 106 #else 107 const std::string& path = 108 (configPath.length() > 0) ? configPath : jsonConfigurationPath; 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 */ 114 try 115 { 116 auto jsonData = parseValidateJson(path); 117 sensorConfig = buildSensorsFromJson(jsonData); 118 std::tie(zoneConfig, zoneDetailsConfig) = buildPIDsFromJson(jsonData); 119 } 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 126 127 SensorManager mgmr = buildSensors(sensorConfig); 128 std::unordered_map<int64_t, std::unique_ptr<PIDZone>> zones = 129 buildZones(zoneConfig, zoneDetailsConfig, mgmr, modeControlBus); 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 142 auto& hostSensorBus = mgmr.getHostBus(); 143 auto& passiveListeningBus = mgmr.getPassiveBus(); 144 145 std::cerr << "Starting threads\n"; 146 147 /* TODO(venture): Ask SensorManager if we have any passive sensors. */ 148 struct ThreadParams p = {std::ref(passiveListeningBus), ""}; 149 std::thread l(busThread, std::ref(p)); 150 151 /* TODO(venture): Ask SensorManager if we have any host sensors. */ 152 static constexpr auto hostBus = "xyz.openbmc_project.Hwmon.external"; 153 struct ThreadParams e = {std::ref(hostSensorBus), hostBus}; 154 std::thread te(busThread, std::ref(e)); 155 156 static constexpr auto modeBus = "xyz.openbmc_project.State.FanCtrl"; 157 struct ThreadParams m = {std::ref(modeControlBus), modeBus}; 158 std::thread tm(busThread, std::ref(m)); 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 */ 168 for (const auto& i : zones) 169 { 170 std::cerr << "pushing zone" << std::endl; 171 zoneThreads.push_back(std::thread(pidControlThread, i.second.get())); 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 } 184