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 "ccu_res_allocator.h"
12 :
13 : #include <climits>
14 :
15 : #include "ccu_res_specs.h"
16 :
17 : namespace Hccl {
18 :
19 107 : HcclResult CcuResIdAllocator::Alloc(const uint32_t num, const bool consecutive,
20 : std::vector<ResInfo> &allocatedResInfos, const std::string &dfxInfo)
21 : {
22 116 : CHK_PRT_RET(num == 0,
23 : HCCL_ERROR("[CcuResIdAllocator][%s] failed, request num is 0.", __func__),
24 : HcclResult::HCCL_E_PARA);
25 :
26 104 : std::unique_lock<std::mutex> lock(innerMutex);
27 : // 快速判断是否可以分配
28 104 : const uint32_t freeSize = capacity_ - allocatedSize;
29 104 : if (num > freeSize) {
30 21 : HCCL_WARNING("[CcuResIdAllocator][%s] failed, resType[%s], requested num[%u] exceeds "
31 : "currently free size[%u].", __func__, dfxInfo.c_str(), num, freeSize);
32 21 : HCCL_RUN_INFO("Insufficient CCU Resource: %s, requestNum[%u], freeNum[%u].",
33 : dfxInfo.c_str(), num, freeSize);
34 7 : return HcclResult::HCCL_E_UNAVAIL;
35 : }
36 :
37 97 : vector<ResInfo> newResInfos;
38 97 : uint32_t leftNum = num;
39 97 : uint32_t tryStartId = 0;
40 : // 对于要求连续的资源需要提供一个足够大小的空闲块
41 : // 对于非连续需要每次不为 0
42 97 : const uint32_t limitSize = consecutive ? num - 1 : 0;
43 : // 顺序优先分配,遍历已分配的连续块,寻找当前块与下个块之间是否有足够大小空间
44 97 : resInfos.emplace_back(capacity_, 0); // 临时添加一个尾资源,简化判断逻辑
45 117 : for (size_t i = 0; i < resInfos.size(); i++) {
46 117 : const auto &resInfo = resInfos[i];
47 117 : uint32_t partNum = std::min(resInfo.startId - tryStartId, leftNum);
48 117 : if (partNum > limitSize) {
49 97 : newResInfos.emplace_back(tryStartId, partNum); // 该空闲块足够大,分配
50 97 : leftNum -= partNum;
51 97 : if (leftNum == 0) {
52 97 : break;
53 : }
54 : }
55 20 : tryStartId = resInfo.startId + resInfo.num; // 更新当前块起始位置
56 : }
57 97 : resInfos.pop_back(); // 删除临时添加的尾资源
58 : // 只有连续要求的资源才可能剩余,此时分配失败,新块为空
59 97 : if (leftNum != 0) {
60 0 : HCCL_WARNING("[CcuResIdAllocator][%s] failed, no enough consecutive free "
61 : "resource ids for requested num[%u].", __func__, num);
62 0 : HCCL_RUN_INFO("Insufficient CCU Resource: consecutived %s, requestNum[%u], freeNum[%u].",
63 : dfxInfo.c_str(), num, freeSize);
64 0 : return HcclResult::HCCL_E_UNAVAIL;
65 : }
66 :
67 97 : allocatedSize += num;
68 97 : AllocResInfo(newResInfos); // 将分配的所有资源记录
69 97 : allocatedResInfos = newResInfos;
70 97 : return HcclResult::HCCL_SUCCESS;
71 104 : }
72 :
73 97 : void CcuResIdAllocator::AllocResInfo(std::vector<ResInfo> newResInfos)
74 : {
75 97 : if (resInfos.empty()) { // 首次分配直接添加块
76 77 : resInfos.emplace_back(newResInfos.front());
77 77 : return;
78 : }
79 : // 内部变量始终维护最简的连续块,对需要合并的块更新
80 20 : size_t newIdx = 0;
81 20 : size_t idx = 0;
82 40 : while (newIdx < newResInfos.size() && idx < resInfos.size()) {
83 20 : auto &newResInfo = newResInfos[newIdx];
84 20 : auto &resInfo = resInfos[idx];
85 : // 跳过无关的资源块,使得resInfo是newResInfo的后续块
86 20 : if (newResInfo.startId >= resInfo.startId) {
87 20 : idx++;
88 20 : continue;
89 : }
90 : // 检查当前块是否与后续块连续,如果连续则合并
91 0 : if (newResInfo.startId == resInfo.startId - newResInfo.num) {
92 0 : newResInfo.num += resInfo.num;
93 0 : resInfos.erase(resInfos.begin() + idx);
94 : }
95 : // 如果当前块是首块则插入首块
96 0 : if (idx == 0) {
97 0 : resInfos.insert(resInfos.begin(), newResInfo);
98 0 : newIdx++;
99 0 : continue;
100 : }
101 : // 分配保证如果当前块不是首块则一定与前一个块连续,更新前一个块
102 0 : resInfos[idx - 1].num += newResInfo.num;
103 0 : newIdx++;
104 : }
105 : // 如果有剩余块一定与最后一个块连续,更新最后一个块
106 20 : if (newIdx < newResInfos.size()) {
107 20 : resInfos.back().num += newResInfos.back().num;
108 : }
109 : }
110 :
111 28 : static HcclResult CheckReleasePara(const uint32_t startId, const uint32_t num,
112 : const uint32_t capacity)
113 : {
114 28 : CHK_PRT_RET(num == 0,
115 : HCCL_ERROR("[CcuResIdAllocator][%s] failed, resource num is 0.", __func__),
116 : HcclResult::HCCL_E_PARA);
117 :
118 28 : CHK_PRT_RET(num > capacity,
119 : HCCL_ERROR("[CcuResIdAllocator][%s] failed, resource num[%u] "
120 : "is greater than capacity[%u]", __func__, num, capacity),
121 : HcclResult::HCCL_E_PARA);
122 :
123 28 : CHK_PRT_RET(startId > capacity - num,
124 : HCCL_ERROR("[CcuResIdAllocator][%s] failed, resource startId[%u] "
125 : "num[%u] capacity[%u]", __func__, startId, num, capacity),
126 : HcclResult::HCCL_E_PARA);
127 :
128 28 : return HcclResult::HCCL_SUCCESS;
129 : }
130 :
131 28 : HcclResult CcuResIdAllocator::Release(const uint32_t startId, const uint32_t num)
132 : {
133 28 : CHK_RET(CheckReleasePara(startId, num, capacity_));
134 :
135 28 : std::unique_lock<std::mutex> lock(innerMutex);
136 :
137 : // 找到需要释放的资源块
138 28 : const size_t resIndex = FindReleaseResIndex(startId);
139 34 : CHK_PRT_RET(resIndex >= resInfos.size(),
140 : HCCL_ERROR("[CcuResIdAllocator][%s] failed, resource startId[%u] num[%u] "
141 : "has not been allocated yet. ", __func__, startId, num),
142 : HcclResult::HCCL_E_PARA);
143 :
144 : // 判断申请释放的资源是否越界
145 26 : const auto &resInfo = resInfos[resIndex];
146 26 : uint32_t allocatedNum = resInfo.startId + resInfo.num - startId;
147 26 : CHK_PRT_RET(num > allocatedNum,
148 : HCCL_ERROR("[CcuResIdAllocator][%s] failed, resource num[%u] is greater "
149 : "than the allocated num[%u].", __func__, num, allocatedNum),
150 : HcclResult::HCCL_E_PARA);
151 :
152 : // 将资源块释放并更新
153 26 : ReleaseResInfo(resIndex, startId, num);
154 26 : return HcclResult::HCCL_SUCCESS;
155 28 : }
156 :
157 : constexpr u32 INVALID_UINT = 0xFFFFFFFF;
158 28 : size_t CcuResIdAllocator::FindReleaseResIndex(const uint32_t startId) const
159 : {
160 28 : size_t resIndex = 0;
161 28 : const size_t maxIndex = resInfos.size();
162 31 : while (resIndex < maxIndex) {
163 30 : const auto &resInfo = resInfos[resIndex];
164 : // 检查resInfo.startId + resInfo.num是否会u32溢出
165 30 : if ((resInfo.startId < INVALID_UINT - resInfo.num) && startId >= resInfo.startId + resInfo.num) {
166 3 : resIndex++;
167 3 : continue;
168 : }
169 27 : if (startId >= resInfo.startId) {
170 26 : break; // 资源id属于该资源块
171 : }
172 1 : return maxIndex; // 无法找到已分配资源块,返回错误索引
173 : }
174 27 : return resIndex;
175 : }
176 :
177 26 : void CcuResIdAllocator::ReleaseResInfo(const size_t resIndex,
178 : const uint32_t startId, const uint32_t num)
179 : {
180 26 : allocatedSize -= num;
181 :
182 26 : auto &resInfo = resInfos[resIndex];
183 : // 释放的资源在资源块起始部分
184 26 : if (startId == resInfo.startId) {
185 : // 恰好是整块资源,则全部释放
186 16 : if (num == resInfo.num) {
187 14 : resInfos.erase(resInfos.begin() + resIndex);
188 25 : return;
189 : }
190 : // 非整块资源则更新资源块起始位置和大小
191 2 : resInfo.startId += num;
192 2 : resInfo.num -= num;
193 2 : return;
194 : }
195 :
196 10 : uint32_t leftNum = startId - resInfo.startId;
197 10 : uint32_t rightNum = resInfo.num - leftNum - num;
198 : // 释放的资源在资源块末尾,更新资源块大小
199 10 : if (rightNum == 0) {
200 9 : resInfo.num -= num;
201 9 : return;
202 : }
203 : // 释放的资源在资源块中间,拆分为两个资源块
204 1 : resInfo.num = leftNum; // 左部分块更新数据
205 : // 右部分块需要新增
206 1 : resInfos.emplace(resInfos.begin() + resIndex + 1, startId + num, rightNum);
207 : }
208 :
209 24 : CcuResAllocator::CcuResAllocator(const int32_t devLogicId, const uint8_t dieId)
210 24 : : devLogicId_(devLogicId), dieId_(dieId)
211 : {
212 24 : auto& ccuResSpecs = CcuResSpecifications::GetInstance(devLogicId);
213 : // 获取静态定义的资源规格查询函数列表,遍历构造
214 192 : for (const auto &pair : GET_RES_SPEC_FUNC_ARRAY) {
215 168 : const ResType resType = pair.first;
216 168 : const GetResSpecFunc getFunc = pair.second;
217 168 : uint32_t capacity = 0; // 获取失败时容量为 0,后续分配按资源不足处理
218 168 : (void)(ccuResSpecs.*getFunc)(dieId, capacity);
219 168 : auto allocatorPtr = std::make_unique<CcuResIdAllocator>(capacity);
220 168 : idAllocatorMap[static_cast<uint8_t>(resType)] = std::move(allocatorPtr);
221 168 : }
222 24 : }
223 :
224 45 : HcclResult CcuResAllocator::Alloc(const ResType resType, const uint32_t num,
225 : const bool consecutive, std::vector<ResInfo> &resInfos)
226 : {
227 45 : auto resTypeIter = idAllocatorMap.find(static_cast<uint8_t>(resType));
228 45 : if (resTypeIter == idAllocatorMap.end()) {
229 0 : HCCL_ERROR("[CcuResAllocator][%s] failed, invalid resource type[%s].",
230 : __func__, resType.Describe().c_str());
231 0 : return HcclResult::HCCL_E_PARA;
232 : }
233 45 : return resTypeIter->second->Alloc(num, consecutive, resInfos, resType.Describe());
234 : }
235 :
236 22 : HcclResult CcuResAllocator::Release(const ResType resType, const uint32_t startId,
237 : const uint32_t num)
238 : {
239 22 : auto resTypeIter = idAllocatorMap.find(static_cast<uint8_t>(resType));
240 22 : if (resTypeIter == idAllocatorMap.end()) {
241 0 : HCCL_ERROR("[CcuResAllocator][%s] failed, invalid resource type[%s].",
242 : __func__, resType.Describe().c_str());
243 0 : return HcclResult::HCCL_E_PARA;
244 : }
245 22 : return resTypeIter->second->Release(startId, num);
246 : }
247 :
248 0 : std::string CcuResIdAllocator::Describe() const
249 : {
250 : return StringFormat("CcuResIdAllocator[capacity=%u, allocatedSize=%u, "
251 0 : "resInfos_size=%u]", capacity_, allocatedSize, resInfos.size());
252 : }
253 :
254 0 : std::string CcuResAllocator::Describe() const
255 : {
256 : return StringFormat("CcuResAllocator[devLogicId=%u, dieId=%u, "
257 0 : "idAllocatorSize=[%u]]", devLogicId_, dieId_, idAllocatorMap.size());
258 : }
259 :
260 : }; // namespace Hccl
|