Skip to content

Commit

Permalink
Added some nic_detection for linux.
Browse files Browse the repository at this point in the history
  • Loading branch information
JonathanHenson committed Oct 17, 2023
1 parent b61c12e commit 41e4fcd
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 3 deletions.
21 changes: 19 additions & 2 deletions bin/system_info/print_system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,27 @@ int main(void) {
size_t numa_nodes = aws_system_environment_get_cpu_group_count(env);

if (numa_nodes > 1) {
fprintf(stdout, " 'numa architecture': 'true',\n");
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, " 'numa architecture': false\n");
}
size_t nic_count = aws_system_environment_get_network_card_count(env);
fprintf(stdout, " 'network_card_count': %lu\n", (unsigned long)nic_count);

if (nic_count > 0) {
fprintf(stdout, " 'network_cards: [\n");

const struct aws_string **nic_array = aws_system_environment_get_network_cards(env);
for (size_t i = 0; i < nic_count; ++i) {
fprintf(stdout, " '%s'", aws_string_c_str(nic_array[i]));

if (i != nic_count - 1) {
fprintf(stdout, ",");
}
fprintf(stdout, "\n");
}
fprintf(stdout, " ]\n");
}

fprintf(stdout, "}\n");
Expand Down
1 change: 1 addition & 0 deletions include/aws/common/private/system_info_priv.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ struct aws_system_environment {
struct aws_ref_count ref_count;
struct aws_byte_buf virtualization_vendor;
struct aws_byte_buf product_name;
struct aws_array_list str_list_network_cards;
enum aws_platform_os os;
size_t cpu_count;
size_t cpu_group_count;
Expand Down
6 changes: 6 additions & 0 deletions include/aws/common/system_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ struct aws_byte_cursor aws_system_environment_get_virtualization_vendor(const st
AWS_COMMON_API
struct aws_byte_cursor aws_system_environment_get_virtualization_product_name(const struct aws_system_environment *env);

AWS_COMMON_API
size_t aws_system_environment_get_network_card_count(const struct aws_system_environment *env);

AWS_COMMON_API
const struct aws_string **aws_system_environment_get_network_cards(const struct aws_system_environment *env);

/**
* Returns the number of processors for the specified compute environment.
*/
Expand Down
31 changes: 31 additions & 0 deletions source/linux/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include <aws/common/file.h>
#include <aws/common/private/system_info_priv.h>

#include <ifaddrs.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <linux/if_packet.h>
#include <net/ethernet.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(
Expand All @@ -15,10 +21,35 @@ int aws_system_environment_load_platform_impl(struct aws_system_environment *env
aws_byte_buf_init_from_file_with_size_hint(
&env->product_name, env->allocator, "/sys/devices/virtual/dmi/id/product_name", 32 /*size_hint*/);

struct ifaddrs *addrs;
struct ifaddrs *iterator;
iterator = addrs;

getifaddrs(&addrs);

while(iterator) {
if (iterator->ifa_addr && iterator->ifa_addr->sa_family == AF_PACKET) {
struct aws_string *device_name = aws_string_new_from_c_str(env->allocator, iterator->ifa_name);
aws_array_list_push_back(&env->str_list_network_cards, &device_name);
}
}

if (addrs) {
freeifaddrs(addrs);
}

return AWS_OP_SUCCESS;
}

void aws_system_environment_destroy_platform_impl(struct aws_system_environment *env) {
size_t length = aws_array_list_length(&env->str_list_network_cards);

for (size_t i = 0; i < length; ++i) {
struct aws_string *device_name = NULL;
aws_array_list_get_at(&env->str_list_network_cards, &device_name, i);
aws_string_destroy(device_name);
}

aws_byte_buf_clean_up(&env->virtualization_vendor);
aws_byte_buf_clean_up(&env->product_name);
}
13 changes: 12 additions & 1 deletion source/system_info.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ void s_destroy_env(void *arg) {

if (env) {
aws_system_environment_destroy_platform_impl(env);
aws_array_list_clean_up(&env->str_list_network_cards);
aws_mem_release(env->allocator, env);
}
}
Expand All @@ -20,6 +21,8 @@ struct aws_system_environment *aws_system_environment_load(struct aws_allocator
env->allocator = allocator;
aws_ref_count_init(&env->ref_count, env, s_destroy_env);

aws_array_list_init_dynamic(&env->str_list_network_cards, env->allocator, 2, sizeof(struct aws_string *));

if (aws_system_environment_load_platform_impl(env)) {
AWS_LOGF_ERROR(
AWS_LS_COMMON_GENERAL,
Expand Down Expand Up @@ -74,7 +77,15 @@ size_t aws_system_environment_get_processor_count(struct aws_system_environment
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;
}

size_t aws_system_environment_get_network_card_count(const struct aws_system_environment *env) {
return aws_array_list_length(&env->str_list_network_cards);
}


const struct aws_string **aws_system_environment_get_network_cards(const struct aws_system_environment *env) {
return env->str_list_network_cards.data;
}

0 comments on commit 41e4fcd

Please sign in to comment.