-
Notifications
You must be signed in to change notification settings - Fork 160
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add an environment loader API and implementation for some DMI related…
… fields (#1063) Co-authored-by: Michael Graeb <[email protected]>
- Loading branch information
1 parent
0baed28
commit b61c12e
Showing
10 changed files
with
304 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
|
||
|
||
#include <aws/common/byte_buf.h> | ||
#include <aws/common/logging.h> | ||
#include <aws/common/system_info.h> | ||
|
||
int main(void) { | ||
struct aws_allocator *allocator = aws_default_allocator(); | ||
aws_common_library_init(allocator); | ||
struct aws_logger_standard_options options = { | ||
.file = stderr, | ||
.level = AWS_LOG_LEVEL_TRACE, | ||
}; | ||
|
||
struct aws_logger logger; | ||
aws_logger_init_standard(&logger, allocator, &options); | ||
aws_logger_set(&logger); | ||
|
||
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); | ||
struct aws_byte_cursor product_name = aws_system_environment_get_virtualization_product_name(env); | ||
fprintf(stdout, " 'product name': '" PRInSTR "',\n", (int)product_name.len, product_name.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_release(env); | ||
aws_logger_clean_up(&logger); | ||
|
||
aws_common_library_clean_up(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
#ifndef AWS_COMMON_PRIVATE_SYSTEM_INFO_PRIV_H | ||
#define AWS_COMMON_PRIVATE_SYSTEM_INFO_PRIV_H | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#include <aws/common/byte_buf.h> | ||
#include <aws/common/ref_count.h> | ||
#include <aws/common/string.h> | ||
#include <aws/common/system_info.h> | ||
|
||
struct aws_system_environment { | ||
struct aws_allocator *allocator; | ||
struct aws_ref_count ref_count; | ||
struct aws_byte_buf virtualization_vendor; | ||
struct aws_byte_buf product_name; | ||
enum aws_platform_os os; | ||
size_t cpu_count; | ||
size_t cpu_group_count; | ||
void *impl; | ||
}; | ||
|
||
/** | ||
* For internal implementors. Fill in info in env that you're able to grab, such as dmi info, os version strings etc... | ||
* in here. The default just returns AWS_OP_SUCCESS. This is currently only implemented for linux. | ||
* | ||
* Returns AWS_OP_ERR if the implementation wasn't able to fill in required information for the platform. | ||
*/ | ||
int aws_system_environment_load_platform_impl(struct aws_system_environment *env); | ||
|
||
/** | ||
* For internal implementors. Cleans up anything allocated in aws_system_environment_load_platform_impl, | ||
* but does not release the memory for env. | ||
*/ | ||
void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env); | ||
|
||
#endif // AWS_COMMON_PRIVATE_SYSTEM_INFO_PRIV_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#include <aws/common/file.h> | ||
#include <aws/common/private/system_info_priv.h> | ||
|
||
int aws_system_environment_load_platform_impl(struct aws_system_environment *env) { | ||
/* provide size_hint when reading "special files", since some platforms mis-report these files' size as 4KB */ | ||
aws_byte_buf_init_from_file_with_size_hint( | ||
&env->virtualization_vendor, env->allocator, "/sys/devices/virtual/dmi/id/sys_vendor", 32 /*size_hint*/); | ||
|
||
/* whether this one works depends on if this is a sysfs filesystem. If it fails, it will just be empty | ||
* and these APIs are a best effort at the moment. We can add fallbacks as the loaders get more complicated. */ | ||
aws_byte_buf_init_from_file_with_size_hint( | ||
&env->product_name, env->allocator, "/sys/devices/virtual/dmi/id/product_name", 32 /*size_hint*/); | ||
|
||
return AWS_OP_SUCCESS; | ||
} | ||
|
||
void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env) { | ||
aws_byte_buf_clean_up(&env->virtualization_vendor); | ||
aws_byte_buf_clean_up(&env->product_name); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#include <aws/common/private/system_info_priv.h> | ||
|
||
#include <aws/common/logging.h> | ||
|
||
int aws_system_environment_load_platform_impl(struct aws_system_environment *env) { | ||
(void)env; | ||
AWS_LOGF_DEBUG( | ||
AWS_LS_COMMON_GENERAL, | ||
"id=%p: platform specific environment loading is not implemented for this platform.", | ||
(void *)env); | ||
|
||
return AWS_OP_SUCCESS; | ||
} | ||
|
||
void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env) { | ||
(void)env; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
/** | ||
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
* SPDX-License-Identifier: Apache-2.0. | ||
*/ | ||
#include <aws/common/private/system_info_priv.h> | ||
|
||
#include <aws/common/logging.h> | ||
|
||
void s_destroy_env(void *arg) { | ||
struct aws_system_environment *env = arg; | ||
|
||
if (env) { | ||
aws_system_environment_destroy_platform_impl(env); | ||
aws_mem_release(env->allocator, env); | ||
} | ||
} | ||
|
||
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; | ||
aws_ref_count_init(&env->ref_count, env, s_destroy_env); | ||
|
||
if (aws_system_environment_load_platform_impl(env)) { | ||
AWS_LOGF_ERROR( | ||
AWS_LS_COMMON_GENERAL, | ||
"id=%p: failed to load system environment with error %s.", | ||
(void *)env, | ||
aws_error_debug_str(aws_last_error())); | ||
goto error; | ||
} | ||
|
||
AWS_LOGF_TRACE( | ||
AWS_LS_COMMON_GENERAL, | ||
"id=%p: virtualization vendor detected as \"" PRInSTR "\"", | ||
(void *)env, | ||
AWS_BYTE_CURSOR_PRI(aws_system_environment_get_virtualization_vendor(env))); | ||
AWS_LOGF_TRACE( | ||
AWS_LS_COMMON_GENERAL, | ||
"id=%p: virtualization product name detected as \"" PRInSTR " \"", | ||
(void *)env, | ||
AWS_BYTE_CURSOR_PRI(aws_system_environment_get_virtualization_vendor(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: | ||
s_destroy_env(env); | ||
return NULL; | ||
} | ||
|
||
struct aws_system_environment *aws_system_environment_acquire(struct aws_system_environment *env) { | ||
aws_ref_count_acquire(&env->ref_count); | ||
return env; | ||
} | ||
|
||
void aws_system_environment_release(struct aws_system_environment *env) { | ||
aws_ref_count_release(&env->ref_count); | ||
} | ||
|
||
struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(const struct aws_system_environment *env) { | ||
struct aws_byte_cursor vendor_string = aws_byte_cursor_from_buf(&env->virtualization_vendor); | ||
return aws_byte_cursor_trim_pred(&vendor_string, aws_char_is_space); | ||
} | ||
|
||
struct aws_byte_cursor aws_system_environment_get_virtualization_product_name( | ||
const struct aws_system_environment *env) { | ||
struct aws_byte_cursor product_name_str = aws_byte_cursor_from_buf(&env->product_name); | ||
return aws_byte_cursor_trim_pred(&product_name_str, aws_char_is_space); | ||
} | ||
|
||
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(const struct aws_system_environment *env) { | ||
return env->cpu_group_count; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters