Skip to content

Commit

Permalink
Added program to dump system info we can determine for use with testi…
Browse files Browse the repository at this point in the history
…ng, added api for detecting virt vendor.
  • Loading branch information
JonathanHenson committed Oct 5, 2023
1 parent d09b75e commit 43d1b92
Show file tree
Hide file tree
Showing 8 changed files with 166 additions and 2 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ else ()
# Don't add the exact path to CoreFoundation as this would hardcode the SDK version
list(APPEND PLATFORM_LIBS dl Threads::Threads "-framework CoreFoundation")
list (APPEND AWS_COMMON_OS_SRC "source/darwin/*.c") # OS specific includes
list (APPEND AWS_COMMON_OS_SRC "source/linux/*.c") # just for the moment

elseif (${CMAKE_SYSTEM_NAME} STREQUAL "Linux") # Android does not link to libpthread nor librt, so this is fine
list(APPEND PLATFORM_LIBS dl m Threads::Threads rt)
list (APPEND AWS_COMMON_OS_SRC "source/linux/*.c") # OS specific includes
Expand Down Expand Up @@ -298,6 +300,7 @@ configure_file(${CONFIG_HEADER_TEMPLATE}
if (ALLOW_CROSS_COMPILED_TESTS OR NOT CMAKE_CROSSCOMPILING)
if (BUILD_TESTING)
add_subdirectory(tests)
add_subdirectory(bin/system_info)
endif()
endif()

Expand Down
25 changes: 25 additions & 0 deletions bin/system_info/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
project(print-sys-info C)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_INSTALL_PREFIX}/lib/cmake")

file(GLOB SI_SRC
"*.c"
)

set(SI_PROJECT_NAME print-sys-info)
add_executable(${SI_PROJECT_NAME} ${SI_SRC})
aws_set_common_properties(${SI_PROJECT_NAME})


target_include_directories(${SI_PROJECT_NAME} PUBLIC
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
$<INSTALL_INTERFACE:include>)

target_link_libraries(${SI_PROJECT_NAME} PRIVATE aws-c-common)

install(TARGETS ${SI_PROJECT_NAME}
EXPORT ${SI_PROJECT_NAME}-targets
COMPONENT Runtime
RUNTIME
DESTINATION bin
COMPONENT Runtime)
28 changes: 28 additions & 0 deletions bin/system_info/print_system_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@



#include <aws/common/system_info.h>
#include <aws/common/byte_buf.h>

int main(void) {
struct aws_allocator *allocator = aws_default_allocator();
struct aws_system_environment *env = aws_system_environment_load(allocator);

fprintf(stdout, "crt-detected env: {\n");

struct aws_byte_cursor virtualization_vendor = aws_system_environment_get_virtualization_vendor(env);
fprintf(stdout, " 'virtualization vendor': '" PRInSTR "'\n", (int)virtualization_vendor.len, virtualization_vendor.ptr);
fprintf(stdout, " 'number of processors': '%lu'\n", (unsigned long)aws_system_environment_get_processor_count(env));
size_t numa_nodes = aws_system_environment_get_cpu_group_count(env);

if (numa_nodes > 1) {
fprintf(stdout, " 'numa architecture': 'true'\n");
fprintf(stdout, " 'number of numa nodes': '%lu'\n", (unsigned long)numa_nodes);
} else {
fprintf(stdout, " 'numa architecture': 'false'\n");
}

fprintf(stdout, "}\n");
aws_system_environment_destroy(env);
return 0;
}
27 changes: 27 additions & 0 deletions include/aws/common/private/system_info_priv.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#ifndef AWS_COMMON_SYSTEM_INFO_PRIV_H
#define AWS_COMMON_SYSTEM_INFO_PRIV_H
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/

#include <aws/common/system_info.h>
#include <aws/common/byte_buf.h>
#include <aws/common/string.h>

struct aws_system_environment {
struct aws_allocator *allocator;
struct aws_byte_buf virtualization_vendor;
enum aws_platform_os os;
size_t cpu_count;
size_t cpu_group_count;
void *additional_impl_data;
};

