Skip to content

Commit

Permalink
Fix runtime error if an entry in lscpu isn't an integer but - (or som…
Browse files Browse the repository at this point in the history
…ething else).
  • Loading branch information
breyerml committed Nov 27, 2024
1 parent 3b08108 commit bac9573
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
7 changes: 7 additions & 0 deletions include/hws/utility.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,13 @@ constexpr bool is_vector_v = is_vector<T>::value;
*/
[[nodiscard]] std::vector<std::string_view> split(std::string_view str, char delim = ' ');

/**
* @brief Check whether @p str is an integer
* @param[in] str the string to check
* @return `true` if @p str is an integer, `false` otherwise
*/
[[nodiscard]] bool is_integer(std::string_view str);

/**
* @brief Convert the @p str to a value of type @p T.
* @tparam T the type to convert the string to
Expand Down
10 changes: 5 additions & 5 deletions src/hws/cpu/hardware_sampler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,15 +78,15 @@ void cpu_hardware_sampler::sampling_loop() {
general_samples_.architecture_ = detail::convert_to<decltype(general_samples_.architecture_)::value_type>(value);
} else if (detail::starts_with(line, "Byte Order")) {
general_samples_.byte_order_ = detail::convert_to<decltype(general_samples_.byte_order_)::value_type>(value);
} else if (detail::starts_with(line, "CPU(s)")) {
} else if (detail::starts_with(line, "CPU(s)") && detail::is_integer(value)) {
general_samples_.num_threads_ = detail::convert_to<decltype(general_samples_.num_threads_)::value_type>(value);
} else if (detail::starts_with(line, "Thread(s) per core")) {
} else if (detail::starts_with(line, "Thread(s) per core") && detail::is_integer(value)) {
general_samples_.threads_per_core_ = detail::convert_to<decltype(general_samples_.threads_per_core_)::value_type>(value);
} else if (detail::starts_with(line, "Core(s) per socket")) {
} else if (detail::starts_with(line, "Core(s) per socket") && detail::is_integer(value)) {
general_samples_.cores_per_socket_ = detail::convert_to<decltype(general_samples_.cores_per_socket_)::value_type>(value);
} else if (detail::starts_with(line, "Socket(s)")) {
} else if (detail::starts_with(line, "Socket(s)") && detail::is_integer(value)) {
general_samples_.num_sockets_ = detail::convert_to<decltype(general_samples_.num_sockets_)::value_type>(value);
} else if (detail::starts_with(line, "NUMA node(s)")) {
} else if (detail::starts_with(line, "NUMA node(s)") && detail::is_integer(value)) {
general_samples_.numa_nodes_ = detail::convert_to<decltype(general_samples_.numa_nodes_)::value_type>(value);
} else if (detail::starts_with(line, "Vendor ID")) {
general_samples_.vendor_id_ = detail::convert_to<decltype(general_samples_.vendor_id_)::value_type>(value);
Expand Down
8 changes: 6 additions & 2 deletions src/hws/utility.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

#include "hws/utility.hpp"

#include <algorithm> // std::min, std::transform
#include <cctype> // std::tolower
#include <algorithm> // std::min, std::transform, std::all_of
#include <cctype> // std::tolower, std::isdigit
#include <string> // std::string
#include <string_view> // std::string_view
#include <vector> // std::vector
Expand Down Expand Up @@ -57,4 +57,8 @@ std::vector<std::string_view> split(const std::string_view str, const char delim
return split_str;
}

bool is_integer(std::string_view str) {
return std::all_of(str.cbegin(), str.cend(), [](const char c) { return std::isdigit(static_cast<unsigned char>(c)); });
}

} // namespace hws::detail

0 comments on commit bac9573

Please sign in to comment.