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

Generated by: LCOV version 2.0-1