LCOV - code coverage report
Current view: top level - basic_component/message_builder - hdc_message_builder.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 90.4 % 198 179
Test Date: 2026-08-12 11:03:52 Functions: 100.0 % 20 20

            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              : #include "hdc_message_builder.h"
      11              : 
      12              : #include <string>
      13              : 
      14              : #include "capability_manager.h"
      15              : #include "env_internal_api.h"
      16              : #include "tsd_log.h"
      17              : #include "tsd_util_func.h"
      18              : 
      19              : namespace tsd {
      20              : namespace {
      21              : // Compute the tsdclient capability bitmap. Side-effect (TSD_INFO log) is
      22              : // intentionally preserved so the produced HDCMessage and surrounding log
      23              : // output are byte-equivalent to the legacy implementation.
      24           23 : uint32_t BuildTsdClientCapabilityLevel()
      25              : {
      26           23 :     uint32_t curSupport = 0U;
      27           23 :     TSD_BITMAP_SET(curSupport, TSDCLIENT_SUPPORT_NEW_ERRORCODE);
      28           23 :     TSD_INFO("Set tsdclient capability level, value is [%u].", curSupport);
      29           23 :     return curSupport;
      30              : }
      31              : 
      32              : // Set both the modulo device id (used for routing) and the real device id
      33              : // (used for callback verification) carried by every per-device HDC message.
      34          164 : void SetDeviceIds(HDCMessage& msg, const MessageContext& ctx)
      35              : {
      36          164 :     msg.set_device_id(ctx.logicDeviceId % PER_OS_CHIP_NUM);
      37              :     // 传递真实的deviceId在回调的时候做校验使用,替代reqId
      38          164 :     msg.set_real_device_id(ctx.logicDeviceId);
      39          164 : }
      40              : 
      41              : // Populate proc_sign_pid with the process tgid, and optionally the sign string.
      42          181 : TSD_StatusT SetProcSignPid(HDCMessage& msg, const MessageContext& ctx, const bool withSign = false)
      43              : {
      44          181 :     ProcessSignPid* const signPid = msg.mutable_proc_sign_pid();
      45          181 :     if (signPid == nullptr) {
      46            0 :         TSD_ERROR("signPid is null.");
      47            0 :         return TSD_INTERNAL_ERROR;
      48              :     }
      49          181 :     signPid->set_proc_pid(static_cast<uint32_t>(ctx.procSign.tgid));
      50          181 :     if (withSign) {
      51          196 :         signPid->set_proc_sign(std::string(ctx.procSign.sign));
      52              :     }
      53          181 :     TSD_RUN_INFO(
      54              :         "[HdcMessageBuilder] tsd get process sign successfully, procpid[%u]", static_cast<uint32_t>(ctx.procSign.tgid));
      55          181 :     return TSD_OK;
      56              : }
      57              : 
      58              : // Append the sub-process entries described by ctx (pid list + optional type
      59              : // list) to the given SubProcStatus repeated field, keeping the type list and
      60              : // the status list parallel.
      61           19 : void FillSubProcList(
      62              :     HDCMessage& msg, const MessageContext& ctx, google::protobuf::RepeatedPtrField<SubProcStatus>* statusList)
      63              : {
      64          145 :     for (size_t index = 0U; index < ctx.subProcPidList.size(); index++) {
      65          126 :         SubProcStatus* const curStatus = statusList->Add();
      66          126 :         if (index < ctx.subProcTypeList.size()) {
      67          118 :             msg.add_sub_proc_type_list(ctx.subProcTypeList[index]);
      68              :         }
      69          126 :         curStatus->set_sub_proc_pid(ctx.subProcPidList[index]);
      70              :     }
      71           19 : }
      72              : } // namespace
      73              : 
      74           23 : TSD_StatusT HdcMessageBuilder::BuildOpen(HDCMessage& hdcMsg, const MessageContext& ctx)
      75              : {
      76           23 :     hdcMsg.set_rank_size(ctx.rankSize);
      77           23 :     hdcMsg.set_start_hccp(ctx.startHccp);
      78           23 :     hdcMsg.set_start_cp(ctx.startCp);
      79           23 :     hdcMsg.set_profiling_mode(ctx.profilingMode);
      80           23 :     SetDeviceIds(hdcMsg, ctx);
      81           23 :     LogLevel* const level = hdcMsg.mutable_log_level();
      82           23 :     if (level == nullptr) {
      83            0 :         TSD_ERROR("mutable log level error");
      84            0 :         return TSD_INTERNAL_ERROR;
      85              :     }
      86           23 :     level->set_log_level(ctx.logLevel);
      87           23 :     CcecpuLogLevel* const ccecpuLogLevel = hdcMsg.mutable_ccecpu_log_level();
      88           23 :     if (ccecpuLogLevel != nullptr) {
      89           23 :         ccecpuLogLevel->set_ccecpu_log_level(ctx.ccecpuLogLevel);
      90              :     }
      91           23 :     AicpuLogLevel* const aicpuLogLevel = hdcMsg.mutable_aicpu_log_level();
      92           23 :     if (aicpuLogLevel != nullptr) {
      93           23 :         aicpuLogLevel->set_aicpu_log_level(ctx.aicpuLogLevel);
      94              :     }
      95           23 :     const TSD_StatusT signRet = SetProcSignPid(hdcMsg, ctx, true);
      96           23 :     if (signRet != TSD_OK) {
      97            0 :         return signRet;
      98              :     }
      99           23 :     hdcMsg.set_check_code(ctx.aicpuKernelCheckCode);
     100           23 :     hdcMsg.set_extendpkg_check_code(ctx.aicpuExtendKernelCheckCode);
     101           23 :     hdcMsg.set_ascendcpppkg_check_code(ctx.ascendcppCheckCode);
     102           23 :     hdcMsg.set_type(HDCMessage::TSD_START_PROC_MSG);
     103           23 :     hdcMsg.set_device_mode(ctx.aicpuDeviceMode);
     104           23 :     hdcMsg.set_aicpu_sched_mode(static_cast<uint32_t>(ctx.aicpuSchedMode));
     105           23 :     const uint32_t tsdclientCapabilityLevel = BuildTsdClientCapabilityLevel();
     106           23 :     hdcMsg.set_tsdclient_capability_level(tsdclientCapabilityLevel);
     107           23 :     std::string aicpuPath;
     108           23 :     GetEnvFromMmSys(MM_ENV_ASCEND_AICPU_PATH, "ASCEND_AICPU_PATH", aicpuPath);
     109           23 :     AscendAicpuPath* const ascendAicpuPath = hdcMsg.mutable_ascend_aicpu_path();
     110           23 :     if (ascendAicpuPath == nullptr) {
     111            0 :         TSD_ERROR("mutable ascend aicpu path error");
     112            0 :         return TSD_INTERNAL_ERROR;
     113              :     }
     114              :     ascendAicpuPath->set_ascend_aicpu_path(aicpuPath);
     115           23 :     return TSD_OK;
     116           23 : }
     117              : 
     118           21 : TSD_StatusT HdcMessageBuilder::BuildClose(HDCMessage& msg, const MessageContext& ctx)
     119              : {
     120           21 :     SetDeviceIds(msg, ctx);
     121           21 :     msg.set_type(HDCMessage::TSD_CLOSE_PROC_MSG);
     122           21 :     msg.set_rank_size(ctx.rankSize);
     123           21 :     return SetProcSignPid(msg, ctx);
     124              : }
     125              : 
     126            4 : TSD_StatusT HdcMessageBuilder::BuildUpdateProfiling(HDCMessage& msg, const MessageContext& ctx)
     127              : {
     128            4 :     SetDeviceIds(msg, ctx);
     129            4 :     msg.set_type(HDCMessage::TSD_UPDATE_PROIFILING_MSG);
     130            4 :     msg.set_profiling_mode(ctx.profilingMode);
     131            4 :     msg.set_rank_size(ctx.rankSize);
     132            4 :     return SetProcSignPid(msg, ctx);
     133              : }
     134              : 
     135            6 : TSD_StatusT HdcMessageBuilder::BuildOmFileDecompress(HDCMessage& msg, const MessageContext& ctx)
     136              : {
     137            6 :     SetDeviceIds(msg, ctx);
     138            6 :     msg.set_type(HDCMessage::TSD_OM_PKG_DECOMPRESS_STATUS);
     139            6 :     msg.set_omfile_name(ctx.omfileName);
     140            6 :     return SetProcSignPid(msg, ctx);
     141              : }
     142              : 
     143           10 : TSD_StatusT HdcMessageBuilder::BuildPackageCheckCode(HDCMessage& msg, const MessageContext& ctx)
     144              : {
     145           10 :     msg.set_real_device_id(ctx.logicDeviceId);
     146           10 :     msg.set_type(static_cast<HDCMessage::MsgType>(ctx.msgType));
     147           10 :     msg.set_check_code(ctx.checkCode);
     148           10 :     msg.set_before_send_pkg(ctx.beforeSendPkg);
     149           10 :     return SetProcSignPid(msg, ctx, true);
     150              : }
     151              : 
     152           66 : TSD_StatusT HdcMessageBuilder::BuildCapability(HDCMessage& msg, const MessageContext& ctx)
     153              : {
     154           66 :     const CapabilitySpec* spec = FindCapabilitySpec(ctx.capabilityType);
     155           66 :     if (spec != nullptr) {
     156           65 :         msg.set_type(spec->msgType);
     157              :     }
     158           66 :     SetDeviceIds(msg, ctx);
     159           66 :     return SetProcSignPid(msg, ctx);
     160              : }
     161              : 
     162            5 : TSD_StatusT HdcMessageBuilder::BuildCloseSubProc(HDCMessage& msg, const MessageContext& ctx)
     163              : {
     164            5 :     SetDeviceIds(msg, ctx);
     165            5 :     msg.set_type(HDCMessage::TSD_CLOSE_SUB_PROC);
     166            5 :     msg.set_close_sub_proc_pid(ctx.closeSubProcPid);
     167            5 :     return SetProcSignPid(msg, ctx);
     168              : }
     169              : 
     170            4 : TSD_StatusT HdcMessageBuilder::BuildRemoveFile(HDCMessage& msg, const MessageContext& ctx)
     171              : {
     172            4 :     SetDeviceIds(msg, ctx);
     173            4 :     msg.set_type(HDCMessage::TSD_REMOVE_FILE);
     174            4 :     msg.set_remove_file_path(ctx.removeFilePath);
     175            4 :     return SetProcSignPid(msg, ctx);
     176              : }
     177              : 
     178            7 : TSD_StatusT HdcMessageBuilder::BuildCannHsCheckCode(HDCMessage& msg, const MessageContext& ctx)
     179              : {
     180            7 :     msg.set_real_device_id(ctx.logicDeviceId);
     181            7 :     msg.set_type(HDCMessage::TSD_GET_DEVICE_CANN_HS_CHECKCODE);
     182            7 :     msg.set_package_max_process_time(ctx.packageMaxProcessTime);
     183            7 :     msg.set_package_worker_type(ctx.packageWorkerType);
     184            7 :     msg.set_package_type(ctx.packageType);
     185            7 :     SinkPackageHashCodeInfo* const pkgHostInfo = msg.add_package_hash_code_list();
     186            7 :     if (pkgHostInfo == nullptr) {
     187            0 :         TSD_ERROR("add package hash code list error");
     188            0 :         return TSD_INTERNAL_ERROR;
     189              :     }
     190            7 :     pkgHostInfo->set_package_name(ctx.packageName);
     191            7 :     pkgHostInfo->set_hash_code(ctx.hashCode);
     192            7 :     return TSD_OK;
     193              : }
     194              : 
     195            7 : TSD_StatusT HdcMessageBuilder::BuildGetSubProcStatus(HDCMessage& msg, const MessageContext& ctx)
     196              : {
     197            7 :     SetDeviceIds(msg, ctx);
     198            7 :     msg.set_type(HDCMessage::TSD_GET_SUB_PROC_STATUS);
     199            7 :     const TSD_StatusT ret = SetProcSignPid(msg, ctx);
     200            7 :     if (ret != TSD_OK) {
     201            0 :         return ret;
     202              :     }
     203            7 :     FillSubProcList(msg, ctx, msg.mutable_sub_proc_status_list());
     204            7 :     return TSD_OK;
     205              : }
     206              : 
     207           12 : TSD_StatusT HdcMessageBuilder::BuildCloseSubProcList(HDCMessage& msg, const MessageContext& ctx)
     208              : {
     209           12 :     SetDeviceIds(msg, ctx);
     210           12 :     msg.set_type(HDCMessage::TSD_CLOSE_SUB_PROC_LIST);
     211           12 :     const TSD_StatusT ret = SetProcSignPid(msg, ctx);
     212           12 :     if (ret != TSD_OK) {
     213            0 :         return ret;
     214              :     }
     215           12 :     FillSubProcList(msg, ctx, msg.mutable_close_sub_list());
     216           12 :     return TSD_OK;
     217              : }
     218              : 
     219           16 : TSD_StatusT HdcMessageBuilder::BuildCommonOpen(HDCMessage& msg, const MessageContext& ctx)
     220              : {
     221           16 :     HelperSubProcess* const subProcessInfo = msg.mutable_helper_sub_proc();
     222           16 :     if (subProcessInfo == nullptr) {
     223            0 :         TSD_ERROR("helper_sub_proc is null.");
     224            0 :         return TSD_INTERNAL_ERROR;
     225              :     }
     226           16 :     subProcessInfo->set_process_type(ctx.subProcOpenType);
     227           16 :     if (ctx.hasSubProcFilePath) {
     228            8 :         subProcessInfo->set_file_path(ctx.subProcFilePath);
     229              :     }
     230           23 :     for (const std::pair<std::string, std::string>& env : ctx.subProcEnvList) {
     231            7 :         EnvPara* const evnParam = subProcessInfo->add_env_list();
     232            7 :         if (evnParam == nullptr) {
     233            0 :             TSD_ERROR("add env list error");
     234            0 :             return TSD_INTERNAL_ERROR;
     235              :         }
     236            7 :         evnParam->set_env_name(env.first);
     237            7 :         evnParam->set_env_value(env.second);
     238              :     }
     239           28 :     for (const std::string& extParam : ctx.subProcExtParamList) {
     240           12 :         subProcessInfo->add_ext_param_list(extParam);
     241              :     }
     242           16 :     if (!ctx.ascendInstallPath.empty()) {
     243           11 :         msg.set_ascend_install_path(ctx.ascendInstallPath);
     244              :     }
     245           16 :     if (ctx.withSubProcLogLevel) {
     246            6 :         LogLevel* const level = msg.mutable_log_level();
     247            6 :         if (level != nullptr) {
     248            6 :             level->set_log_level(ctx.logLevel);
     249              :         }
     250              :     }
     251           16 :     msg.set_type(HDCMessage::TSD_OPEN_SUB_PROC);
     252           16 :     SetDeviceIds(msg, ctx);
     253           16 :     return SetProcSignPid(msg, ctx, true);
     254              : }
     255              : 
     256            4 : TSD_StatusT HdcMessageBuilder::BuildCheckPackageRetry(HDCMessage& msg, const MessageContext& ctx)
     257              : {
     258            4 :     msg.set_real_device_id(ctx.logicDeviceId);
     259            4 :     msg.set_type(HDCMessage::TSD_CHECK_PACKAGE_RETRY);
     260            4 :     msg.set_check_code(ctx.checkCode);
     261            4 :     msg.set_package_type(ctx.packageType);
     262            4 :     msg.set_wait_flag(ctx.waitFlag);
     263            4 :     return TSD_OK;
     264              : }
     265              : 
     266            9 : TSD_StatusT HdcMessageBuilder::BuildCheckPackage(HDCMessage& msg, const MessageContext& ctx)
     267              : {
     268            9 :     msg.set_real_device_id(ctx.logicDeviceId);
     269            9 :     msg.set_type(HDCMessage::TSD_CHECK_PACKAGE);
     270            9 :     msg.set_asan(ctx.asan);
     271            9 :     if (ctx.checkCode != 0U) {
     272            4 :         msg.set_check_code(ctx.checkCode);
     273              :     }
     274            9 :     if (ctx.extendpkgCheckCode != 0U) {
     275            4 :         msg.set_extendpkg_check_code(ctx.extendpkgCheckCode);
     276              :     }
     277            9 :     if (ctx.ascendcppCheckCode != 0U) {
     278            4 :         msg.set_ascendcpppkg_check_code(ctx.ascendcppCheckCode);
     279              :     }
     280            9 :     return TSD_OK;
     281              : }
     282              : 
     283            7 : TSD_StatusT HdcMessageBuilder::BuildUpdatePackageConfig(HDCMessage& msg, const MessageContext& ctx)
     284              : {
     285            7 :     msg.set_real_device_id(ctx.logicDeviceId);
     286            7 :     msg.set_type(HDCMessage::TSD_UPDATE_PACKAGE_PROCESS_CONFIG);
     287            7 :     return SetProcSignPid(msg, ctx, false);
     288              : }
     289              : 
     290           10 : TSD_StatusT HdcMessageBuilder::BuildNormalCheckCode(HDCMessage& msg, const MessageContext& ctx)
     291              : {
     292           10 :     msg.set_real_device_id(ctx.logicDeviceId);
     293           10 :     msg.set_type(HDCMessage::TSD_GET_DEVICE_PACKAGE_CHECKCODE_NORMAL);
     294           10 :     SinkPackageHashCodeInfo* const pkgHostInfo = msg.add_package_hash_code_list();
     295           10 :     if (pkgHostInfo == nullptr) {
     296            0 :         TSD_ERROR("add package hash code list error");
     297            0 :         return TSD_INTERNAL_ERROR;
     298              :     }
     299           10 :     pkgHostInfo->set_package_name(ctx.packageName);
     300           10 :     pkgHostInfo->set_hash_code(ctx.hashCode);
     301           10 :     if (!HostPluginVersionEmpty(ctx.hostPluginVersion)) {
     302            4 :         PluginPackageVersionInfo* const info = msg.add_host_plugin_versions();
     303            4 :         if (info == nullptr) {
     304            0 :             TSD_ERROR("add host plugin versions error");
     305            0 :             return TSD_INTERNAL_ERROR;
     306              :         }
     307            4 :         info->set_package_name(ctx.packageName);
     308            4 :         info->set_version(ctx.hostPluginVersion.version);
     309            4 :         info->set_timestamp(ctx.hostPluginVersion.timestamp);
     310              :     }
     311           10 :     msg.set_package_worker_type(ctx.packageWorkerType);
     312           10 :     msg.set_package_max_process_time(ctx.packageMaxProcessTime);
     313           10 :     msg.set_package_type(ctx.packageType);
     314           10 :     return TSD_OK;
     315              : }
     316              : } // namespace tsd
        

Generated by: LCOV version 2.0-1