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 % 17 17
Test Date: 2026-08-17 10:19:35 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          306 :     bool operator==(const MemMgrCacheKey& other) const
      37              :     {
      38          306 :         return devPhyId == other.devPhyId && protocol == other.protocol && ip == other.ip && portType == other.portType;
      39              :     }
      40              : };
      41              : 
      42              : struct MemMgrCacheKeyHash {
      43          430 :     size_t operator()(const MemMgrCacheKey& k) const
      44              :     {
      45          430 :         return Hccl::HashCombine({
      46          430 :             std::hash<u32>{}(k.devPhyId),
      47          430 :             std::hash<int>{}(static_cast<int>(k.protocol)),
      48          430 :             std::hash<Hccl::IpAddress>{}(k.ip),
      49          860 :             std::hash<int>{}(static_cast<int>(k.portType)),
      50          860 :         });
      51              :     }
      52              : };
      53              : 
      54              : // 由 endpointDesc.loc.locType 推导 PortDeploymentType
      55          199 : inline Hccl::PortDeploymentType LocTypeToPortType(EndpointLocType locType)
      56              : {
      57          199 :     return (locType == ENDPOINT_LOC_TYPE_DEVICE) ? Hccl::PortDeploymentType::DEV_NET :
      58          199 :                                                    Hccl::PortDeploymentType::HOST_NET;
      59              : }
      60              : 
      61              : struct MemMgrEntry {
      62              :     std::shared_ptr<RegedMemMgr> mgrPtr{nullptr};
      63              :     u64 refCount{0};
      64              : };
      65              : 
      66              : class ProcRegedMemMgrCache {
      67              : public:
      68          418 :     static ProcRegedMemMgrCache& GetInstance()
      69              :     {
      70          418 :         static ProcRegedMemMgrCache instance;
      71          418 :         return instance;
      72              :     }
      73              : 
      74              :     // hit: refCount++ 返已有 shared_ptr; miss: 调 creator() 建实例 insert refCount=1
      75              :     std::shared_ptr<RegedMemMgr>
      76              :     GetOrCreate(const MemMgrCacheKey& key, std::function<std::shared_ptr<RegedMemMgr>()> creator);
      77              : 
      78              :     // refCount--, 归 0 则 erase cacheMap_ 条目
      79              :     void Release(const MemMgrCacheKey& key);
      80              : 
      81              :     ProcRegedMemMgrCache(const ProcRegedMemMgrCache&) = delete;
      82              :     ProcRegedMemMgrCache& operator=(const ProcRegedMemMgrCache&) = delete;
      83              : 
      84              : private:
      85            3 :     ProcRegedMemMgrCache() = default;
      86            3 :     ~ProcRegedMemMgrCache() = default;
      87              : 
      88              :     std::mutex mtx_;
      89              :     std::unordered_map<MemMgrCacheKey, MemMgrEntry, MemMgrCacheKeyHash> cacheMap_;
      90              : };
      91              : 
      92              : } // namespace hcomm
      93              : 
      94              : #endif // PROC_REGED_MEM_MGR_CACHE_H
        

Generated by: LCOV version 2.0-1