From f76f989b53645136a5e83a9a1a9ab1335e9a2cbf Mon Sep 17 00:00:00 2001 From: ltrk2 <107155950+ltrk2@users.noreply.github.com> Date: Wed, 24 May 2023 13:33:05 +0000 Subject: [PATCH] Implement a uniform way to query processor core IDs --- src/Common/AsynchronousMetrics.cpp | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Common/AsynchronousMetrics.cpp b/src/Common/AsynchronousMetrics.cpp index ac2180103c5..3753aaca405 100644 --- a/src/Common/AsynchronousMetrics.cpp +++ b/src/Common/AsynchronousMetrics.cpp @@ -1041,18 +1041,16 @@ void AsynchronousMetrics::update(TimePoint update_time) // It doesn't read the EOL itself. ++cpuinfo->position(); - if (s.rfind("processor", 0) == 0) + static constexpr std::string_view PROCESSOR = "processor"; + if (s.starts_with(PROCESSOR)) { /// s390x example: processor 0: version = FF, identification = 039C88, machine = 3906 /// non s390x example: processor : 0 - if (auto colon = s.find_first_of(':')) - { -#ifdef __s390x__ - core_id = std::stoi(s.substr(10)); /// 10: length of "processor" plus 1 -#else - core_id = std::stoi(s.substr(colon + 2)); -#endif - } + auto core_id_start = std::ssize(PROCESSOR); + while (core_id_start < std::ssize(s) && !std::isdigit(s[core_id_start])) + ++core_id_start; + + core_id = std::stoi(s.substr(core_id_start)); } else if (s.rfind("cpu MHz", 0) == 0) {