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