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