LCOV - code coverage report
Current view: top level - legacy/ascend950/framework/resource_manager/socket - host_socket_handle_manager.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 83.6 % 67 56
Test Date: 2026-08-18 17:47:01 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 "host_socket_handle_manager.h"
      12              : #include "exception_util.h"
      13              : 
      14              : namespace Hccl {
      15              : 
      16            1 : HostSocketHandleManager::HostSocketHandleManager() { hostSocketHandleMap.resize(MAX_DEVICE_NUM); }
      17              : 
      18            1 : HostSocketHandleManager::~HostSocketHandleManager() { DECTOR_TRY_CATCH("HostSocketHandleManager", DestroyAll()); }
      19              : 
      20           27 : HostSocketHandleManager& HostSocketHandleManager::GetInstance()
      21              : {
      22           27 :     static HostSocketHandleManager hostSocketHandleManager;
      23           27 :     return hostSocketHandleManager;
      24              : }
      25              : 
      26            2 : SocketHandle HostSocketHandleManager::Create(DevId devicePhyId, const IpAddress& hostIp)
      27              : {
      28            6 :     HCCL_INFO("[HostSocketHandleManager::%s] start", __func__);
      29              : 
      30              :     // 加锁
      31            2 :     std::lock_guard<std::mutex> lock(socketHandleLock);
      32              : 
      33            2 :     if (isDestroy) {
      34            0 :         HCCL_WARNING(
      35              :             "[HostSocketHandleManager::%s] devicePhyId[%u] HostSocketHandleManager has been destroy", __func__,
      36              :             devicePhyId);
      37            0 :         return nullptr;
      38              :     }
      39              : 
      40              :     // 校验devicePhyId
      41            2 :     CHK_PRT_THROW(
      42              :         (devicePhyId > hostSocketHandleMap.size() - 1),
      43              :         HCCL_ERROR("[HostSocketHandleManager::%s] devicePhyId[%u] error", __func__, devicePhyId),
      44              :         InvalidParamsException, "devicePhyId error");
      45              : 
      46              :     // 若socketHandle已存在,引用计数+1
      47            2 :     auto& handles = hostSocketHandleMap[devicePhyId];
      48            2 :     auto ip = hostIp.GetIpStr();
      49            2 :     if (handles.count(ip) != 0) {
      50            1 :         handles[ip].second.Ref();
      51            3 :         HCCL_INFO(
      52              :             "[HostSocketHandleManager::%s] devicePhyId[%u] hostIp[%d] socket has initialized,"
      53              :             " ref[%u]",
      54              :             __func__, devicePhyId, ip.c_str(), handles[ip].second.Count());
      55            1 :         return handles[ip].first;
      56              :     }
      57              : 
      58              :     // 初始化socketHandle
      59            1 :     RaInterface intf{};
      60            1 :     intf.phyId = devicePhyId;
      61            1 :     intf.address = hostIp;
      62              : 
      63            1 :     SocketHandle socketHandle = HrtRaSocketInit(HrtNetworkMode::PEER, intf);
      64            1 :     handles[ip] = std::make_pair(socketHandle, Referenced());
      65            1 :     handles[ip].second.Ref();
      66              : 
      67            3 :     HCCL_INFO(
      68              :         "[HostSocketHandleManager::%s] devicePhyId[%u] hostIp[%s] create end.", __func__, devicePhyId,
      69              :         hostIp.GetIpStr().c_str());
      70            1 :     return socketHandle;
      71            2 : }
      72              : 
      73            2 : SocketHandle HostSocketHandleManager::Get(DevId devicePhyId, const IpAddress& hostIp)
      74              : {
      75            2 :     std::lock_guard<std::mutex> lock(socketHandleLock);
      76              : 
      77            2 :     if (isDestroy) {
      78            0 :         HCCL_WARNING(
      79              :             "[HostSocketHandleManager::%s] devicePhyId[%u] HostSocketHandleManager has been detroy", __func__,
      80              :             devicePhyId);
      81            0 :         return nullptr;
      82              :     }
      83              : 
      84            2 :     if (devicePhyId > hostSocketHandleMap.size() - 1) {
      85            0 :         HCCL_WARNING("HostSocketHandleManager for devicePhyId=%u dose not exist", devicePhyId);
      86            0 :         return nullptr;
      87              :     }
      88              : 
      89            2 :     auto tempiter = hostSocketHandleMap[devicePhyId].find(hostIp.GetIpStr());
      90            2 :     if (tempiter == hostSocketHandleMap[devicePhyId].end()) {
      91            3 :         HCCL_WARNING("HostSocketHandleManager for IpAddress=%s dose not exist", hostIp.GetIpStr().c_str());
      92            1 :         return nullptr;
      93              :     }
      94              : 
      95            1 :     return tempiter->second.first;
      96            2 : }
      97              : 
      98            2 : void HostSocketHandleManager::DestroyAll()
      99              : {
     100            2 :     std::lock_guard<std::mutex> lock(socketHandleLock);
     101              : 
     102            2 :     isDestroy = true;
     103              : 
     104          132 :     for (u32 i = 0; i < hostSocketHandleMap.size(); ++i) {
     105          131 :         for (const auto& innerMap : hostSocketHandleMap[i]) {
     106            1 :             u32 count = innerMap.second.second.Count();
     107            3 :             CHK_PRT_CONT(
     108              :                 count != 0, HCCL_WARNING(
     109              :                                 "[HostSocketHandleManager::%s] release is not as expected, "
     110              :                                 "devicePhyId[%u] hostIp[%s] ref[%u]",
     111              :                                 __func__, i, innerMap.first.c_str(), count));
     112            1 :             DECTOR_TRY_CATCH("HrtRaSocketDeInit Exception", HrtRaSocketDeInit(innerMap.second.first));
     113              :         }
     114          130 :         hostSocketHandleMap[i].clear();
     115              :     }
     116            2 : }
     117              : 
     118            1 : void HostSocketHandleManager::Destroy(DevId devicePhyId, const IpAddress& hostIp)
     119              : {
     120            1 :     std::lock_guard<std::mutex> lock(socketHandleLock);
     121              : 
     122            1 :     if (isDestroy) {
     123            0 :         HCCL_WARNING(
     124              :             "[HostSocketHandleManager::%s] devicePhyId[%u] HostSocketHandleManager has been detroy", __func__,
     125              :             devicePhyId);
     126            0 :         return;
     127              :     }
     128              : 
     129              :     // 校验devicePhyId
     130            1 :     CHK_PRT_THROW(
     131              :         (devicePhyId > hostSocketHandleMap.size() - 1),
     132              :         HCCL_ERROR("[HostSocketHandleManager::%s] devicePhyId[%u] invalid", __func__, devicePhyId),
     133              :         InvalidParamsException, "devicePhyId invalid");
     134              : 
     135              :     // 校验hostIp
     136            1 :     CHK_PRT_THROW(
     137              :         hostSocketHandleMap[devicePhyId].count(hostIp.GetIpStr()) == 0,
     138              :         HCCL_ERROR(
     139              :             "[HostSocketHandleManager::%s] devicePhyId[%u] hostIp[%s] dose not exist", __func__, devicePhyId,
     140              :             hostIp.GetIpStr().c_str()),
     141              :         InvalidParamsException, "hostIp not exist");
     142              : 
     143              :     // 引用计数-1
     144            1 :     auto& socketHandleRef = hostSocketHandleMap[devicePhyId][hostIp.GetIpStr()];
     145            1 :     socketHandleRef.second.Unref();
     146              : 
     147              :     // 打印
     148            1 :     u32 count = socketHandleRef.second.Count();
     149            3 :     HCCL_INFO(
     150              :         "[HostSocketHandleManager::%s] devicePhyId[%u] hostIp[%s] release one, ref[%u].", __func__, devicePhyId,
     151              :         hostIp.GetIpStr().c_str(), count);
     152              : 
     153              :     // 若引用计数为0则deinit
     154            1 :     if (count == 0) {
     155            0 :         HrtRaSocketDeInit(socketHandleRef.first);
     156            0 :         hostSocketHandleMap[devicePhyId].erase(hostIp.GetIpStr());
     157            0 :         HCCL_INFO(
     158              :             "[HostSocketHandleManager::%s] devicePhyId[%u] hostIp[%s] deinit success.", __func__, devicePhyId,
     159              :             hostIp.GetIpStr().c_str());
     160              :     }
     161            1 : }
     162              : 
     163              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1