Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include "occ_manager.hpp" |
| 4 | |
| 5 | #include "i2c_occ.hpp" |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 6 | #include "occ_dbus.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 7 | #include "utils.hpp" |
| 8 | |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame^] | 9 | #include <chrono> |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 10 | #include <cmath> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 11 | #include <experimental/filesystem> |
| 12 | #include <phosphor-logging/elog-errors.hpp> |
| 13 | #include <phosphor-logging/log.hpp> |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 14 | #include <regex> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 15 | #include <xyz/openbmc_project/Common/error.hpp> |
| 16 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 17 | namespace open_power |
| 18 | { |
| 19 | namespace occ |
| 20 | { |
| 21 | |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 22 | constexpr uint32_t fruTypeNotAvailable = 0xFF; |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 23 | constexpr auto fruTypeSuffix = "fru_type"; |
| 24 | constexpr auto faultSuffix = "fault"; |
| 25 | constexpr auto inputSuffix = "input"; |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 26 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 27 | using namespace phosphor::logging; |
| 28 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 29 | template <typename T> |
| 30 | T readFile(const std::string& path) |
| 31 | { |
| 32 | std::ifstream ifs; |
| 33 | ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
| 34 | std::ifstream::eofbit); |
| 35 | T data; |
| 36 | |
| 37 | try |
| 38 | { |
| 39 | ifs.open(path); |
| 40 | ifs >> data; |
| 41 | ifs.close(); |
| 42 | } |
| 43 | catch (const std::exception& e) |
| 44 | { |
| 45 | auto err = errno; |
| 46 | throw std::system_error(err, std::generic_category()); |
| 47 | } |
| 48 | |
| 49 | return data; |
| 50 | } |
| 51 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 52 | void Manager::findAndCreateObjects() |
| 53 | { |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame^] | 54 | #ifndef POWER10 |
Deepak Kodihalli | 370f06b | 2017-10-25 04:26:07 -0500 | [diff] [blame] | 55 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 56 | { |
Deepak Kodihalli | 30417a1 | 2017-12-04 00:54:01 -0600 | [diff] [blame] | 57 | // Create one occ per cpu |
| 58 | auto occ = std::string(OCC_NAME) + std::to_string(id); |
| 59 | createObjects(occ); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 60 | } |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame^] | 61 | #else |
| 62 | // Create the OCCs based on on the /dev/occX devices |
| 63 | auto occs = findOCCsInDev(); |
| 64 | |
| 65 | if (occs.empty() || (prevOCCSearch.size() != occs.size())) |
| 66 | { |
| 67 | // Something changed or no OCCs yet, try again in 10s. |
| 68 | // Note on the first pass prevOCCSearch will be empty, |
| 69 | // so there will be at least one delay to give things |
| 70 | // a chance to settle. |
| 71 | prevOCCSearch = occs; |
| 72 | |
| 73 | using namespace std::literals::chrono_literals; |
| 74 | discoverTimer->restartOnce(10s); |
| 75 | } |
| 76 | else |
| 77 | { |
| 78 | discoverTimer.reset(); |
| 79 | |
| 80 | // createObjects requires OCC0 first. |
| 81 | std::sort(occs.begin(), occs.end()); |
| 82 | |
| 83 | for (auto id : occs) |
| 84 | { |
| 85 | createObjects(std::string(OCC_NAME) + std::to_string(id)); |
| 86 | } |
| 87 | } |
| 88 | #endif |
| 89 | } |
| 90 | |
| 91 | std::vector<int> Manager::findOCCsInDev() |
| 92 | { |
| 93 | std::vector<int> occs; |
| 94 | std::regex expr{R"(occ(\d+)$)"}; |
| 95 | |
| 96 | for (auto& file : fs::directory_iterator("/dev")) |
| 97 | { |
| 98 | std::smatch match; |
| 99 | std::string path{file.path().string()}; |
| 100 | if (std::regex_search(path, match, expr)) |
| 101 | { |
| 102 | auto num = std::stoi(match[1].str()); |
| 103 | |
| 104 | // /dev numbering starts at 1, ours starts at 0. |
| 105 | occs.push_back(num - 1); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | return occs; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 110 | } |
| 111 | |
| 112 | int Manager::cpuCreated(sdbusplus::message::message& msg) |
| 113 | { |
| 114 | namespace fs = std::experimental::filesystem; |
| 115 | |
| 116 | sdbusplus::message::object_path o; |
| 117 | msg.read(o); |
| 118 | fs::path cpuPath(std::string(std::move(o))); |
| 119 | |
| 120 | auto name = cpuPath.filename().string(); |
| 121 | auto index = name.find(CPU_NAME); |
| 122 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 123 | |
| 124 | createObjects(name); |
| 125 | |
| 126 | return 0; |
| 127 | } |
| 128 | |
| 129 | void Manager::createObjects(const std::string& occ) |
| 130 | { |
| 131 | auto path = fs::path(OCC_CONTROL_ROOT) / occ; |
| 132 | |
| 133 | passThroughObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 134 | std::make_unique<PassThrough>(path.c_str())); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 135 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 136 | statusObjects.emplace_back(std::make_unique<Status>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 137 | event, path.c_str(), *this, |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 138 | std::bind(std::mem_fn(&Manager::statusCallBack), this, |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 139 | std::placeholders::_1) |
| 140 | #ifdef PLDM |
| 141 | , |
| 142 | std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(), |
| 143 | std::placeholders::_1) |
| 144 | #endif |
| 145 | )); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 146 | |
| 147 | // Create the power cap monitor object for master occ (0) |
| 148 | if (!pcap) |
| 149 | { |
| 150 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 151 | *statusObjects.front()); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 152 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 153 | |
| 154 | #ifdef POWER10 |
| 155 | // Create the power mode monitor object for master occ (0) |
| 156 | if (!pmode) |
| 157 | { |
| 158 | pmode = std::make_unique<open_power::occ::powermode::PowerMode>( |
| 159 | *statusObjects.front()); |
| 160 | } |
| 161 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | void Manager::statusCallBack(bool status) |
| 165 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 166 | using InternalFailure = |
| 167 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 168 | |
| 169 | // At this time, it won't happen but keeping it |
| 170 | // here just in case something changes in the future |
| 171 | if ((activeCount == 0) && (!status)) |
| 172 | { |
| 173 | log<level::ERR>("Invalid update on OCCActive"); |
| 174 | elog<InternalFailure>(); |
| 175 | } |
| 176 | |
| 177 | activeCount += status ? 1 : -1; |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 178 | |
| 179 | // Only start presence detection if all the OCCs are bound |
| 180 | if (activeCount == statusObjects.size()) |
| 181 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 182 | for (auto& obj : statusObjects) |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 183 | { |
| 184 | obj->addPresenceWatchMaster(); |
| 185 | } |
| 186 | } |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 187 | |
| 188 | if ((!_pollTimer->isEnabled()) && (activeCount > 0)) |
| 189 | { |
| 190 | log<level::INFO>(fmt::format("Manager::statusCallBack(): {} OCCs will " |
| 191 | "be polled every {} seconds", |
| 192 | activeCount, pollInterval) |
| 193 | .c_str()); |
| 194 | |
| 195 | // Send poll and start OCC poll timer |
| 196 | pollerTimerExpired(); |
| 197 | } |
| 198 | else if ((_pollTimer->isEnabled()) && (activeCount == 0)) |
| 199 | { |
| 200 | // Stop OCC poll timer |
| 201 | log<level::INFO>("Manager::statusCallBack(): OCCs are not running, " |
| 202 | "stopping poll timer"); |
| 203 | _pollTimer->setEnabled(false); |
Matt Spinler | 53f6814 | 2021-08-25 15:47:31 -0500 | [diff] [blame] | 204 | |
| 205 | #ifdef READ_OCC_SENSORS |
| 206 | for (auto& obj : statusObjects) |
| 207 | { |
| 208 | setSensorValueToNaN(obj->getOccInstanceID()); |
| 209 | } |
| 210 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 211 | } |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | #ifdef I2C_OCC |
| 215 | void Manager::initStatusObjects() |
| 216 | { |
| 217 | // Make sure we have a valid path string |
| 218 | static_assert(sizeof(DEV_PATH) != 0); |
| 219 | |
| 220 | auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH); |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 221 | auto occMasterName = deviceNames.front(); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 222 | for (auto& name : deviceNames) |
| 223 | { |
| 224 | i2c_occ::i2cToDbus(name); |
Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 225 | name = std::string(OCC_NAME) + '_' + name; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 226 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
| 227 | statusObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 228 | std::make_unique<Status>(event, path.c_str(), *this)); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 229 | } |
Lei YU | 41470e5 | 2017-11-30 16:03:50 +0800 | [diff] [blame] | 230 | // The first device is master occ |
| 231 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 232 | *statusObjects.front(), occMasterName); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 233 | #ifdef POWER10 |
| 234 | pmode = std::make_unique<open_power::occ::powermode::PowerMode>( |
| 235 | *statusObjects.front()); |
| 236 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 237 | } |
| 238 | #endif |
| 239 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 240 | #ifdef PLDM |
| 241 | bool Manager::updateOCCActive(instanceID instance, bool status) |
| 242 | { |
| 243 | return (statusObjects[instance])->occActive(status); |
| 244 | } |
| 245 | #endif |
| 246 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 247 | void Manager::pollerTimerExpired() |
| 248 | { |
| 249 | if (activeCount == 0) |
| 250 | { |
| 251 | // No OCCs running, so poll timer will not be restarted |
| 252 | log<level::INFO>("Manager::pollerTimerExpire(): No OCCs running, poll " |
| 253 | "timer not restarted"); |
| 254 | } |
| 255 | |
| 256 | if (!_pollTimer) |
| 257 | { |
| 258 | log<level::ERR>( |
| 259 | "Manager::pollerTimerExpired() ERROR: Timer not defined"); |
| 260 | return; |
| 261 | } |
| 262 | |
| 263 | for (auto& obj : statusObjects) |
| 264 | { |
| 265 | // Read sysfs to force kernel to poll OCC |
| 266 | obj->readOccState(); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 267 | |
| 268 | #ifdef READ_OCC_SENSORS |
| 269 | // Read occ sensor values |
| 270 | auto id = obj->getOccInstanceID(); |
| 271 | if (!obj->occActive()) |
| 272 | { |
| 273 | // Occ not activated |
| 274 | setSensorValueToNaN(id); |
| 275 | continue; |
| 276 | } |
| 277 | getSensorValues(id, obj->isMasterOcc()); |
| 278 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | // Restart OCC poll timer |
| 282 | _pollTimer->restartOnce(std::chrono::seconds(pollInterval)); |
| 283 | } |
| 284 | |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 285 | #ifdef READ_OCC_SENSORS |
| 286 | void Manager::readTempSensors(const fs::path& path, uint32_t id) |
| 287 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 288 | std::regex expr{"temp\\d+_label$"}; // Example: temp5_label |
| 289 | for (auto& file : fs::directory_iterator(path)) |
| 290 | { |
| 291 | if (!std::regex_search(file.path().string(), expr)) |
| 292 | { |
| 293 | continue; |
| 294 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 295 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 296 | uint32_t labelValue{0}; |
| 297 | |
| 298 | try |
| 299 | { |
| 300 | labelValue = readFile<uint32_t>(file.path()); |
| 301 | } |
| 302 | catch (const std::system_error& e) |
| 303 | { |
| 304 | log<level::DEBUG>( |
| 305 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 306 | file.path().string(), e.code().value()) |
| 307 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 308 | continue; |
| 309 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 310 | |
| 311 | const std::string& tempLabel = "label"; |
| 312 | const std::string filePathString = file.path().string().substr( |
| 313 | 0, file.path().string().length() - tempLabel.length()); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 314 | |
| 315 | uint32_t fruTypeValue{0}; |
| 316 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 317 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 318 | fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix); |
| 319 | } |
| 320 | catch (const std::system_error& e) |
| 321 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 322 | log<level::DEBUG>( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 323 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 324 | filePathString + fruTypeSuffix, e.code().value()) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 325 | .c_str()); |
| 326 | continue; |
| 327 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 328 | |
| 329 | std::string sensorPath = |
| 330 | OCC_SENSORS_ROOT + std::string("/temperature/"); |
| 331 | |
| 332 | if (fruTypeValue == VRMVdd) |
| 333 | { |
| 334 | sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp"); |
| 335 | } |
| 336 | else |
| 337 | { |
Matt Spinler | 14d1402 | 2021-08-25 15:38:29 -0500 | [diff] [blame] | 338 | uint16_t type = (labelValue & 0xFF000000) >> 24; |
| 339 | uint16_t instanceID = labelValue & 0x0000FFFF; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 340 | |
| 341 | if (type == OCC_DIMM_TEMP_SENSOR_TYPE) |
| 342 | { |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 343 | if (fruTypeValue == fruTypeNotAvailable) |
| 344 | { |
| 345 | // Not all DIMM related temps are available to read |
| 346 | // (no _input file in this case) |
| 347 | continue; |
| 348 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 349 | auto iter = dimmTempSensorName.find(fruTypeValue); |
| 350 | if (iter == dimmTempSensorName.end()) |
| 351 | { |
| 352 | log<level::ERR>(fmt::format("readTempSensors: Fru type " |
| 353 | "error! fruTypeValue = {}) ", |
| 354 | fruTypeValue) |
| 355 | .c_str()); |
| 356 | continue; |
| 357 | } |
| 358 | |
| 359 | sensorPath.append("dimm" + std::to_string(instanceID) + |
| 360 | iter->second); |
| 361 | } |
| 362 | else if (type == OCC_CPU_TEMP_SENSOR_TYPE) |
| 363 | { |
| 364 | if (fruTypeValue != processorCore) |
| 365 | { |
Matt Spinler | abbcfc5 | 2021-08-25 15:22:22 -0500 | [diff] [blame] | 366 | // TODO: support IO ring temp |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 367 | continue; |
| 368 | } |
| 369 | |
| 370 | sensorPath.append("proc" + std::to_string(id) + "_core" + |
| 371 | std::to_string(instanceID) + "_temp"); |
| 372 | } |
| 373 | else |
| 374 | { |
| 375 | continue; |
| 376 | } |
| 377 | } |
| 378 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 379 | uint32_t faultValue{0}; |
| 380 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 381 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 382 | faultValue = readFile<uint32_t>(filePathString + faultSuffix); |
| 383 | } |
| 384 | catch (const std::system_error& e) |
| 385 | { |
| 386 | log<level::DEBUG>( |
| 387 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 388 | filePathString + faultSuffix, e.code().value()) |
| 389 | .c_str()); |
| 390 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 391 | } |
| 392 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 393 | if (faultValue != 0) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 394 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 395 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 396 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 397 | |
| 398 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 399 | .setOperationalStatus(sensorPath, false); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 400 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 401 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 402 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 403 | |
| 404 | double tempValue{0}; |
| 405 | |
| 406 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 407 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 408 | tempValue = readFile<double>(filePathString + inputSuffix); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 409 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 410 | catch (const std::system_error& e) |
| 411 | { |
| 412 | log<level::DEBUG>( |
| 413 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 414 | filePathString + inputSuffix, e.code().value()) |
| 415 | .c_str()); |
| 416 | continue; |
| 417 | } |
| 418 | |
| 419 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
| 420 | sensorPath, tempValue * std::pow(10, -3)); |
| 421 | |
| 422 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
| 423 | .setOperationalStatus(sensorPath, true); |
| 424 | |
| 425 | existingSensors[sensorPath] = id; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 426 | } |
| 427 | return; |
| 428 | } |
| 429 | |
| 430 | std::optional<std::string> |
| 431 | Manager::getPowerLabelFunctionID(const std::string& value) |
| 432 | { |
| 433 | // If the value is "system", then the FunctionID is "system". |
| 434 | if (value == "system") |
| 435 | { |
| 436 | return value; |
| 437 | } |
| 438 | |
| 439 | // If the value is not "system", then the label value have 3 numbers, of |
| 440 | // which we only care about the middle one: |
| 441 | // <sensor id>_<function id>_<apss channel> |
| 442 | // eg: The value is "0_10_5" , then the FunctionID is "10". |
| 443 | if (value.find("_") == std::string::npos) |
| 444 | { |
| 445 | return std::nullopt; |
| 446 | } |
| 447 | |
| 448 | auto powerLabelValue = value.substr((value.find("_") + 1)); |
| 449 | |
| 450 | if (powerLabelValue.find("_") == std::string::npos) |
| 451 | { |
| 452 | return std::nullopt; |
| 453 | } |
| 454 | |
| 455 | return powerLabelValue.substr(0, powerLabelValue.find("_")); |
| 456 | } |
| 457 | |
| 458 | void Manager::readPowerSensors(const fs::path& path, uint32_t id) |
| 459 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 460 | std::regex expr{"power\\d+_label$"}; // Example: power5_label |
| 461 | for (auto& file : fs::directory_iterator(path)) |
| 462 | { |
| 463 | if (!std::regex_search(file.path().string(), expr)) |
| 464 | { |
| 465 | continue; |
| 466 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 467 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 468 | std::string labelValue; |
| 469 | try |
| 470 | { |
| 471 | labelValue = readFile<std::string>(file.path()); |
| 472 | } |
| 473 | catch (const std::system_error& e) |
| 474 | { |
| 475 | log<level::DEBUG>( |
| 476 | fmt::format("readPowerSensors: Failed reading {}, errno = {}", |
| 477 | file.path().string(), e.code().value()) |
| 478 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 479 | continue; |
| 480 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 481 | |
| 482 | auto functionID = getPowerLabelFunctionID(labelValue); |
| 483 | if (functionID == std::nullopt) |
| 484 | { |
| 485 | continue; |
| 486 | } |
| 487 | |
| 488 | const std::string& tempLabel = "label"; |
| 489 | const std::string filePathString = file.path().string().substr( |
| 490 | 0, file.path().string().length() - tempLabel.length()); |
| 491 | |
| 492 | std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/"); |
| 493 | |
| 494 | auto iter = powerSensorName.find(*functionID); |
| 495 | if (iter == powerSensorName.end()) |
| 496 | { |
| 497 | continue; |
| 498 | } |
| 499 | sensorPath.append(iter->second); |
| 500 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 501 | double tempValue{0}; |
| 502 | |
| 503 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 504 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 505 | tempValue = readFile<double>(filePathString + inputSuffix); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 506 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 507 | catch (const std::system_error& e) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 508 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 509 | log<level::DEBUG>( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 510 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 511 | filePathString + inputSuffix, e.code().value()) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 512 | .c_str()); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 513 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 514 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 515 | |
| 516 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
| 517 | sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3)); |
| 518 | |
| 519 | open_power::occ::dbus::OccDBusSensors::getOccDBus() |
| 520 | .setOperationalStatus(sensorPath, true); |
| 521 | |
| 522 | existingSensors[sensorPath] = id; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 523 | } |
| 524 | return; |
| 525 | } |
| 526 | |
| 527 | void Manager::setSensorValueToNaN(uint32_t id) |
| 528 | { |
| 529 | for (const auto& [sensorPath, occId] : existingSensors) |
| 530 | { |
| 531 | if (occId == id) |
| 532 | { |
| 533 | open_power::occ::dbus::OccDBusSensors::getOccDBus().setValue( |
| 534 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
| 535 | } |
| 536 | } |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | void Manager::getSensorValues(uint32_t id, bool masterOcc) |
| 541 | { |
| 542 | const auto occ = std::string("occ-hwmon.") + std::to_string(id + 1); |
| 543 | |
| 544 | fs::path fileName{OCC_HWMON_PATH + occ + "/hwmon/"}; |
| 545 | |
| 546 | // Need to get the hwmonXX directory name, there better only be 1 dir |
| 547 | assert(std::distance(fs::directory_iterator(fileName), |
| 548 | fs::directory_iterator{}) == 1); |
| 549 | // Now set our path to this full path, including this hwmonXX directory |
| 550 | fileName = fs::path(*fs::directory_iterator(fileName)); |
| 551 | |
| 552 | // Read temperature sensors |
| 553 | readTempSensors(fileName, id); |
| 554 | |
| 555 | if (masterOcc) |
| 556 | { |
| 557 | // Read power sensors |
| 558 | readPowerSensors(fileName, id); |
| 559 | } |
| 560 | |
| 561 | return; |
| 562 | } |
| 563 | #endif |
| 564 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 565 | } // namespace occ |
| 566 | } // namespace open_power |