LCOV - code coverage report
Current view: top level - base_comm/resources/reged_mems - proc_reged_mem_mgr_cache.h (source / functions) Coverage Total Hit
Test: coverage.info Lines: 100.0 % 20 20
Test Date: 2026-08-04 10:52:23 Functions: 100.0 % 6 6

            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              : #ifndef PROC_REGED_MEM_MGR_CACHE_H
      11              : #define PROC_REGED_MEM_MGR_CACHE_H
      12              : 
      13              : #include <cstdint>
      14              : #include <functional>
      15              : #include <memory>
      16              : #include <mutex>
      17              : #include <unordered_map>
      18              : #include "reged_mem_mgr.h"
      19              : #include "ip_address.h"
      20              : #include "port.h"
      21              : #include "hcomm_res_defs.h"
      22              : 
      23              : namespace hcomm {
      24              : 
      25              : /**
      26              :  * @note 职责:进程级RegedMemMgr复用缓存。
      27              :  *           同一网卡跨EndpointHandle复用整个RegedMemMgr实例,
      28              :  *           跳过冗余硬件内存注册。
      29              :  */
      30              : struct MemMgrCacheKey {
      31              :     u32 devPhyId{0};
      32              :     CommProtocol protocol{COMM_PROTOCOL_ROCE};
      33              :     Hccl::IpAddress ip{};
      34              :     Hccl::PortDeploymentType portType{Hccl::PortDeploymentType::HOST_NET};
      35              : 
      36          279 :     bool operator==(const MemMgrCacheKey &other) const
      37              :     {
      38          558 :         return devPhyId == other.devPhyId &&
      39          558 :                protocol == other.protocol &&
      40          837 :                ip == other.ip &&
      41          558 :                portType == other.portType;
      42              :     }
      43              : };
      44              : 
      45              : struct MemMgrCacheKeyHash {
      46          376 :     size_t operator()(const MemMgrCacheKey &k) const
      47              :     {
      48          376 :         return Hccl::HashCombine({
      49          376 :             std::hash<u32>{}(k.devPhyId),
      50          376 :             std::hash<int>{}(static_cast<int>(k.protocol)),
      51          376 :             std::hash<Hccl::IpAddress>{}(k.ip),
      52          752 :             std::hash<int>{}(static_cast<int>(k.portType)),
      53          752 :         });
      54              :     }
      55              : };
      56              : 
      57              : // 由 endpointDesc.loc.locType 推导 PortDeploymentType
      58          177 : inline Hccl::PortDeploymentType LocTypeToPortType(EndpointLocType locType)
      59              : {
      60          177 :     return (locType == ENDPOINT_LOC_TYPE_DEVICE) ? Hccl::PortDeploymentType::DEV_NET
      61          177 :                                                   : Hccl::PortDeploymentType::HOST_NET;
      62              : }
      63              : 
      64              : struct MemMgrEntry {
      65              :     std::shared_ptr<RegedMemMgr> mgrPtr{nullptr};
      66              :     u64 refCount{0};
      67              : };
      68              : 
      69              : class ProcRegedMemMgrCache {
      70              : public:
      71          374 :     static ProcRegedMemMgrCache &GetInstance()
      72              :     {
      73          374 :         static ProcRegedMemMgrCache instance;
      74          374 :         return instance;
      75              :     }
      76              : 
      77              :     // hit: refCount++ 返已有 shared_ptr; miss: 调 creator() 建实例 insert refCount=1
      78              :     std::shared_ptr<RegedMemMgr> GetOrCreate(const MemMgrCacheKey &key,
      79              :         std::function<std::shared_ptr<RegedMemMgr>()> creator);
      80              : 
      81              :     // refCount--, 归 0 则 erase cacheMap_ 条目
      82              :     void Release(const MemMgrCacheKey &key);
      83              : 
      84              :     ProcRegedMemMgrCache(const ProcRegedMemMgrCache &) = delete;
      85              :     ProcRegedMemMgrCache &operator=(const ProcRegedMemMgrCache &) = delete;
      86              : 
      87              : private:
      88            2 :     ProcRegedMemMgrCache() = default;
      89            2 :     ~ProcRegedMemMgrCache() = default;
      90              : 
      91              :     std::mutex mtx_;
      92              :     std::unordered_map<MemMgrCacheKey, MemMgrEntry, MemMgrCacheKeyHash> cacheMap_;
      93              : };
      94              : 
      95              : } // namespace hcomm
      96              : 
      97              : #endif // PROC_REGED_MEM_MGR_CACHE_H
        

Generated by: LCOV version 2.0-1