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 : #include "proc_reged_mem_mgr_cache.h"
12 : #include "log.h"
13 :
14 : namespace hcomm {
15 :
16 : std::shared_ptr<RegedMemMgr>
17 201 : ProcRegedMemMgrCache::GetOrCreate(const MemMgrCacheKey& key, std::function<std::shared_ptr<RegedMemMgr>()> creator)
18 : {
19 201 : std::lock_guard<std::mutex> lock(mtx_);
20 201 : auto it = cacheMap_.find(key);
21 201 : if (it != cacheMap_.end()) {
22 136 : it->second.refCount++;
23 136 : HCCL_INFO(
24 : "[ProcRegedMemMgrCache][GetOrCreate] cache hit, refCount[%llu]",
25 : static_cast<unsigned long long>(it->second.refCount));
26 136 : return it->second.mgrPtr;
27 : }
28 :
29 65 : auto mgrPtr = creator();
30 65 : MemMgrEntry entry{mgrPtr, 1};
31 65 : cacheMap_.emplace(key, entry);
32 65 : HCCL_INFO("[ProcRegedMemMgrCache][GetOrCreate] cache miss, new instance inserted");
33 65 : return mgrPtr;
34 201 : }
35 :
36 221 : void ProcRegedMemMgrCache::Release(const MemMgrCacheKey& key)
37 : {
38 221 : std::lock_guard<std::mutex> lock(mtx_);
39 221 : auto it = cacheMap_.find(key);
40 221 : if (it == cacheMap_.end()) {
41 49 : HCCL_WARNING("[ProcRegedMemMgrCache][Release] key not found, skip release");
42 49 : return;
43 : }
44 172 : if (it->second.refCount > 0) {
45 172 : it->second.refCount--;
46 : }
47 172 : if (it->second.refCount == 0) {
48 60 : cacheMap_.erase(it);
49 60 : HCCL_INFO("[ProcRegedMemMgrCache][Release] refCount zero, entry erased");
50 : } else {
51 112 : HCCL_INFO(
52 : "[ProcRegedMemMgrCache][Release] refCount remain [%llu]",
53 : static_cast<unsigned long long>(it->second.refCount));
54 : }
55 221 : }
56 :
57 : } // namespace hcomm
|