LCOV - code coverage report
Current view: top level - tsdclient/src - process_mode_manager.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 97.6 % 167 163
Test Date: 2026-07-28 10:52:48 Functions: 100.0 % 27 27

            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 "inc/process_mode_manager.h"
      12              : #include <string>
      13              : #include "tsd_log.h"
      14              : #include "tsd/status.h"
      15              : #include "env_internal_api.h"
      16              : #include "tsd_msg_parse_reg.h"
      17              : 
      18              : namespace {
      19              : constexpr int32_t ERROR_LOG = 3;
      20              : constexpr int32_t DEBUG_LOG = 0;
      21              : constexpr uint32_t DEFAULT_NET_SERVICE = 2U;
      22              : } // namespace
      23              : 
      24              : namespace tsd {
      25              : 
      26              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      27              :     ServerToClientMsgProc1, HDCMessage::TSD_START_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
      28              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      29              :     ServerToClientMsgProc2, HDCMessage::TSD_CLOSE_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
      30              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      31              :     ServerToClientMsgProc3, HDCMessage::TSD_UPDATE_PROIFILING_RSP, &ProcessModeManager::ServerToClientMsgProc);
      32              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      33              :     ServerToClientMsgProc4, HDCMessage::TSD_CHECK_PACKAGE_RSP, &ProcessModeManager::PackageInfoMsgProc);
      34              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      35              :     ServerToClientMsgProc5, HDCMessage::TSD_GET_PID_QOS_RSP, &ProcessModeManager::CapabilityResMsgProc);
      36              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      37              :     ServerToClientMsgProc6, HDCMessage::TSD_DRV_BIND_HOST_PID_RSP, &ProcessModeManager::ServerToClientMsgProc);
      38              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      39              :     ServerToClientMsgProc7, HDCMessage::TSD_GET_DEVICE_RUNTIME_CHECKCODE_RSP, &ProcessModeManager::PackageInfoMsgProc);
      40              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      41              :     ServerToClientMsgProc8, HDCMessage::TSD_REMOVE_FILE_RSP, &ProcessModeManager::ServerToClientMsgProc);
      42              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      43              :     ServerToClientMsgProc9, HDCMessage::TSD_OPEN_SUB_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
      44              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      45              :     ServerToClientMsgProc10, HDCMessage::TSD_GET_SUB_PROC_STATUS_RSP, &ProcessModeManager::ServerToClientMsgProc);
      46              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      47              :     ServerToClientMsgProc11, HDCMessage::TSD_OM_PKG_DECOMPRESS_STATUS_RSP, &ProcessModeManager::ServerToClientMsgProc);
      48              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      49              :     ServerToClientMsgProc12, HDCMessage::TSD_CLOSE_SUB_PROC_RSP, &ProcessModeManager::ServerToClientMsgProc);
      50              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      51              :     ServerToClientMsgProc13, HDCMessage::TSD_GET_SUPPORT_CAPABILITY_LEVEL_RSP,
      52              :     &ProcessModeManager::ServerToClientMsgProc);
      53              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      54              :     ServerToClientMsgProc14, HDCMessage::TSD_GET_DEVICE_DSHAPE_CHECKCODE_RSP, &ProcessModeManager::PackageInfoMsgProc);
      55              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      56              :     ServerToClientMsgProc15, HDCMessage::TSD_SUPPORT_OM_INNER_DEC_RSP, &ProcessModeManager::ServerToClientMsgProc);
      57              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      58              :     ServerToClientMsgProc16, HDCMessage::TSD_CLOSE_SUB_PROC_LIST_RSP, &ProcessModeManager::ServerToClientMsgProc);
      59              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      60              :     ServerToClientMsgProc17, HDCMessage::TSD_GET_SUPPORT_ADPROF_RSP, &ProcessModeManager::ServerToClientMsgProc);
      61              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      62              :     ServerToClientMsgProc18, HDCMessage::TSD_CHECK_PACKAGE_RETRY_RSP, &ProcessModeManager::PackageInfoMsgProc);
      63              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      64              :     ServerToClientMsgProc19, HDCMessage::TSD_GET_DEVICE_PACKAGE_CHECKCODE_NORMAL_RSP,
      65              :     &ProcessModeManager::PackageInfoMsgProc);
      66              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      67              :     ServerToClientMsgProc20, HDCMessage::TSD_UPDATE_PACKAGE_PROCESS_CONFIG_RSP,
      68              :     &ProcessModeManager::ServerToClientMsgProc);
      69              : TDT_REGISTER_NORMAL_MSG_PARSE_FUNC(
      70              :     ServerToClientMsgProc21, HDCMessage::TSD_GET_DEVICE_CANN_HS_CHECKCODE_RSP, &ProcessModeManager::PackageInfoMsgProc);
      71              : 
      72          263 : ProcessModeManager::ProcessModeManager(const uint32_t deviceId, const uint32_t deviceMode)
      73              :     : ClientManager(deviceId),
      74          263 :       logLevel_("003"),
      75          263 :       commAgent_(deviceId),
      76          263 :       sharedCtx_(),
      77          263 :       capabilityMgr_(deviceId, commAgent_, IsAdcEnv()),
      78          263 :       packageMgr_(deviceId, commAgent_, capabilityMgr_, GetPlatInfoMode(), IsAdcEnv(), GetPlatInfoChipType()),
      79          263 :       rspDispatcher_(sharedCtx_, packageMgr_, capabilityMgr_),
      80          263 :       tsdCtrl_(commAgent_, capabilityMgr_, packageMgr_, sharedCtx_, deviceMode),
      81          263 :       subProcCtrl_(tsdCtrl_, commAgent_, capabilityMgr_, packageMgr_, sharedCtx_),
      82          526 :       ccecpuLogLevel_(""),
      83          789 :       aicpuLogLevel_("")
      84              : {
      85          263 :     sharedCtx_.logicDeviceId = deviceId;
      86          263 :     sharedCtx_.isAdcEnv = IsAdcEnv();
      87          263 :     sharedCtx_.platInfoMode = GetPlatInfoMode();
      88          263 :     sharedCtx_.aicpuSchedMode = aicpuSchedMode_;
      89          263 :     GetLogLevel();
      90          263 :     SyncSharedCtxLogLevels();
      91          263 :     tsdCtrl_.SetTsdStartInfo(false, false, false);
      92          263 : }
      93              : 
      94            4 : void ProcessModeManager::Destroy()
      95              : {
      96            4 :     tsdCtrl_.SetTsdStartInfo(false, false, false);
      97            4 :     commAgent_.ReleaseDeviceConnection();
      98            4 :     packageMgr_.ResetOnClose();
      99            4 : }
     100              : 
     101            5 : void ProcessModeManager::ParseModuleLogLevelByKey(const std::string& keyStr, const std::string& valStr)
     102              : {
     103            5 :     int32_t val = 0;
     104            5 :     if (TransStrToInt(valStr, val)) {
     105            4 :         if (val >= DEBUG_LOG && val <= ERROR_LOG) {
     106            4 :             if (keyStr == "CCECPU") {
     107            3 :                 ccecpuLogLevel_ = valStr;
     108            1 :             } else if (keyStr == "AICPU") {
     109            1 :                 aicpuLogLevel_ = valStr;
     110              :             } else {
     111            0 :                 TSD_INFO("[TsdClient] invalid key[%s]", keyStr.c_str());
     112              :             }
     113              :         }
     114              :     }
     115            5 : }
     116              : 
     117          270 : void ProcessModeManager::ParseModuleLogLevel(const std::string& envModuleLogLevel)
     118              : {
     119          270 :     std::size_t offsetColon = 0;
     120          270 :     std::size_t offsetEqual = 0;
     121          540 :     std::string keyStr = "";
     122          270 :     std::string valStr = "";
     123          270 :     std::string moduleLogLevel = envModuleLogLevel;
     124              :     while (true) {
     125          272 :         offsetColon = moduleLogLevel.find(":");
     126          272 :         if (offsetColon == std::string::npos) {
     127          270 :             offsetEqual = moduleLogLevel.find("=");
     128          270 :             if (offsetEqual != std::string::npos) {
     129            3 :                 keyStr = moduleLogLevel.substr(0U, offsetEqual);
     130            3 :                 valStr = moduleLogLevel.substr(offsetEqual + 1UL);
     131            3 :                 ParseModuleLogLevelByKey(keyStr, valStr);
     132              :             }
     133          270 :             return;
     134              :         } else {
     135            2 :             std::string moduleLogLevelPart1 = moduleLogLevel.substr(0U, offsetColon);
     136            2 :             offsetEqual = moduleLogLevelPart1.find("=");
     137            2 :             if (offsetEqual != std::string::npos) {
     138            2 :                 keyStr = moduleLogLevelPart1.substr(0U, offsetEqual);
     139            2 :                 valStr = moduleLogLevelPart1.substr(offsetEqual + 1UL);
     140            2 :                 ParseModuleLogLevelByKey(keyStr, valStr);
     141              :             }
     142            2 :             moduleLogLevel = moduleLogLevel.substr(offsetColon + 1UL);
     143            2 :         }
     144            2 :     }
     145          270 : }
     146              : 
     147          266 : void ProcessModeManager::GetLogLevel()
     148              : {
     149          266 :     int32_t defaultEventLevel = 1;
     150          266 :     int32_t defaultLogLevel = 3;
     151          266 :     if (&dlog_getlevel != nullptr) {
     152          266 :         defaultLogLevel = dlog_getlevel(AICPU, &defaultEventLevel);
     153              :     }
     154          266 :     std::string eventLevelString = std::to_string(defaultEventLevel);
     155          266 :     std::string logLevelString = std::to_string(defaultLogLevel);
     156          266 :     if (!IsAdcEnv()) {
     157          530 :         const std::string logPattern = "^[0-4]{1}$";
     158          265 :         const std::string eventPattern = "^[0-1]{1}$";
     159          265 :         std::string envLogLevel;
     160          265 :         std::string envModuleLogLevel;
     161          265 :         std::string envEventLevel;
     162          265 :         GetEnvFromMmSys(MM_ENV_ASCEND_GLOBAL_LOG_LEVEL, "ASCEND_GLOBAL_LOG_LEVEL", envLogLevel);
     163          265 :         GetEnvFromMmSys(MM_ENV_ASCEND_GLOBAL_EVENT_ENABLE, "ASCEND_GLOBAL_EVENT_ENABLE", envEventLevel);
     164          265 :         GetEnvFromMmSys(MM_ENV_ASCEND_MODULE_LOG_LEVEL, "ASCEND_MODULE_LOG_LEVEL", envModuleLogLevel);
     165          265 :         TSD_RUN_INFO(
     166              :             "[TsdClient] get ASCEND_GLOBAL_LOG_LEVEL [%s] ASCEND_GLOBAL_EVENT_ENABLE [%s] "
     167              :             "ASCEND_MODULE_LOG_LEVEL [%s]",
     168              :             envLogLevel.c_str(), envEventLevel.c_str(), envModuleLogLevel.c_str());
     169         1060 :         if (ValidateStr(envLogLevel.c_str(), logPattern.c_str())) {
     170            2 :             logLevelString = envLogLevel;
     171              :         }
     172         1060 :         if (ValidateStr(envEventLevel.c_str(), eventPattern.c_str())) {
     173            2 :             eventLevelString = envEventLevel;
     174              :         }
     175         1060 :         if (ValidateStr(envModuleLogLevel.c_str(), envModuleLogLevel.c_str())) {
     176          265 :             ParseModuleLogLevel(envModuleLogLevel);
     177              :         }
     178          265 :     }
     179          266 :     logLevel_ = eventLevelString + "0" + logLevelString;
     180          266 :     TSD_INFO("[TsdClient] Get Log Level[%s]", logLevel_.c_str());
     181          266 :     SyncSharedCtxLogLevels();
     182          266 : }
     183              : 
     184          531 : void ProcessModeManager::SyncSharedCtxLogLevels()
     185              : {
     186          531 :     sharedCtx_.logLevel = logLevel_;
     187          531 :     sharedCtx_.ccecpuLogLevel = ccecpuLogLevel_;
     188          531 :     sharedCtx_.aicpuLogLevel = aicpuLogLevel_;
     189          531 :     sharedCtx_.profilingMode = static_cast<uint32_t>(profilingMode_);
     190          531 :     sharedCtx_.aicpuSchedMode = aicpuSchedMode_;
     191          531 : }
     192              : 
     193           19 : TSD_StatusT ProcessModeManager::Open(const uint32_t rankSize) { return tsdCtrl_.Open(rankSize); }
     194              : 
     195            1 : TSD_StatusT ProcessModeManager::OpenAicpuSd() { return tsdCtrl_.OpenAicpuSd(); }
     196              : 
     197           20 : TSD_StatusT ProcessModeManager::Close(const uint32_t flag) { return tsdCtrl_.Close(flag); }
     198              : 
     199            4 : TSD_StatusT ProcessModeManager::GetHdcConctStatus(int32_t& hdcSessStat)
     200              : {
     201            4 :     return tsdCtrl_.GetHdcConctStatus(hdcSessStat);
     202              : }
     203              : 
     204            5 : TSD_StatusT ProcessModeManager::UpdateProfilingConf(const uint32_t& flag) { return tsdCtrl_.UpdateProfilingConf(flag); }
     205              : 
     206            3 : TSD_StatusT ProcessModeManager::InitQs(const InitFlowGwInfo* const initInfo) { return tsdCtrl_.InitQs(initInfo); }
     207              : 
     208          267 : ProcessModeManager::~ProcessModeManager() {}
     209              : 
     210            8 : TSD_StatusT ProcessModeManager::CapabilityGet(const int32_t type, const uint64_t ptr)
     211              : {
     212            8 :     return capabilityMgr_.CapabilityGet(type, ptr);
     213              : }
     214              : 
     215            1 : TSD_StatusT ProcessModeManager::LoadFileToDevice(
     216              :     const char_t* const filePath, const uint64_t pathLen, const char_t* const fileName, const uint64_t fileNameLen)
     217              : {
     218            1 :     return packageMgr_.LoadFileToDevice(filePath, pathLen, fileName, fileNameLen, tsdCtrl_.BuildBaseMessageContext());
     219              : }
     220              : 
     221           18 : TSD_StatusT ProcessModeManager::ProcessOpenSubProc(ProcOpenArgs* openArgs)
     222              : {
     223           18 :     return subProcCtrl_.OpenSubProc(openArgs);
     224              : }
     225              : 
     226            6 : TSD_StatusT ProcessModeManager::ProcessCloseSubProc(const pid_t closePid)
     227              : {
     228            6 :     return subProcCtrl_.CloseSubProc(closePid);
     229              : }
     230              : 
     231            4 : TSD_StatusT ProcessModeManager::GetSubProcStatus(ProcStatusInfo* pidInfo, const uint32_t arrayLen)
     232              : {
     233            4 :     return subProcCtrl_.GetSubProcStatus(pidInfo, arrayLen);
     234              : }
     235              : 
     236            2 : TSD_StatusT ProcessModeManager::RemoveFileOnDevice(const char_t* const filePath, const uint64_t pathLen)
     237              : {
     238            2 :     return subProcCtrl_.RemoveFileOnDevice(filePath, pathLen);
     239              : }
     240              : 
     241            6 : TSD_StatusT ProcessModeManager::ProcessCloseSubProcList(const ProcStatusParam* closeList, const uint32_t listSize)
     242              : {
     243            6 :     return subProcCtrl_.CloseSubProcList(closeList, listSize);
     244              : }
     245              : 
     246            3 : TSD_StatusT ProcessModeManager::GetSubProcListStatus(ProcStatusParam* pidInfo, const uint32_t arrayLen)
     247              : {
     248            3 :     return subProcCtrl_.GetSubProcListStatus(pidInfo, arrayLen);
     249              : }
     250              : 
     251            5 : TSD_StatusT ProcessModeManager::OpenNetService(const NetServiceOpenArgs* args)
     252              : {
     253            5 :     if (!capabilityMgr_.IsSupportCommonInterface(TSD_SUPPORT_MUL_HCCP)) {
     254            1 :         TSD_RUN_INFO("current package does not support opening net service with args");
     255            1 :         if (tsdCtrl_.Open(DEFAULT_NET_SERVICE) != TSD_OK) {
     256            1 :             TSD_ERROR("open default net service failed");
     257            1 :             return TSD_OPEN_DEFAULT_NET_SERVICE_FAILED;
     258              :         } else {
     259            0 :             return TSD_OK;
     260              :         }
     261              :     } else {
     262            4 :         if (args == nullptr) {
     263            1 :             TSD_ERROR("openArgs is null");
     264            1 :             return TSD_INTERNAL_ERROR;
     265              :         }
     266              :         ProcOpenArgs openArgs;
     267            3 :         pid_t subPid = 0;
     268            3 :         openArgs.procType = SubProcType::TSD_SUB_PROC_HCCP;
     269            3 :         openArgs.envParaList = nullptr;
     270            3 :         openArgs.envCnt = 0UL;
     271            3 :         openArgs.filePath = nullptr;
     272            3 :         openArgs.pathLen = 0UL;
     273            3 :         openArgs.extParamCnt = args->extParamCnt;
     274            3 :         openArgs.extParamList = args->extParamList;
     275            3 :         openArgs.subPid = &subPid;
     276            3 :         return subProcCtrl_.OpenSubProc(&openArgs);
     277              :     }
     278              : }
     279              : 
     280            2 : TSD_StatusT ProcessModeManager::CloseNetService()
     281              : {
     282            2 :     if (!capabilityMgr_.IsSupportCommonInterface(TSD_SUPPORT_MUL_HCCP)) {
     283            0 :         TSD_RUN_INFO("current package does not support closing net service with args");
     284            0 :         return TsdClose(0U);
     285              :     } else {
     286              :         ProcStatusParam closeList;
     287            2 :         closeList.pid = tsdCtrl_.GetHccpPid();
     288            2 :         closeList.curStat = SubProcessStatus::SUB_PROCESS_STATUS_NORMAL;
     289            2 :         closeList.procType = SubProcType::TSD_SUB_PROC_HCCP;
     290            2 :         return subProcCtrl_.CloseSubProcList(&closeList, 1U);
     291              :     }
     292              : }
     293              : 
     294           86 : void ProcessModeManager::ServerToClientMsgProc(const uint32_t sessionID, const HDCMessage& msg)
     295              : {
     296           86 :     TSD_INFO("[ServerToClientMsgProc] [sessionID=%u]", sessionID);
     297           86 :     const uint32_t realDeviceId = msg.real_device_id();
     298              :     const std::shared_ptr<ProcessModeManager> client =
     299           86 :         std::dynamic_pointer_cast<ProcessModeManager>(ClientManager::GetInstance(realDeviceId, DIE_MODE, false));
     300           86 :     TSD_CHECK_NULLPTR_VOID(client);
     301           86 :     client->GetDispatcher().DeviceMsgProcess(msg);
     302           86 : }
     303              : 
     304            3 : void ProcessModeManager::PackageInfoMsgProc(const uint32_t sessionID, const HDCMessage& msg)
     305              : {
     306            3 :     TSD_INFO("[PackageInfoMsgProc] [sessionID=%u]", sessionID);
     307            3 :     const uint32_t realDeviceId = msg.real_device_id();
     308            3 :     TSD_INFO(
     309              :         "[PackageInfoMsgProc] [sessionID=%u] deviceId[%u], checkCode[%u]", sessionID, realDeviceId, msg.check_code());
     310              :     const std::shared_ptr<ProcessModeManager> client =
     311            3 :         std::dynamic_pointer_cast<ProcessModeManager>(ClientManager::GetInstance(realDeviceId, DIE_MODE, false));
     312            3 :     TSD_CHECK_NULLPTR_VOID(client);
     313            3 :     client->GetDispatcher().SaveDeviceCheckCode(msg);
     314            3 : }
     315              : 
     316            3 : void ProcessModeManager::CapabilityResMsgProc(const uint32_t sessionID, const HDCMessage& msg)
     317              : {
     318            3 :     TSD_INFO("[CapabilityResMsgProc] [sessionID=%u]", sessionID);
     319            3 :     const uint32_t realDeviceId = msg.real_device_id();
     320              :     const std::shared_ptr<ProcessModeManager> client =
     321            3 :         std::dynamic_pointer_cast<ProcessModeManager>(ClientManager::GetInstance(realDeviceId, DIE_MODE, false));
     322            3 :     TSD_CHECK_NULLPTR_VOID(client);
     323            3 :     client->GetDispatcher().PidQosMsgProc(msg);
     324            3 : }
     325              : 
     326              : } // namespace tsd
        

Generated by: LCOV version 2.0-1