From 1c40d93edcfddaa814ee7f149a3e27161fcd7dac Mon Sep 17 00:00:00 2001 From: Chen Shaoyuan Date: Thu, 19 Dec 2024 18:57:20 +0800 Subject: [PATCH] format source code --- mooncake-transfer-engine/include/topology.h | 5 +- mooncake-transfer-engine/src/topology.cpp | 162 +++++++----------- .../tests/topology_test.cpp | 11 +- 3 files changed, 73 insertions(+), 105 deletions(-) diff --git a/mooncake-transfer-engine/include/topology.h b/mooncake-transfer-engine/include/topology.h index 988e292..e31ba50 100644 --- a/mooncake-transfer-engine/include/topology.h +++ b/mooncake-transfer-engine/include/topology.h @@ -1,6 +1,5 @@ #include -namespace mooncake -{ - std::string discoverTopologyMatrix(); +namespace mooncake { +std::string discoverTopologyMatrix(); } diff --git a/mooncake-transfer-engine/src/topology.cpp b/mooncake-transfer-engine/src/topology.cpp index 5f49808..8a71f0c 100644 --- a/mooncake-transfer-engine/src/topology.cpp +++ b/mooncake-transfer-engine/src/topology.cpp @@ -1,52 +1,46 @@ -#include +#include +#include + +#include +#include #include #include #include -#include -#include - -#include -#include +#include #ifdef USE_CUDA #include "cuda_runtime.h" #endif -#include -#include #include -#include +#include #include #include #include +#include #include "topology.h" -struct InfinibandDevice -{ +struct InfinibandDevice { std::string name; std::string pci_bus_id; int numa_node; }; -struct TopologyEntry -{ +struct TopologyEntry { std::string name; std::vector preferred_hca; std::vector avail_hca; - Json::Value to_json() - { + Json::Value to_json() { Json::Value matrix(Json::arrayValue); Json::Value hca_list(Json::arrayValue); - for (auto &hca : preferred_hca) - { + for (auto &hca : preferred_hca) { hca_list.append(hca); } matrix.append(hca_list); hca_list.clear(); - for (auto &hca : avail_hca) - { + for (auto &hca : avail_hca) { hca_list.append(hca); } matrix.append(hca_list); @@ -54,21 +48,17 @@ struct TopologyEntry } }; -static std::vector list_infiniband_devices() -{ +static std::vector list_infiniband_devices() { DIR *dir = opendir("/sys/class/infiniband"); struct dirent *entry; std::vector devices; - if (dir == NULL) - { + if (dir == NULL) { LOG(WARNING) << "failed to list /sys/class/infiniband"; return {}; } - while ((entry = readdir(dir))) - { - if (entry->d_name[0] == '.') - { + while ((entry = readdir(dir))) { + if (entry->d_name[0] == '.') { continue; } @@ -76,9 +66,9 @@ static std::vector list_infiniband_devices() char path[PATH_MAX]; char resolved_path[PATH_MAX]; - snprintf(path, sizeof(path), "/sys/class/infiniband/%s/../..", entry->d_name); - if (realpath(path, resolved_path) == NULL) - { + snprintf(path, sizeof(path), "/sys/class/infiniband/%s/../..", + entry->d_name); + if (realpath(path, resolved_path) == NULL) { LOG(ERROR) << "realpath: " << strerror(errno); continue; } @@ -96,41 +86,36 @@ static std::vector list_infiniband_devices() return devices; } -static std::vector discover_cpu_topology(const std::vector &all_hca) -{ +static std::vector discover_cpu_topology( + const std::vector &all_hca) { DIR *dir = opendir("/sys/devices/system/node"); struct dirent *entry; std::vector topology; - if (dir == NULL) - { + if (dir == NULL) { LOG(WARNING) << "failed to list /sys/devices/system/node"; return {}; } - while ((entry = readdir(dir))) - { + while ((entry = readdir(dir))) { const char *prefix = "node"; - if (entry->d_type != DT_DIR || strncmp(entry->d_name, prefix, strlen(prefix)) != 0) - { + if (entry->d_type != DT_DIR || + strncmp(entry->d_name, prefix, strlen(prefix)) != 0) { continue; } int node_id = atoi(entry->d_name + strlen(prefix)); std::vector preferred_hca; std::vector avail_hca; - for (const auto &hca : all_hca) - { - if (hca.numa_node == node_id) - { + for (const auto &hca : all_hca) { + if (hca.numa_node == node_id) { preferred_hca.push_back(hca.name); - } - else - { + } else { avail_hca.push_back(hca.name); } } - topology.push_back(TopologyEntry{.name = "cpu:" + std::to_string(node_id), - .preferred_hca = std::move(preferred_hca), - .avail_hca = std::move(avail_hca)}); + topology.push_back( + TopologyEntry{.name = "cpu:" + std::to_string(node_id), + .preferred_hca = std::move(preferred_hca), + .avail_hca = std::move(avail_hca)}); } (void)closedir(dir); return topology; @@ -138,55 +123,47 @@ static std::vector discover_cpu_topology(const std::vector discover_cuda_topology(const std::vector &all_hca) -{ +static std::vector discover_cuda_topology( + const std::vector &all_hca) { std::vector topology; int device_count; - if (cudaGetDeviceCount(&device_count) != cudaSuccess) - { + if (cudaGetDeviceCount(&device_count) != cudaSuccess) { device_count = 0; } - for (int i = 0; i < device_count; i++) - { + for (int i = 0; i < device_count; i++) { char pci_bus_id[20]; - if (cudaDeviceGetPCIBusId(pci_bus_id, sizeof(pci_bus_id), i) != cudaSuccess) - { + if (cudaDeviceGetPCIBusId(pci_bus_id, sizeof(pci_bus_id), i) != + cudaSuccess) { continue; } for (char *ch = pci_bus_id; (*ch = tolower(*ch)); ch++) @@ -194,42 +171,35 @@ static std::vector discover_cuda_topology(const std::vector preferred_hca; std::vector avail_hca; - for (const auto &hca : all_hca) - { - if (get_pci_distance(hca.pci_bus_id.c_str(), pci_bus_id) == 0) - { + for (const auto &hca : all_hca) { + if (get_pci_distance(hca.pci_bus_id.c_str(), pci_bus_id) == 0) { preferred_hca.push_back(hca.name); - } - else - { + } else { avail_hca.push_back(hca.name); } } - topology.push_back(TopologyEntry{.name = "cuda:" + std::to_string(i), - .preferred_hca = std::move(preferred_hca), - .avail_hca = std::move(avail_hca)}); + topology.push_back( + TopologyEntry{.name = "cuda:" + std::to_string(i), + .preferred_hca = std::move(preferred_hca), + .avail_hca = std::move(avail_hca)}); } return topology; } -#endif // USE_CUDA - -namespace mooncake -{ - std::string discoverTopologyMatrix() - { - auto all_hca = list_infiniband_devices(); - Json::Value value(Json::objectValue); - for (auto &ent : discover_cpu_topology(all_hca)) - { - value[ent.name] = ent.to_json(); - } +#endif // USE_CUDA + +namespace mooncake { +std::string discoverTopologyMatrix() { + auto all_hca = list_infiniband_devices(); + Json::Value value(Json::objectValue); + for (auto &ent : discover_cpu_topology(all_hca)) { + value[ent.name] = ent.to_json(); + } #ifdef USE_CUDA - for (auto &ent : discover_cuda_topology(all_hca)) - { - value[ent.name] = ent.to_json(); - } -#endif - return value.toStyledString(); + for (auto &ent : discover_cuda_topology(all_hca)) { + value[ent.name] = ent.to_json(); } +#endif + return value.toStyledString(); } +} // namespace mooncake diff --git a/mooncake-transfer-engine/tests/topology_test.cpp b/mooncake-transfer-engine/tests/topology_test.cpp index b98a01b..fc1428e 100644 --- a/mooncake-transfer-engine/tests/topology_test.cpp +++ b/mooncake-transfer-engine/tests/topology_test.cpp @@ -1,11 +1,11 @@ -#include +#include "topology.h" + #include +#include #include "transfer_metadata.h" -#include "topology.h" -TEST(ToplogyTest, GetTopologyMatrix) -{ +TEST(ToplogyTest, GetTopologyMatrix) { std::string topo = mooncake::discoverTopologyMatrix(); LOG(INFO) << topo; mooncake::TransferMetadata::PriorityMatrix matrix; @@ -13,8 +13,7 @@ TEST(ToplogyTest, GetTopologyMatrix) mooncake::TransferMetadata::parseNicPriorityMatrix(topo, matrix, rnic_list); } -int main(int argc, char **argv) -{ +int main(int argc, char **argv) { ::testing::InitGoogleTest(&argc, argv); return RUN_ALL_TESTS(); } \ No newline at end of file