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