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