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