LCOV - code coverage report
Current view: top level - base_comm/common - eid_info_mgr.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.1 % 42 37
Test Date: 2026-08-18 17:47:01 Functions: 100.0 % 4 4

            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 "eid_info_mgr.h"
      12              : 
      13              : #include "hccl_common.h"
      14              : #include "orion_adpt_utils.h"
      15              : 
      16              : namespace hcomm {
      17              : 
      18          332 : EidInfoMgr& EidInfoMgr::GetInstance(const uint32_t devicePhyId)
      19              : {
      20          464 :     static EidInfoMgr eidInfoMgr[MAX_MODULE_DEVICE_NUM + 1];
      21              : 
      22          332 :     uint32_t devPhyId = devicePhyId;
      23          332 :     if (devPhyId >= MAX_MODULE_DEVICE_NUM) {
      24            0 :         HCCL_WARNING(
      25              :             "[EidInfoMgr][%s] use the backup device, devPhyId[%u] should be "
      26              :             "less than %u.",
      27              :             __func__, devPhyId, MAX_MODULE_DEVICE_NUM);
      28            0 :         devPhyId = MAX_MODULE_DEVICE_NUM; // 使用备份设备
      29              :     }
      30              : 
      31          332 :     eidInfoMgr[devPhyId].devPhyId_ = devPhyId;
      32          332 :     return eidInfoMgr[devPhyId];
      33              : }
      34              : 
      35           29 : HcclResult EidInfoMgr::Init()
      36              : {
      37           29 :     const RaInfo raInfo{NetworkMode::NETWORK_OFFLINE, devPhyId_};
      38           29 :     CHK_RET(RaGetDevEidInfos(raInfo, eidInfos_));
      39              : 
      40           29 :     initflag_ = true;
      41           29 :     if (eidInfos_.empty()) {
      42            1 :         HCCL_ERROR(
      43              :             "[EidInfoMgr][%s] failed to find any eid info, "
      44              :             "devPhyId[%u].",
      45              :             __func__, devPhyId_);
      46            1 :         return HcclResult::HCCL_E_NOT_FOUND;
      47              :     }
      48              : 
      49          109 :     for (const auto& eidInfo : eidInfos_) {
      50           81 :         Hccl::IpAddress ipAddr{}; // 暂时使用orion ip addr 用作索引
      51           81 :         CHK_RET(CommAddrToIpAddress(eidInfo.commAddr, ipAddr));
      52           81 :         eidInfoMap_[ipAddr] = eidInfo;
      53              :     }
      54              : 
      55           28 :     return HcclResult::HCCL_SUCCESS;
      56              : }
      57              : 
      58          302 : HcclResult EidInfoMgr::GetEidInfos(std::vector<DevEidInfo>& eidInfos)
      59              : {
      60          302 :     std::unique_lock<std::mutex> lock(innerMutex_);
      61          302 :     if (!initflag_) {
      62           29 :         CHK_RET(Init());
      63              :     }
      64              : 
      65              :     // 不允许外部修改eidInfo,传递拷贝结果
      66          301 :     eidInfos.assign(eidInfos_.begin(), eidInfos_.end());
      67              : 
      68          301 :     HCCL_INFO("[EidInfoMgr][%s] found %zu eid info, devPhyId[%u].", __func__, eidInfos.size(), devPhyId_);
      69              : 
      70          301 :     return HCCL_SUCCESS;
      71          302 : }
      72              : 
      73           30 : HcclResult EidInfoMgr::GetEidInfoByAddr(const CommAddr& commAddr, DevEidInfo& eidInfo)
      74              : {
      75           30 :     std::unique_lock<std::mutex> lock(innerMutex_);
      76           30 :     if (!initflag_) {
      77            0 :         CHK_RET(Init());
      78              :     }
      79              : 
      80           30 :     Hccl::IpAddress ipAddr{}; // 暂时使用orion ipaddr 用作索引
      81           30 :     CHK_RET(CommAddrToIpAddress(commAddr, ipAddr));
      82              :     // 兼容上层传递ipv4查询eid
      83           30 :     const auto eidAddr = Hccl::IpAddress(ipAddr.GetEid());
      84              : 
      85           30 :     const auto addrIter = eidInfoMap_.find(eidAddr);
      86           30 :     if (addrIter == eidInfoMap_.end()) {
      87            0 :         HCCL_ERROR(
      88              :             "[EidInfoMgr][%s] failed to find eid info by ip addr[%s], "
      89              :             "devPhyId[%u].",
      90              :             __func__, ipAddr.Describe().c_str(), devPhyId_);
      91            0 :         return HcclResult::HCCL_E_NOT_FOUND;
      92              :     }
      93              : 
      94           30 :     eidInfo = addrIter->second; // 触发深拷贝,避免外部修改值
      95           30 :     return HcclResult::HCCL_SUCCESS;
      96           30 : }
      97              : 
      98              : }; // namespace hcomm
        

Generated by: LCOV version 2.0-1