From 0b207a624f925460797a51974b77b275d4c05e30 Mon Sep 17 00:00:00 2001 From: Lei YU Date: Wed, 20 Oct 2021 13:41:51 +0800 Subject: [PATCH] cpusensor: Search in peci-0 directory The code was searching /sys/bus/peci/devices for PECI sensors. This results in below directories to be searched on typical 2 socket x86 systems. * peci-0 * 0-30 * 0-31 Where 0-30 and 0-31 exist in peci-0 as well. It is unnecessary, and the files in 0-30 and 0-31 will be searched twice because they exist in peci-0. Change the code to search in /sys/bus/peci/devices/peci-0 to make sure it only search the files within the expected directory. Note that the current peci driver only create peci-0, this code could be easily extended to support peci-1/2/3 in the future. Tested: The time for cpusensor to search the peci sensors reduces from 0.5s to about 0.3s. Signed-off-by: Lei YU Change-Id: I7b0f3ad7bea7a3dced13a985f0ad19ffda33f6d2 --- src/CPUSensorMain.cpp | 5 ++--- tests/test_Utils.cpp | 18 ++++++++++++++++++ 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/src/CPUSensorMain.cpp b/src/CPUSensorMain.cpp index a35273521..9e991e741 100644 --- a/src/CPUSensorMain.cpp +++ b/src/CPUSensorMain.cpp @@ -168,9 +168,8 @@ bool createSensors(boost::asio::io_service& io, } std::vector hwmonNamePaths; - if (!findFiles(fs::path(R"(/sys/bus/peci/devices)"), - R"(peci-\d+/\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", - hwmonNamePaths, 6)) + if (!findFiles(fs::path(R"(/sys/bus/peci/devices/peci-0)"), + R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", hwmonNamePaths, 5)) { std::cerr << "No CPU sensors in system\n"; return true; diff --git a/tests/test_Utils.cpp b/tests/test_Utils.cpp index 170e22d79..6f76933a2 100644 --- a/tests/test_Utils.cpp +++ b/tests/test_Utils.cpp @@ -145,3 +145,21 @@ TEST_F(TestUtils, findFiles_peciPath_end_with_slash) EXPECT_TRUE(ret); EXPECT_EQ(foundPaths.size(), 3u); } + +TEST_F(TestUtils, findFiles_in_sub_peci_match) +{ + std::vector foundPaths; + auto ret = + findFiles(peciDir / "peci-0", R"(\d+-.+/peci-.+/hwmon/hwmon\d+/name$)", + foundPaths, 5); + EXPECT_TRUE(ret); + EXPECT_EQ(foundPaths.size(), 1u); + + foundPaths.clear(); + + ret = findFiles(peciDir / "peci-0", + R"(\d+-.+/peci-.+/hwmon/hwmon\d+/temp\d+_input)", + foundPaths, 5); + EXPECT_TRUE(ret); + EXPECT_EQ(foundPaths.size(), 3u); +}