LCOV - code coverage report
Current view: top level - legacy/ascend910/pub_inc - mr_manager.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 0.0 % 39 0
Test Date: 2026-07-28 12:11:00 Functions: 0.0 % 10 0

            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              : #ifndef HCCL_MR_MANAGER_H
      12              : #define HCCL_MR_MANAGER_H
      13              : 
      14              : #include <string>
      15              : #include <map>
      16              : #include <unordered_map>
      17              : #include <vector>
      18              : #include <atomic>
      19              : #include <mutex>
      20              : #include "hccl/base.h"
      21              : #include "hccl_common.h"
      22              : #include "hccl_network_pub.h"
      23              : 
      24              : namespace hccl {
      25              : using HcclMrInfo = struct TagHcclMrInfo {
      26              :     void *addr; /**< starting address of mr */
      27              :     unsigned long long size; /**< size of mr */
      28              :     int access; /**< access of mr, reference to RaAccessFlags */
      29              :     unsigned int lkey; /**< local addr access key */
      30              : };
      31              : 
      32              : using MrInfo = struct TagMrInfo {
      33              :     void *addr; /**< starting address of mr */
      34              :     void *devVirAddr;
      35              :     unsigned long long size; /**< size of mr */
      36              :     int access; /**< access of mr, reference to RaAccessFlags */
      37              :     unsigned int lkey; /**< local addr access key */
      38              :     MrHandle mrHandle; /**< ibv_mr handle */
      39              :     int gloMemRef;
      40              :     int tmpMemRef;
      41              : 
      42            0 :     TagMrInfo() : addr(nullptr), size(0), access(0), lkey(0), mrHandle(nullptr), gloMemRef(0), tmpMemRef(0)
      43              :     {
      44            0 :     }
      45              : 
      46            0 :     TagMrInfo(void *addr, u64 size) : addr(addr), size(size), access(0), lkey(0), mrHandle(nullptr),
      47            0 :         gloMemRef(0), tmpMemRef(0)
      48              :     {
      49            0 :     }
      50              : 
      51            0 :     TagMrInfo &operator=(const TagMrInfo &that)
      52              :     {
      53            0 :         if (&that != this) {
      54            0 :             addr = that.addr;
      55            0 :             devVirAddr = that.devVirAddr;
      56            0 :             mrHandle = that.mrHandle;
      57            0 :             size = that.size;
      58            0 :             access = that.access;
      59            0 :             lkey = that.lkey;
      60            0 :             mrHandle = that.mrHandle;
      61            0 :             gloMemRef = that.gloMemRef;
      62            0 :             tmpMemRef = that.tmpMemRef;
      63              :         }
      64            0 :         return *this;
      65              :     }
      66              : 
      67            0 :     TagMrInfo &operator=(const HcclMrInfo &mrInfo)
      68              :     {
      69            0 :         addr = mrInfo.addr;
      70            0 :         size = mrInfo.size;
      71            0 :         access = mrInfo.access;
      72            0 :         lkey = mrInfo.lkey;
      73            0 :         return *this;
      74              :     }
      75              : };
      76              : 
      77              : struct MrMapKey {
      78              :     u64 addr;
      79              :     u64 size;
      80              : 
      81            0 :     MrMapKey() : addr(0), size(0)
      82              :     {
      83            0 :     }
      84              : 
      85            0 :     MrMapKey(u64 addr, u64 size) : addr(addr), size(size)
      86              :     {
      87            0 :     }
      88              : 
      89            0 :     MrMapKey(const MrMapKey &that) : addr(that.addr), size(that.size)
      90              :     {
      91            0 :     }
      92              : 
      93              :     MrMapKey &operator=(const MrMapKey &that)
      94              :     {
      95              :         if (&that != this) {
      96              :             addr = that.addr;
      97              :             size = that.size;
      98              :         }
      99              :         return *this;
     100              :     }
     101              : 
     102            0 :     bool operator <(const MrMapKey &that) const
     103              :     {
     104            0 :         return ((this->addr < that.addr) || ((this->addr == that.addr) && (this->size < that.size)));
     105              :     }
     106              : 
     107              :     bool operator == (const MrMapKey &that) const
     108              :     {
     109              :         return ((this->addr == that.addr) && (this->size == that.size));
     110              :     }
     111              : };
     112              : 
     113              : struct HostMappingKey {
     114              :     u64 addr = 0;
     115              :     u64 size = 0;
     116              :     u32 devId = 0;
     117              : 
     118            0 :     HostMappingKey(u64 addr, u64 size, u32 devId) : addr(addr), size(size), devId(devId)
     119              :     {
     120            0 :     }
     121              : 
     122            0 :     bool operator <(const HostMappingKey &that) const
     123              :     {
     124            0 :         if (addr != that.addr) {
     125            0 :             return addr < that.addr;
     126              :         }
     127            0 :         if (size != that.size) {
     128            0 :             return size < that.size;
     129              :         }
     130            0 :         return devId < that.devId;
     131              :     }
     132              : };
     133              : 
     134              : struct HostMappingInfo {
     135              :     void *devVirAddr = nullptr;
     136              :     int mappingRef = 0;
     137              : };
     138              : 
     139              : class MrManager {
     140              : public:
     141              :     static MrManager &GetInstance();
     142              :     MrManager();
     143              :     explicit MrManager(HcclNetDevCtx netDevCtx);
     144              :     ~MrManager();
     145              :     HcclResult Init(RdmaHandle rdmaHandle);                 // 初始化给rdmaHandle赋值,每初始化一次count++
     146              :     HcclResult Init(QpHandle qpHandle, u32 devId, bool isHostMem, std::map<MrMapKey, MrInfo>& unRegMrMap);
     147              :     HcclResult Init(RdmaHandle rdmaHandle, u32 devId, bool isHostMem);
     148              :     HcclResult DeInit(const void *handle);         // 判断rdmaHandle是否一致,每去初始化一次count--
     149              :     HcclResult Init();
     150              :     HcclResult DeInit();
     151              :     HcclResult RegGlobalMr(void *addr, u64 size);       // 注册全局mr,网卡未初始化时先记录mr信息
     152              :     HcclResult GetKey(void *addr, u64 size, u32 &lkey); // 拿到内存对应的key
     153              :     HcclResult ReleaseKey(void *addr, u64 size);        // 释放key操作权限
     154              :     HcclResult DeRegGlobalMr(void *addr);               // 解注册全局mr 地址必须是起始地址
     155              :     HcclResult DelayedReg(void *addr, u64 size);
     156              :     HcclResult GetDevVirAddr(void *addr, u64 size, u64 &devVirAddr);
     157              :     HcclResult MapMem(void *addr, u64 size, void *&devVirAddr);
     158              :     std::map<MrMapKey, MrInfo> GetUnregMap();
     159              :     void SetHdcPara(u32 devId, bool isHostMem, bool isUseQPHandle);
     160              :     HcclResult InitUnRegMrMap(std::map<MrMapKey, MrInfo>& unRegMrMap);
     161              : 
     162              :     // delete copy and move constructors and assign operators
     163              :     MrManager(MrManager const&) = delete;             // Copy construct
     164              :     MrManager(MrManager&&) = delete;                  // Move construct
     165              :     MrManager& operator=(MrManager const&) = delete;  // Copy assign
     166              :     MrManager& operator=(MrManager &&) = delete;      // Move assign
     167              :     std::unordered_map<void *, u64> addrSize_;
     168              :     std::vector<void *> deAddr_;
     169              :     static u64 g_devAddr;
     170              : 
     171              : private:
     172              :     static constexpr s32 COUNT_ONE = 1;
     173              :     bool isUseQPHandle_ = false;
     174              :     bool IsHostMem_ = false;
     175              :     u32 curDevId_ = -1;
     176              : 
     177              :     HcclResult InitMrManager(void *handle);
     178              :     HcclResult RegMr(void *addr, u64 size);                     // 注册全局mr
     179              :     HcclResult RegTmpMr(void *addr, u64 size, u32 &lkey);       // 注册临时mr
     180              :     HcclResult GetMrInfo(MrInfo &mrInfo, bool &isInfoNotFound);  // 通过地址查询mrMap中是否存在对应的内存
     181              :     HcclResult ReleaseMrResource();
     182              :     HcclResult RegMrImpl(void *addr, u64 size, HcclMrInfo &mrInfo, MrHandle &mrHandle, void *&devVirAddr);
     183              :     HcclResult DeRegMrImpl(MrInfo mrInfo);
     184              :     HcclResult UnmapMem(MrInfo mrInfo);
     185              :     std::map<HostMappingKey, HostMappingInfo>::iterator SearchMappingMap(u64 userAddr, u64 userSize);
     186              :     void TransMrInfo(void* addr, u64 size, HcclMrInfo& mrInfo);
     187              :     bool IsRequireMapping(void *addr, u64 size, void *&devVirAddr);
     188              :     HcclResult InitUnRegMrMap();
     189              : 
     190              :     RdmaHandle rdmaHandle_ = nullptr;
     191              :     QpHandle qpHandle_ = nullptr;
     192              :     std::atomic<int> count_; // 网卡初始化计数
     193              :     std::map<MrMapKey, MrInfo> unRegMrMap_;   // 网卡未初始化时记录内存map
     194              :     std::map<MrMapKey, MrInfo> regedMrMap_;    // 全局内存map
     195              :     std::map<void *, u64> globalAddrSizeMap_;
     196              :     static std::map<HostMappingKey, HostMappingInfo> mappedHostToDevMap_;
     197              :     std::mutex addrSizeMutex_;
     198              :     std::mutex mrMapSpinMutex_;                // 自旋锁,锁外部注册全局内存map、内部注册全局内存map
     199              :     std::mutex unMrMapSpinMutex_;              // 自旋锁,锁网卡未初始化时记录内存map
     200              :     static std::mutex mappedHostToDevMutex_;
     201              :     HcclNetDevCtx netDevCtx_;
     202              : };
     203              : }  // namespace hccl
     204              : #endif  // HCCL_MR_MANAGER_H
        

Generated by: LCOV version 2.0-1