LCOV - code coverage report
Current view: top level - legacy/ascend910/platform/typical - rdma_resource_manager.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 69.9 % 146 102
Test Date: 2026-07-28 12:11:00 Functions: 83.3 % 12 10

            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              : #include "rdma_resource_manager.h"
      11              : #include "dltdt_function.h"
      12              : #include "adapter_hccp.h"
      13              : #include "adapter_tdt.h"
      14              : #include "externalinput_pub.h"
      15              : #include "adapter_hccp_common.h"
      16              : #include "adapter_rts_common.h"
      17              : #include "network_manager_pub.h"
      18              : 
      19              : namespace hccl{
      20              : //预留后续扩展多个池子信息,使用逗号,间隔resv_mem_$dev_id=$type_$pool_id_$total_mem_size_$page_size
      21              : #define HCCN_RESV_MEM_TYPE_OFFSET (0)
      22              : #define HCCN_RESV_MEM_POOLID_OFFSET (1)
      23              : #define HCCN_RESV_MEM_PAGESIZE_OFFSET (3)
      24              : #define HCCN_RSCV_MEM_COUNT (4)
      25              : #define HCCN_RESV_MEM_PAGESIZE_64K (64)
      26              : constexpr u32 RETRY_CQE_ARRAY_SIZE = 128; // 重执行时获取的CQE数组的最大数量,最大128
      27           66 : RdmaResourceManager::RdmaResourceManager() : nicDeploy_(NICDeployment::NIC_DEPLOYMENT_DEVICE)
      28              : {
      29           66 : }
      30              : 
      31           66 : RdmaResourceManager::~RdmaResourceManager()
      32              : {
      33           66 :     DeInit();
      34           66 : }
      35              : 
      36          541 : RdmaResourceManager& RdmaResourceManager::GetInstance() 
      37              : {
      38          607 :     static RdmaResourceManager rdmaInstance[MAX_MODULE_DEVICE_NUM + 1];
      39          541 :     s32 deviceLogicId = INVALID_INT;
      40          541 :     HcclResult ret = hrtGetDevice(&deviceLogicId);
      41          541 :     if (ret == HCCL_SUCCESS && (static_cast<u32>(deviceLogicId) < MAX_MODULE_DEVICE_NUM)) {
      42          541 :         HCCL_INFO("[RdmaResourceManager::GetInstance]deviceLogicID[%d]", deviceLogicId);
      43          541 :         return rdmaInstance[deviceLogicId];
      44              :     }
      45            0 :     HCCL_WARNING("[RdmaResourceManager::GetInstance]deviceLogicID[%d] is invalid, ret[%d]", deviceLogicId, ret);
      46            0 :     return rdmaInstance[MAX_MODULE_DEVICE_NUM];
      47              : }
      48              : 
      49           32 : HcclResult RdmaResourceManager::Init()
      50              : {
      51           32 :     HcclResult ret = InitExternalInput();
      52           32 :     CHK_PRT_RET(ret != HCCL_SUCCESS, HCCL_ERROR("[RdmaResourceManager][Init]errNo[0x%016llx] init external input error.",
      53              :         HCCL_ERROR_CODE(ret)), HCCL_E_PARA);
      54           32 :     HCCL_INFO("[RdmaResourceManager][Init] Init ExternalInput success!");
      55              : 
      56           32 :     CHK_RET(hrtGetDevice(&deviceLogicId_));
      57           32 :     CHK_RET(hrtGetDevicePhyIdByIndex(static_cast<u32>(deviceLogicId_), devicePhyId_, true));
      58           32 :     CHK_RET(NetworkManager::GetInstance(deviceLogicId_).Init(nicDeploy_, false, devicePhyId_, true));
      59           32 :     HCCL_DEBUG("[RdmaResourceManager][Init] NetworkManager Init, deviceLogicId[%d], devicePhyId[%u], nicDeployment_[%d]",
      60              :         deviceLogicId_, devicePhyId_, nicDeploy_);
      61              :         
      62           32 :     std::vector<HcclIpAddress> deviceIPs;
      63           32 :     CHK_RET(hrtRaGetDeviceIP(devicePhyId_, deviceIPs));
      64           32 :     CHK_PRT_RET(deviceIPs.size() < 1,
      65              :         HCCL_ERROR("[RdmaResourceManager][Init] Get ip address failed, deviceLogicId[%d], devicePhyId[%u]",
      66              :         deviceLogicId_, devicePhyId_), HCCL_E_INTERNAL);
      67              : 
      68           32 :     for (u32 ipIdex = 0; ipIdex < deviceIPs.size(); ipIdex++) {
      69           32 :         if (deviceIPs[ipIdex].IsInvalid()) {
      70            0 :             continue;
      71              :         }
      72              :         // port传入的值为无效值0xFFFFFFFF, 不启动监听
      73           32 :         ret = NetworkManager::GetInstance(deviceLogicId_).StartNic(deviceIPs[ipIdex], port_, true);
      74           32 :         CHK_PRT_RET(ret != HCCL_SUCCESS,
      75              :         HCCL_ERROR("[RdmaResourceManager][Init] Start nic ipaddr[%s] failed", deviceIPs[ipIdex].GetReadableAddress()),
      76              :             ret);
      77           32 :         ipAddr_ = deviceIPs[ipIdex];
      78           32 :         break;
      79              :     }
      80           32 :     if (ipAddr_.IsInvalid()) {
      81            0 :         HCCL_ERROR("[RdmaResourceManager][Init] No valid ipAddr, devicePhyId is [%u].", devicePhyId_);
      82            0 :         return HCCL_E_NOT_FOUND;
      83              :     }
      84           32 :     CHK_RET(NetworkManager::GetInstance(deviceLogicId_).GetRdmaHandleByIpAddr(ipAddr_, rdmaHandle_));
      85           32 :     CHK_PTR_NULL(rdmaHandle_);
      86           32 :     HCCL_INFO("[RdmaResourceManager][Init] Start nic, devicePhyId is [%u], ip address[%s], port[%u].",
      87              :         devicePhyId_, this->ipAddr_.GetReadableAddress(), this->port_);
      88              : 
      89           32 :     CHK_RET(HrtRaGetNotifyBaseAddr(rdmaHandle_, &notifyBaseVa_, &notifyTotalSize_));
      90           32 :     HCCL_INFO("[RdmaResourceManager][Init] NotifyBaseAddr addr[%llu] size[%llu].", notifyBaseVa_,
      91              :         notifyTotalSize_);
      92              :  
      93           32 :     notifyMrInfo_.addr = reinterpret_cast<void*>(notifyBaseVa_);
      94           32 :     notifyMrInfo_.size = notifyTotalSize_;
      95           32 :     notifyMrInfo_.access = RA_ACCESS_REMOTE_WRITE | RA_ACCESS_LOCAL_WRITE | RA_ACCESS_REMOTE_READ;
      96           32 :     CHK_RET(hrtRaRegGlobalMr(rdmaHandle_, notifyMrInfo_, mrHandle_));
      97           32 :     HCCL_RUN_INFO("[RdmaResourceManager][Init] Register NotifyBaseAddr addr[%p] size[%llu] key[%u]", notifyMrInfo_.addr,
      98              :         notifyMrInfo_.size, notifyMrInfo_.lkey);
      99           32 :     CHK_RET(InitResvMemInfo());
     100           32 :     return HCCL_SUCCESS;
     101           32 : }
     102              : 
     103            2 : void RdmaResourceManager::SetDisableLiteThread(bool disable)
     104              : {
     105            2 :     s32 deviceLogicId = INVALID_INT;
     106            2 :     hrtGetDevice(&deviceLogicId);
     107            2 :     NetworkManager::GetInstance(deviceLogicId).SetDisableLiteThread(disable);
     108            2 : }
     109              : 
     110          358 : HcclResult RdmaResourceManager::GetRdmaHandle(RdmaHandle& rdmaHandle)
     111              : {
     112          358 :     CHK_PTR_NULL(rdmaHandle_);
     113          358 :     rdmaHandle = rdmaHandle_;
     114          358 :     return HCCL_SUCCESS;
     115              : }
     116              : 
     117           98 : HcclResult RdmaResourceManager::DeInit()
     118              : {
     119           98 :     if (rdmaHandle_ == nullptr) {
     120           66 :         HCCL_INFO("[RdmaResourceManager][DeInit] RDMA resource has been already deinited!");
     121           66 :         return HCCL_SUCCESS;
     122              :     }
     123           32 :     CHK_PTR_NULL(mrHandle_);
     124           32 :     CHK_RET(hrtRaDeRegGlobalMr(rdmaHandle_, mrHandle_));
     125           32 :     CHK_RET(NetworkManager::GetInstance(deviceLogicId_).StopNic(ipAddr_, port_));
     126           32 :     rdmaHandle_ = nullptr;
     127           32 :     HCCL_INFO("[RdmaResourceManager][DeInit] Stop nic, devicePhyId is [%u], ip address[%s], port[%u].",
     128              :         devicePhyId_, ipAddr_.GetReadableAddress(), port_);
     129           32 :     CHK_RET(NetworkManager::GetInstance(deviceLogicId_).DeInit(nicDeploy_));
     130           32 :     return HCCL_SUCCESS;
     131              : }
     132              : 
     133            0 : HcclResult RdmaResourceManager::GetCqeErrInfo(struct CqeErrInfo *infoList, u32 *num)
     134              : {
     135            0 :     CHK_PTR_NULL(rdmaHandle_);
     136            0 :     return hrtRaGetCqeErrInfoList(rdmaHandle_, infoList, num);
     137              : }
     138              : 
     139            0 : HcclResult RdmaResourceManager::GetCqeErrInfoByQpn(u32 qpn, struct HcclErrCqeInfo *errCqeList, u32 *num)
     140              : {
     141            0 :     CHK_PTR_NULL(rdmaHandle_);
     142            0 :     u32 qpnNum = RETRY_CQE_ARRAY_SIZE;
     143            0 :     u32 listLen = *num;
     144            0 :     std::unique_lock<std::mutex> lock(cqeErrMapMutex_);
     145            0 :     struct CqeErrInfo cqeErrInfolist[qpnNum] = {};
     146            0 :     CHK_RET(hrtRaGetCqeErrInfoList(rdmaHandle_, cqeErrInfolist, &qpnNum));
     147            0 :     for (u32 i = 0; i < qpnNum; i++) {
     148            0 :         u32 errQpn = cqeErrInfolist[i].qpn;
     149            0 :         cqeErrPerQP_[errQpn].push(cqeErrInfolist[i]);
     150              :     }
     151            0 :     if (cqeErrPerQP_.empty() || cqeErrPerQP_.find(qpn) == cqeErrPerQP_.end()) {
     152            0 :         *num = 0;
     153              :     } else {
     154            0 :         *num = cqeErrPerQP_[qpn].size();
     155            0 :         if (*num > listLen) {
     156            0 :             *num = listLen;
     157            0 :             HCCL_WARNING("[GetCqeErrInfoByQpn] GetCqeErrInfo num is larger than infoList user given.");
     158              :         } 
     159            0 :         u32 i = 0;
     160            0 :         while (!cqeErrPerQP_[qpn].empty() && i < listLen) {
     161            0 :             errCqeList[i].qpn = cqeErrPerQP_[qpn].front().qpn;
     162            0 :             errCqeList[i].status = cqeErrPerQP_[qpn].front().status;
     163            0 :             errCqeList[i].time = cqeErrPerQP_[qpn].front().time;
     164            0 :             time_t tmpt = static_cast<time_t>(errCqeList[i].time.tv_sec);
     165              :             struct tm errTime;
     166            0 :             localtime_r(&tmpt, &errTime);
     167            0 :             HCCL_INFO("[GetCqeErrInfoByQpn] Err Cqe status[%d], qpn[%d], time[%04u-%02d-%02d %02d:%0d:%02d.%06u]", 
     168              :                 errCqeList[i].status, errCqeList[i].qpn, errTime.tm_year + TIME_FROM_1900,
     169              :                 errTime.tm_mon + 1,
     170              :                 errTime.tm_mday,
     171              :                 errTime.tm_hour,
     172              :                 errTime.tm_min,
     173              :                 errTime.tm_sec,
     174              :                 static_cast<u32>(errCqeList[i].time.tv_usec));
     175            0 :             cqeErrPerQP_[qpn].pop();    
     176            0 :             i++;
     177              :         }
     178              :     }
     179            0 :     return HCCL_SUCCESS;
     180            0 : }
     181              : 
     182           90 : HcclResult RdmaResourceManager::GetNotifyMrInfo(struct MrInfoT& mrInfo)
     183              : {
     184           90 :     CHK_PTR_NULL(rdmaHandle_);
     185           90 :     mrInfo.lkey = notifyMrInfo_.lkey;
     186           90 :     HCCL_RUN_INFO("[RdmaResourceManager][GetNotifyMrInfo] SyncMem key[%u].", mrInfo.lkey);
     187           90 :     return HCCL_SUCCESS;
     188              : }
     189              : 
     190           32 : HcclResult RdmaResourceManager::InitResvMemInfo() {
     191           32 :     std::string flagValue;
     192           32 :     std::vector<std::string> parts;
     193           32 :     std::vector<std::string> tokens;
     194           32 :     std::string token;
     195           32 :     HcclResult vRet = HrtRaGetHccnCfg(static_cast<std::uint32_t>(nicDeploy_), devicePhyId_,
     196              :         HccnCfgKeyT::HCCN_RESV_MEM_INFO, flagValue);
     197           32 :     if ((vRet != HCCL_SUCCESS) || (flagValue.empty())) {
     198           31 :         return vRet;
     199              :     }
     200            1 :     HCCL_DEBUG("[InitResvMemInfo] resvMemInfo[%s].", flagValue.c_str());
     201            1 :     std::istringstream partStream(flagValue);
     202            2 :     while (std::getline(partStream, token, ',')) {
     203            1 :         parts.push_back(token);
     204              :     }
     205            2 :     for (auto s:parts) {
     206            1 :         std::istringstream tokenStream(s);
     207            5 :         while (std::getline(tokenStream, token, '_')) {
     208            4 :             tokens.push_back(token);
     209              :         }
     210            1 :         if (tokens.size() != HCCN_RSCV_MEM_COUNT) {
     211            0 :             HCCL_ERROR("[InitResvMemInfo] resvMemInfo count[%u] is not equal [%u].",
     212              :                 tokens.size(), HCCN_RSCV_MEM_COUNT);
     213            0 :             return HCCL_E_PARA;
     214              :         }
     215            5 :         for (u32 i=0; i<tokens.size(); i++) {
     216           10 :             for (char c:tokens[i]) {
     217            6 :                 if (!std::isdigit(c)) {
     218            0 :                     HCCL_ERROR("[InitResvMemInfo] resvMemInfo[%s] is invalid.", tokens[i].c_str());
     219            0 :                     return HCCL_E_PARA;
     220              :                 }
     221              :             }
     222              :         }
     223              : 
     224            1 :         u32 type = std::stoi(tokens[HCCN_RESV_MEM_TYPE_OFFSET]);
     225            1 :         u32 pageSize = std::stoi(tokens[HCCN_RESV_MEM_PAGESIZE_OFFSET]);
     226            1 :         u32 poolId = std::stoi(tokens[HCCN_RESV_MEM_POOLID_OFFSET]);
     227              :         int supportLite;
     228            1 :         if (HCCL_SUCCESS == HrtGetRdmaLiteStatus(rdmaHandle_, &supportLite)) {
     229            1 :             if (((2 == supportLite) && (HCCN_RESV_MEM_PAGESIZE_64K == pageSize)) || (1 == supportLite)) {
     230            0 :                 resvMemInfo_.insert({type, poolId});
     231              :             }
     232              :         }
     233            1 :         tokens.clear();
     234            1 :     }
     235              : 
     236            1 :     return HCCL_SUCCESS;
     237           32 : }
     238              : 
     239           10 : HcclResult RdmaResourceManager::GetResvMemPoolIdByType(u32 type, u32& poolId)
     240              : {
     241           10 :     auto it = resvMemInfo_.find(type);
     242           10 :     if (it == resvMemInfo_.end()) {
     243           10 :         HCCL_WARNING("[RdmaResourceManager][GetResvMemPoolIdByType] can not find [%u].", type);
     244           10 :         return HCCL_E_NOT_FOUND;
     245              :     }
     246              : 
     247            0 :     poolId = resvMemInfo_[type];
     248            0 :     HCCL_RUN_INFO("[RdmaResourceManager][GetResvMemPoolIdByType] type[%u], resvMemPoolId [%u].", type, poolId);
     249            0 :     return HCCL_SUCCESS;
     250              : }
     251              : 
     252              : }
        

Generated by: LCOV version 2.0-1