Skip to content

Commit

Permalink
Add regex parsing for xeon cpu names
Browse files Browse the repository at this point in the history
  • Loading branch information
Steffen Winter committed Aug 11, 2024
1 parent 00c9088 commit 38f9cf0
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
2 changes: 1 addition & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ elseif(CMAKE_SYSTEM_NAME STREQUAL "OpenBSD")
elseif(CMAKE_SYSTEM_NAME STREQUAL "NetBSD")
target_sources(btop PRIVATE src/netbsd/btop_collect.cpp)
elseif(LINUX)
target_sources(btop PRIVATE src/linux/btop_collect.cpp)
target_sources(btop PRIVATE src/linux/btop_collect.cpp src/linux/parse_cpu_names.cpp)
else()
message(FATAL_ERROR "${CMAKE_SYSTEM_NAME} is not supported")
endif()
Expand Down
6 changes: 6 additions & 0 deletions src/linux/btop_collect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ tab-size = 4
#include <pwd.h>
#endif

#include "parse_cpu_names.hpp"
#include "../btop_shared.hpp"
#include "../btop_config.hpp"
#include "../btop_tools.hpp"
Expand Down Expand Up @@ -352,6 +353,11 @@ namespace Cpu {

}

auto xeon_name = parse_xeon_name(name);
if (xeon_name) {
return xeon_name.value();
}

auto name_vec = ssplit(name, ' ');

if ((s_contains(name, "Xeon"s) or v_contains(name_vec, "Duo"s)) and v_contains(name_vec, "CPU"s)) {
Expand Down
21 changes: 21 additions & 0 deletions src/linux/parse_cpu_names.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: Apache-2.0

#include "parse_cpu_names.hpp"

#include <cstddef>
#include <optional>
#include <regex>
#include <string>

auto parse_xeon_name(const std::string& name) -> std::optional<std::string> {
std::regex regex{R"((?:\S+\(R\) ?)+ ([a-zA-Z0-9\- ]+[^ ])? ?CPU ([a-zA-Z0-9\- ]+[^ ])? ?(?:@ \d\.\d\dGHz))"};
std::smatch match{};
if (std::regex_match(name, match, regex)) {
for (std::size_t i = 1; i < match.size(); ++i) {
if (!match[i].str().empty()) {
return match[i].str();
}
}
}
return std::nullopt;
}
8 changes: 8 additions & 0 deletions src/linux/parse_cpu_names.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <optional>
#include <string>

auto parse_xeon_name(const std::string& name) -> std::optional<std::string>;

0 comments on commit 38f9cf0

Please sign in to comment.