int aws_system_environment_load_platform_impl(struct aws_system_environment *env);
void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env);


void aws_system_environment_load_virtualization_vendor_impl(struct aws_system_environment *env);

#endif /* AWS_COMMON_SYSTEM_INFO_PRIV_H */
18 changes: 18 additions & 0 deletions include/aws/common/system_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,26 @@ struct aws_cpu_info {
bool suspected_hyper_thread;
};

struct aws_system_environment;

AWS_EXTERN_C_BEGIN

AWS_COMMON_API
struct aws_system_environment *aws_system_environment_load(struct aws_allocator *allocator);

AWS_COMMON_API
void aws_system_environment_destroy(struct aws_system_environment *env);


AWS_COMMON_API
struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(struct aws_system_environment *env);

AWS_COMMON_API
size_t aws_system_environment_get_processor_count(struct aws_system_environment *env);

AWS_COMMON_API
size_t aws_system_environment_get_cpu_group_count(struct aws_system_environment *env);

/* Returns the OS this was built under */
AWS_COMMON_API
enum aws_platform_os aws_get_platform_build_os(void);
Expand Down
17 changes: 17 additions & 0 deletions source/linux/system_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

#include <aws/common/private/system_info_priv.h>
#include <aws/common/file.h>

int aws_system_environment_load_platform_impl(struct aws_system_environment *env) {
(void)env;

return AWS_OP_SUCCESS;
}

void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env) {
(void)env;
}

void aws_system_environment_load_virtualization_vendor_impl(struct aws_system_environment *env) {
aws_byte_buf_init_from_file(&env->virtualization_vendor, env->allocator, "/sys/devices/virtual/dmi/id/sys_vendor");
}
46 changes: 46 additions & 0 deletions source/system_info.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/**
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0.
*/
#include <aws/common/private/system_info_priv.h>

struct aws_system_environment *aws_system_environment_load(struct aws_allocator *allocator) {
struct aws_system_environment *env = aws_mem_calloc(allocator, 1, sizeof(struct aws_system_environment));
env->allocator = allocator;

if (aws_system_environment_load_platform_impl(env)) {
goto error;
}

aws_system_environment_load_virtualization_vendor_impl(env);
env->os = aws_get_platform_build_os();
env->cpu_count = aws_system_info_processor_count();
env->cpu_group_count = aws_get_cpu_group_count();

return env;
error:
aws_mem_release(allocator, env);

return NULL;
}

void aws_system_environment_destroy(struct aws_system_environment *env) {
if (env) {
aws_system_environment_destroy_platform_impl(env);
aws_mem_release(env->allocator, env);
}

}

struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(struct aws_system_environment *env) {
return aws_byte_cursor_from_buf(&env->virtualization_vendor);
}

size_t aws_system_environment_get_processor_count(struct aws_system_environment *env) {
return env->cpu_count;
}

AWS_COMMON_API
size_t aws_system_environment_get_cpu_group_count(struct aws_system_environment *env) {
return env->cpu_group_count;
}
4 changes: 2 additions & 2 deletions tests/system_info_tests.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ static int s_test_cpu_count_at_least_works_superficially_fn(struct aws_allocator
(void)allocator;
(void)ctx;

size_t processor_count = aws_system_info_processor_count();
size_t processor_count = aws_system_environment_get_processor_count();
/* I think this is a fairly reasonable assumption given the circumstances
* (you know this test is part of a program
* that must be running on at least one core).... */
Expand Down Expand Up @@ -119,7 +119,7 @@ static int s_test_sanity_check_numa_discovery(struct aws_allocator *allocator, v
(void)ctx;

aws_common_library_init(allocator);
size_t processor_count = aws_system_info_processor_count();
size_t processor_count = aws_system_environment_get_processor_count();
ASSERT_TRUE(processor_count > 0);

uint16_t group_count = aws_get_cpu_group_count();
Expand Down

0 comments on commit 43d1b92

Please sign in to comment.