Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Xeon cpu names not being displayed properly #910

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 6 additions & 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 Expand Up @@ -241,6 +241,11 @@ else()
message(WARNING "Command 'lowdown' not found: skipping generating man page btop.1")
endif()

include(CTest)
if(BUILD_TESTING)
add_subdirectory(tests)
endif()

install(TARGETS btop RUNTIME)
install(FILES "btop.desktop" DESTINATION "share/applications")
install(FILES "Img/icon.png" DESTINATION "share/icons/hicolor/48x48/apps" RENAME "btop.png")
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>;
6 changes: 6 additions & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# SPDX-License-Identifier: Apache-2.0

include_directories(${PROJECT_SOURCE_DIR}/src)

add_executable(test_parse_xeon_name test_parse_xeon_name.cpp ${PROJECT_SOURCE_DIR}/src/linux/parse_cpu_names.cpp)
add_test(parse_xeon_name test_parse_xeon_name)
13 changes: 13 additions & 0 deletions tests/cpu_names.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// SPDX-License-Identifier: Apache-2.0

#pragma once

#include <string>

const static std::string xeon_E5_2623_v3 = "Intel(R) Xeon(R) CPU E5-2623 v3 @ 3.00GHz";
const static std::string xeon_gold_6240 = "Intel(R) Xeon(R) Gold 6240 CPU @ 2.60GHz";
const static std::string xeon_gold_6338N = "Intel(R) Xeon(R) Gold 6338N CPU @ 2.20GHz";

const static std::string core_i9_13900H = "13th Gen Intel(R) Core(TM) i9-13900H";

const static std::string pentium_III = "Intel(R) Pentium(R) III CPU family 1400MHz";
27 changes: 27 additions & 0 deletions tests/test_parse_xeon_name.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// SPDX-License-Identifier: Apache-2.0

// TODO: Use GTest or Catch or something
#undef NDEBUG
#include <cassert>
#include <optional>
#include <string>

#include "cpu_names.hpp"
#include "linux/parse_cpu_names.hpp"

auto main() -> int {
auto result = parse_xeon_name(xeon_E5_2623_v3);
assert(result.value() == "E5-2623 v3");

result = parse_xeon_name(xeon_gold_6240);
assert(result.value() == "Gold 6240");

result = parse_xeon_name(xeon_gold_6338N);
assert(result.value() == "Gold 6338N");

result = parse_xeon_name(core_i9_13900H);
assert(result == std::nullopt);

result = parse_xeon_name(pentium_III);
assert(result == std::nullopt);
}
Loading