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