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