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

Generated by: LCOV version 2.0-1