LCOV - code coverage report
Current view: top level - base_comm/resources/ccu/ccu_dfx - ccu_dfx_schema.cc (source / functions) Coverage Total Hit
Test: coverage.info Lines: 87.1 % 139 121
Test Date: 2026-07-28 12:11:00 Functions: 100.0 % 10 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              : #include "ccu_dfx_schema.h"
      11              : 
      12              : #include "securec.h"
      13              : #include "log.h"
      14              : #include "ccu_error_info_v1.h"
      15              : #include "ccu_error_info_v2.h"
      16              : #include "adapter_rts_common.h"
      17              : 
      18              : namespace hcomm {
      19              : namespace {
      20              :     struct CcumDfxCommonInfo {
      21              :         unsigned int ccumSqeRecvCnt;
      22              :         unsigned int ccumSqeSendCnt;
      23              :         unsigned int ccumMissionDfx;
      24              :         unsigned int ccumSqeDropCnt;
      25              :         unsigned int ccumSqeAddrLenErrDropCnt;
      26              :         unsigned int lqcCcuSecReg0;
      27              :         unsigned int ccumTifSqeCnt;
      28              :         unsigned int ccumTifCqeCnt;
      29              :         unsigned int ccumCifSqeCnt;
      30              :         unsigned int ccumCifCqeCnt;
      31              :     };
      32              : 
      33              :     struct ccumDfxInfo {
      34              :         unsigned int queryResult; // 0:success, 1:fail
      35              :         CcumDfxCommonInfo commonInfo;
      36              :     };
      37              : 
      38              :     struct ccumDfxInfoV2 {
      39              :         union {
      40              :             struct {
      41              :                 unsigned int queryResult : 1; // 0:success, 1:fail
      42              :                 unsigned int sqeRecvCnt : 1;
      43              :                 unsigned int sqeSendCnt : 1;
      44              :                 unsigned int missionDfx : 1;
      45              :                 unsigned int sqeDropCnt : 1;
      46              :                 unsigned int sqeErrDropCnt : 1;
      47              :                 unsigned int secReg0 : 1;
      48              :                 unsigned int tifSqeCnt : 1;
      49              :                 unsigned int tifCqeCnt : 1;
      50              :                 unsigned int cifSqeCnt : 1;
      51              :                 unsigned int cifCqeCnt : 1;
      52              :                 unsigned int mcmDfx : 1;
      53              :                 unsigned int resv : 20;
      54              :             } bs;
      55              :             unsigned int validBits : 32;
      56              :         };
      57              :         union {
      58              :             struct {
      59              :                 CcumDfxCommonInfo commonInfo;
      60              :                 unsigned int ccumMcmDfx;
      61              :                 unsigned int resv[20];
      62              :             } dfxInfo;
      63              :             unsigned int regVal[31];
      64              :         };
      65              :     };
      66            3 :     void PrintCcumDfxInfoV1(const void *rawData, std::ostringstream &oss)
      67              :     {
      68            3 :         if (rawData == nullptr) {
      69            1 :             oss << " [rawData is null]";
      70            1 :             HCCL_ERROR("[PrintCcumDfxInfoV1] rawData is null");
      71            1 :             return;
      72              :         }
      73            2 :         struct ccumDfxInfo info{};
      74            2 :         const auto copyRet = memcpy_s(&info, sizeof(info), rawData, sizeof(info));
      75            2 :         if (copyRet != EOK) {
      76            0 :             oss << " [decode failed]";
      77            0 :             HCCL_ERROR("[PrintCcumDfxInfoV1] memcpy_s failed, ret[%d]", copyRet);
      78            0 :             return;
      79              :         }
      80            2 :         if (info.queryResult != 0U) {
      81            1 :             HCCL_ERROR("get ccu dfx info fail, ccu dfx info not all correct");
      82              :         }
      83            2 :         oss << " SQE_RECV_CNT[" << info.commonInfo.ccumSqeRecvCnt << ']';
      84            2 :         oss << " SQE_SEND_CNT[" << info.commonInfo.ccumSqeSendCnt << ']';
      85            2 :         oss << " MISSION_DFX[" << info.commonInfo.ccumMissionDfx << ']';
      86            2 :         oss << " TIF_SQE_CNT[" << info.commonInfo.ccumTifSqeCnt << ']';
      87            2 :         oss << " TIF_CQE_CNT[" << info.commonInfo.ccumTifCqeCnt << ']';
      88            2 :         oss << " CIF_SQE_CNT[" << info.commonInfo.ccumCifSqeCnt << ']';
      89            2 :         oss << " CIF_CQE_CNT[" << info.commonInfo.ccumCifCqeCnt << ']';
      90            2 :         oss << " SQE_DROP_CNT[" << info.commonInfo.ccumSqeDropCnt << ']';
      91            2 :         oss << " SQE_ADDR_LEN_ERR_DROP_CNT[" << info.commonInfo.ccumSqeAddrLenErrDropCnt << ']';
      92            2 :         oss << " ccumIsEnable[" << (info.commonInfo.lqcCcuSecReg0 & 1U) << ']';
      93              :     }
      94              : 
      95            5 :     void PrintCcumDfxInfoV2(const void *rawData, std::ostringstream &oss)
      96              :     {
      97            5 :         if (rawData == nullptr) {
      98            1 :             oss << " [rawData is null]";
      99            1 :             HCCL_ERROR("[PrintCcumDfxInfoV2] rawData is null");
     100            1 :             return;
     101              :         }
     102            4 :         struct ccumDfxInfoV2 info{};
     103            4 :         const auto copyRet = memcpy_s(&info, sizeof(info), rawData, sizeof(info));
     104            4 :         if (copyRet != EOK) {
     105            0 :             oss << " [decode failed]";
     106            0 :             HCCL_ERROR("[PrintCcumDfxInfoV2] memcpy_s failed, ret[%d]", copyRet);
     107            0 :             return;
     108              :         }
     109            4 :         if (info.bs.queryResult != 0U) {
     110            1 :             HCCL_ERROR("get ccu dfx info fail, ccu dfx info not all correct");
     111              :         }
     112           44 :         auto dump = [&oss](const char *name, unsigned int value, bool hwValid) {
     113           44 :             oss << ' ' << name << '[';
     114           44 :             if (hwValid) {
     115           32 :                 oss << value;
     116              :             } else {
     117           12 :                 oss << "INVALID(hardware)";
     118           12 :                 HCCL_WARNING("[CCU DFX][V2] %s marked invalid by hardware", name);
     119              :             }
     120           44 :             oss << ']';
     121           48 :         };
     122            4 :         dump("SQE_RECV_CNT", info.dfxInfo.commonInfo.ccumSqeRecvCnt, info.bs.sqeRecvCnt != 0U);
     123            4 :         dump("SQE_SEND_CNT", info.dfxInfo.commonInfo.ccumSqeSendCnt, info.bs.sqeSendCnt != 0U);
     124            4 :         dump("MISSION_DFX", info.dfxInfo.commonInfo.ccumMissionDfx, info.bs.missionDfx != 0U);
     125            4 :         dump("TIF_SQE_CNT", info.dfxInfo.commonInfo.ccumTifSqeCnt, info.bs.tifSqeCnt != 0U);
     126            4 :         dump("TIF_CQE_CNT", info.dfxInfo.commonInfo.ccumTifCqeCnt, info.bs.tifCqeCnt != 0U);
     127            4 :         dump("CIF_SQE_CNT", info.dfxInfo.commonInfo.ccumCifSqeCnt, info.bs.cifSqeCnt != 0U);
     128            4 :         dump("CIF_CQE_CNT", info.dfxInfo.commonInfo.ccumCifCqeCnt, info.bs.cifCqeCnt != 0U);
     129            4 :         dump("SQE_DROP_CNT", info.dfxInfo.commonInfo.ccumSqeDropCnt, info.bs.sqeDropCnt != 0U);
     130            4 :         dump(
     131            4 :             "SQE_ADDR_LEN_ERR_DROP_CNT", info.dfxInfo.commonInfo.ccumSqeAddrLenErrDropCnt, info.bs.sqeErrDropCnt != 0U);
     132            4 :         dump("ccumIsEnable", info.dfxInfo.commonInfo.lqcCcuSecReg0 & 1U, info.bs.secReg0 != 0U);
     133            4 :         dump("MCM_DFX", info.dfxInfo.ccumMcmDfx, info.bs.mcmDfx != 0U);
     134              :     }
     135              : 
     136            3 :     HcclResult GetCcuMissionInfoV1(const void *rawData, CcuMissionInfo *out)
     137              :     {
     138            3 :         if (rawData == nullptr || out == nullptr) {
     139            2 :             HCCL_ERROR("[GetCcuMissionInfoV1] invalid input: rawData=%p, out=%p", rawData, out);
     140            2 :             return HCCL_E_PARA;
     141              :         }
     142            1 :         CcuMissionContext ctx{};
     143            1 :         const auto copyRet = memcpy_s(&ctx, sizeof(ctx), rawData, sizeof(ctx));
     144            1 :         if (copyRet != EOK) {
     145            0 :             HCCL_ERROR("[GetCcuMissionInfoV1] memcpy_s failed, ret[%d]", copyRet);
     146            0 :             return HCCL_E_INTERNAL;
     147              :         }
     148            1 :         out->currentIns = ctx.GetCurrentIns();
     149            1 :         out->endIns = ctx.GetEndIns();
     150            1 :         out->startIns = ctx.GetStartIns();
     151            1 :         return HCCL_SUCCESS;
     152              :     }
     153              : 
     154            3 :     HcclResult GetCcuLoopInfoV1(const void *rawData, CcuLoopInfo *out)
     155              :     {
     156            3 :         if (rawData == nullptr || out == nullptr) {
     157            2 :             HCCL_ERROR("[GetCcuLoopInfoV1] invalid input: rawData=%p, out=%p", rawData, out);
     158            2 :             return HCCL_E_PARA;
     159              :         }
     160            1 :         CcuLoopContext ctx{};
     161            1 :         const auto copyRet = memcpy_s(&ctx, sizeof(ctx), rawData, sizeof(ctx));
     162            1 :         if (copyRet != EOK) {
     163            0 :             HCCL_ERROR("[GetCcuLoopInfoV1] memcpy_s failed, ret[%d]", copyRet);
     164            0 :             return HCCL_E_INTERNAL;
     165              :         }
     166            1 :         out->currentCnt = ctx.GetCurrentCnt();
     167            1 :         out->addrStride = ctx.GetAddrStride();
     168            1 :         return HCCL_SUCCESS;
     169              :     }
     170              : 
     171            3 :     HcclResult GetCcuMissionInfoV2(const void *rawData, CcuMissionInfo *out)
     172              :     {
     173            3 :         if (rawData == nullptr || out == nullptr) {
     174            2 :             HCCL_ERROR("[GetCcuMissionInfoV2] invalid input: rawData=%p, out=%p", rawData, out);
     175            2 :             return HCCL_E_PARA;
     176              :         }
     177              : 
     178            1 :         CcuMissionContextV2 ctx{};
     179            1 :         const auto copyRet = memcpy_s(&ctx, sizeof(ctx), rawData, sizeof(ctx));
     180            1 :         if (copyRet != EOK) {
     181            0 :             HCCL_ERROR("[GetCcuMissionInfoV2] memcpy_s failed, ret[%d]", copyRet);
     182            0 :             return HCCL_E_INTERNAL;
     183              :         }
     184            1 :         out->currentIns = ctx.GetCurrentIns();
     185            1 :         out->endIns = ctx.GetEndIns();
     186            1 :         out->startIns = ctx.GetStartIns();
     187            1 :         return HCCL_SUCCESS;
     188              :     }
     189              : 
     190            3 :     HcclResult GetCcuLoopInfoV2(const void *rawData, CcuLoopInfo *out)
     191              :     {
     192            3 :         if (rawData == nullptr || out == nullptr) {
     193            2 :             HCCL_ERROR("[GetCcuLoopInfoV2] invalid input: rawData=%p, out=%p", rawData, out);
     194            2 :             return HCCL_E_PARA;
     195              :         }
     196              : 
     197            1 :         CcuLoopContextV2 ctx{};
     198            1 :         const auto copyRet = memcpy_s(&ctx, sizeof(ctx), rawData, sizeof(ctx));
     199            1 :         if (copyRet != EOK) {
     200            0 :             HCCL_ERROR("[GetCcuLoopInfoV2] memcpy_s failed, ret[%d]", copyRet);
     201            0 :             return HCCL_E_INTERNAL;
     202              :         }
     203            1 :         out->currentCnt = ctx.GetCurrentCnt();
     204            1 :         out->addrStride = ctx.GetAddrStride();
     205              : 
     206            1 :         return HCCL_SUCCESS;
     207              :     }
     208              : 
     209              :     enum class CcuSchemaVersion : uint8_t { CCU_SCHEMA_V1 = 0, CCU_SCHEMA_V2 = 1, CCU_SCHEMA_COUNT = 2 };
     210              : 
     211              :     const CcuVersionOps CCU_V1_OPS = {
     212              :         "CCU_V1",
     213              :         PrintCcumDfxInfoV1,
     214              :         GetCcuMissionInfoV1,
     215              :         GetCcuLoopInfoV1,
     216              :     };
     217              : 
     218              :     const CcuVersionOps CCU_V2_OPS = {
     219              :         "CCU_V2",
     220              :         PrintCcumDfxInfoV2,
     221              :         GetCcuMissionInfoV2,
     222              :         GetCcuLoopInfoV2,
     223              :     };
     224              : 
     225              :     const CcuVersionOps *const CCU_OPS_TABLE[] = {
     226              :         &CCU_V1_OPS, // [0] V1
     227              :         &CCU_V2_OPS, // [1] V2
     228              :     };
     229              : 
     230              :     // 编译期校验:ops 表下标必须与 CcuSchemaVersion 枚举值一一对应,
     231              :     // 后续新增 schema 版本时若漏改表项会立即编译失败。
     232              :     static_assert(
     233              :         sizeof(CCU_OPS_TABLE) / sizeof(CCU_OPS_TABLE[0]) == static_cast<size_t>(CcuSchemaVersion::CCU_SCHEMA_COUNT),
     234              :         "CCU_OPS_TABLE size must match CcuSchemaVersion::CCU_SCHEMA_COUNT");
     235              : 
     236           34 :     HcclResult GetCcuSchemaVersion(CcuSchemaVersion &schemaVersion)
     237              :     {
     238           34 :         DevType deviceType = DevType::DEV_TYPE_COUNT;
     239           34 :         HcclResult ret = hrtGetDeviceType(deviceType);
     240           34 :         if (ret != HCCL_SUCCESS) {
     241            2 :             HCCL_ERROR("[GetCcuSchemaVersion] hrtGetDeviceType failed, ret[%d].", ret);
     242            2 :             return ret;
     243              :         }
     244           32 :         if (deviceType == DevType::DEV_TYPE_950) {
     245           10 :             schemaVersion = CcuSchemaVersion::CCU_SCHEMA_V1;
     246           22 :         } else if (deviceType == DevType::DEV_TYPE_960) {
     247           12 :             schemaVersion = CcuSchemaVersion::CCU_SCHEMA_V2;
     248              :         } else {
     249           10 :             HCCL_ERROR("[GetCcuSchemaVersion] Unsupported deviceType[%u], only 950/960 supported.",
     250              :                 static_cast<uint32_t>(deviceType));
     251           10 :             return HCCL_E_INTERNAL;
     252              :         }
     253           22 :         return HCCL_SUCCESS;
     254              :     }
     255              : 
     256           22 :     HcclResult ResolveCcuOps(CcuSchemaVersion schemaVersion, const CcuVersionOps *&ops)
     257              :     {
     258           22 :         const uint8_t versionIdx = static_cast<uint8_t>(schemaVersion);
     259           22 :         const size_t tableSize = sizeof(CCU_OPS_TABLE) / sizeof(CCU_OPS_TABLE[0]);
     260           22 :         if (versionIdx >= tableSize || CCU_OPS_TABLE[versionIdx] == nullptr) {
     261            0 :             HCCL_ERROR("[ResolveCcuOps] Invalid schema version[%u], table_size[%zu].", versionIdx, tableSize);
     262            0 :             ops = nullptr;
     263            0 :             return HCCL_E_INTERNAL;
     264              :         }
     265              : 
     266           22 :         ops = CCU_OPS_TABLE[versionIdx];
     267           22 :         return HCCL_SUCCESS;
     268              :     }
     269              : 
     270              : } // namespace
     271              : 
     272           34 : HcclResult GetCcuOps(const CcuVersionOps *&ops)
     273              : {
     274           34 :     ops = nullptr;
     275           34 :     CcuSchemaVersion schemaVersion = CcuSchemaVersion::CCU_SCHEMA_COUNT;
     276           34 :     const HcclResult ret = GetCcuSchemaVersion(schemaVersion);
     277           34 :     if (ret != HCCL_SUCCESS) {
     278           12 :         HCCL_ERROR("[GetCcuOps] GetCcuSchemaVersion failed, ret[%d].", ret);
     279           12 :         return ret;
     280              :     }
     281              : 
     282           22 :     const HcclResult resolveRet = ResolveCcuOps(schemaVersion, ops);
     283           22 :     if (resolveRet != HCCL_SUCCESS) {
     284            0 :         return resolveRet;
     285              :     }
     286           22 :     return HCCL_SUCCESS;
     287              : }
     288              : } // namespace hcomm
        

Generated by: LCOV version 2.0-1