Debian Codegraph @

crt/aws-c-common/tests/system_info_tests.c

raw symbols SCIP
1 /**
2 * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
3 * SPDX-License-Identifier: Apache-2.0.
4 */
5
6 #include <aws/common/system_info.h>
7
8 #include "logging/test_logger.h"
9 #include <aws/testing/aws_test_harness.h>
10
11 static int s_test_cpu_count_at_least_works_superficially_fn(struct aws_allocator *allocator, void *ctx) {
12 (void)allocator;
13 (void)ctx;
14
15 size_t processor_count = aws_system_info_processor_count();
16 /* I think this is a fairly reasonable assumption given the circumstances
17 * (you know this test is part of a program
18 * that must be running on at least one core).... */
19 ASSERT_TRUE(processor_count > 0);
20
21 return 0;
22 }
23
24 AWS_TEST_CASE(test_cpu_count_at_least_works_superficially, s_test_cpu_count_at_least_works_superficially_fn)
25
26 static int s_test_page_size_at_least_works_superficially_fn(struct aws_allocator *allocator, void *ctx) {
27 (void)allocator;
28 (void)ctx;
29
30 size_t page_size = aws_system_info_page_size();
31 ASSERT_TRUE(page_size > 0);
32 /* Page size should be a power of 2 */
33 ASSERT_TRUE((page_size & (page_size - 1)) == 0);
34
35 return 0;
36 }
37
38 AWS_TEST_CASE(test_page_size_at_least_works_superficially, s_test_page_size_at_least_works_superficially_fn)
39
40 #if defined(_WIN32)
41 # include <io.h>
42 # define DIRSEP "\\"
43 #else
44 # define DIRSEP "/"
45 #endif
46
47 static int s_test_stack_trace_decoding(struct aws_allocator *allocator, void *ctx) {
48 (void)ctx;
49
50 struct aws_logger test_log;
51 test_logger_init(&test_log, allocator, AWS_LL_TRACE, 0);
52 aws_logger_set(&test_log);
53
54 int line = 0; /* captured on line of aws_backtrace_log call to match call site */
55 (void)line; /* may not be used if debug info is unavailable */
56 aws_backtrace_log(AWS_LL_TRACE), (line = __LINE__); /* NOLINT */
57
58 struct test_logger_impl *log = test_log.p_impl;
59 ASSERT_NOT_NULL(log);
60
61 struct aws_byte_buf *buffer = &log->log_buffer;
62 (void)buffer;
63
64 #if defined(AWS_BACKTRACE_STACKS_AVAILABLE) && defined(DEBUG_BUILD)
65 /* ensure that this file/function is found */
66 char *file = __FILE__;
67 char *next = strstr(file, DIRSEP);
68 /* strip path info, just filename will be found */
69 while (next) {
70 file = next + 1;
71 next = strstr(file, DIRSEP);
72 }
73
74 struct aws_byte_cursor null_term = aws_byte_cursor_from_array("", 1);
75 aws_byte_buf_append_dynamic(buffer, &null_term);
76 fprintf(stderr, "%s", (const char *)buffer->buffer);
77 const char *func = __func__;
78 if (func[0] == 's' && func[1] == '_') {
79 func += 2; /* skip over s_ */
80 }
81 ASSERT_NOT_NULL(strstr((const char *)buffer->buffer, func));
82 /* if this is not a debug build, there may not be symbols, so the test cannot
83 * verify if a best effort was made */
84 if (strstr((const char *)buffer->buffer, file)) {
85 /* check for the call site of aws_backtrace_print. Note that line numbers are off by one
86 * in both directions depending on compiler, so we check a range around the call site __LINE__
87 * The line number can also be ? on old compilers
88 */
89 char fileline[4096];
90 uint32_t found_file_line = 0;
91 for (int lineno = line - 1; lineno <= line + 1; ++lineno) {
92 snprintf(fileline, sizeof(fileline), "%s:%d", file, lineno);
93 found_file_line = strstr((const char *)buffer->buffer, fileline) != NULL;
94 if (found_file_line) {
95 break;
96 }
97 }
98 if (!found_file_line) {
99 snprintf(fileline, sizeof(fileline), "%s:?", file);
100 found_file_line = strstr((const char *)buffer->buffer, fileline) != NULL;
101 }
102
103 ASSERT_TRUE(found_file_line);
104 }
105 #endif
106
107 aws_logger_clean_up(&test_log);
108 return 0;
109 }
110
111 AWS_TEST_CASE(test_stack_trace_decoding, s_test_stack_trace_decoding);
112
113 static int s_test_platform_build_os_fn(struct aws_allocator *allocator, void *ctx) {
114 (void)allocator;
115 (void)ctx;
116
117 enum aws_platform_os build_os = aws_get_platform_build_os();
118
119 #if defined(AWS_OS_MACOS)
120 ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_MAC);
121 #elif defined(AWS_OS_APPLE)
122 ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_IOS);
123 #elif defined(AWS_OS_ANDROID)
124 ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_ANDROID);
125 #elif defined(_WIN32)
126 ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_WINDOWS);
127 #else
128 ASSERT_INT_EQUALS(build_os, AWS_PLATFORM_OS_UNIX);
129 #endif
130
131 return 0;
132 }
133
134 AWS_TEST_CASE(test_platform_build_os, s_test_platform_build_os_fn)
135
136 static int s_test_platform_build_os_string_fn(struct aws_allocator *allocator, void *ctx) {
137 (void)allocator;
138 (void)ctx;
139
140 struct aws_byte_cursor os_string = aws_get_platform_build_os_string();
141 ASSERT_TRUE(aws_byte_cursor_is_valid(&os_string));
142 ASSERT_TRUE(os_string.len > 0);
143
144 #if defined(AWS_OS_MACOS)
145 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "macOS"));
146 #elif defined(AWS_OS_APPLE)
147 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "iOS"));
148 #elif defined(AWS_OS_ANDROID)
149 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "Android"));
150 #elif defined(_WIN32)
151 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "Windows"));
152 #else
153 ASSERT_TRUE(aws_byte_cursor_eq_c_str(&os_string, "Unix"));
154 #endif
155
156 return 0;
157 }
158
159 AWS_TEST_CASE(test_platform_build_os_string, s_test_platform_build_os_string_fn)
160
161 static int s_test_sanity_check_numa_discovery(struct aws_allocator *allocator, void *ctx) {
162 (void)ctx;
163
164 aws_common_library_init(allocator);
165 size_t processor_count = aws_system_info_processor_count();
166 ASSERT_TRUE(processor_count > 0);
167
168 uint16_t group_count = aws_get_cpu_group_count();
169 ASSERT_TRUE(group_count > 0);
170
171 /* log for the test output since it's the only way I can verify on certain platforms this looks correct. */
172 AWS_LOGF_INFO(AWS_LS_COMMON_GENERAL, "found %d cpu groups", (int)group_count);
173
174 size_t total_cpus_found_via_numa = 0;
175 for (uint16_t i = 0; i < group_count; ++i) {
176 size_t cpus_per_group = aws_get_cpu_count_for_group(i);
177 AWS_LOGF_INFO(
178 AWS_LS_COMMON_GENERAL, "found cpu count %d, which lives on group node %d", (int)cpus_per_group, (int)i);
179
180 ASSERT_TRUE(cpus_per_group > 0);
181 total_cpus_found_via_numa += cpus_per_group;
182
183 struct aws_cpu_info *cpus_for_group = aws_mem_calloc(allocator, cpus_per_group, sizeof(struct aws_cpu_info));
184 ASSERT_NOT_NULL(cpus_per_group);
185 aws_get_cpu_ids_for_group(i, cpus_for_group, cpus_per_group);
186
187 /* make sure at least one is set */
188 bool at_least_one = false;
189 for (size_t cpu_idx = 0; cpu_idx < cpus_per_group; ++cpu_idx) {
190 AWS_LOGF_INFO(
191 AWS_LS_COMMON_GENERAL,
192 "found cpu_id %d, which lives on group node %d. Is it likely a hyper-thread ? %s",
193 (int)cpus_for_group[cpu_idx].cpu_id,
194 (int)i,
195 cpus_for_group[cpu_idx].suspected_hyper_thread ? "Yes" : "No");
196 if (cpus_for_group[cpu_idx].cpu_id >= 0) {
197 at_least_one = true;
198 }
199 }
200
201 ASSERT_TRUE(at_least_one);
202 aws_mem_release(allocator, cpus_for_group);
203 }
204
205 ASSERT_TRUE(total_cpus_found_via_numa <= processor_count);
206
207 aws_common_library_clean_up();
208 return 0;
209 }
210
211 AWS_TEST_CASE(test_sanity_check_numa_discovery, s_test_sanity_check_numa_discovery)
212
213 static int s_test_sanity_check_environment_loader(struct aws_allocator *allocator, void *ctx) {
214 (void)ctx;
215
216 aws_common_library_init(allocator);
217 struct aws_system_environment *env = aws_system_environment_load(allocator);
218 ASSERT_NOT_NULL(env);
219 struct aws_byte_cursor virt_vendor = aws_system_environment_get_virtualization_vendor(env);
220 ASSERT_TRUE(aws_byte_cursor_is_valid(&virt_vendor));
221 struct aws_byte_cursor virt_product = aws_system_environment_get_virtualization_product_name(env);
222 ASSERT_TRUE(aws_byte_cursor_is_valid(&virt_product));
223
224 aws_system_environment_release(env);
225
226 aws_common_library_clean_up();
227 return 0;
228 }
229
230 AWS_TEST_CASE(test_sanity_check_environment_loader, s_test_sanity_check_environment_loader)