feat: first working set

This commit is contained in:
Tobias Schmidl 2025-06-16 21:22:22 +02:00
parent cccebf674a
commit bed7360063
5 changed files with 59 additions and 42 deletions

View file

@ -4,22 +4,39 @@
#pragma once #pragma once
#include <cstdint>
#include <map>
#include <string> #include <string>
namespace glimpses { #include "glimpses/version.h"
/** namespace glimpses
* @brief A class for saying hello in multiple languages
*/
class Glimpses
{ {
/** @brief A class for saying retrieving system information */
class Glimpses
{
public: public:
/** /** @brief Type for representing the number of CPU cores. */
* @brief Creates a new glimpses using CPUCount = uint32_t;
* @param name the name to greet
*/ /** @brief Type for representing a set of CPU cores, mapped by their names. */
using CPUSet = std::map<std::string, CPUCount>;
/** @brief Maximum number of CPU cores supported by Glimpses. */
constexpr static CPUCount MAX_CPU_COUNT = 1024;
/** @brief Version of the Glimpses library. */
constexpr static const char *VERSION = GLIMPSES_VERSION;
/** @brief Creates a new glimpses */
Glimpses(); Glimpses();
/**
* @brief Returns the number of CPU cores available on the system.
* @return The number of CPU cores available on the system.
*/
CPUSet getCPUCount() const;
}; };
} // namespace glimpses } // namespace glimpses

View file

@ -3,7 +3,25 @@
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
#include <glimpses/glimpses.hpp> #include <glimpses/glimpses.hpp>
#include <sched.h>
#include <thread>
#include <unistd.h>
using namespace glimpses; using namespace glimpses;
Glimpses::Glimpses() {} Glimpses::Glimpses()
{
}
Glimpses::CPUSet Glimpses::getCPUCount() const
{
cpu_set_t cpuset;
auto minmax = [](int64_t min, int64_t value, int64_t max) -> CPUCount {
return std::min(std::max(value, min), max);
};
sched_getaffinity(0, sizeof(cpuset), &cpuset);
return CPUSet{{"configured", minmax(0, sysconf(_SC_NPROCESSORS_CONF), MAX_CPU_COUNT)},
{"logical", minmax(0, std::thread::hardware_concurrency(), MAX_CPU_COUNT)},
{"online", minmax(0, sysconf(_SC_NPROCESSORS_ONLN), MAX_CPU_COUNT)},
{"allowed", minmax(0, CPU_COUNT(&cpuset), MAX_CPU_COUNT)}};
}

View file

@ -18,8 +18,8 @@ include(../cmake/tools.cmake)
include(../cmake/get_cpm.cmake) include(../cmake/get_cpm.cmake)
# CPMAddPackage("gh:doctest/doctest@2.4.9") CPMAddPackage(NAME Catch2 GITHUB_REPOSITORY catchorg/Catch2 VERSION 3.8.1)
CPMAddPackage("gh:TheLartians/Format.cmake@1.7.3") CPMAddPackage("gh:TheLartians/Format.cmake@1.8.3")
if(TEST_INSTALLED_VERSION) if(TEST_INSTALLED_VERSION)
find_package(Glimpses REQUIRED) find_package(Glimpses REQUIRED)
@ -31,11 +31,7 @@ endif()
file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp) file(GLOB sources CONFIGURE_DEPENDS ${CMAKE_CURRENT_SOURCE_DIR}/source/*.cpp)
add_executable(${PROJECT_NAME} ${sources}) add_executable(${PROJECT_NAME} ${sources})
target_link_libraries( target_link_libraries(${PROJECT_NAME} Catch2::Catch2WithMain Glimpses::Glimpses)
${PROJECT_NAME}
# doctest::doctest
Glimpses::Glimpses
)
set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17) set_target_properties(${PROJECT_NAME} PROPERTIES CXX_STANDARD 17)
# enable compiler warnings # enable compiler warnings
@ -52,6 +48,9 @@ endif()
enable_testing() enable_testing()
include(CTest)
add_test(GlimpsesTests GlimpsesTests)
# Note: doctest and similar testing frameworks can automatically configure CMake tests. For other testing frameworks add the tests target # Note: doctest and similar testing frameworks can automatically configure CMake tests. For other testing frameworks add the tests target
# instead: add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME}) # instead: add_test(NAME ${PROJECT_NAME} COMMAND ${PROJECT_NAME})

View file

@ -2,24 +2,21 @@
// //
// SPDX-License-Identifier: AGPL-3.0-or-later // SPDX-License-Identifier: AGPL-3.0-or-later
#include <catch2/catch_test_macros.hpp>
#include <glimpses/glimpses.hpp> #include <glimpses/glimpses.hpp>
#include <glimpses/version.h> #include <glimpses/version.h>
#include <string> #include <string>
/*
TEST_CASE("Glimpses") {
using namespace greeter;
Glimpses greeter("Tests"); TEST_CASE("CPU count")
{
CHECK(greeter.greet(LanguageCode::EN) == "Hello, Tests!"); glimpses::Glimpses glimpses{};
CHECK(greeter.greet(LanguageCode::DE) == "Hallo Tests!");
CHECK(greeter.greet(LanguageCode::ES) == "¡Hola Tests!"); CHECK(glimpses.getCPUCount().size() > 0);
CHECK(greeter.greet(LanguageCode::FR) == "Bonjour Tests!");
} }
TEST_CASE("Glimpses version") { /*
static_assert(std::string_view(GREETER_VERSION) == std::string_view("1.0")); TEST_CASE("Glimpses version") { static_assert(std::string_view(GREETER_VERSION) == std::string_view("0.1.0"));
CHECK(std::string(GREETER_VERSION) == std::string("1.0")); CHECK(std::string(GREETER_VERSION) == std::string("1.0"));
} }
*/ */

View file

@ -1,14 +0,0 @@
// SPDX-FileCopyrightText: 2023 Tobias Schmidl
//
// SPDX-License-Identifier: AGPL-3.0-or-later
#if 0
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
#include <doctest/doctest.h>
#endif
int
main()
{
return 0;
}