| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 1 | /* | 
|  | 2 | // Copyright (c) 2018 Intel Corporation | 
|  | 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 | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 17 | #include "filesystem.hpp" | 
|  | 18 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 19 | #include <fcntl.h> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 20 |  | 
|  | 21 | #include <CPUSensor.hpp> | 
|  | 22 | #include <Utils.hpp> | 
|  | 23 | #include <VariantVisitors.hpp> | 
|  | 24 | #include <boost/algorithm/string/predicate.hpp> | 
|  | 25 | #include <boost/algorithm/string/replace.hpp> | 
|  | 26 | #include <boost/container/flat_set.hpp> | 
|  | 27 | #include <boost/date_time/posix_time/posix_time.hpp> | 
|  | 28 | #include <boost/process/child.hpp> | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 29 | #include <fstream> | 
|  | 30 | #include <regex> | 
|  | 31 | #include <sdbusplus/asio/connection.hpp> | 
|  | 32 | #include <sdbusplus/asio/object_server.hpp> | 
|  | 33 |  | 
| James Feist | f87dc4c | 2018-12-05 14:39:51 -0800 | [diff] [blame] | 34 | // clang-format off | 
|  | 35 | // this needs to be included last or we'll have build issues | 
|  | 36 | #include <linux/peci-ioctl.h> | 
|  | 37 | // clang-format on | 
|  | 38 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 39 | static constexpr bool DEBUG = false; | 
|  | 40 |  | 
|  | 41 | enum State | 
|  | 42 | { | 
|  | 43 | OFF,  // host powered down | 
|  | 44 | ON,   // host powered on | 
|  | 45 | READY // host powered on and mem test passed - fully ready | 
|  | 46 | }; | 
|  | 47 |  | 
|  | 48 | struct CPUConfig | 
|  | 49 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 50 | CPUConfig(const uint64_t& bus, const uint64_t& addr, | 
|  | 51 | const std::string& name, const State& state) : | 
|  | 52 | bus(bus), | 
|  | 53 | addr(addr), name(name), state(state) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 54 | { | 
|  | 55 | } | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 56 | int bus; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 57 | int addr; | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 58 | std::string name; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 59 | State state; | 
|  | 60 |  | 
|  | 61 | bool operator<(const CPUConfig& rhs) const | 
|  | 62 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 63 | return (name < rhs.name); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 64 | } | 
|  | 65 | }; | 
|  | 66 |  | 
| Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 67 | static constexpr const char* peciDev = "/dev/peci-"; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 68 | static constexpr const unsigned int rankNumMax = 8; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 69 |  | 
| James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 70 | namespace fs = std::filesystem; | 
| Yoo, Jae Hyun | 5093805 | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 71 | namespace variant_ns = sdbusplus::message::variant_ns; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 72 | static constexpr const char* configPrefix = | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 73 | "xyz.openbmc_project.Configuration."; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 74 | static constexpr std::array<const char*, 3> sensorTypes = { | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 75 | "SkylakeCPU", "BroadwellCPU", "HaswellCPU"}; | 
|  | 76 |  | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 77 | void detectCpuAsync( | 
|  | 78 | boost::asio::deadline_timer& pingTimer, | 
|  | 79 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, | 
|  | 80 | sdbusplus::asio::object_server& objectServer, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 81 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 82 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>& | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 83 | cpuSensors, | 
|  | 84 | boost::container::flat_set<CPUConfig>& cpuConfigs, | 
|  | 85 | ManagedObjectType& sensorConfigs); | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 86 |  | 
| Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 87 | bool createSensors( | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 88 | boost::asio::io_service& io, sdbusplus::asio::object_server& objectServer, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 89 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 90 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>& | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 91 | cpuSensors, | 
|  | 92 | boost::container::flat_set<CPUConfig>& cpuConfigs, | 
|  | 93 | ManagedObjectType& sensorConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 94 | { | 
|  | 95 | bool available = false; | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 96 | for (const CPUConfig& cpu : cpuConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 97 | { | 
|  | 98 | if (cpu.state != State::OFF) | 
|  | 99 | { | 
|  | 100 | available = true; | 
|  | 101 | break; | 
|  | 102 | } | 
|  | 103 | } | 
|  | 104 | if (!available) | 
|  | 105 | { | 
| Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 106 | return false; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 107 | } | 
|  | 108 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 109 | if (sensorConfigs.empty()) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 110 | { | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 111 | return false; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 112 | } | 
|  | 113 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 114 | std::vector<fs::path> hwmonNamePaths; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 115 | if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"), | 
|  | 116 | R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", | 
|  | 117 | hwmonNamePaths, 1)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 118 | { | 
|  | 119 | std::cerr << "No CPU sensors in system\n"; | 
| Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 120 | return true; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 121 | } | 
|  | 122 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 123 | boost::container::flat_set<std::string> scannedDirectories; | 
|  | 124 | boost::container::flat_set<std::string> createdSensors; | 
|  | 125 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 126 | for (const fs::path& hwmonNamePath : hwmonNamePaths) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 127 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 128 | const std::string& pathStr = hwmonNamePath.string(); | 
|  | 129 | auto hwmonDirectory = hwmonNamePath.parent_path(); | 
|  | 130 |  | 
|  | 131 | auto ret = scannedDirectories.insert(hwmonDirectory.string()); | 
|  | 132 | if (!ret.second) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 133 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 134 | continue; // already searched this path | 
|  | 135 | } | 
|  | 136 |  | 
|  | 137 | fs::path::iterator it = hwmonNamePath.begin(); | 
|  | 138 | std::advance(it, 6); // pick the 6th part for a PECI client device name | 
|  | 139 | std::string deviceName = *it; | 
|  | 140 | auto findHyphen = deviceName.find("-"); | 
|  | 141 | if (findHyphen == std::string::npos) | 
|  | 142 | { | 
|  | 143 | std::cerr << "found bad device " << deviceName << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 144 | continue; | 
|  | 145 | } | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 146 | std::string busStr = deviceName.substr(0, findHyphen); | 
|  | 147 | std::string addrStr = deviceName.substr(findHyphen + 1); | 
|  | 148 |  | 
|  | 149 | size_t bus = 0; | 
|  | 150 | size_t addr = 0; | 
|  | 151 | try | 
|  | 152 | { | 
|  | 153 | bus = std::stoi(busStr); | 
|  | 154 | addr = std::stoi(addrStr, 0, 16); | 
|  | 155 | } | 
|  | 156 | catch (std::invalid_argument) | 
|  | 157 | { | 
|  | 158 | continue; | 
|  | 159 | } | 
|  | 160 |  | 
|  | 161 | std::ifstream nameFile(hwmonNamePath); | 
|  | 162 | if (!nameFile.good()) | 
|  | 163 | { | 
|  | 164 | std::cerr << "Failure reading " << hwmonNamePath << "\n"; | 
|  | 165 | continue; | 
|  | 166 | } | 
|  | 167 | std::string hwmonName; | 
|  | 168 | std::getline(nameFile, hwmonName); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 169 | nameFile.close(); | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 170 | if (hwmonName.empty()) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 171 | { | 
|  | 172 | // shouldn't have an empty name file | 
|  | 173 | continue; | 
|  | 174 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 175 | if (DEBUG) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 176 | { | 
|  | 177 | std::cout << "Checking: " << hwmonNamePath << ": " << hwmonName | 
|  | 178 | << "\n"; | 
|  | 179 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 180 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 181 | std::string sensorType; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 182 | const SensorData* sensorData = nullptr; | 
|  | 183 | const std::string* interfacePath = nullptr; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 184 | const std::pair<std::string, boost::container::flat_map< | 
|  | 185 | std::string, BasicVariantType>>* | 
|  | 186 | baseConfiguration = nullptr; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 187 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 188 | for (const std::pair<sdbusplus::message::object_path, SensorData>& | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 189 | sensor : sensorConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 190 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 191 | sensorData = &(sensor.second); | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 192 | for (const char* type : sensorTypes) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 193 | { | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 194 | sensorType = configPrefix + std::string(type); | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 195 | auto sensorBase = sensorData->find(sensorType); | 
|  | 196 | if (sensorBase != sensorData->end()) | 
|  | 197 | { | 
|  | 198 | baseConfiguration = &(*sensorBase); | 
|  | 199 | break; | 
|  | 200 | } | 
|  | 201 | } | 
|  | 202 | if (baseConfiguration == nullptr) | 
|  | 203 | { | 
|  | 204 | std::cerr << "error finding base configuration for" << hwmonName | 
|  | 205 | << "\n"; | 
|  | 206 | continue; | 
|  | 207 | } | 
|  | 208 | auto configurationBus = baseConfiguration->second.find("Bus"); | 
|  | 209 | auto configurationAddress = | 
|  | 210 | baseConfiguration->second.find("Address"); | 
|  | 211 |  | 
|  | 212 | if (configurationBus == baseConfiguration->second.end() || | 
|  | 213 | configurationAddress == baseConfiguration->second.end()) | 
|  | 214 | { | 
|  | 215 | std::cerr << "error finding bus or address in configuration"; | 
|  | 216 | continue; | 
|  | 217 | } | 
|  | 218 |  | 
|  | 219 | if (sdbusplus::message::variant_ns::get<uint64_t>( | 
|  | 220 | configurationBus->second) != bus || | 
|  | 221 | sdbusplus::message::variant_ns::get<uint64_t>( | 
|  | 222 | configurationAddress->second) != addr) | 
|  | 223 | { | 
|  | 224 | continue; | 
|  | 225 | } | 
|  | 226 |  | 
|  | 227 | interfacePath = &(sensor.first.str); | 
|  | 228 | break; | 
|  | 229 | } | 
|  | 230 | if (interfacePath == nullptr) | 
|  | 231 | { | 
|  | 232 | std::cerr << "failed to find match for " << hwmonName << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 233 | continue; | 
|  | 234 | } | 
|  | 235 |  | 
|  | 236 | auto findCpuId = baseConfiguration->second.find("CpuID"); | 
|  | 237 | if (findCpuId == baseConfiguration->second.end()) | 
|  | 238 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 239 | std::cerr << "could not determine CPU ID for " << hwmonName << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 240 | continue; | 
|  | 241 | } | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 242 | int cpuId = | 
|  | 243 | variant_ns::visit(VariantToUnsignedIntVisitor(), findCpuId->second); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 244 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 245 | auto directory = hwmonNamePath.parent_path(); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 246 | std::vector<fs::path> inputPaths; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 247 | if (!findFiles(fs::path(directory), R"(temp\d+_input$)", inputPaths, 0)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 248 | { | 
|  | 249 | std::cerr << "No temperature sensors in system\n"; | 
|  | 250 | continue; | 
|  | 251 | } | 
|  | 252 |  | 
|  | 253 | // iterate through all found temp sensors | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 254 | for (const auto& inputPath : inputPaths) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 255 | { | 
|  | 256 | auto inputPathStr = inputPath.string(); | 
|  | 257 | auto labelPath = | 
|  | 258 | boost::replace_all_copy(inputPathStr, "input", "label"); | 
|  | 259 | std::ifstream labelFile(labelPath); | 
|  | 260 | if (!labelFile.good()) | 
|  | 261 | { | 
|  | 262 | std::cerr << "Failure reading " << labelPath << "\n"; | 
|  | 263 | continue; | 
|  | 264 | } | 
|  | 265 | std::string label; | 
|  | 266 | std::getline(labelFile, label); | 
|  | 267 | labelFile.close(); | 
|  | 268 | std::string sensorName = label + " CPU" + std::to_string(cpuId); | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 269 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 270 | auto findSensor = cpuSensors.find(sensorName); | 
|  | 271 | if (findSensor != cpuSensors.end()) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 272 | { | 
|  | 273 | if (DEBUG) | 
|  | 274 | { | 
|  | 275 | std::cout << "Skipped: " << inputPath << ": " << sensorName | 
|  | 276 | << " is already created\n"; | 
|  | 277 | } | 
|  | 278 | continue; | 
|  | 279 | } | 
|  | 280 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 281 | std::vector<thresholds::Threshold> sensorThresholds; | 
|  | 282 | std::string labelHead = label.substr(0, label.find(" ")); | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 283 | parseThresholdsFromConfig(*sensorData, sensorThresholds, | 
| Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 284 | &labelHead); | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 285 | if (sensorThresholds.empty()) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 286 | { | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 287 | if (!parseThresholdsFromAttr(sensorThresholds, inputPathStr, | 
|  | 288 | CPUSensor::sensorScaleFactor)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 289 | { | 
| Yoo, Jae Hyun | 81a464c | 2018-10-09 16:38:58 -0700 | [diff] [blame] | 290 | std::cerr << "error populating thresholds for " | 
|  | 291 | << sensorName << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 292 | } | 
|  | 293 | } | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 294 | cpuSensors[sensorName] = std::make_unique<CPUSensor>( | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 295 | inputPathStr, sensorType, objectServer, dbusConnection, io, | 
|  | 296 | sensorName, std::move(sensorThresholds), *interfacePath); | 
|  | 297 | createdSensors.insert(sensorName); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 298 | if (DEBUG) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 299 | { | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 300 | std::cout << "Mapped: " << inputPath << " to " << sensorName | 
|  | 301 | << "\n"; | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 302 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 303 | } | 
|  | 304 | } | 
| Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 305 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 306 | if (createdSensors.size()) | 
|  | 307 | { | 
|  | 308 | std::cout << "Sensor" << (createdSensors.size() == 1 ? " is" : "s are") | 
|  | 309 | << " created\n"; | 
|  | 310 | } | 
| Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 311 |  | 
| Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 312 | return true; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 313 | } | 
|  | 314 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 315 | void exportDevice(const CPUConfig& config) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 316 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 317 | std::ostringstream hex; | 
|  | 318 | hex << std::hex << config.addr; | 
|  | 319 | const std::string& addrHexStr = hex.str(); | 
|  | 320 | std::string busStr = std::to_string(config.bus); | 
|  | 321 |  | 
|  | 322 | std::string parameters = "peci-client 0x" + addrHexStr; | 
|  | 323 | std::string device = "/sys/bus/peci/devices/peci-" + busStr + "/new_device"; | 
|  | 324 |  | 
| James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 325 | std::filesystem::path devicePath(device); | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 326 | const std::string& dir = devicePath.parent_path().string(); | 
| James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 327 | for (const auto& path : std::filesystem::directory_iterator(dir)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 328 | { | 
| James Feist | cf3bce6 | 2019-01-08 10:07:19 -0800 | [diff] [blame] | 329 | if (!std::filesystem::is_directory(path)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 330 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 331 | continue; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 332 | } | 
|  | 333 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 334 | const std::string& directoryName = path.path().filename(); | 
|  | 335 | if (boost::starts_with(directoryName, busStr) && | 
|  | 336 | boost::ends_with(directoryName, addrHexStr)) | 
|  | 337 | { | 
|  | 338 | if (DEBUG) | 
|  | 339 | { | 
|  | 340 | std::cout << parameters << " on bus " << busStr | 
|  | 341 | << " is already exported\n"; | 
|  | 342 | } | 
|  | 343 | return; | 
|  | 344 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 345 | } | 
|  | 346 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 347 | std::ofstream deviceFile(device); | 
|  | 348 | if (!deviceFile.good()) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 349 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 350 | std::cerr << "Error writing " << device << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 351 | return; | 
|  | 352 | } | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 353 | deviceFile << parameters; | 
|  | 354 | deviceFile.close(); | 
|  | 355 |  | 
|  | 356 | std::cout << parameters << " on bus " << busStr << " is exported\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 357 | } | 
|  | 358 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 359 | void detectCpu( | 
|  | 360 | boost::asio::deadline_timer& pingTimer, | 
|  | 361 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, | 
|  | 362 | sdbusplus::asio::object_server& objectServer, | 
|  | 363 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, | 
|  | 364 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>& | 
|  | 365 | cpuSensors, | 
|  | 366 | boost::container::flat_set<CPUConfig>& cpuConfigs, | 
|  | 367 | ManagedObjectType& sensorConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 368 | { | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 369 | size_t rescanDelaySeconds = 0; | 
|  | 370 | bool keepPinging = false; | 
| Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 371 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 372 | for (CPUConfig& config : cpuConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 373 | { | 
| Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 374 | std::string peciDevPath = peciDev + std::to_string(config.bus); | 
|  | 375 | auto file = open(peciDevPath.c_str(), O_RDWR | O_CLOEXEC); | 
|  | 376 | if (file < 0) | 
|  | 377 | { | 
|  | 378 | std::cerr << "unable to open " << peciDevPath << "\n"; | 
|  | 379 | std::exit(EXIT_FAILURE); | 
|  | 380 | } | 
|  | 381 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 382 | State state; | 
|  | 383 | struct peci_ping_msg msg; | 
|  | 384 | msg.addr = config.addr; | 
|  | 385 | if (!ioctl(file, PECI_IOC_PING, &msg)) | 
|  | 386 | { | 
|  | 387 | bool dimmReady = false; | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 388 | for (unsigned int rank = 0; rank < rankNumMax; rank++) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 389 | { | 
|  | 390 | struct peci_rd_pkg_cfg_msg msg; | 
|  | 391 | msg.addr = config.addr; | 
|  | 392 | msg.index = MBX_INDEX_DDR_DIMM_TEMP; | 
|  | 393 | msg.param = rank; | 
|  | 394 | msg.rx_len = 4; | 
|  | 395 | if (!ioctl(file, PECI_IOC_RD_PKG_CFG, &msg)) | 
|  | 396 | { | 
|  | 397 | if (msg.pkg_config[0] || msg.pkg_config[1] || | 
|  | 398 | msg.pkg_config[2]) | 
|  | 399 | { | 
|  | 400 | dimmReady = true; | 
|  | 401 | break; | 
|  | 402 | } | 
|  | 403 | } | 
|  | 404 | else | 
|  | 405 | { | 
|  | 406 | break; | 
|  | 407 | } | 
|  | 408 | } | 
| Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 409 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 410 | if (dimmReady) | 
|  | 411 | { | 
|  | 412 | state = State::READY; | 
|  | 413 | } | 
|  | 414 | else | 
|  | 415 | { | 
|  | 416 | state = State::ON; | 
|  | 417 | } | 
|  | 418 | } | 
|  | 419 | else | 
|  | 420 | { | 
|  | 421 | state = State::OFF; | 
|  | 422 | } | 
|  | 423 |  | 
| Jae Hyun Yoo | 9c55e6a | 2018-10-26 10:09:01 -0700 | [diff] [blame] | 424 | close(file); | 
|  | 425 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 426 | if (config.state != state) | 
|  | 427 | { | 
|  | 428 | if (config.state == State::OFF) | 
|  | 429 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 430 | std::cout << config.name << " is detected\n"; | 
|  | 431 | exportDevice(config); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 432 | } | 
| Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 433 | if (state == State::READY) | 
|  | 434 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 435 | std::cout << "DIMM(s) on " << config.name | 
| Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 436 | << " is/are detected\n"; | 
|  | 437 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 438 | config.state = state; | 
|  | 439 | } | 
|  | 440 |  | 
| Yoo, Jae Hyun | f78d0a4 | 2018-10-10 11:03:11 -0700 | [diff] [blame] | 441 | if (config.state != State::OFF) | 
|  | 442 | { | 
|  | 443 | if (config.state == State::ON) | 
|  | 444 | { | 
|  | 445 | rescanDelaySeconds = 1; | 
|  | 446 | } | 
|  | 447 | else | 
|  | 448 | { | 
|  | 449 | rescanDelaySeconds = 5; | 
|  | 450 | } | 
|  | 451 | } | 
|  | 452 |  | 
|  | 453 | if (config.state != State::READY) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 454 | { | 
|  | 455 | keepPinging = true; | 
|  | 456 | } | 
|  | 457 |  | 
|  | 458 | if (DEBUG) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 459 | { | 
|  | 460 | std::cout << config.name << ", state: " << config.state << "\n"; | 
|  | 461 | } | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 462 | } | 
|  | 463 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 464 | if (rescanDelaySeconds) | 
|  | 465 | { | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 466 | creationTimer.expires_from_now( | 
|  | 467 | boost::posix_time::seconds(rescanDelaySeconds)); | 
|  | 468 | creationTimer.async_wait([&](const boost::system::error_code& ec) { | 
|  | 469 | if (ec == boost::asio::error::operation_aborted) | 
|  | 470 | { | 
|  | 471 | return; // we're being canceled | 
|  | 472 | } | 
|  | 473 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 474 | if (!createSensors(io, objectServer, dbusConnection, cpuSensors, | 
|  | 475 | cpuConfigs, sensorConfigs)) | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 476 | { | 
|  | 477 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 478 | dbusConnection, cpuSensors, cpuConfigs, | 
|  | 479 | sensorConfigs); | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 480 | } | 
|  | 481 | }); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 482 | } | 
|  | 483 |  | 
|  | 484 | if (keepPinging) | 
|  | 485 | { | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 486 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, | 
|  | 487 | dbusConnection, cpuSensors, cpuConfigs, sensorConfigs); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 488 | } | 
|  | 489 | } | 
|  | 490 |  | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 491 | void detectCpuAsync( | 
|  | 492 | boost::asio::deadline_timer& pingTimer, | 
|  | 493 | boost::asio::deadline_timer& creationTimer, boost::asio::io_service& io, | 
|  | 494 | sdbusplus::asio::object_server& objectServer, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 495 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection, | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 496 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>>& | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 497 | cpuSensors, | 
|  | 498 | boost::container::flat_set<CPUConfig>& cpuConfigs, | 
|  | 499 | ManagedObjectType& sensorConfigs) | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 500 | { | 
|  | 501 | pingTimer.expires_from_now(boost::posix_time::seconds(1)); | 
|  | 502 | pingTimer.async_wait([&](const boost::system::error_code& ec) { | 
|  | 503 | if (ec == boost::asio::error::operation_aborted) | 
|  | 504 | { | 
|  | 505 | return; // we're being canceled | 
|  | 506 | } | 
|  | 507 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 508 | detectCpu(pingTimer, creationTimer, io, objectServer, dbusConnection, | 
|  | 509 | cpuSensors, cpuConfigs, sensorConfigs); | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 510 | }); | 
|  | 511 | } | 
|  | 512 |  | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 513 | bool getCpuConfig(const std::shared_ptr<sdbusplus::asio::connection>& systemBus, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 514 | boost::container::flat_set<CPUConfig>& cpuConfigs, | 
|  | 515 | ManagedObjectType& sensorConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 516 | { | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 517 | bool useCache = false; | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 518 | sensorConfigs.clear(); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 519 | // use new data the first time, then refresh | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 520 | for (const char* type : sensorTypes) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 521 | { | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 522 | if (!getSensorConfiguration(configPrefix + std::string(type), systemBus, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 523 | sensorConfigs, useCache)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 524 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 525 | return false; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 526 | } | 
|  | 527 | useCache = true; | 
|  | 528 | } | 
|  | 529 |  | 
|  | 530 | // check PECI client addresses and DT overlay names from CPU configuration | 
|  | 531 | // before starting ping operation | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 532 | for (const char* type : sensorTypes) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 533 | { | 
|  | 534 | for (const std::pair<sdbusplus::message::object_path, SensorData>& | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 535 | sensor : sensorConfigs) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 536 | { | 
|  | 537 | for (const std::pair< | 
|  | 538 | std::string, | 
|  | 539 | boost::container::flat_map<std::string, BasicVariantType>>& | 
|  | 540 | config : sensor.second) | 
|  | 541 | { | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 542 | if ((configPrefix + std::string(type)) != config.first) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 543 | { | 
|  | 544 | continue; | 
|  | 545 | } | 
|  | 546 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 547 | auto findName = config.second.find("Name"); | 
|  | 548 | if (findName == config.second.end()) | 
|  | 549 | { | 
|  | 550 | continue; | 
|  | 551 | } | 
| Yoo, Jae Hyun | 5093805 | 2018-10-17 18:19:02 -0700 | [diff] [blame] | 552 | std::string nameRaw = variant_ns::visit( | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 553 | VariantToStringVisitor(), findName->second); | 
|  | 554 | std::string name = | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 555 | std::regex_replace(nameRaw, illegalDbusRegex, "_"); | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 556 |  | 
|  | 557 | auto findBus = config.second.find("Bus"); | 
|  | 558 | if (findBus == config.second.end()) | 
|  | 559 | { | 
|  | 560 | std::cerr << "Can't find 'Bus' setting in " << name << "\n"; | 
|  | 561 | continue; | 
|  | 562 | } | 
|  | 563 | uint64_t bus = variant_ns::visit(VariantToUnsignedIntVisitor(), | 
|  | 564 | findBus->second); | 
|  | 565 |  | 
|  | 566 | auto findAddress = config.second.find("Address"); | 
|  | 567 | if (findAddress == config.second.end()) | 
|  | 568 | { | 
|  | 569 | std::cerr << "Can't find 'Address' setting in " << name | 
|  | 570 | << "\n"; | 
|  | 571 | continue; | 
|  | 572 | } | 
|  | 573 | uint64_t addr = variant_ns::visit(VariantToUnsignedIntVisitor(), | 
|  | 574 | findAddress->second); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 575 |  | 
|  | 576 | if (DEBUG) | 
|  | 577 | { | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 578 | std::cout << "bus: " << bus << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 579 | std::cout << "addr: " << addr << "\n"; | 
|  | 580 | std::cout << "name: " << name << "\n"; | 
|  | 581 | std::cout << "type: " << type << "\n"; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 582 | } | 
|  | 583 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 584 | cpuConfigs.emplace(bus, addr, name, State::OFF); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 585 | } | 
|  | 586 | } | 
|  | 587 | } | 
| Yoo, Jae Hyun | a441f3c | 2018-10-09 16:43:03 -0700 | [diff] [blame] | 588 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 589 | if (cpuConfigs.size()) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 590 | { | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 591 | std::cout << "CPU config" << (cpuConfigs.size() == 1 ? " is" : "s are") | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 592 | << " parsed\n"; | 
|  | 593 | return true; | 
|  | 594 | } | 
|  | 595 |  | 
|  | 596 | return false; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 597 | } | 
|  | 598 |  | 
|  | 599 | int main(int argc, char** argv) | 
|  | 600 | { | 
|  | 601 | boost::asio::io_service io; | 
|  | 602 | auto systemBus = std::make_shared<sdbusplus::asio::connection>(io); | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 603 | boost::container::flat_set<CPUConfig> cpuConfigs; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 604 |  | 
|  | 605 | systemBus->request_name("xyz.openbmc_project.CPUSensor"); | 
|  | 606 | sdbusplus::asio::object_server objectServer(systemBus); | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 607 | boost::container::flat_map<std::string, std::unique_ptr<CPUSensor>> | 
|  | 608 | cpuSensors; | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 609 | std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches; | 
|  | 610 | boost::asio::deadline_timer pingTimer(io); | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 611 | boost::asio::deadline_timer creationTimer(io); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 612 | boost::asio::deadline_timer filterTimer(io); | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 613 | ManagedObjectType sensorConfigs; | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 614 |  | 
|  | 615 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); | 
|  | 616 | filterTimer.async_wait([&](const boost::system::error_code& ec) { | 
|  | 617 | if (ec == boost::asio::error::operation_aborted) | 
|  | 618 | { | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 619 | return; // we're being canceled | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 620 | } | 
|  | 621 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 622 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs)) | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 623 | { | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 624 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, | 
|  | 625 | systemBus, cpuSensors, cpuConfigs, sensorConfigs); | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 626 | } | 
|  | 627 | }); | 
|  | 628 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 629 | std::function<void(sdbusplus::message::message&)> eventHandler = | 
|  | 630 | [&](sdbusplus::message::message& message) { | 
|  | 631 | if (message.is_method_error()) | 
|  | 632 | { | 
|  | 633 | std::cerr << "callback method error\n"; | 
|  | 634 | return; | 
|  | 635 | } | 
| Jae Hyun Yoo | 8d9886d | 2018-10-22 15:24:29 -0700 | [diff] [blame] | 636 |  | 
|  | 637 | if (DEBUG) | 
|  | 638 | { | 
|  | 639 | std::cout << message.get_path() << " is changed\n"; | 
|  | 640 | } | 
|  | 641 |  | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 642 | // this implicitly cancels the timer | 
|  | 643 | filterTimer.expires_from_now(boost::posix_time::seconds(1)); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 644 | filterTimer.async_wait([&](const boost::system::error_code& ec) { | 
|  | 645 | if (ec == boost::asio::error::operation_aborted) | 
|  | 646 | { | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 647 | return; // we're being canceled | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 648 | } | 
|  | 649 |  | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 650 | if (getCpuConfig(systemBus, cpuConfigs, sensorConfigs)) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 651 | { | 
| Jae Hyun Yoo | d64262b | 2018-11-01 13:31:16 -0700 | [diff] [blame] | 652 | detectCpuAsync(pingTimer, creationTimer, io, objectServer, | 
| Jae Hyun Yoo | e8b60d0 | 2019-01-14 15:27:13 -0800 | [diff] [blame] | 653 | systemBus, cpuSensors, cpuConfigs, | 
|  | 654 | sensorConfigs); | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 655 | } | 
|  | 656 | }); | 
|  | 657 | }; | 
|  | 658 |  | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 659 | for (const char* type : sensorTypes) | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 660 | { | 
|  | 661 | auto match = std::make_unique<sdbusplus::bus::match::match>( | 
|  | 662 | static_cast<sdbusplus::bus::bus&>(*systemBus), | 
|  | 663 | "type='signal',member='PropertiesChanged',path_namespace='" + | 
| Jae Hyun Yoo | 9ced0a3 | 2018-10-25 10:42:39 -0700 | [diff] [blame] | 664 | std::string(inventoryPath) + "',arg0namespace='" + | 
|  | 665 | configPrefix + type + "'", | 
| James Feist | 6714a25 | 2018-09-10 15:26:18 -0700 | [diff] [blame] | 666 | eventHandler); | 
|  | 667 | matches.emplace_back(std::move(match)); | 
|  | 668 | } | 
|  | 669 |  | 
|  | 670 | io.run(); | 
|  | 671 | } |