From 262682632ee493d0b6593540cfc902d11286b7c3 Mon Sep 17 00:00:00 2001 From: Iwona Winiarska Date: Tue, 13 Jul 2021 15:16:16 +0200 Subject: [PATCH] CPUSensor: Create CPUConfig for each PECI adapter Currently CPUSensor is based on configuration that defines CPUs for the specific PECI adapter (usually peci-wire). As a consequence, MFD devices are created only for the defined adapter. Since duplicating static CPU records in configuration file for other PECI adapters may be confusing to users, let's add CPUConfig for other PECI adapters dynamically by detecting if there is more than one PECI adapter driver bound in the system. Please note, that this change is limited to enabling HWMON driver, sensors for all adapters will not be exposed on D-Bus. Tested: No changes if only one PECI interface is available, HWMON drivers creation will be based on provided configuration. When both peci-aspeed (peci-wire) and peci-mctp are bound in the system, HWMON drivers are created for each PECI interface. Signed-off-by: Iwona Winiarska --- src/CPUSensorMain.cpp | 90 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) diff --git a/src/CPUSensorMain.cpp b/src/CPUSensorMain.cpp index 744ca50..6850df7 100644 --- a/src/CPUSensorMain.cpp +++ b/src/CPUSensorMain.cpp @@ -33,6 +33,7 @@ #include #include #include +#include #include #include #include @@ -658,6 +659,91 @@ void detectCpuAsync( }); } +std::optional getPeciDeviceNum(const fs::path& peciAdapterNamePath) +{ + fs::path::iterator it = peciAdapterNamePath.begin(); + std::advance(it, 5); // /sys/bus/peci/devices/peci-xxxx + std::string peciDeviceName = *it; + auto pos = peciDeviceName.find('-'); + if (pos == std::string::npos) + { + std::cerr << "Incorrect PECI device name: " << peciDeviceName << "\n"; + return std::nullopt; + } + + try + { + return std::stoull(peciDeviceName.substr(pos + 1, 1)); + } + catch (std::logic_error&) + { + return std::nullopt; + } +} + +std::optional + readPeciAdapterNameFromFile(const fs::path& peciAdapterNamePath) +{ + std::ifstream nameFile(peciAdapterNamePath); + if (!nameFile.good()) + { + std::cerr << "Cannot read: " << peciAdapterNamePath << "\n"; + return std::nullopt; + } + + std::string peciAdapterName; + std::getline(nameFile, peciAdapterName); + nameFile.close(); + if (peciAdapterName.empty()) + { + return std::nullopt; + } + + auto pos = peciAdapterName.find('-'); + peciAdapterName = peciAdapterName.substr(pos + 1); + + return peciAdapterName; +} + +void addConfigsForOtherPeciAdapters( + boost::container::flat_set& cpuConfigs, uint64_t& bus, + uint64_t& addr, std::string& name, const State& state) +{ + std::vector peciAdapterNamePaths; + if (!findFiles(fs::path(peciDevPath), R"(peci-\d+/name$)", + peciAdapterNamePaths, 1)) + { + std::cerr << "No PECI adapters in system\n"; + return; + } + + for (const fs::path& peciAdapterNamePath : peciAdapterNamePaths) + { + std::optional peciDeviceNum = + getPeciDeviceNum(peciAdapterNamePath); + if (!peciDeviceNum || peciDeviceNum == bus) + { + continue; + } + + std::optional peciAdapterName = + readPeciAdapterNameFromFile(peciAdapterNamePath); + if (!peciAdapterName) + { + continue; + } + + // Change result for peci-aspeed + if (peciAdapterName->compare("bus") == 0) + { + peciAdapterName = "wire"; + } + + cpuConfigs.emplace(*peciDeviceNum, addr, name + "_" + *peciAdapterName, + state); + } +} + bool getCpuConfig(std::shared_ptr& systemBus, boost::container::flat_set& cpuConfigs, ManagedObjectType& sensorConfigs, boost::asio::io_service& io, @@ -789,7 +875,11 @@ bool getCpuConfig(std::shared_ptr& systemBus, std::cout << "name: " << name << "\n"; std::cout << "type: " << type << "\n"; } + cpuConfigs.emplace(bus, addr, name, State::OFF); + + addConfigsForOtherPeciAdapters(cpuConfigs, bus, addr, name, + State::OFF); } } } -- 2.31.1