Debian Codegraph @

crt/aws-c-s3/source/s3_platform_info.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/auth/aws_imds_client.h>
7 #include <aws/common/clock.h>
8 #include <aws/common/condition_variable.h>
9 #include <aws/common/hash_table.h>
10 #include <aws/common/mutex.h>
11 #include <aws/common/system_info.h>
12 #include <aws/io/channel_bootstrap.h>
13 #include <aws/io/event_loop.h>
14 #include <aws/io/host_resolver.h>
15 #include <aws/s3/private/s3_platform_info.h>
16
17 /* note: setting `has_recommended_configuration` to true would mean crt client
18 * can be loaded by default on the instance. We are not ready for this behavior
19 * on certain instance families yet so this might be set to false. */
20
21 /**** Configuration info for the c5n.18xlarge *****/
22 static struct aws_s3_platform_info s_c5n_18xlarge_platform_info = {
23 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("c5n.18xlarge"),
24 .max_throughput_gbps = 100u,
25 /** not yet **/
26 .has_recommended_configuration = false,
27 };
28
29 static struct aws_s3_platform_info s_c5n_metal_platform_info = {
30 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("c5n.metal"),
31 .max_throughput_gbps = 100u,
32 /** not yet **/
33 .has_recommended_configuration = false,
34 };
35
36 /****** End c5n.18xlarge *****/
37
38 /****** Begin c5n.large ******/
39 static struct aws_s3_platform_info s_c5n_9xlarge_platform_info = {
40 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("c5n.9xlarge"),
41 .max_throughput_gbps = 50u,
42 /** not yet **/
43 .has_recommended_configuration = false,
44 };
45
46 /****** End c5n.9large *****/
47
48 /***** Begin p4d.24xlarge and p4de.24xlarge ****/
49 static struct aws_s3_platform_info s_p4d_platform_info = {
50 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p4d.24xlarge"),
51 .max_throughput_gbps = 400u,
52 .has_recommended_configuration = true,
53 };
54
55 static struct aws_s3_platform_info s_p4de_platform_info = {
56 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p4de.24xlarge"),
57 .max_throughput_gbps = 400u,
58 .has_recommended_configuration = true,
59 };
60
61 /***** End p4d.24xlarge and p4de.24xlarge ****/
62
63 /***** Begin p5.48xlarge ******/
64
65 /* note: the p5 is a stunningly massive instance type.
66 * While the specs have 3.2 TB/s for the network bandwidth
67 * not all of that is accessible from the CPU. From the CPU we'll
68 * be able to get around 400 Gbps. Also note, 3.2 TB/s
69 * with 2 sockets on a nitro instance inplies 16 NICs
70 * per node. However, practically, due to the topology of this instance
71 * as far as this client is concerned, there are two NICs per node, similar
72 * to the p4d. The rest is for other things on the machine to use. */
73 struct aws_s3_platform_info s_p5_platform_info = {
74 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p5.48xlarge"),
75 .max_throughput_gbps = 400u,
76 .has_recommended_configuration = true,
77 };
78 /***** End p5.48xlarge *****/
79
80 /* For all instances from p5e.48xlarge - p6-b300.48xlarge,
81 * the max_throughput_gbps values configured are based on the maximum
82 * bandwidth offered from a single NIC in these instances. CRT clients
83 * default to using a single NIC unless configured to use multiple NICs
84 * by identifying the number of NICs and providing the names in an array
85 * (refer s3_client.h - struct aws_s3_client_config). The max_throughput_gbps
86 * is only a default we set can be overridden by the user's client config.
87 * TODO: Once we are able to auto-detect NICs and add them, default values
88 * should be updated with maximum ENA network bandwidth allowed by these
89 * instances.
90 */
91 /***** Begin p5e.48xlarge ******/
92
93 static struct aws_s3_platform_info s_p5e_platform_info = {
94 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p5e.48xlarge"),
95 .max_throughput_gbps = 100u,
96 .has_recommended_configuration = true,
97 };
98 /***** End p5e.48xlarge *****/
99
100 /***** Begin p5en.48xlarge ******/
101
102 static struct aws_s3_platform_info s_p5en_platform_info = {
103 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p5en.48xlarge"),
104 .max_throughput_gbps = 100u,
105 .has_recommended_configuration = true,
106 };
107 /***** End p5en.48xlarge *****/
108
109 /***** Begin p6-b200.48xlarge ******/
110
111 static struct aws_s3_platform_info s_p6_b200_platform_info = {
112 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p6-b200.48xlarge"),
113 .max_throughput_gbps = 200u,
114 .has_recommended_configuration = true,
115 };
116 /***** End p6-b200.48xlarge *****/
117
118 /***** Begin p6-b300.48xlarge ******/
119
120 static struct aws_s3_platform_info s_p6_b300_platform_info = {
121 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("p6-b300.48xlarge"),
122 .max_throughput_gbps = 350u,
123 .has_recommended_configuration = true,
124 };
125 /***** End p6-b300.48xlarge *****/
126
127 /**** Begin trn1_32_large *****/
128 static struct aws_s3_platform_info s_trn1_n_platform_info = {
129 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("trn1n.32xlarge"),
130 /* not all of the advertised 1600 Gbps bandwidth can be hit from the cpu in user-space */
131 .max_throughput_gbps = 800u,
132 .has_recommended_configuration = true,
133 };
134
135 static struct aws_s3_platform_info s_trn1_platform_info = {
136 .instance_type = AWS_BYTE_CUR_INIT_FROM_STRING_LITERAL("trn1.32xlarge"),
137 /* not all of the advertised 800 Gbps bandwidth can be hit from the cpu in user-space */
138 .max_throughput_gbps = 600u,
139 .has_recommended_configuration = true,
140 };
141
142 /**** End trn1.x32_large ******/
143
144 struct aws_s3_platform_info_loader {
145 struct aws_allocator *allocator;
146 struct aws_ref_count ref_count;
147 struct {
148 struct aws_string *detected_instance_type;
149 struct aws_s3_platform_info current_env_platform_info;
150 /* aws_hash_table<aws_byte_cursor*, aws_s3_platform_info *>
151 * the table does not "own" any of the data inside it. */
152 struct aws_hash_table compute_platform_info_table;
153 struct aws_mutex lock;
154 } lock_data;
155 struct aws_system_environment *current_env;
156 };
157
158 void s_add_platform_info_to_table(struct aws_s3_platform_info_loader *loader, struct aws_s3_platform_info *info) {
159 AWS_PRECONDITION(info->instance_type.len > 0);
160 AWS_LOGF_TRACE(
161 AWS_LS_S3_GENERAL,
162 "id=%p: adding platform entry for \"" PRInSTR "\".",
163 (void *)loader,
164 AWS_BYTE_CURSOR_PRI(info->instance_type));
165
166 struct aws_hash_element *platform_info_element = NULL;
167 aws_hash_table_find(&loader->lock_data.compute_platform_info_table, &info->instance_type, &platform_info_element);
168 if (platform_info_element) {
169 AWS_LOGF_TRACE(
170 AWS_LS_S3_GENERAL,
171 "id=%p: existing entry for \"" PRInSTR "\" found, syncing the values.",
172 (void *)loader,
173 AWS_BYTE_CURSOR_PRI(info->instance_type));
174
175 /* detected runtime NIC data is better than the pre-known config data but we don't always have it,
176 * so copy over any better info than we have. Assume if info has NIC data, it was discovered at runtime.
177 * The other data should be identical and we don't want to add complications to the memory model.
178 * You're guaranteed only one instance of an instance type's info, the initial load is static memory */
179 struct aws_s3_platform_info *existing = platform_info_element->value;
180 // TODO: sync the cpu group and NIC data
181 info->has_recommended_configuration = existing->has_recommended_configuration;
182 /* always prefer a pre-known bandwidth, as we estimate low on EC2 by default for safety. */
183 info->max_throughput_gbps = existing->max_throughput_gbps;
184 } else {
185 AWS_FATAL_ASSERT(
186 !aws_hash_table_put(
187 &loader->lock_data.compute_platform_info_table, &info->instance_type, (void *)info, NULL) &&
188 "hash table put failed!");
189 }
190 }
191
192 static void s_destroy_loader(void *arg) {
193 struct aws_s3_platform_info_loader *loader = arg;
194
195 aws_hash_table_clean_up(&loader->lock_data.compute_platform_info_table);
196 aws_mutex_clean_up(&loader->lock_data.lock);
197
198 if (loader->lock_data.detected_instance_type) {
199 aws_string_destroy(loader->lock_data.detected_instance_type);
200 }
201
202 aws_system_environment_release(loader->current_env);
203 aws_mem_release(loader->allocator, loader);
204 }
205
206 struct aws_s3_platform_info_loader *aws_s3_platform_info_loader_new(struct aws_allocator *allocator) {
207 struct aws_s3_platform_info_loader *loader =
208 aws_mem_calloc(allocator, 1, sizeof(struct aws_s3_platform_info_loader));
209
210 loader->allocator = allocator;
211 loader->current_env = aws_system_environment_load(allocator);
212 AWS_FATAL_ASSERT(loader->current_env && "Failed to load system environment");
213 aws_mutex_init(&loader->lock_data.lock);
214 aws_ref_count_init(&loader->ref_count, loader, s_destroy_loader);
215
216 /* TODO: Implement runtime CPU information retrieval from the system. Currently, Valgrind detects a memory leak
217 * associated with the g_numa_node_of_cpu_ptr function (see: https://github.com/numactl/numactl/issues/3). This
218 * issue was addressed in version v2.0.13 of libnuma (see: https://github.com/numactl/numactl/pull/43). However,
219 * Amazon Linux 2 defaults to libnuma version v2.0.9, which lacks this fix. We need to suppress this
220 * warning as a false positive in older versions of libnuma. In the future, however, we will probably eliminate the
221 * use of numactl altogether. */
222
223 AWS_FATAL_ASSERT(
224 !aws_hash_table_init(
225 &loader->lock_data.compute_platform_info_table,
226 allocator,
227 32,
228 aws_hash_byte_cursor_ptr_ignore_case,
229 (aws_hash_callback_eq_fn *)aws_byte_cursor_eq_ignore_case,
230 NULL,
231 NULL) &&
232 "Hash table init failed!");
233
234 s_add_platform_info_to_table(loader, &s_c5n_18xlarge_platform_info);
235 s_add_platform_info_to_table(loader, &s_c5n_9xlarge_platform_info);
236 s_add_platform_info_to_table(loader, &s_c5n_metal_platform_info);
237 s_add_platform_info_to_table(loader, &s_p4d_platform_info);
238 s_add_platform_info_to_table(loader, &s_p4de_platform_info);
239 s_add_platform_info_to_table(loader, &s_p5_platform_info);
240 s_add_platform_info_to_table(loader, &s_p5e_platform_info);
241 s_add_platform_info_to_table(loader, &s_p5en_platform_info);
242 s_add_platform_info_to_table(loader, &s_p6_b200_platform_info);
243 s_add_platform_info_to_table(loader, &s_p6_b300_platform_info);
244 s_add_platform_info_to_table(loader, &s_trn1_n_platform_info);
245 s_add_platform_info_to_table(loader, &s_trn1_platform_info);
246
247 return loader;
248 }
249
250 struct aws_s3_platform_info_loader *aws_s3_platform_info_loader_acquire(struct aws_s3_platform_info_loader *loader) {
251 aws_ref_count_acquire(&loader->ref_count);
252 return loader;
253 }
254
255 struct aws_s3_platform_info_loader *aws_s3_platform_info_loader_release(struct aws_s3_platform_info_loader *loader) {
256 if (loader) {
257 aws_ref_count_release(&loader->ref_count);
258 }
259 return NULL;
260 }
261
262 struct imds_callback_info {
263 struct aws_allocator *allocator;
264 struct aws_string *instance_type;
265 struct aws_condition_variable c_var;
266 int error_code;
267 bool shutdown_completed;
268 struct aws_mutex mutex;
269 };
270
271 static void s_imds_client_shutdown_completed(void *user_data) {
272 struct imds_callback_info *info = user_data;
273 aws_mutex_lock(&info->mutex);
274 info->shutdown_completed = true;
275 aws_condition_variable_notify_all(&info->c_var);
276 aws_mutex_unlock(&info->mutex);
277 }
278
279 static bool s_client_shutdown_predicate(void *arg) {
280 struct imds_callback_info *info = arg;
281 return info->shutdown_completed;
282 }
283
284 static void s_imds_client_on_get_instance_info_callback(
285 const struct aws_imds_instance_info *instance_info,
286 int error_code,
287 void *user_data) {
288 struct imds_callback_info *info = user_data;
289
290 aws_mutex_lock(&info->mutex);
291 if (error_code) {
292 info->error_code = error_code;
293 } else {
294 info->instance_type = aws_string_new_from_cursor(info->allocator, &instance_info->instance_type);
295 }
296 aws_condition_variable_notify_all(&info->c_var);
297 aws_mutex_unlock(&info->mutex);
298 }
299
300 static bool s_completion_predicate(void *arg) {
301 struct imds_callback_info *info = arg;
302 return info->error_code != 0 || info->instance_type != NULL;
303 }
304
305 struct aws_string *s_query_imds_for_instance_type(struct aws_allocator *allocator) {
306
307 struct imds_callback_info callback_info = {
308 .mutex = AWS_MUTEX_INIT,
309 .c_var = AWS_CONDITION_VARIABLE_INIT,
310 .allocator = allocator,
311 };
312
313 struct aws_event_loop_group *el_group = NULL;
314 struct aws_host_resolver *resolver = NULL;
315 struct aws_client_bootstrap *client_bootstrap = NULL;
316 /* now call IMDS */
317 el_group = aws_event_loop_group_new_default(allocator, 1, NULL);
318
319 if (!el_group) {
320 goto tear_down;
321 }
322
323 struct aws_host_resolver_default_options resolver_options = {
324 .max_entries = 1,
325 .el_group = el_group,
326 };
327
328 resolver = aws_host_resolver_new_default(allocator, &resolver_options);
329
330 if (!resolver) {
331 goto tear_down;
332 }
333
334 struct aws_client_bootstrap_options bootstrap_options = {
335 .event_loop_group = el_group,
336 .host_resolver = resolver,
337 };
338
339 client_bootstrap = aws_client_bootstrap_new(allocator, &bootstrap_options);
340
341 if (!client_bootstrap) {
342 goto tear_down;
343 }
344
345 struct aws_imds_client_shutdown_options imds_shutdown_options = {
346 .shutdown_callback = s_imds_client_shutdown_completed,
347 .shutdown_user_data = &callback_info,
348 };
349
350 struct aws_imds_client_options imds_options = {
351 .bootstrap = client_bootstrap,
352 .imds_version = IMDS_PROTOCOL_V2,
353 .shutdown_options = imds_shutdown_options,
354 };
355
356 struct aws_imds_client *imds_client = aws_imds_client_new(allocator, &imds_options);
357
358 if (!imds_client) {
359 goto tear_down;
360 }
361
362 aws_mutex_lock(&callback_info.mutex);
363
364 if (aws_imds_client_get_instance_info(imds_client, s_imds_client_on_get_instance_info_callback, &callback_info)) {
365 aws_condition_variable_wait_for_pred(
366 &callback_info.c_var, &callback_info.mutex, AWS_TIMESTAMP_SECS, s_completion_predicate, &callback_info);
367 }
368 aws_imds_client_release(imds_client);
369 aws_condition_variable_wait_pred(
370 &callback_info.c_var, &callback_info.mutex, s_client_shutdown_predicate, &callback_info);
371
372 aws_mutex_unlock(&callback_info.mutex);
373
374 if (callback_info.error_code) {
375 aws_raise_error(callback_info.error_code);
376 AWS_LOGF_ERROR(
377 AWS_LS_S3_CLIENT, "IMDS call failed with error %s.", aws_error_debug_str(callback_info.error_code));
378 }
379
380 tear_down:
381 if (client_bootstrap) {
382 aws_client_bootstrap_release(client_bootstrap);
383 }
384
385 if (resolver) {
386 aws_host_resolver_release(resolver);
387 }
388
389 if (el_group) {
390 aws_event_loop_group_release(el_group);
391 }
392 return callback_info.instance_type;
393 }
394
395 struct aws_byte_cursor aws_s3_get_ec2_instance_type(struct aws_s3_platform_info_loader *loader, bool cached_only) {
396 aws_mutex_lock(&loader->lock_data.lock);
397 struct aws_byte_cursor return_cur;
398 AWS_ZERO_STRUCT(return_cur);
399
400 if (loader->lock_data.detected_instance_type) {
401 AWS_LOGF_TRACE(
402 AWS_LS_S3_CLIENT,
403 "id=%p: Instance type has already been determined to be %s. Returning cached version.",
404 (void *)loader,
405 aws_string_bytes(loader->lock_data.detected_instance_type));
406 goto return_instance_and_unlock;
407 }
408 if (cached_only) {
409 AWS_LOGF_TRACE(
410 AWS_LS_S3_CLIENT,
411 "id=%p: Instance type has not been cached. Returning without trying to determine instance type since "
412 "cached_only is set.",
413 (void *)loader);
414 goto return_instance_and_unlock;
415 }
416
417 AWS_LOGF_TRACE(
418 AWS_LS_S3_CLIENT,
419 "id=%p: Instance type has not been determined, checking to see if running in EC2 nitro environment.",
420 (void *)loader);
421 /*
422 * We want to only imds call if we know that we are on an ec2 instance. All new instances are Nitro and we don't
423 * care about the old ones.
424 */
425 if (aws_s3_is_running_on_ec2_nitro(loader)) {
426 AWS_LOGF_INFO(
427 AWS_LS_S3_CLIENT, "id=%p: Detected Amazon EC2 with nitro as the current environment.", (void *)loader);
428 /* easy case not requiring any calls out to IMDS. If we detected we're running on ec2, then the dmi info is
429 * correct, and we can use it if we have it. Otherwise call out to IMDS. */
430 struct aws_byte_cursor product_name =
431 aws_system_environment_get_virtualization_product_name(loader->current_env);
432
433 if (product_name.len) {
434 loader->lock_data.detected_instance_type = aws_string_new_from_cursor(loader->allocator, &product_name);
435 loader->lock_data.current_env_platform_info.instance_type =
436 aws_byte_cursor_from_string(loader->lock_data.detected_instance_type);
437 s_add_platform_info_to_table(loader, &loader->lock_data.current_env_platform_info);
438
439 AWS_LOGF_INFO(
440 AWS_LS_S3_CLIENT,
441 "id=%p: Determined instance type to be %s, from dmi info. Caching.",
442 (void *)loader,
443 aws_string_bytes(loader->lock_data.detected_instance_type));
444 goto return_instance_and_unlock;
445 }
446
447 AWS_LOGF_DEBUG(
448 AWS_LS_S3_CLIENT,
449 "static: DMI info was insufficient to determine instance type. Making call to IMDS to determine");
450 struct aws_string *instance_type = s_query_imds_for_instance_type(loader->allocator);
451 if (instance_type) {
452 loader->lock_data.detected_instance_type = instance_type;
453 loader->lock_data.current_env_platform_info.instance_type = aws_byte_cursor_from_string(instance_type);
454 s_add_platform_info_to_table(loader, &loader->lock_data.current_env_platform_info);
455 AWS_LOGF_INFO(
456 AWS_LS_S3_CLIENT,
457 "id=%p: Determined instance type to be %s, from IMDS.",
458 (void *)loader,
459 aws_string_bytes(loader->lock_data.detected_instance_type));
460 }
461 }
462
463 return_instance_and_unlock:
464 return_cur = loader->lock_data.current_env_platform_info.instance_type;
465 aws_mutex_unlock(&loader->lock_data.lock);
466
467 return return_cur;
468 }
469
470 const struct aws_s3_platform_info *aws_s3_get_platform_info_for_current_environment(
471 struct aws_s3_platform_info_loader *loader) {
472 /* getting the instance type will set it on the loader the first time if it can */
473 aws_s3_get_ec2_instance_type(loader, false /*cached_only*/);
474 /* will never be mutated after the above call. */
475 return &loader->lock_data.current_env_platform_info;
476 }
477
478 struct aws_array_list aws_s3_get_recommended_platforms(struct aws_s3_platform_info_loader *loader) {
479 struct aws_array_list array_list;
480 aws_mutex_lock(&loader->lock_data.lock);
481 aws_array_list_init_dynamic(&array_list, loader->allocator, 5, sizeof(struct aws_byte_cursor));
482 /* Iterate over the map and add instance types to the array list which have
483 * platform_info->has_recommended_configuration == true */
484 for (struct aws_hash_iter iter = aws_hash_iter_begin(&loader->lock_data.compute_platform_info_table);
485 !aws_hash_iter_done(&iter);
486 aws_hash_iter_next(&iter)) {
487 struct aws_s3_platform_info *platform_info = iter.element.value;
488
489 if (platform_info->has_recommended_configuration) {
490 aws_array_list_push_back(&array_list, &platform_info->instance_type);
491 }
492 }
493 aws_mutex_unlock(&loader->lock_data.lock);
494 return array_list;
495 }
496
497 const struct aws_s3_platform_info *aws_s3_get_platform_info_for_instance_type(
498 struct aws_s3_platform_info_loader *loader,
499 struct aws_byte_cursor instance_type_name) {
500 aws_mutex_lock(&loader->lock_data.lock);
501 struct aws_hash_element *platform_info_element = NULL;
502 aws_hash_table_find(&loader->lock_data.compute_platform_info_table, &instance_type_name, &platform_info_element);
503 aws_mutex_unlock(&loader->lock_data.lock);
504
505 if (platform_info_element) {
506 return platform_info_element->value;
507 }
508
509 return NULL;
510 }
511
512 bool aws_s3_is_running_on_ec2_nitro(struct aws_s3_platform_info_loader *loader) {
513 struct aws_byte_cursor system_virt_name = aws_system_environment_get_virtualization_vendor(loader->current_env);
514
515 if (aws_byte_cursor_eq_c_str_ignore_case(&system_virt_name, "amazon ec2")) {
516 return true;
517 }
518
519 return false;
520 }