Line data Source code
1 : /**
2 : * Copyright (c) 2026 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 "ccu_res_desc_mgr.h"
12 :
13 : #include <algorithm>
14 : #include <mutex>
15 : #include <vector>
16 :
17 : #include "ccu_dev_mgr_imp.h"
18 : #include "ccu_log.h"
19 : #include "exception_handler.h"
20 :
21 : namespace hcomm {
22 :
23 166 : CcuResult CcuResDescMgr::Create(uint32_t dieId, HcommCcuResDescHandle &handle)
24 : {
25 166 : std::unique_lock<std::shared_timed_mutex> lock(descMapMutex_);
26 :
27 166 : std::unique_ptr<CcuResDesc> desc{nullptr};
28 166 : EXCEPTION_CATCH(
29 : desc = std::make_unique<CcuResDesc>(),
30 : return CcuResult::CCU_E_INTERNAL);
31 :
32 166 : desc->dieId = dieId;
33 166 : nextHandle_ += 1;
34 166 : EXCEPTION_CATCH(
35 : descMap_.emplace(nextHandle_, std::move(desc)),
36 : return CcuResult::CCU_E_INTERNAL);
37 166 : handle = nextHandle_;
38 166 : return CcuResult::CCU_SUCCESS;
39 166 : }
40 :
41 1281 : CcuResult CcuResDescMgr::FindDesc(
42 : HcommCcuResDescHandle handle, const char *funcName, ConstDescIterator &it) const
43 : {
44 1281 : it = descMap_.find(handle);
45 1281 : if (it == descMap_.cend()) {
46 17 : HCCL_ERROR("[CcuResDescMgr][%s] handle[%llx] is not existed.", funcName, handle);
47 17 : return CcuResult::CCU_E_NOT_FOUND;
48 : }
49 :
50 1264 : return CcuResult::CCU_SUCCESS;
51 : }
52 :
53 125 : const CcuResDesc *CcuResDescMgr::Get(HcommCcuResDescHandle handle) const
54 : {
55 125 : std::shared_lock<std::shared_timed_mutex> lock(descMapMutex_);
56 125 : auto it = descMap_.cend();
57 125 : if (FindDesc(handle, __func__, it) != CcuResult::CCU_SUCCESS) {
58 4 : return nullptr;
59 : }
60 :
61 121 : return it->second.get();
62 125 : }
63 :
64 131 : CcuResult CcuResDescMgr::Destroy(HcommCcuResDescHandle handle)
65 : {
66 131 : std::unique_lock<std::shared_timed_mutex> lock(descMapMutex_);
67 131 : auto it = descMap_.cend();
68 131 : CCU_CHK_RET(FindDesc(handle, __func__, it));
69 :
70 128 : descMap_.erase(it);
71 128 : return CcuResult::CCU_SUCCESS;
72 131 : }
73 :
74 852 : CcuResult CcuResDescMgr::SetResNum(HcommCcuResDescHandle handle, ResType resType, uint32_t resNum)
75 : {
76 852 : std::unique_lock<std::shared_timed_mutex> lock(descMapMutex_);
77 852 : auto it = descMap_.cend();
78 852 : CCU_CHK_RET(FindDesc(handle, __func__, it));
79 :
80 850 : return it->second->SetResNum(resType, resNum);
81 852 : }
82 :
83 138 : CcuResult CcuResDescMgr::QueryResNum(HcommCcuResDescHandle handle, ResType resType, uint32_t &resNum) const
84 : {
85 138 : std::shared_lock<std::shared_timed_mutex> lock(descMapMutex_);
86 138 : auto it = descMap_.cend();
87 138 : CCU_CHK_RET(FindDesc(handle, __func__, it));
88 :
89 136 : return it->second->QueryResNum(resType, resNum);
90 138 : }
91 :
92 32 : CcuResult CcuResDescMgr::QueryDieId(HcommCcuResDescHandle handle, uint32_t &dieId) const
93 : {
94 32 : std::shared_lock<std::shared_timed_mutex> lock(descMapMutex_);
95 32 : auto it = descMap_.cend();
96 32 : CCU_CHK_RET(FindDesc(handle, __func__, it));
97 :
98 26 : dieId = it->second->dieId;
99 26 : return CcuResult::CCU_SUCCESS;
100 32 : }
101 :
102 3 : CcuResult CcuResDescMgr::QueryRemainRes(HcommCcuResDescHandle handle, int32_t devLogicId) const
103 : {
104 3 : std::unique_lock<std::shared_timed_mutex> lock(descMapMutex_);
105 3 : auto it = descMap_.cend();
106 3 : CCU_CHK_RET(FindDesc(handle, __func__, it));
107 :
108 3 : CcuResDesc &desc = *it->second;
109 3 : const uint8_t dieId = static_cast<uint8_t>(desc.dieId);
110 :
111 : // 遍历全部 ResType (LOOP..MISSION, 不含 INS)
112 3 : constexpr ResType kResTypes[] = {
113 : ResType::LOOP, ResType::MS, ResType::CKE,
114 : ResType::XN, ResType::GSA, ResType::MISSION
115 : };
116 :
117 3 : uint32_t remainNum = 0;
118 21 : for (auto internalType : kResTypes) {
119 : // 查询最大连续剩余
120 18 : if (CcuDevMgrImp::QueryRemainRes(devLogicId, dieId, internalType, remainNum) != HCCL_SUCCESS) {
121 0 : HCCL_ERROR("[CcuResDescMgr][%s] devLogicId[%d] dieId[%u] resType[%s] query failed",
122 : __func__, devLogicId, dieId, internalType.Describe().c_str());
123 0 : return CcuResult::CCU_E_INTERNAL;
124 : }
125 18 : HCCL_INFO("[CcuResDescMgr][%s] devLogicId[%d] dieId[%u] resType[%s] remainNum[%u]",
126 : __func__, devLogicId, dieId, internalType.Describe().c_str(), remainNum);
127 : // 写入最大连续剩余
128 18 : CCU_CHK_RET(desc.SetResNum(internalType, remainNum));
129 : }
130 :
131 : // 单独处理 INSTRUCTION: 从 CcuComponent 查询实际剩余量
132 3 : uint32_t insFreeSize = CcuDevMgrImp::GetInsConsecutiveRemainSize(devLogicId, dieId);
133 3 : HCCL_INFO("[CcuResDescMgr][%s] devLogicId[%d] dieId[%u] resType[ResType::INS] remainNum[%u]",
134 : __func__, devLogicId, dieId, insFreeSize);
135 3 : CCU_CHK_RET(desc.SetResNum(ResType::INS, insFreeSize));
136 :
137 3 : return CcuResult::CCU_SUCCESS;
138 3 : }
139 :
140 228 : CcuResult CcuResDescMgr::Deinit()
141 : {
142 228 : std::unique_lock<std::shared_timed_mutex> lock(descMapMutex_);
143 228 : descMap_.clear();
144 228 : nextHandle_ = 0;
145 228 : return CcuResult::CCU_SUCCESS;
146 228 : }
147 :
148 : } // namespace hcomm
|