LCOV - code coverage report
Current view: top level - legacy/ascend950/unified_platform/ccu/ccu_device/ccu_component - ccu_res_allocator.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 82.4 % 142 117
Test Date: 2026-08-18 17:47:01 Functions: 76.9 % 13 10

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

Generated by: LCOV version 2.0-1