LCOV - code coverage report
Current view: top level - tsdclient/src - tsd_process_controller.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 91.5 % 355 325
Test Date: 2026-08-12 11:03:52 Functions: 100.0 % 28 28

            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              : 
      11              : #include "inc/tsd_process_controller.h"
      12              : #include <sstream>
      13              : #include <string>
      14              : #include <vector>
      15              : #include <chrono>
      16              : #include <memory>
      17              : #include "error_manager.h"
      18              : #include "tsd_log.h"
      19              : #include "tsd/status.h"
      20              : #include "tsd_util_func.h"
      21              : #include "env_internal_api.h"
      22              : #include "hdc_message_builder.h"
      23              : #include "weak_ascend_hal.h"
      24              : #include "driver/ascend_hal.h"
      25              : #include "common/type_def.h"
      26              : 
      27              : namespace {
      28              : constexpr uint32_t DEFAULT_QS_RANKSIZE = 0U;
      29              : constexpr uint32_t ASAN_OPEN_TIMEOUT = 3600000U;
      30              : constexpr uint32_t MAX_QUEUE_ID_NUM = 8192U;
      31              : } // namespace
      32              : 
      33              : namespace tsd {
      34              : 
      35          365 : TsdProcessController::TsdProcessController(
      36              :     DeviceCommAgent& commAgent, CapabilityManager& capabilityMgr, PackageManager& packageMgr,
      37          365 :     ProcessSharedContext& sharedCtx, uint32_t aicpuDeviceMode)
      38          365 :     : commAgent_(commAgent),
      39          365 :       capabilityMgr_(capabilityMgr),
      40          365 :       packageMgr_(packageMgr),
      41          365 :       sharedCtx_(sharedCtx),
      42          365 :       aicpuDeviceMode_(aicpuDeviceMode)
      43          365 : {}
      44              : 
      45           19 : TSD_StatusT TsdProcessController::Open(const uint32_t rankSize)
      46              : {
      47           19 :     if (sharedCtx_.isAdcEnv) {
      48            1 :         TSD_INFO("skip to start aicpu-sd process.");
      49            1 :         return TSD_OPEN_NOT_SUPPORT_FOR_ADC;
      50              :     }
      51           18 :     return OpenProcess(rankSize);
      52              : }
      53              : 
      54            1 : TSD_StatusT TsdProcessController::OpenAicpuSd() { return OpenProcess(0U); }
      55              : 
      56           20 : TSD_StatusT TsdProcessController::OpenProcess(const uint32_t rankSize)
      57              : {
      58           20 :     TSD_RUN_INFO(
      59              :         "[ProcessModeManager] enter into open process deviceId[%u] rankSize[%u]", sharedCtx_.logicDeviceId, rankSize);
      60           20 :     TsdStartStatusInfo startInfo = {};
      61           20 :     const TimePoint beginOpen = std::chrono::high_resolution_clock::now();
      62           20 :     if (!CheckNeedToOpen(rankSize, startInfo)) {
      63            1 :         TSD_INFO("Open has already been done before.");
      64            1 :         return TSD_OK;
      65              :     }
      66              : 
      67           19 :     TSD_StatusT ret = LoadPackagesToDevice();
      68           19 :     TSD_CHECK(ret == TSD_OK, ret, "Load packages to device failed.");
      69              : 
      70           19 :     ret = InitTsdClient();
      71           19 :     TSD_CHECK(ret == TSD_OK, ret, "Init hdc client failed.");
      72           18 :     startInfo.startQs_ = false;
      73           18 :     ret = SendOpenMsg(rankSize, startInfo);
      74           18 :     TSD_CHECK(ret == TSD_OK, ret, "Send open message to device failed.");
      75           18 :     const TimePoint finSendOpenMsg = std::chrono::high_resolution_clock::now();
      76              : 
      77           18 :     ret = WaitOpenRsp(rankSize);
      78           18 :     if (ret != TSD_OK) {
      79            2 :         return ret;
      80              :     }
      81           16 :     const TimePoint finRecvRsp = std::chrono::high_resolution_clock::now();
      82           16 :     SetTsdStartInfo(startInfo.startCp_, startInfo.startHccp_, false);
      83              : 
      84           16 :     ret = ProcessQueueForAdc();
      85           16 :     if (ret != TSD_OK) {
      86            1 :         TSD_ERROR("ProcessQueueForAdc error");
      87            1 :         return ret;
      88              :     }
      89           15 :     const TimePoint finOpen = std::chrono::high_resolution_clock::now();
      90           15 :     LogOpenProcessDuration(beginOpen, finSendOpenMsg, finRecvRsp, finOpen);
      91           15 :     return TSD_OK;
      92              : }
      93              : 
      94           19 : TSD_StatusT TsdProcessController::LoadPackagesToDevice()
      95              : {
      96           19 :     TSD_StatusT ret = packageMgr_.LoadPackageConfigInfoToDevice(true);
      97           19 :     TSD_CHECK(ret == TSD_OK, ret, "Load package config info to device failed.");
      98              : 
      99           19 :     ret = packageMgr_.LoadSysOpKernel();
     100           19 :     TSD_CHECK(ret == TSD_OK, ret, "Send aicpu package to device failed.");
     101              : 
     102           19 :     ret = packageMgr_.LoadPackageToDeviceByConfig();
     103           19 :     TSD_CHECK(ret == TSD_OK, ret, "Load package to device by config failed");
     104           19 :     return TSD_OK;
     105              : }
     106              : 
     107           18 : TSD_StatusT TsdProcessController::WaitOpenRsp(const uint32_t rankSize)
     108              : {
     109           18 :     TSD_RUN_INFO(
     110              :         "[ProcessModeManager] deviceId[%u] sessionId[%u] rankSize[%u], wait subprocess start response",
     111              :         sharedCtx_.logicDeviceId, commAgent_.GetSessionId(), rankSize);
     112           18 :     const TSD_StatusT ret = IsAsanMmSysEnv() ? WaitRsp(ASAN_OPEN_TIMEOUT) : WaitRsp(0U);
     113           18 :     if (ret != TSD_OK) {
     114              :         REPORT_INPUT_ERROR("E39007", std::vector<std::string>(), std::vector<std::string>());
     115            2 :         TSD_ERROR("Wait open response from device failed.");
     116              :     }
     117           18 :     return ret;
     118              : }
     119              : 
     120           15 : void TsdProcessController::LogOpenProcessDuration(
     121              :     const TimePoint& beginOpen, const TimePoint& finSendOpenMsg, const TimePoint& finRecvRsp,
     122              :     const TimePoint& finOpen) const
     123              : {
     124           15 :     TSD_RUN_INFO(
     125              :         "[TsdClient][deviceId=%u] [sessionId=%u] start hccp and computer process success,"
     126              :         "phase4:[%zu]ms send open message to device,"
     127              :         "phase5:[%zu]ms receive open message to device,"
     128              :         "phase6:[%zu]ms process for adc on host,"
     129              :         "whole duration:[%zu]ms",
     130              :         sharedCtx_.logicDeviceId, commAgent_.GetSessionId(),
     131              :         std::chrono::duration_cast<std::chrono::milliseconds>(finSendOpenMsg - beginOpen).count(),
     132              :         std::chrono::duration_cast<std::chrono::milliseconds>(finRecvRsp - finSendOpenMsg).count(),
     133              :         std::chrono::duration_cast<std::chrono::milliseconds>(finOpen - finRecvRsp).count(),
     134              :         std::chrono::duration_cast<std::chrono::milliseconds>(finOpen - beginOpen).count());
     135           15 : }
     136              : 
     137           20 : TSD_StatusT TsdProcessController::ConstructCloseMsg(HDCMessage& msg) const
     138              : {
     139           20 :     return HdcMessageBuilder::BuildClose(msg, BuildBaseMessageContext());
     140              : }
     141              : 
     142           20 : TSD_StatusT TsdProcessController::Close(uint32_t flag)
     143              : {
     144           20 :     if (!commAgent_.IsInit()) {
     145            2 :         TSD_RUN_INFO("[TsdClient] tsd client no need to close");
     146            2 :         return TSD_OK;
     147              :     }
     148           18 :     TSD_CHECK_NULLPTR(
     149              :         commAgent_.GetDeviceComm(), TSD_INSTANCE_NOT_INITIALED, "[TsdClient] devCommClient_ is null in Close function");
     150           18 :     TsdCloseFlag tsdCloseFlag = {};
     151           18 :     ParseTsdCloseFlag(flag, tsdCloseFlag);
     152           18 :     if (tsdCloseFlag.quickCloseFlag != static_cast<uint32_t>(TsdCloseMode::QUICK_CLOSE_MODE)) {
     153           18 :         TSD_RUN_INFO(
     154              :             "[TsdClient] Close [deviceId=%u][sessionId=%u] hccp and computer enter", sharedCtx_.logicDeviceId,
     155              :             commAgent_.GetSessionId());
     156           18 :         TSD_StatusT ret = SendCloseMsg();
     157           18 :         TSD_CHECK(ret == TSD_OK, ret, "Send close message to device failed.");
     158           18 :         TSD_RUN_INFO(
     159              :             "[TsdClient][deviceId=%u] [sessionId=%u] wait hccp and computer process close response",
     160              :             sharedCtx_.logicDeviceId, commAgent_.GetSessionId());
     161           18 :         ret = WaitRsp(0U, false, true);
     162           18 :         TSD_CHECK(ret == TSD_OK, ret, "Wait open response from device failed.");
     163           18 :         TSD_RUN_INFO(
     164              :             "[TsdClient][logicDeviceId_=%u]has recv close hccp and computer process response",
     165              :             sharedCtx_.logicDeviceId);
     166              :     } else {
     167            0 :         TSD_RUN_INFO("Enable quick tsd close, close will only in host.");
     168              :     }
     169              : 
     170           18 :     commAgent_.ReleaseDeviceConnection();
     171           18 :     sharedCtx_.rspCode = ResponseCode::FAIL;
     172           18 :     isStartedHccp_ = false;
     173           18 :     hccpPid_ = 0U;
     174           18 :     SetTsdStartInfo(false, false, false);
     175           18 :     packageMgr_.ResetOnClose();
     176           18 :     TSD_RUN_INFO(
     177              :         "[TsdClient][deviceId=%u] [sessionId=%u] close hccp and "
     178              :         "computer process success",
     179              :         sharedCtx_.logicDeviceId, commAgent_.GetSessionId());
     180           18 :     return TSD_OK;
     181              : }
     182              : 
     183           65 : TSD_StatusT TsdProcessController::WaitRsp(const uint32_t timeout, const bool ignoreRecvErr, const bool isClose)
     184              : {
     185           65 :     const TSD_StatusT ret = commAgent_.RecvData(ignoreRecvErr, timeout);
     186           65 :     if (!sharedCtx_.isAdcEnv) {
     187           65 :         if (isClose && (ret == TSD_HDC_SERVER_CLIENT_SOCKET_CLOSED)) {
     188            1 :             TSD_RUN_INFO("close rsp not receive, server close the session");
     189            1 :             return TSD_OK;
     190              :         }
     191              :     }
     192           64 :     if ((ret != TSD_OK) || (static_cast<uint32_t>(sharedCtx_.rspCode) != 0U)) {
     193           14 :         const std::string reportMsg = BuildWaitRspErrReport(ret);
     194           14 :         if (!ignoreRecvErr) {
     195           13 :             TSD_ERROR("%s", reportMsg.c_str());
     196              :             TSD_REPORT_INNER_ERROR("%s", reportMsg.c_str());
     197              :         }
     198           14 :         if ((ret == TSD_OK) && (!sharedCtx_.startOrStopFailCode.empty())) {
     199              :             REPORT_INPUT_ERROR(
     200              :                 sharedCtx_.startOrStopFailCode.c_str(), std::vector<std::string>(), std::vector<std::string>());
     201              :         }
     202           14 :         return MapFailCodeToStatus();
     203           14 :     }
     204           50 :     return TSD_OK;
     205              : }
     206              : 
     207           20 : TSD_StatusT TsdProcessController::MapFailCodeToStatus() const
     208              : {
     209           20 :     if (sharedCtx_.startOrStopFailCode == "E30003") {
     210            3 :         return TSD_SUBPROCESS_NUM_EXCEED_THE_LIMIT;
     211           17 :     } else if (sharedCtx_.startOrStopFailCode == "E30004") {
     212            2 :         return TSD_SUBPROCESS_BINARY_FILE_DAMAGED;
     213           15 :     } else if (sharedCtx_.startOrStopFailCode == "E30006") {
     214            3 :         return TSD_VERIFY_OPP_FAIL;
     215           12 :     } else if (sharedCtx_.startOrStopFailCode == "E30007") {
     216            2 :         return TSD_ADD_AICPUSD_TO_CGROUP_FAILED;
     217              :     } else {
     218           10 :         return TSD_CLT_OPEN_FAILED;
     219              :     }
     220              : }
     221              : 
     222           17 : std::string TsdProcessController::BuildWaitRspErrReport(const TSD_StatusT recvRet) const
     223              : {
     224           17 :     std::ostringstream reportMsg;
     225           17 :     reportMsg << "tsd client wait response fail, hostpid:" << static_cast<uint32_t>(commAgent_.GetProcSign().tgid);
     226           17 :     if (recvRet != TSD_OK) {
     227           12 :         reportMsg << ", receive device response data failed, check hdc service.";
     228              :     } else {
     229            5 :         reportMsg << ", device response code[" << static_cast<uint32_t>(sharedCtx_.rspCode) << "]";
     230            5 :         if (!sharedCtx_.errMsg.empty()) {
     231            2 :             reportMsg << ". " << sharedCtx_.errMsg;
     232            2 :             reportMsg << ", error stack:\n" << sharedCtx_.errorLog;
     233              :         } else {
     234            3 :             reportMsg << ". unknown device error.";
     235              :         }
     236              :     }
     237           34 :     return reportMsg.str();
     238           17 : }
     239              : 
     240           25 : TSD_StatusT TsdProcessController::InitTsdClient()
     241              : {
     242           25 :     if (commAgent_.IsInit()) {
     243           22 :         TSD_INFO("[TsdClient] tsd client has already been initialized");
     244           22 :         return TSD_OK;
     245              :     }
     246            3 :     return commAgent_.InitTsdClient(sharedCtx_.isAdcEnv);
     247              : }
     248              : 
     249           87 : MessageContext TsdProcessController::BuildBaseMessageContext() const
     250              : {
     251           87 :     MessageContext ctx{};
     252           87 :     ctx.logicDeviceId = sharedCtx_.logicDeviceId;
     253           87 :     ctx.rankSize = rankSize_;
     254           87 :     ctx.procSign = commAgent_.GetProcSign();
     255           87 :     ctx.profilingMode = static_cast<uint32_t>(sharedCtx_.profilingMode);
     256           87 :     ctx.logLevel = sharedCtx_.logLevel;
     257           87 :     ctx.ccecpuLogLevel = sharedCtx_.ccecpuLogLevel;
     258           87 :     ctx.aicpuLogLevel = sharedCtx_.aicpuLogLevel;
     259           87 :     ctx.aicpuKernelCheckCode = packageMgr_.GetHostCheckCode(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL);
     260           87 :     ctx.aicpuExtendKernelCheckCode = packageMgr_.GetHostCheckCode(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL);
     261           87 :     ctx.ascendcppCheckCode = packageMgr_.GetHostCheckCode(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP);
     262           87 :     ctx.aicpuDeviceMode = aicpuDeviceMode_;
     263           87 :     if (sharedCtx_.aicpuSchedMode >= static_cast<uint64_t>(AICPU_SCHED_MODE_INVALID)) {
     264            0 :         TSD_RUN_WARN(
     265              :             "[TsdClient] invalid aicpuSchedMode:%llu", static_cast<unsigned long long>(sharedCtx_.aicpuSchedMode));
     266            0 :         ctx.aicpuSchedMode = AICPU_SCHED_MODE_INTERRUPT;
     267              :     } else {
     268           87 :         ctx.aicpuSchedMode = static_cast<SchedMode>(sharedCtx_.aicpuSchedMode);
     269              :     }
     270           87 :     ctx.qsInitGroupName = qsInitGrpName_;
     271           87 :     ctx.schedPolicy = schedPolicy_;
     272           87 :     return ctx;
     273            0 : }
     274              : 
     275           18 : TSD_StatusT TsdProcessController::ConstructOpenMsg(HDCMessage& hdcMsg, const TsdStartStatusInfo& startInfo) const
     276              : {
     277           18 :     MessageContext ctx = BuildBaseMessageContext();
     278           18 :     ctx.startHccp = startInfo.startHccp_;
     279           18 :     ctx.startCp = startInfo.startCp_;
     280           36 :     return HdcMessageBuilder::BuildOpen(hdcMsg, ctx);
     281           18 : }
     282              : 
     283           16 : TSD_StatusT TsdProcessController::SendOpenMsg(const uint32_t rankSize, const TsdStartStatusInfo startInfo)
     284              : {
     285           16 :     rankSize_ = rankSize;
     286           16 :     HDCMessage hdcMsg;
     287           16 :     if (ConstructOpenMsg(hdcMsg, startInfo) != TSD_OK) {
     288            0 :         TSD_ERROR("ConstructOpenMsg open msg error");
     289            0 :         return TSD_CLT_OPEN_FAILED;
     290              :     }
     291           16 :     if (startInfo.startQs_) {
     292            2 :         hdcMsg.set_type(HDCMessage::TSD_START_QS_MSG);
     293            2 :         qsInitGroupName* const groupName = hdcMsg.mutable_qs_init_group_name();
     294            2 :         if (groupName == nullptr) {
     295            0 :             return TSD_INTERNAL_ERROR;
     296              :         }
     297            2 :         groupName->set_qs_init_group_name(qsInitGrpName_);
     298            2 :         hdcMsg.set_sched_policy(schedPolicy_);
     299            2 :         std::string installPath;
     300            2 :         packageMgr_.GetAscendLatestIntallPath(installPath);
     301            2 :         if (!installPath.empty()) {
     302              :             hdcMsg.set_ascend_install_path(installPath);
     303              :         }
     304            2 :     } else {
     305           14 :         hdcMsg.set_type(HDCMessage::TSD_START_PROC_MSG);
     306           14 :         if (isStartedHccp_) {
     307            0 :             hdcMsg.set_start_hccp(true);
     308              :         }
     309              :     }
     310           16 :     const TSD_StatusT ret = commAgent_.SendMsg(hdcMsg);
     311           16 :     if (ret != TSD_OK) {
     312            0 :         TSD_ERROR(
     313              :             "[TsdClient][deviceId=%u][rankSize_=%u][procpid =%u] "
     314              :             "tsdclient start hccp and computer process failed",
     315              :             sharedCtx_.logicDeviceId, rankSize_, static_cast<uint32_t>(commAgent_.GetProcSign().tgid));
     316            0 :         return TSD_CLT_OPEN_FAILED;
     317              :     }
     318           16 :     return TSD_OK;
     319           16 : }
     320              : 
     321           19 : TSD_StatusT TsdProcessController::SendCloseMsg()
     322              : {
     323           19 :     HDCMessage msg;
     324           19 :     if (ConstructCloseMsg(msg) != TSD_OK) {
     325            1 :         TSD_ERROR("ConstructCloseMsg failed");
     326            1 :         return TSD_INTERNAL_ERROR;
     327              :     }
     328           18 :     const TSD_StatusT ret = commAgent_.SendMsg(msg);
     329           18 :     if (ret != TSD_OK) {
     330            1 :         TSD_ERROR(
     331              :             "[TsdClient][deviceId=%u] tsdclient close hccp and "
     332              :             "computer process failed",
     333              :             sharedCtx_.logicDeviceId);
     334            1 :         return TSD_CLT_CLOSE_FAILED;
     335              :     }
     336              : 
     337           17 :     return TSD_OK;
     338           19 : }
     339              : 
     340            5 : TSD_StatusT TsdProcessController::UpdateProfilingConf(const uint32_t flag)
     341              : {
     342            5 :     TSD_RUN_INFO(
     343              :         "[TsdClient] Update profiling mode [deviceId=%u][sessionId=%u][flag=%u]", sharedCtx_.logicDeviceId,
     344              :         commAgent_.GetSessionId(), flag);
     345              : 
     346            5 :     if (!commAgent_.IsInit()) {
     347            1 :         TSD_WARN("[TsdClient] tsd client need open first");
     348            1 :         return TSD_OK;
     349              :     }
     350            4 :     TSD_CHECK_NULLPTR(
     351              :         commAgent_.GetDeviceComm(), TSD_INSTANCE_NOT_INITIALED,
     352              :         "[TsdClient] devCommClient_ is null in update profiling function");
     353            4 :     TSD_StatusT ret = SendUpdateProfilingMsg(flag);
     354            4 :     TSD_CHECK(ret == TSD_OK, ret, "Send profiling message to tsdaemon failed.");
     355              : 
     356            3 :     TSD_RUN_INFO(
     357              :         "[TsdClient][deviceId=%u] [sessionId=%u] wait update profiling msg response", sharedCtx_.logicDeviceId,
     358              :         commAgent_.GetSessionId());
     359              : 
     360            3 :     ret = WaitRsp(0U);
     361            3 :     TSD_CHECK(ret == TSD_OK, ret, "Wait UpdateProfiling response from device failed.");
     362            2 :     return TSD_OK;
     363              : }
     364              : 
     365            3 : TSD_StatusT TsdProcessController::SendUpdateProfilingMsg(const uint32_t flag)
     366              : {
     367            3 :     HDCMessage msg;
     368            3 :     MessageContext ctx = BuildBaseMessageContext();
     369            3 :     ctx.profilingMode = flag;
     370            3 :     if (HdcMessageBuilder::BuildUpdateProfiling(msg, ctx) != TSD_OK) {
     371            0 :         return TSD_INTERNAL_ERROR;
     372              :     }
     373            3 :     const TSD_StatusT ret = commAgent_.SendMsg(msg);
     374            3 :     if (ret != TSD_OK) {
     375            1 :         TSD_ERROR("[TsdClient][deviceId=%u] tsdclient update profiling mode failed", sharedCtx_.logicDeviceId);
     376            1 :         return TSD_CLT_UPDATE_PROFILING_FAILED;
     377              :     }
     378              : 
     379            2 :     return TSD_OK;
     380            3 : }
     381              : 
     382            4 : TSD_StatusT TsdProcessController::GetHdcConctStatus(int32_t& hdcSessStat)
     383              : {
     384            4 :     return commAgent_.GetHdcConctStatus(hdcSessStat, sharedCtx_.isAdcEnv);
     385              : }
     386              : 
     387            3 : TSD_StatusT TsdProcessController::InitQs(const InitFlowGwInfo* const initInfo)
     388              : {
     389            3 :     TSD_RUN_INFO(
     390              :         "[ProcessModeManager][logicDeviceId_=%u] begin to prepare send open message", sharedCtx_.logicDeviceId);
     391            3 :     if (initInfo == nullptr) {
     392            0 :         TSD_ERROR("TsdInitQs failed, initInfo is nullptr.");
     393            0 :         return tsd::TSD_INTERNAL_ERROR;
     394              :     }
     395              : 
     396            3 :     const char_t* const groupName = initInfo->groupName;
     397            3 :     if (tsdStartStatus_.startQs_) {
     398            1 :         TSD_INFO("[ProcessModeManager] QS has opened already, no need open again");
     399            1 :         return TSD_OK;
     400              :     }
     401            2 :     TSD_StatusT ret = InitTsdClient();
     402            2 :     TSD_CHECK(ret == TSD_OK, ret, "Init hdc client failed.");
     403              : 
     404            2 :     if (groupName != nullptr) {
     405            2 :         TSD_INFO("[TsdClient]QS open with group[%s]", groupName);
     406            2 :         qsInitGrpName_ = groupName;
     407              :     } else {
     408            0 :         TSD_INFO("[TsdClient]QS open with empty groupName");
     409            0 :         qsInitGrpName_.clear();
     410              :     }
     411            2 :     schedPolicy_ = initInfo->schedPolicy;
     412              : 
     413            2 :     constexpr TsdStartStatusInfo startInfo = {false, false, true};
     414            2 :     ret = SendOpenMsg(DEFAULT_QS_RANKSIZE, startInfo);
     415            2 :     TSD_CHECK(ret == TSD_OK, ret, "Send InitQs message to device failed.");
     416              : 
     417            2 :     ret = WaitRsp(0U);
     418            2 :     TSD_CHECK(ret == TSD_OK, ret, "Wait InitQs response from device failed.");
     419              : 
     420            2 :     tsdStartStatus_.startQs_ = true;
     421            2 :     TSD_RUN_INFO(
     422              :         "[TsdClient][logicDeviceId_=%u] [sessionId=%u] start QS process success", sharedCtx_.logicDeviceId,
     423              :         commAgent_.GetSessionId());
     424            2 :     return TSD_OK;
     425              : }
     426              : 
     427          399 : void TsdProcessController::SetTsdStartInfo(const bool cpStatus, const bool hccpStatus, const bool qsStatus)
     428              : {
     429          399 :     tsdStartStatus_.startCp_ = cpStatus;
     430          399 :     tsdStartStatus_.startHccp_ = hccpStatus;
     431          399 :     tsdStartStatus_.startQs_ = qsStatus;
     432          399 :     capabilityMgr_.SetStartCpStatus(cpStatus);
     433          399 : }
     434              : 
     435           22 : bool TsdProcessController::CheckNeedToOpen(const uint32_t rankSize, TsdStartStatusInfo& startInfo)
     436              : {
     437           22 :     constexpr uint32_t HCCP_START_RANK_SIZE = 1U;
     438           22 :     if (rankSize <= HCCP_START_RANK_SIZE) {
     439           14 :         if (tsdStartStatus_.startCp_) {
     440            2 :             TSD_INFO("[TsdClient] cp has already opened, no need open again");
     441            2 :             return false;
     442              :         } else {
     443           12 :             startInfo.startHccp_ = false;
     444           12 :             startInfo.startCp_ = true;
     445              :         }
     446              :     } else {
     447            8 :         if ((tsdStartStatus_.startCp_) && (tsdStartStatus_.startHccp_)) {
     448            1 :             TSD_INFO("[TsdClient] hccp and cp has already opened, no need open again");
     449            1 :             return false;
     450              :         }
     451            7 :         startInfo.startCp_ = true;
     452            7 :         startInfo.startHccp_ = true;
     453              :     }
     454           19 :     TSD_INFO(
     455              :         "[TsdClient] get open para startCp[%u] startHccp[%u] (0:false 1:true)",
     456              :         static_cast<uint32_t>(startInfo.startCp_), static_cast<uint32_t>(startInfo.startHccp_));
     457           19 :     return true;
     458              : }
     459              : 
     460           18 : void TsdProcessController::ParseTsdCloseFlag(const uint32_t flag, TsdCloseFlag& tsdCloseFlag) const
     461              : {
     462           18 :     TSD_RUN_INFO("Parse tsd close flag [%u]", flag);
     463           18 :     tsdCloseFlag.quickCloseFlag = static_cast<uint32_t>(TSD_BITMAP_GET(flag, 0U));
     464           18 :     return;
     465              : }
     466              : 
     467           16 : TSD_StatusT TsdProcessController::ProcessQueueForAdc()
     468              : {
     469           16 :     if ((sharedCtx_.platInfoMode != static_cast<uint32_t>(ModeType::OFFLINE)) || !sharedCtx_.isAdcEnv) {
     470           15 :         TSD_RUN_INFO(
     471              :             "[TsdClient] it is unnecessary for current mode[%u] to grant queue auth to aicpusd",
     472              :             static_cast<uint32_t>(ModeType::OFFLINE));
     473           15 :         return TSD_OK;
     474              :     }
     475            1 :     const auto ret = SyncQueueAuthority();
     476            1 :     if (ret != TSD_OK) {
     477            1 :         constexpr uint32_t flag = 0;
     478            1 :         const auto closeRet = Close(flag);
     479            1 :         if (closeRet != TSD_OK) {
     480            0 :             TSD_RUN_WARN("[TsdClient]Sync queue authority failed, call close failed!");
     481            0 :             return closeRet;
     482              :         }
     483            1 :         TSD_RUN_WARN("[TsdClient]Sync queue authority failed, call close success!");
     484            1 :         return ret;
     485              :     }
     486            0 :     return TSD_OK;
     487              : }
     488              : 
     489            7 : TSD_StatusT TsdProcessController::ProcessQueueGrant(
     490              :     const QueueQueryOutputPara& queueInfoOutBuff, const QueueQueryOutput* const queueInfoList,
     491              :     const pid_t aicpuPid) const
     492              : {
     493            7 :     const uint32_t outLen = queueInfoOutBuff.outLen;
     494            7 :     if (outLen == 0U) {
     495            0 :         TSD_RUN_INFO("[TsdClient] Current process does not create queue yet. need not to sync");
     496            0 :         return TSD_OK;
     497              :     }
     498            7 :     if ((outLen % sizeof(queueInfoList->queQueryQuesOfProcInfo[0U])) != 0U) {
     499            2 :         TSD_ERROR("[TsdClient] QueueInfo outbuff size[%d] is invalid", outLen);
     500            2 :         return TSD_INTERNAL_ERROR;
     501              :     }
     502            5 :     const uint32_t queueNum = static_cast<uint32_t>(outLen / sizeof(queueInfoList->queQueryQuesOfProcInfo[0U]));
     503            5 :     TSD_RUN_INFO("[TsdClient] ProcessQueueGrant queueNum[%u]", queueNum);
     504         8199 :     for (uint32_t i = 0U; i < queueNum; ++i) {
     505         8197 :         const uint32_t queueId = static_cast<uint32_t>(queueInfoList->queQueryQuesOfProcInfo[i].qid);
     506         8197 :         if (queueId >= MAX_QUEUE_ID_NUM) {
     507            2 :             TSD_ERROR("Get invalid queueid[%d]", queueId);
     508            2 :             return TSD_INTERNAL_ERROR;
     509              :         }
     510         8195 :         if (!static_cast<bool>(queueInfoList->queQueryQuesOfProcInfo[i].attr.manage)) {
     511         8193 :             continue;
     512              :         }
     513            4 :         const auto drvRet = halQueueGrant(
     514            2 :             sharedCtx_.logicDeviceId, static_cast<int32_t>(queueId), aicpuPid,
     515              :             queueInfoList->queQueryQuesOfProcInfo[i].attr);
     516            2 :         if ((drvRet != DRV_ERROR_NONE) && (drvRet != DRV_ERROR_NOT_SUPPORT)) {
     517            1 :             TSD_ERROR("[TsdClient]Grant qid[%u] failed, ret[%d]", queueId, drvRet);
     518            1 :             return TSD_INTERNAL_ERROR;
     519              :         }
     520              :     }
     521            2 :     TSD_RUN_INFO("[TsdClient] ProcessQueueGrant process success");
     522            2 :     return TSD_OK;
     523              : }
     524              : 
     525           10 : TSD_StatusT TsdProcessController::SyncQueueAuthority() const
     526              : {
     527           10 :     TSD_RUN_INFO("[TsdClient] Current type is ADC. Sync queue auth start.");
     528           10 :     auto drvRet = halQueueInit(sharedCtx_.logicDeviceId);
     529           10 :     if ((drvRet != DRV_ERROR_NONE) && (drvRet != DRV_ERROR_REPEATED_INIT) && (drvRet != DRV_ERROR_NOT_SUPPORT)) {
     530            0 :         TSD_ERROR("[TsdClient] halQueueInit error, drvRet[%d]", drvRet);
     531            0 :         return TSD_INTERNAL_ERROR;
     532              :     }
     533           10 :     TSD_RUN_INFO("[TsdClient]Interface halQueueInit success.");
     534           10 :     pid_t aicpuPid = -1;
     535           10 :     const auto ret = GetAicpusdPid(aicpuPid);
     536           10 :     if ((aicpuPid == -1) || (ret != TSD_OK)) {
     537            2 :         return ret;
     538              :     }
     539            8 :     pid_t srcPid = drvDeviceGetBareTgid();
     540            8 :     std::unique_ptr<QueueQueryOutput> queueInfoListPtr(new (std::nothrow) QueueQueryOutput());
     541            8 :     if (queueInfoListPtr == nullptr) {
     542            0 :         TSD_ERROR("[TsdClient] fail to alloc queueInfoListPtr");
     543            0 :         return TSD_INTERNAL_ERROR;
     544              :     }
     545            8 :     QueueQueryOutput* queueInfoList = queueInfoListPtr.get();
     546            8 :     if (queueInfoList == nullptr) {
     547            0 :         TSD_ERROR("[TsdClient] queueInfoList is null");
     548            0 :         return TSD_INTERNAL_ERROR;
     549              :     }
     550            8 :     QueueQueryOutputPara queueInfoOutBuff = {
     551            8 :         PtrToPtr<QueueQueryOutput, void>(queueInfoList), static_cast<uint32_t>(sizeof(QueueQueryOutput))};
     552            8 :     QueueQueryInputPara queueInput = {PtrToPtr<pid_t, void>(&srcPid), static_cast<uint32_t>(sizeof(srcPid))};
     553            8 :     drvRet = halQueueQuery(sharedCtx_.logicDeviceId, QUEUE_QUERY_QUES_OF_CUR_PROC, &queueInput, &queueInfoOutBuff);
     554            8 :     if (drvRet == DRV_ERROR_NOT_SUPPORT) {
     555            0 :         TSD_RUN_INFO("[TsdClient] halQueueQuery is not supported in current chip type.");
     556            0 :         return TSD_OK;
     557              :     }
     558            8 :     if (drvRet != DRV_ERROR_NONE) {
     559            1 :         TSD_ERROR("[TsdClient] halQueueQuery execute failed, ret[%d]", drvRet);
     560            1 :         return TSD_INTERNAL_ERROR;
     561              :     }
     562              : 
     563            7 :     return ProcessQueueGrant(queueInfoOutBuff, queueInfoList, aicpuPid);
     564            8 : }
     565              : 
     566           10 : TSD_StatusT TsdProcessController::GetAicpusdPid(pid_t& aicpusdPid) const
     567              : {
     568           10 :     aicpusdPid = -1;
     569           10 :     const pid_t srcPid = drvDeviceGetBareTgid();
     570           10 :     if (&halQueryDevpid == nullptr) {
     571            0 :         TSD_RUN_INFO("[TsdClient] halQueryDevpid is nullptr, interface is not supported in current version.");
     572            0 :         return TSD_OK;
     573              :     }
     574           10 :     halQueryDevpidInfo para = {};
     575           10 :     para.hostpid = srcPid;
     576           10 :     para.proc_type = DEVDRV_PROCESS_CP1;
     577           10 :     para.vfid = 0U;
     578           10 :     para.devid = sharedCtx_.logicDeviceId;
     579           10 :     const auto drvRet = halQueryDevpid(para, &aicpusdPid);
     580           10 :     if (drvRet == DRV_ERROR_NOT_SUPPORT) {
     581            1 :         TSD_RUN_INFO("[TsdClient] halQueryDevpid is not supported in current chip type.");
     582            1 :         return TSD_OK;
     583              :     }
     584            9 :     if ((drvRet != DRV_ERROR_NONE) || (aicpusdPid == -1)) {
     585            1 :         TSD_ERROR("[TsdClient]Get aicpusd pid failed, result[%d]", drvRet);
     586            1 :         return TSD_INTERNAL_ERROR;
     587              :     }
     588            8 :     TSD_RUN_INFO(
     589              :         "Get aicpusd[%d] from host[%d] success.", static_cast<int32_t>(srcPid), static_cast<int32_t>(aicpusdPid));
     590            8 :     return TSD_OK;
     591              : }
     592              : 
     593              : } // namespace tsd
        

Generated by: LCOV version 2.0-1