LCOV - code coverage report
Current view: top level - coll_communicator_mgr/common/loggers - comm_addr_logger.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.7 % 62 55
Test Date: 2026-08-04 10:52:23 Functions: 100.0 % 7 7

            Line data    Source code
       1              : /**
       2              :  * Copyright (c) 2025 Huawei Technologies Co., Ltd.
       3              :  * This program is free software, you can redistribute it and/or modify it under the terms and conditions of
       4              :  * CANN Open Software License Agreement Version 2.0 (the "License").
       5              :  * Please refer to the License for details. You may not use this file except in compliance with the License.
       6              :  * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
       7              :  * INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
       8              :  * See LICENSE in the root of the software repository for the full text of the License.
       9              :  */
      10              : 
      11              : #include "comm_addr_logger.h"
      12              : #include "hccl_comm_pub.h"
      13              : #include <arpa/inet.h>
      14              : #include <array>
      15              : #include <securec.h>
      16              : 
      17              : namespace hcomm {
      18              : namespace logger {
      19              : 
      20            5 : std::string CommAddrLogger::GetTypeString(CommAddrType type)
      21              : {
      22            5 :     switch (type) {
      23            1 :         case COMM_ADDR_TYPE_IP_V4:
      24            2 :             return "IPv4";
      25            1 :         case COMM_ADDR_TYPE_IP_V6:
      26            2 :             return "IPv6";
      27            1 :         case COMM_ADDR_TYPE_ID:
      28            2 :             return "ID";
      29            1 :         case COMM_ADDR_TYPE_EID:
      30            2 :             return "EID";
      31            1 :         default:
      32            1 :             return "Unknown(" + std::to_string(type) + ")";
      33              :     }
      34              : }
      35              : 
      36           52 : std::string CommAddrLogger::ConvertIPv4(const struct in_addr& addr)
      37              : {
      38              :     std::array<char, INET_ADDRSTRLEN> buffer;
      39          104 :     const char* result = inet_ntop(AF_INET, &addr, buffer.data(), buffer.size());
      40           52 :     if (result == nullptr) {
      41            0 :         HCCL_ERROR("[%s] inet_ntop failed for IPv4 address", __func__);
      42            0 :         return "conversion failed";
      43              :     }
      44          104 :     return std::string(result);
      45              : }
      46              : 
      47            2 : std::string CommAddrLogger::ConvertIPv6(const struct in6_addr& addr6)
      48              : {
      49              :     std::array<char, INET6_ADDRSTRLEN> buffer;
      50            4 :     const char* result = inet_ntop(AF_INET6, &addr6, buffer.data(), buffer.size());
      51            2 :     if (result == nullptr) {
      52            0 :         HCCL_ERROR("[%s] inet_ntop failed for IPv6 address", __func__);
      53            0 :         return "conversion failed";
      54              :     }
      55            4 :     return std::string(result);
      56              : }
      57              : 
      58            2 : std::string CommAddrLogger::ConvertID(uint32_t id)
      59              : {
      60            2 :     return "id:0x" + std::to_string(id);
      61              : }
      62              : 
      63           54 : std::string CommAddrLogger::ConvertEID(const uint8_t eid[16])
      64              : {
      65              :     // 模仿 Eid::Describe 的格式
      66              :     // 输出:eid[xxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxx]
      67              :     // 其中前 8 字节是 subnetPrefix,后 8 字节是 interfaceId(网络字节序)
      68              : 
      69              :     // 从字节数组中提取两个 64 位整数(网络字节序)
      70           54 :     uint64_t subnetPrefix = 0;
      71           54 :     uint64_t interfaceId = 0;
      72              : 
      73              :     // 使用 memcpy 避免对齐问题
      74           54 :     (void)memcpy_s(&subnetPrefix, sizeof(subnetPrefix), eid, sizeof(subnetPrefix));
      75           54 :     (void)memcpy_s(&interfaceId, sizeof(interfaceId), eid + 8, sizeof(interfaceId));
      76              : 
      77              :     // 转换字节序(网络字节序 -> 主机字节序)
      78           54 :     subnetPrefix = be64toh(subnetPrefix);
      79           54 :     interfaceId = be64toh(interfaceId);
      80              : 
      81              :     char buffer[64];
      82           54 :     int32_t ret = snprintf_s(buffer, sizeof(buffer), sizeof(buffer) - 1,
      83              :         "eid[%016llx:%016llx]",
      84              :         static_cast<unsigned long long>(subnetPrefix),
      85              :         static_cast<unsigned long long>(interfaceId));
      86           54 :     if (ret < 0) {
      87            0 :         HCCL_ERROR("[%s] snprintf_s failed for EID", __func__);
      88            0 :         return "conversion failed";
      89              :     }
      90          108 :     return std::string(buffer);
      91              : }
      92              : 
      93           55 : std::string CommAddrLogger::ToString(const CommAddr& commAddr)
      94              : {
      95              :     // 模仿 IpAddress::Describe 的格式
      96              :     // 输出:IpAddress[eid[xxxxxxxxxxxxxxxx:xxxxxxxxxxxxxxxx], AF=v4/v6, addr=xxx.xxx.xxx.xxx, scopeId=0x0]
      97           55 :     std::string desc = "IpAddress[";
      98              : 
      99           55 :     switch (commAddr.type) {
     100           51 :         case COMM_ADDR_TYPE_IP_V4:
     101           51 :             desc += ConvertEID(commAddr.eid) + ", AF=v4, addr=" + ConvertIPv4(commAddr.addr) + "]";
     102           51 :             break;
     103            1 :         case COMM_ADDR_TYPE_IP_V6:
     104            1 :             desc += ConvertEID(commAddr.eid) + ", AF=v6, addr=" + ConvertIPv6(commAddr.addr6) + ", scopeId=0x0]";
     105            1 :             break;
     106            1 :         case COMM_ADDR_TYPE_ID:
     107            1 :             desc += "id:" + ConvertID(commAddr.id) + "]";
     108            1 :             break;
     109            1 :         case COMM_ADDR_TYPE_EID:
     110            1 :             desc += ConvertEID(commAddr.eid) + "]";
     111            1 :             break;
     112            1 :         default:
     113            1 :             desc += "Unknown]";
     114            1 :             break;
     115              :     }
     116              : 
     117           55 :     return desc;
     118            0 : }
     119              : 
     120           10 : void CommAddrLogger::Print(uint32_t idx, const char* endpointName, const CommAddr& commAddr)
     121              : {
     122           10 :     std::string addrStr = ToString(commAddr);
     123           10 :     HCCL_INFO("[%s] channelDescs[%u] %s commAddr: type[%d], addr[%s]",
     124              :         __func__, idx, endpointName, commAddr.type, addrStr.c_str());
     125           10 : }
     126              : 
     127              : } // namespace logger
     128              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1