| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#include <aws/common/private/system_info_priv.h> |
| 6 |
|
| 7 |
#include <aws/common/logging.h> |
| 8 |
|
| 9 |
void s_destroy_env(void *arg) { |
| 10 |
struct aws_system_environment *env = arg; |
| 11 |
|
| 12 |
if (env) { |
| 13 |
aws_system_environment_destroy_platform_impl(env); |
| 14 |
aws_mem_release(env->allocator, env); |
| 15 |
} |
| 16 |
} |
| 17 |
|
| 18 |
struct aws_system_environment *aws_system_environment_load(struct aws_allocator *allocator) { |
| 19 |
struct aws_system_environment *env = aws_mem_calloc(allocator, 1, sizeof(struct aws_system_environment)); |
| 20 |
env->allocator = allocator; |
| 21 |
aws_ref_count_init(&env->ref_count, env, s_destroy_env); |
| 22 |
|
| 23 |
if (aws_system_environment_load_platform_impl(env)) { |
| 24 |
AWS_LOGF_ERROR( |
| 25 |
AWS_LS_COMMON_GENERAL, |
| 26 |
"id=%p: failed to load system environment with error %s.", |
| 27 |
(void *)env, |
| 28 |
aws_error_debug_str(aws_last_error())); |
| 29 |
goto error; |
| 30 |
} |
| 31 |
|
| 32 |
AWS_LOGF_TRACE( |
| 33 |
AWS_LS_COMMON_GENERAL, |
| 34 |
"id=%p: virtualization vendor detected as \"" PRInSTR "\"", |
| 35 |
(void *)env, |
| 36 |
AWS_BYTE_CURSOR_PRI(aws_system_environment_get_virtualization_vendor(env))); |
| 37 |
AWS_LOGF_TRACE( |
| 38 |
AWS_LS_COMMON_GENERAL, |
| 39 |
"id=%p: virtualization product name detected as \"" PRInSTR " \"", |
| 40 |
(void *)env, |
| 41 |
AWS_BYTE_CURSOR_PRI(aws_system_environment_get_virtualization_vendor(env))); |
| 42 |
|
| 43 |
env->os = aws_get_platform_build_os(); |
| 44 |
env->cpu_count = aws_system_info_processor_count(); |
| 45 |
env->cpu_group_count = aws_get_cpu_group_count(); |
| 46 |
|
| 47 |
return env; |
| 48 |
error: |
| 49 |
s_destroy_env(env); |
| 50 |
return NULL; |
| 51 |
} |
| 52 |
|
| 53 |
struct aws_system_environment *aws_system_environment_acquire(struct aws_system_environment *env) { |
| 54 |
aws_ref_count_acquire(&env->ref_count); |
| 55 |
return env; |
| 56 |
} |
| 57 |
|
| 58 |
void aws_system_environment_release(struct aws_system_environment *env) { |
| 59 |
aws_ref_count_release(&env->ref_count); |
| 60 |
} |
| 61 |
|
| 62 |
struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(const struct aws_system_environment *env) { |
| 63 |
struct aws_byte_cursor vendor_string = aws_byte_cursor_from_buf(&env->virtualization_vendor); |
| 64 |
return aws_byte_cursor_trim_pred(&vendor_string, aws_char_is_space); |
| 65 |
} |
| 66 |
|
| 67 |
struct aws_byte_cursor aws_system_environment_get_virtualization_product_name( |
| 68 |
const struct aws_system_environment *env) { |
| 69 |
struct aws_byte_cursor product_name_str = aws_byte_cursor_from_buf(&env->product_name); |
| 70 |
return aws_byte_cursor_trim_pred(&product_name_str, aws_char_is_space); |
| 71 |
} |
| 72 |
|
| 73 |
size_t aws_system_environment_get_processor_count(struct aws_system_environment *env) { |
| 74 |
return env->cpu_count; |
| 75 |
} |
| 76 |
|
| 77 |
AWS_COMMON_API |
| 78 |
size_t aws_system_environment_get_cpu_group_count(const struct aws_system_environment *env) { |
| 79 |
return env->cpu_group_count; |
| 80 |
} |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
AWS_STATIC_STRING_FROM_LITERAL(s_windows_str, "Windows"); |
| 93 |
AWS_STATIC_STRING_FROM_LITERAL(s_macos_str, "macOS"); |
| 94 |
AWS_STATIC_STRING_FROM_LITERAL(s_ios_str, "iOS"); |
| 95 |
AWS_STATIC_STRING_FROM_LITERAL(s_android_str, "Android"); |
| 96 |
AWS_STATIC_STRING_FROM_LITERAL(s_unix_str, "Unix"); |
| 97 |
AWS_STATIC_STRING_FROM_LITERAL(s_unknown_str, "Unknown"); |
| 98 |
|
| 99 |
struct aws_byte_cursor aws_get_platform_build_os_string(void) { |
| 100 |
enum aws_platform_os os = aws_get_platform_build_os(); |
| 101 |
switch (os) { |
| 102 |
case AWS_PLATFORM_OS_WINDOWS: |
| 103 |
return aws_byte_cursor_from_string(s_windows_str); |
| 104 |
case AWS_PLATFORM_OS_MAC: |
| 105 |
return aws_byte_cursor_from_string(s_macos_str); |
| 106 |
case AWS_PLATFORM_OS_IOS: |
| 107 |
return aws_byte_cursor_from_string(s_ios_str); |
| 108 |
case AWS_PLATFORM_OS_ANDROID: |
| 109 |
return aws_byte_cursor_from_string(s_android_str); |
| 110 |
case AWS_PLATFORM_OS_UNIX: |
| 111 |
return aws_byte_cursor_from_string(s_unix_str); |
| 112 |
} |
| 113 |
|
| 114 |
AWS_LOGF_WARN(AWS_LS_COMMON_GENERAL, "Unknown platform OS enum value: %d", (int)os); |
| 115 |
return aws_byte_cursor_from_string(s_unknown_str); |
| 116 |
} |