LCOV - code coverage report
Current view: top level - legacy/ascend950/unified_platform/resource/stream/aicpu - sqe_mgr.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 86.4 % 162 140
Test Date: 2026-08-04 10:52:23 Functions: 100.0 % 13 13

            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 "sqe_mgr.h"
      12              : #include <chrono>
      13              : #include "stl_util.h"
      14              : #include "exception_util.h"
      15              : #include "null_ptr_exception.h"
      16              : #include "internal_exception.h"
      17              : #include "string_util.h"
      18              : #include "drv_api_exception.h"
      19              : 
      20              : namespace Hccl {
      21              : 
      22              : constexpr u64 COMMIT_TIMEOUT = 30;
      23              : 
      24            4 : HcclResult SqeMgr::Begin(u32 sqId)
      25              : {
      26           12 :     HCCL_INFO("SqeMgr::%s start, sqId %u", __func__, sqId);
      27            4 :     if (Contain(sqInfos, sqId)) {
      28            3 :         if (sqInfos[sqId]->sqeCnt != 0) {
      29              :             //called Begin twice in the same round
      30            3 :             HCCL_ERROR("sqInfos[sqId]->sqeCnt != 0");
      31            1 :             return HcclResult::HCCL_E_INTERNAL;
      32              :         }
      33            2 :         return HcclResult::HCCL_SUCCESS;
      34              :     }
      35            1 :     std::unique_ptr<SqInfo> sqInfo = std::make_unique<SqInfo>();
      36            1 :     sqInfo->sqeCnt                 = 0;
      37            1 :     sqInfo->sqDepth                = QuerySqDepth(sqId);
      38            1 :     sqInfo->sqTail                 = QuerySqTail(sqId);
      39            1 :     sqInfo->sqHead                 = QuerySqHead(sqId);
      40            1 :     sqInfo->sqBaseAddr             = QuerySqBaseAddr(sqId);
      41            1 :     s32 ret = memset_s(sqInfo->sqeBuffer, sizeof(sqInfo->sqeBuffer), 0, sizeof(sqInfo->sqeBuffer));
      42            1 :     if (ret != EOK) {
      43            0 :         std::string formatStr = StringFormat("SqeMgr::%s memcpy_s failed. errorno[%d]", __func__, ret);
      44            0 :         HCCL_ERROR("%s", formatStr.c_str());
      45            0 :         THROW<InternalException>(formatStr);
      46            0 :     }
      47              : 
      48            1 :     sqInfos[sqId] = std::move(sqInfo);
      49            1 :     if (sqInfos[sqId] == nullptr) {
      50            0 :         HCCL_ERROR("SqeMgr::Begin sqInfos[sqId] == nullptr, sq_id=%u", sqId);
      51            0 :         return HcclResult::HCCL_E_INTERNAL;
      52              :     }
      53            3 :     HCCL_INFO("SqeMgr::%s end", __func__);
      54            1 :     return HcclResult::HCCL_SUCCESS;
      55            1 : }
      56              : 
      57            7 : HcclResult SqeMgr::Add(u32 sqId, HcclSqe *sqe)
      58              : {
      59            7 :     if (sqe == nullptr) {
      60            6 :         HCCL_ERROR("SqeMgr::Add HcclSqe *sqe is nullptr");
      61            2 :         return HcclResult::HCCL_E_PTR;
      62              :     }
      63           15 :     HCCL_INFO("SqeMgr::%s start sq_id=%u", __func__, sqId);
      64            5 :     if (!Contain(sqInfos, sqId)) {
      65            0 :         HCCL_ERROR("no available buffer found for sq_id=%u", sqId);
      66            0 :         return HcclResult::HCCL_E_NOT_FOUND;
      67              :     }
      68              : 
      69            5 :     SqInfo *sqInfo         = sqInfos[sqId].get();
      70            5 :     if (sqInfo == nullptr) {
      71            3 :         HCCL_ERROR("SqeMgr::add failed sqInfo is null sq_id=%u", sqId);
      72            1 :         return HcclResult::HCCL_E_NOT_FOUND;
      73              :     }
      74              : 
      75            4 :     void   *nextBufferAddr = static_cast<u8 *>(sqInfo->sqeBuffer) + sqInfo->sqeCnt * AC_SQE_SIZE;
      76              : 
      77           12 :     HCCL_INFO("SqeMgr::%s sqe->GetSqe() %llu", __func__, sqe->GetSqe());
      78              : 
      79            4 :     AddSqeToBuffer(nextBufferAddr, reinterpret_cast<void *>(sqe->GetSqe()));
      80            4 :     sqInfo->sqeCnt++;
      81           12 :     HCCL_INFO("SqeMgr::%s end sqInfo->sqeCnt[%u]", __func__, sqInfo->sqeCnt);
      82            4 :     return HcclResult::HCCL_SUCCESS;
      83              : }
      84              : 
      85            3 : HcclResult SqeMgr::Commit(u32 sqId)
      86              : {
      87            9 :     HCCL_INFO("SqeMgr::%s start sq_id=%u", __func__, sqId);
      88            3 :     if (!Contain(sqInfos, sqId)) {
      89            3 :         HCCL_ERROR("no available buffer found for sq_id=%u", sqId);
      90            1 :         return HcclResult::HCCL_E_NOT_FOUND;
      91              :     }
      92              :     
      93            2 :     SqInfo *sqInfo         = sqInfos[sqId].get();
      94            2 :     CHECK_NULLPTR(sqInfo, "[SqeMgr::Commit] sqInfo is nullptr!");
      95            2 :     u32     availableSpace = GetTailToHeadDist(sqId, sqInfo->sqHead, sqInfo->sqTail);
      96            2 :     auto    startTime      = std::chrono::steady_clock::now();
      97            2 :     auto    timeout        = std::chrono::seconds(COMMIT_TIMEOUT);
      98            6 :     HCCL_INFO("SqeMgr::%s start", __func__);
      99            2 :     while (availableSpace <= sqInfo->sqeCnt) {
     100            0 :         HCCL_INFO("SqeMgr::%s while loop availableSpace %u <= sqInfo->sqeCnt %u", __func__, availableSpace,
     101              :                     sqInfo->sqeCnt);
     102            0 :         if ((std::chrono::steady_clock::now() - startTime) >= timeout) {
     103            0 :             HCCL_ERROR("Rtsq full, timeout 30s. cur head: %u, sqId: %u", sqInfo->sqHead, sqId);
     104            0 :             return HcclResult::HCCL_E_TIMEOUT;
     105              :         }
     106            0 :         sqInfo->sqHead = QuerySqHead(sqId);
     107            0 :         availableSpace = GetTailToHeadDist(sqId, sqInfo->sqHead, sqInfo->sqTail);
     108              :     }
     109            2 :     u32 depthLeft = sqInfo->sqDepth - sqInfo->sqTail;
     110            2 :     if (sqInfo->sqeCnt <= depthLeft) { // 没有回绕
     111            3 :         HCCL_INFO("SqeMgr::%s copy sqe from sqe buffer, cur tail: %u, size: %u", __func__, sqInfo->sqTail,
     112              :                     sqInfo->sqeCnt);
     113            2 :         int ret = memcpy_s(reinterpret_cast<u8 *>(sqInfo->sqBaseAddr) + sqInfo->sqTail * AC_SQE_SIZE,
     114            1 :                            sqInfo->sqeCnt * AC_SQE_SIZE, sqInfo->sqeBuffer, sqInfo->sqeCnt * AC_SQE_SIZE);
     115            1 :         if (ret != 0) {
     116            0 :             THROW<InternalException>(StringFormat("SqeMgr::%s sqe memcpy_s failed, ret = %d", __func__, ret));
     117              :         }
     118              :     } else {
     119            3 :         HCCL_INFO("SqeMgr::%s copy sqe twice, cnt: %u, tail: %u, depth remain: %u", __func__, sqInfo->sqeCnt,
     120              :                     sqInfo->sqTail, depthLeft);
     121              :         // 先拷贝rtsq里剩余空间大小
     122            2 :         int ret = memcpy_s(reinterpret_cast<u8 *>(sqInfo->sqBaseAddr) + sqInfo->sqTail * AC_SQE_SIZE,
     123            1 :                            depthLeft * AC_SQE_SIZE, sqInfo->sqeBuffer, depthLeft * AC_SQE_SIZE);
     124            1 :         if (ret != 0) {
     125            0 :             THROW<InternalException>(StringFormat("SqeMgr::%s rtsq remaining space memcpy_s failed, ret = %d",
     126              :                                                     __func__, ret));
     127              :         }
     128              :         // 拷贝剩余sqe
     129            2 :         ret = memcpy_s(reinterpret_cast<u8 *>(sqInfo->sqBaseAddr), sqInfo->sqHead * AC_SQE_SIZE,
     130            1 :                        sqInfo->sqeBuffer + depthLeft * AC_SQE_SIZE, (sqInfo->sqeCnt - depthLeft) * AC_SQE_SIZE);
     131            1 :         if (ret != 0) {
     132            0 :             THROW<InternalException>(StringFormat("SqeMgr::%s remaining sqe memcpy_s failed, ret = %d", __func__, ret));
     133              :         }
     134              :     }
     135              : 
     136              :     // set new tail position
     137            2 :     u32 newTail = static_cast<u32>((static_cast<u64>(sqInfo->sqTail) + sqInfo->sqeCnt) % sqInfo->sqDepth);
     138            2 :     ConfigSqTail(sqId, newTail);
     139            2 :     sqInfo->sqTail = newTail;
     140              :     // clear sqe buffer
     141            2 :     auto sRet = memset_s(sqInfo->sqeBuffer, sizeof(sqInfo->sqeBuffer), 0, sqInfo->sqeCnt * AC_SQE_SIZE);
     142            2 :     if (sRet != 0) {
     143            0 :         THROW<InternalException>(StringFormat("SqeMgr::%s remaining sqe memcpy_s failed, ret = %d", __func__, sRet));
     144              :     }
     145            2 :     sqInfo->sqeCnt = 0;
     146            6 :     HCCL_INFO("SqeMgr::%s end", __func__);
     147            2 :     return HcclResult::HCCL_SUCCESS;
     148              : }
     149              : 
     150            1 : u32 SqeMgr::QuerySqHead(u32 sqId)
     151              : {
     152            3 :     HCCL_INFO("SqeMgr::%s", __func__);
     153            1 :     return QuerySqStatusByType(sqId, DRV_SQCQ_PROP_SQ_HEAD);
     154              : }
     155              : 
     156            1 : u32 SqeMgr::QuerySqTail(u32 sqId)
     157              : {
     158            3 :     HCCL_INFO("SqeMgr::%s", __func__);
     159            1 :     return QuerySqStatusByType(sqId, DRV_SQCQ_PROP_SQ_TAIL);
     160              : }
     161              : 
     162            1 : u32 SqeMgr::QuerySqDepth(u32 sqId)
     163              : {
     164            3 :     HCCL_INFO("SqeMgr::%s", __func__);
     165            1 :     return QuerySqStatusByType(sqId, DRV_SQCQ_PROP_SQ_DEPTH);
     166              : }
     167              : 
     168            2 : u64 SqeMgr::QuerySqBaseAddr(u32 sqId)
     169              : {
     170              :     halSqCqQueryInfo queryInfo;
     171            2 :     queryInfo.tsId = 0;
     172            2 :     queryInfo.sqId = sqId;
     173            2 :     queryInfo.cqId = 0;
     174            2 :     queryInfo.type = DRV_NORMAL_TYPE;
     175            2 :     queryInfo.prop = DRV_SQCQ_PROP_SQ_BASE;
     176              : 
     177            6 :     HCCL_INFO("SqeMgr::%s begin", __func__);
     178            2 :     drvError_t ret = halSqCqQuery(devPhyId, &queryInfo);
     179            2 :     if (ret != 0) {
     180              :         std::string formatStr = StringFormat("SqeMgr::%s call halSqCqQuery failed, devPhyId %d, ret %d", 
     181            1 :                                             __func__, devPhyId, ret);
     182            3 :         HCCL_ERROR("%s", formatStr.c_str());
     183            1 :         THROW<DrvApiException>(formatStr);
     184            1 :     }
     185            3 :     HCCL_INFO("SqeMgr::%s end", __func__);
     186              : 
     187            1 :     return ((static_cast<u64>(queryInfo.value[1])) << UINT32_BIT_NUM) | queryInfo.value[0];
     188              : }
     189              : 
     190            4 : u32 SqeMgr::QuerySqStatusByType(u32 sqId, drvSqCqPropType_t type) const
     191              : {
     192              :     halSqCqQueryInfo queryInfo;
     193            4 :     queryInfo.tsId = 0;
     194            4 :     queryInfo.sqId = sqId;
     195            4 :     queryInfo.cqId = 0;
     196            4 :     queryInfo.type = DRV_NORMAL_TYPE;
     197            4 :     queryInfo.prop = type;
     198           12 :     HCCL_INFO("%s::halSqCqQuery begin, type %d sqiId %u", __func__, type, sqId);
     199            4 :     drvError_t ret = halSqCqQuery(devPhyId, &queryInfo);
     200            4 :     if (ret != 0) {
     201              :         std::string formatStr = StringFormat("SqeMgr::%s call halSqCqQuery failed, devPhyId %d, ret %d", 
     202            1 :                                             __func__, devPhyId, ret);
     203            3 :         HCCL_ERROR("%s", formatStr.c_str());
     204            1 :         THROW<DrvApiException>(formatStr);
     205            1 :     }
     206            9 :     HCCL_INFO("%s::halSqCqQuery end", __func__);
     207              : 
     208            3 :     return queryInfo.value[0];
     209              : }
     210              : 
     211            1 : void SqeMgr::ConfigSqTail(u32 sqId, u32 value)
     212              : {
     213            3 :     HCCL_INFO("SqeMgr::%s sqId %u, value %u", __func__, sqId, value);
     214            1 :     ConfigSqStatusByType(sqId, DRV_SQCQ_PROP_SQ_TAIL, value);
     215            1 : }
     216              : 
     217            1 : void SqeMgr::ConfigSqStatusByType(u32 sqId, drvSqCqPropType_t type, u32 value) const
     218              : {
     219              :     halSqCqConfigInfo configInfo;
     220            1 :     configInfo.tsId     = 0;
     221            1 :     configInfo.sqId     = sqId;
     222            1 :     configInfo.cqId     = 0;
     223            1 :     configInfo.type     = DRV_NORMAL_TYPE;
     224            1 :     configInfo.prop     = type;
     225            1 :     configInfo.value[0] = value;
     226              : 
     227            3 :     HCCL_INFO("SqeMgr::%s start", __func__);
     228            1 :     drvError_t ret = halSqCqConfig(devPhyId, &configInfo);
     229            1 :     if (ret != 0) {
     230            3 :         HCCL_ERROR("call halSqCqConfig failed, ret %d",ret);
     231              :     }
     232            3 :     HCCL_INFO("SqeMgr::%s end", __func__);
     233            1 : }
     234              : 
     235            2 : u32 SqeMgr::GetTailToHeadDist(u32 sqId, u32 head, u32 tail)
     236              : {
     237            2 :     if (head == tail) {
     238            1 :         return sqInfos[sqId]->sqDepth;
     239              :     }
     240            1 :     return (tail < head) ? head - tail : sqInfos[sqId]->sqDepth - (tail - head);
     241              : }
     242              : 
     243            5 : void SqeMgr::AddSqeToBuffer(void *bufferAddr, void *sqeAddr) const
     244              : {
     245            5 :     if (bufferAddr == nullptr || sqeAddr == nullptr) {
     246            1 :         std::string formatStr = StringFormat("SqeMgr::%s bufferAddr[%u], sqeAddr[%u]", __func__, bufferAddr, sqeAddr);
     247            3 :         HCCL_ERROR("%s", formatStr.c_str());
     248            1 :         THROW<NullPtrException>(formatStr);
     249            1 :     }
     250            4 :     s32 ret = memcpy_s(bufferAddr, AC_SQE_SIZE, sqeAddr, AC_SQE_SIZE);
     251            4 :     if (ret != 0) {
     252            0 :         std::string formatStr = StringFormat("SqeMgr::%s memcpy_s failed, ret = %d", __func__, ret);
     253            0 :         HCCL_ERROR("%s", formatStr.c_str());
     254            0 :         THROW<InternalException>(formatStr);
     255            0 :     }
     256            4 : }
     257              : 
     258           89 : SqeMgr::SqeMgr(u32 devPhysicalId) : devPhyId(devPhysicalId)
     259              : {
     260           89 : }
     261              : 
     262              : } // namespace Hccl
        

Generated by: LCOV version 2.0-1