LCOV - code coverage report
Current view: top level - aicpu_cust_schedule/core - aicpusd_interface_process.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 95.0 % 279 265
Test Date: 2026-08-12 11:05:02 Functions: 95.0 % 20 19

            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 <sys/file.h>
      11              : #include <sys/ioctl.h>
      12              : #include "aicpusd_interface_process.h"
      13              : #include <unistd.h>
      14              : #include "ascend_hal.h"
      15              : #include "status.h"
      16              : #include "aicpusd_event_manager.h"
      17              : #include "aicpusd_event_process.h"
      18              : #include "aicpusd_status.h"
      19              : #include "aicpusd_drv_manager.h"
      20              : #include "aicpusd_threads_process.h"
      21              : #include "aicpusd_common.h"
      22              : #include "aicpusd_monitor.h"
      23              : #include "aicpusd_hal_interface_ref.h"
      24              : #include "aicpu_cust_sd_dump_process.h"
      25              : #include "core/aicpusd_op_executor.h"
      26              : #include "core/aicpusd_resource_limit.h"
      27              : #include "stackcore_interface_weak.h"
      28              : #include "aicpu_cust_sd_mc2_maintenance_thread_api.h"
      29              : 
      30              : namespace AicpuSchedule {
      31              : namespace {
      32           20 : void RegOpenCustSoCallBack()
      33              : {
      34           20 :     SubProcEventCallBackInfo openCustSoInfo = {};
      35           20 :     openCustSoInfo.callBackFunc = CustomOpExecutor::OpenKernelSoByAicpuEvent;
      36           20 :     openCustSoInfo.eventType = AICPU_SUB_EVENT_OPEN_CUSTOM_SO;
      37           20 :     const int32_t ret = RegEventMsgCallBackFunc(&openCustSoInfo);
      38           20 :     if (ret != 0) {
      39            1 :         aicpusd_run_warn(
      40              :             "Aicpu-sd-cust main thread reg open custom so callback function failed. eventid[%d]",
      41              :             static_cast<int32_t>(openCustSoInfo.eventType));
      42            1 :         return;
      43              :     }
      44              : }
      45              : 
      46           20 : void RegCloseMonitorCallBack()
      47              : {
      48           20 :     SubProcEventCallBackInfo closeMonitorInfo = {};
      49           20 :     closeMonitorInfo.callBackFunc = AicpuEventProcess::AICPUEventCustCloseMonitor;
      50           20 :     closeMonitorInfo.eventType = AICPU_SUB_EVENT_CUST_CLOSE_MONITOR;
      51           20 :     const int32_t ret = RegEventMsgCallBackFunc(&closeMonitorInfo);
      52           20 :     if (ret != 0) {
      53            1 :         aicpusd_run_warn("Register close monitor callback failed. eventId[%u]", closeMonitorInfo.eventType);
      54              :     }
      55           20 : }
      56              : } // namespace
      57              : constexpr int32_t SUCCESS_VALUE = 0;
      58              : constexpr int32_t ATTACH_TIME_OUT = 20000; // attach fail after 20s blocking attach
      59              : /**
      60              :  * @ingroup AicpuScheduleCore
      61              :  * @brief it is used to construct a object of AicpuScheduleCore.
      62              :  */
      63            1 : AicpuScheduleInterface::AicpuScheduleInterface() : noThreadFlag_(true), initFlag_(false), aicpuSdPid_(-1), tsdPid_(-1)
      64            1 : {}
      65              : 
      66              : /**
      67              :  * @ingroup AicpuScheduleCore
      68              :  * @brief it is used to destructor a object of AicpuScheduleCore.
      69              :  */
      70            1 : AicpuScheduleInterface::~AicpuScheduleInterface() {}
      71              : 
      72          132 : AicpuScheduleInterface& AicpuScheduleInterface::GetInstance()
      73              : {
      74          132 :     static AicpuScheduleInterface instance;
      75          132 :     return instance;
      76              : }
      77              : 
      78            4 : void SetCustAicpuMainThreadContext(const uint32_t deviceId, const pid_t hostPid, const uint32_t vfId)
      79              : {
      80            4 :     aicpu::aicpuContext_t context = {};
      81            4 :     context.tsId = 0U;
      82            4 :     context.deviceId = deviceId;
      83            4 :     context.hostPid = hostPid;
      84            4 :     context.vfId = vfId;
      85            4 :     aicpu::SetUniqueVfId(AicpuDrvManager::GetInstance().GetUniqueVfId());
      86            4 :     (void)aicpu::aicpuSetContext(&context);
      87            4 : }
      88              : 
      89              : /**
      90              :  * @ingroup AicpuScheduleInterface
      91              :  * @brief it use to initialize aicpu schedule.
      92              :  * @param [in] deviceId
      93              :  * @param [in] hostPid : the process id of host process
      94              :  * @param [in] pidSign : the signature of pid ,it is used in drv.
      95              :  * @param [in] profilingMode
      96              :  * @param [in] aicpuPid : the process id of aicpu-sd process
      97              :  * @param [in] vfId : vf id
      98              :  * @param [in] isOnline : true: online  false offline
      99              :  * @return AICPU_SCHEDULE_OK: success, other: error code
     100              :  */
     101           10 : int32_t AicpuScheduleInterface::InitAICPUScheduler(
     102              :     const uint32_t deviceId, const pid_t hostPid, const std::string& pidSign, const uint32_t profilingMode,
     103              :     const pid_t aicpuPid, const uint32_t vfId, const bool isOnline)
     104              : {
     105              :     (void)pidSign;
     106           10 :     const std::unique_lock<std::mutex> initLock(mutexForInit_);
     107           10 :     if (initFlag_) {
     108            1 :         aicpusd_warn("Aicpu schedule is already init.");
     109            1 :         return AICPU_SCHEDULE_OK;
     110              :     }
     111              : 
     112            9 :     noThreadFlag_ = false;
     113            9 :     if (AicpuDrvManager::GetInstance().InitDrvMgr(deviceId, hostPid, vfId, true) != AICPU_SCHEDULE_OK) {
     114            2 :         aicpusd_err("Failed to init aicpu drv manager");
     115            2 :         return AICPU_SCHEDULE_ERROR_DRV_ERR;
     116              :     }
     117            7 :     aicpu::AicpuRunMode runModeCur = aicpu::AicpuRunMode::THREAD_MODE;
     118            7 :     if (GetCurrentRunMode(isOnline, runModeCur) != AICPU_SCHEDULE_OK) {
     119            1 :         aicpusd_err("Aicpu custom get run mode failed");
     120            1 :         return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     121              :     }
     122              : 
     123            6 :     const int32_t bindPidRet = AicpuSchedule::AicpuDrvManager::GetInstance().CheckBindHostPid();
     124            6 :     if (bindPidRet != AICPU_SCHEDULE_OK) {
     125            0 :         aicpusd_err("Check bind pid sign failed, ret[%d].", bindPidRet);
     126            0 :         return static_cast<int32_t>(ComputProcessRetCode::CP_RET_COMMON_ERROR);
     127              :     }
     128            6 :     aicpusd_info("Bind pid sign success.");
     129            6 :     const uint32_t groupIdCur = DEFAULT_GROUP_ID;
     130            6 :     int32_t retRes = AicpuDrvManager::GetInstance().InitDrvSchedModule(groupIdCur);
     131            6 :     if (retRes != static_cast<int32_t>(DRV_ERROR_NONE)) {
     132            2 :         aicpusd_err("Failed to init the drv schedule module, ret[%d].", retRes);
     133            2 :         return AICPU_SCHEDULE_ERROR_DRV_ERR;
     134              :     }
     135            4 :     SetCustAicpuMainThreadContext(deviceId, hostPid, vfId);
     136            4 :     retRes = AicpuSchedule::AicpuMonitor::GetInstance().InitAicpuMonitor(deviceId, isOnline);
     137            4 :     if (retRes != AICPU_SCHEDULE_OK) {
     138            1 :         aicpusd_err("aicpu monitor init failed, ret[%d]", retRes);
     139            1 :         return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     140              :     }
     141            3 :     retRes = ComputeProcess::GetInstance().Start(deviceId, hostPid, profilingMode, aicpuPid, vfId, runModeCur);
     142            3 :     if (retRes != static_cast<int32_t>(ComputProcessRetCode::CP_RET_SUCCESS)) {
     143            1 :         aicpusd_err("Compute process start failed, ret[%d].", retRes);
     144            1 :         AicpuSchedule::AicpuMonitor::GetInstance().Stop();
     145            1 :         ComputeProcess::GetInstance().Stop();
     146            1 :         return AICPU_SCHEDULE_ERROR_INIT_CP_FAILED;
     147              :     }
     148            2 :     aicpusd_info(
     149              :         "Successfully started compute process, deviceId[%u], hostPid[%d], mode[%d].", deviceId, hostPid,
     150              :         static_cast<int32_t>(isOnline));
     151            2 :     initFlag_ = true;
     152            2 :     return AICPU_SCHEDULE_OK;
     153           10 : }
     154              : 
     155              : /**
     156              :  * @ingroup AicpuScheduleInterface
     157              :  * @brief it use to stop AICPU scheduler.
     158              :  * @param [in] deviceId
     159              :  * @param [in] hostPid :the process id of host
     160              :  * @return AICPU_SCHEDULE_OK: success, other: error code
     161              :  */
     162           16 : int32_t AicpuScheduleInterface::StopAICPUScheduler(const uint32_t deviceId)
     163              : {
     164           16 :     aicpusd_info("Begin to stop aicpu custom scheduler.");
     165           16 :     const std::unique_lock<std::mutex> stopLock(mutexForInit_);
     166           16 :     if (!noThreadFlag_) {
     167            4 :         ComputeProcess::GetInstance().Stop();
     168              :     }
     169              : 
     170           16 :     noThreadFlag_ = true;
     171           16 :     initFlag_ = false;
     172              :     // GetInstance is not null, checked in InitAICPUScheduler
     173           16 :     AicpuSchedule::AicpuMonitor::GetInstance().Stop();
     174           16 :     AicpuSchedule::AicpuEventManager::GetInstance().SetRunningFlag(false);
     175           16 :     AicpuSchedule::AicpuCustDumpProcess::GetInstance().UnitDataDumpProcess();
     176           16 :     FeatureCtrl::ClearTsMsgVersionInfo();
     177           16 :     constexpr uint32_t sleepUsecs = 1000U; // sleep 1ms to avoid SchedWaitEvent error
     178           16 :     (void)usleep(sleepUsecs);
     179           16 :     if ((FeatureCtrl::ShouldInitDrvThread()) && (&halDrvEventThreadUninit != nullptr)) {
     180           16 :         const auto unInitRet = halDrvEventThreadUninit(deviceId);
     181           16 :         aicpusd_run_info("Uninit process drv queue msg on ccpu, ret is %d.", static_cast<int32_t>(unInitRet));
     182              :     }
     183           16 :     const auto ret = halEschedDettachDevice(deviceId);
     184           16 :     if (ret != DRV_ERROR_NONE) {
     185            2 :         aicpusd_err("Failed to detach device[%u], result[%d].", deviceId, static_cast<int32_t>(ret));
     186            2 :         return static_cast<int32_t>(ret);
     187              :     }
     188           14 :     aicpusd_info("Successfully stopped AICPU custom scheduler.");
     189           14 :     return AICPU_SCHEDULE_OK;
     190           16 : }
     191              : 
     192           10 : int32_t AicpuScheduleInterface::GetCurrentRunMode(const bool isOnline, aicpu::AicpuRunMode& runMode) const
     193              : {
     194           10 :     if (!isOnline) {
     195            1 :         runMode = aicpu::AicpuRunMode::THREAD_MODE;
     196            1 :         aicpusd_info("Current aicpu mode is offline (call by api).");
     197            1 :         return AICPU_SCHEDULE_OK;
     198              :     }
     199              : 
     200              :     drvHdcCapacity capacity;
     201            9 :     capacity.chanType = HDC_CHAN_TYPE_SOCKET;
     202            9 :     capacity.maxSegment = 0U;
     203            9 :     const hdcError_t hdcRet = drvHdcGetCapacity(&capacity);
     204            9 :     if (hdcRet != DRV_ERROR_NONE) {
     205            1 :         aicpusd_err("Aicpu cust scheduler get capacity failed, ret[%d].", static_cast<int32_t>(hdcRet));
     206            1 :         return AICPU_SCHEDULE_ERROR_DRV_ERR;
     207              :     }
     208              : 
     209              :     // online: with host, offline: without host only device
     210            8 :     if (capacity.chanType == HDC_CHAN_TYPE_SOCKET) {
     211            7 :         runMode = aicpu::AicpuRunMode::PROCESS_SOCKET_MODE;
     212            7 :         aicpusd_info("Set aicpu online mode false. current mode is socket_mode.");
     213            7 :         return AICPU_SCHEDULE_OK;
     214              :     }
     215              : 
     216            1 :     runMode = aicpu::AicpuRunMode::PROCESS_PCIE_MODE;
     217            1 :     aicpusd_info("Current aicpu mode is online, called from host.");
     218            1 :     return AICPU_SCHEDULE_OK;
     219              : }
     220              : 
     221           20 : void AicpuScheduleInterface::SetAicpuSdProcId(const pid_t aicpuSdPid)
     222              : {
     223           20 :     aicpuSdPid_ = aicpuSdPid;
     224           20 :     aicpusd_info("Set aicpusd pid:%u", static_cast<uint32_t>(aicpuSdPid_));
     225           20 : }
     226              : 
     227            4 : pid_t AicpuScheduleInterface::GetAicpuSdProcId() const { return aicpuSdPid_; }
     228              : 
     229           20 : void AicpuScheduleInterface::SetTsdProcId(const pid_t tsdPid)
     230              : {
     231           20 :     tsdPid_ = tsdPid;
     232           20 :     aicpusd_info("Set tsd pid:%u", static_cast<uint32_t>(tsdPid_));
     233           20 : }
     234              : 
     235            0 : pid_t AicpuScheduleInterface::GetTsdProcId() const { return tsdPid_; }
     236              : 
     237           22 : void AicpuScheduleInterface::SetLogLevel(const AicpuSchedule::ArgsParser& startParams) const
     238              : {
     239           22 :     if (&dlog_setlevel != nullptr) {
     240           22 :         if (dlog_setlevel(-1, startParams.GetLogLevel(), startParams.GetEventLevel()) != AicpuSchedule::SUCCESS_VALUE) {
     241            0 :             aicpusd_warn("Set log level failed");
     242              :         }
     243           22 :         if ((startParams.GetCcecpuLogLevel() >= DEBUG_LOG) && (startParams.GetCcecpuLogLevel() <= ERROR_LOG)) {
     244            0 :             if (dlog_setlevel(CCECPU, startParams.GetCcecpuLogLevel(), startParams.GetEventLevel()) != SUCCESS_VALUE) {
     245            0 :                 aicpusd_warn("Set ccecpu log level failed");
     246              :             }
     247              :         }
     248           22 :         if ((startParams.GetAicpuLogLevel() >= DEBUG_LOG) && (startParams.GetAicpuLogLevel() <= ERROR_LOG)) {
     249            0 :             if (dlog_setlevel(AICPU, startParams.GetAicpuLogLevel(), startParams.GetEventLevel()) != SUCCESS_VALUE) {
     250            0 :                 aicpusd_warn("Set aicpu log failed");
     251              :             }
     252              :         }
     253              :     }
     254           22 :     if (&DlogSetAttr != nullptr) {
     255           22 :         LogAttr logAttr = {};
     256           22 :         logAttr.type = APPLICATION;
     257           22 :         logAttr.pid = static_cast<uint32_t>(startParams.GetHostPid());
     258           22 :         logAttr.deviceId = startParams.GetDeviceId();
     259           22 :         if (DlogSetAttr(logAttr) != AicpuSchedule::SUCCESS_VALUE) {
     260            0 :             aicpusd_warn("Set log attr failed");
     261              :         }
     262              :     }
     263           22 : }
     264           22 : void AicpuScheduleInterface::SetTsdEventDstPidByArgs(const AicpuSchedule::ArgsParser& startParams) const
     265              : {
     266           22 :     if ((!startParams.HasTsdPid()) || (startParams.GetTsdPid() == 0U)) {
     267           14 :         aicpusd_run_info("hastsdpid:%d, tsdpid:%u", startParams.HasTsdPid(), startParams.GetTsdPid());
     268           14 :         return;
     269              :     }
     270            8 :     SetDstTsdEventPid(startParams.GetTsdPid());
     271            8 :     aicpusd_run_info("cust finish set tsdpid:%u", startParams.GetTsdPid());
     272              : }
     273           22 : void AicpuScheduleInterface::SetCustAicpuMainThreadAffinity(const AicpuSchedule::ArgsParser& startParams) const
     274              : {
     275           22 :     if (!startParams.HasControlCpuList()) {
     276           18 :         return;
     277              :     }
     278            8 :     pthread_t threadId = pthread_self();
     279            8 :     std::vector<uint32_t> cpuIds = startParams.GetControlCpuList();
     280              :     cpu_set_t cpuset;
     281            8 :     CPU_ZERO(&cpuset);
     282           24 :     for (const auto cpuId : cpuIds) {
     283           16 :         CPU_SET(cpuId, &cpuset);
     284           16 :         aicpusd_info("prepare bind threadId=%lu to cpuId=%u", threadId, static_cast<uint32_t>(cpuId));
     285              :     }
     286            8 :     aicpusd_run_info("begin call pthread_setaffinity_np threadId=%lu", threadId);
     287              :     // 设置线程亲和性
     288            8 :     int32_t ret = pthread_setaffinity_np(threadId, sizeof(cpu_set_t), &cpuset);
     289            8 :     if (ret != 0) {
     290            2 :         aicpusd_err("BindCpu threadId=%lu set affinity failed, ret=%d", threadId, ret);
     291            2 :         return;
     292              :     }
     293              :     // 检查线程实际亲和性
     294            6 :     ret = pthread_getaffinity_np(threadId, sizeof(cpu_set_t), &cpuset);
     295            6 :     if (ret != 0) {
     296            2 :         aicpusd_err("BindCpu threadId=%lu get affinity failed, ret=%d", threadId, ret);
     297            2 :         return;
     298              :     }
     299         4100 :     for (int32_t cpuId = 0; cpuId < CPU_SETSIZE; cpuId++) {
     300         4096 :         if (CPU_ISSET(cpuId, &cpuset) != 0) {
     301            8 :             aicpusd_info("BindCpu threadId=%lu to cpuId=%d success", threadId, static_cast<int32_t>(cpuId));
     302              :         }
     303              :     }
     304            8 : }
     305              : 
     306           15 : bool AicpuScheduleInterface::AttachHostGroup(
     307              :     const std::vector<std::string>& groupNameVec, const uint32_t grpNameNum) const
     308              : {
     309           15 :     if (grpNameNum == 0U) {
     310            1 :         aicpusd_info("There is not group need to be attached");
     311            1 :         return true;
     312              :     }
     313           14 :     if (groupNameVec.empty()) {
     314            1 :         aicpusd_err("Aicpu start failed. Wrong parameters of groupNum[%d] with empty group name", grpNameNum);
     315            1 :         return false;
     316              :     }
     317              : 
     318           13 :     if (static_cast<uint32_t>(groupNameVec.size()) != grpNameNum) {
     319            1 :         aicpusd_err(
     320              :             "Aicpu start failed. parse group name num[%u] is consistence with num[%u] in parameter",
     321              :             groupNameVec.size(), grpNameNum);
     322            1 :         return false;
     323              :     }
     324           32 :     for (size_t i = 0UL; i < groupNameVec.size(); ++i) {
     325           21 :         aicpusd_info("halGrpAttach group[%s].", groupNameVec[i].c_str());
     326           21 :         const auto drvRet = halGrpAttach(groupNameVec[i].c_str(), ATTACH_TIME_OUT);
     327           21 :         if (drvRet != DRV_ERROR_NONE) {
     328            1 :             aicpusd_err("halGrpAttach group[%s] failed. ret[%d]", groupNameVec[i].c_str(), drvRet);
     329            1 :             return false;
     330              :         }
     331           20 :         aicpusd_run_info("halGrpAttach group[%s] success", groupNameVec[i].c_str());
     332              :     }
     333           11 :     BuffCfg buffCfg = {};
     334           11 :     const auto initDrvRet = halBuffInit(&buffCfg);
     335           11 :     if ((initDrvRet != DRV_ERROR_NONE) && (initDrvRet != DRV_ERROR_REPEATED_INIT)) {
     336            1 :         aicpusd_err("halBuffInit execute failed. ret[%d]", initDrvRet);
     337            1 :         return false;
     338              :     }
     339           10 :     return true;
     340              : }
     341              : 
     342           26 : bool AicpuScheduleInterface::SendVfMsgToDrv(const uint32_t cmd, const uint32_t deviceId, const uint32_t vfId) const
     343              : {
     344           26 :     if (FeatureCtrl::IsVfMode(deviceId, vfId)) {
     345            8 :         const int32_t fd = open("/dev/qos", O_RDWR);
     346            8 :         if (fd < 0) {
     347            6 :             aicpusd_warn("no such file. error[%s],errorno[%d]", strerror(errno), errno);
     348            8 :             return false;
     349              :         }
     350              :         VfMsgInfo vfMsg;
     351            2 :         vfMsg.deviceId = deviceId;
     352            2 :         vfMsg.vfId = vfId;
     353            2 :         const auto ret = ioctl(fd, cmd, PtrToPtr<VfMsgInfo, void>(&vfMsg));
     354            2 :         if (ret != 0) {
     355            2 :             aicpusd_warn("ioctl failed, error[%s],errorno[%d]", strerror(errno), errno);
     356            2 :             (void)close(fd);
     357            2 :             return false;
     358              :         }
     359            0 :         (void)close(fd);
     360              :     }
     361           18 :     return true;
     362              : }
     363              : 
     364           20 : void RegCreateCustMc2MaintenanceThreadCallBack()
     365              : {
     366           20 :     SubProcEventCallBackInfo createCustMc2MaintenanceThreadInfo = {};
     367           20 :     createCustMc2MaintenanceThreadInfo.callBackFunc = CreateCustMc2MaintenanceThread;
     368           20 :     createCustMc2MaintenanceThreadInfo.eventType = TSD_EVENT_START_MC2_THREAD;
     369           20 :     int32_t ret = RegEventMsgCallBackFunc(&createCustMc2MaintenanceThreadInfo);
     370           20 :     if (ret != 0) {
     371            1 :         aicpusd_err("Reg create cust mc2 maintenance thread failed");
     372            1 :         return;
     373              :     }
     374              : }
     375              : 
     376           37 : int32_t AicpuScheduleInterface::CustAicpuMainProcess(int32_t argc, char_t* argv[]) const
     377              : {
     378              :     try {
     379           37 :         AicpuSchedule::ArgsParser startParams;
     380           37 :         if (!startParams.ParseArgs(argc, argv)) {
     381           15 :             return -1;
     382              :         }
     383           22 :         SetLogLevel(startParams);
     384           22 :         SetTsdEventDstPidByArgs(startParams);
     385           22 :         SetCustAicpuMainThreadAffinity(startParams);
     386           22 :         aicpusd_run_info("Start parameter. %s", startParams.GetParaParsedStr().c_str());
     387           22 :         if (&StackcoreSetSubdirectory != nullptr) {
     388           22 :             (void)StackcoreSetSubdirectory("udf");
     389              :         }
     390           22 :         const pid_t aicpuPid = static_cast<pid_t>(startParams.GetAicpuPid());
     391           22 :         const uint32_t vfId = startParams.GetVfId();
     392           22 :         const uint32_t deviceId = startParams.GetDeviceId();
     393           22 :         const pid_t hostPid = static_cast<pid_t>(startParams.GetHostPid());
     394           22 :         const uint32_t mode = startParams.GetProfilingMode();
     395           22 :         if (!startParams.HasTsdPid()) {
     396           14 :             if (!aicpu::AddToCgroup(deviceId, vfId)) {
     397            1 :                 return -1;
     398              :             }
     399              :         }
     400              : 
     401              :         // Init custom op executor
     402           21 :         AicpuSchedule::CustomOpExecutor::GetInstance().InitOpExecutor(hostPid, startParams.GetCustSoPath());
     403           21 :         AicpuSchedule::AicpuEventManager::GetInstance().InitEventManager(false, true);
     404           42 :         int32_t ret = AicpuSchedule::AicpuScheduleInterface::GetInstance().InitAICPUScheduler(
     405           42 :             deviceId, hostPid, startParams.GetPidSign(), mode, aicpuPid, vfId, true);
     406           21 :         if (ret != AicpuSchedule::AICPU_SCHEDULE_OK) {
     407            1 :             aicpusd_err("Aicpu custom schedule start failed, ret[%d].", ret);
     408            1 :             (void)AicpuSchedule::AicpuScheduleInterface::GetInstance().StopAICPUScheduler(deviceId);
     409            1 :             return -1;
     410              :         }
     411           20 :         const pid_t tsdPid = startParams.HasTsdPid() ? startParams.GetTsdPid() : getppid();
     412           20 :         AicpuSchedule::AicpuScheduleInterface::GetInstance().SetAicpuSdProcId(aicpuPid);
     413           20 :         AicpuSchedule::AicpuScheduleInterface::GetInstance().SetTsdProcId(tsdPid);
     414           20 :         const char_t* const libPath = getenv("LD_LIBRARY_PATH");
     415           20 :         if (libPath == nullptr) {
     416            0 :             aicpusd_run_info("Aicpu schedule start success, but LD_LIBRARY_PATH is empty.");
     417              :         } else {
     418           20 :             aicpusd_run_info("Aicpu schedule start success, LD_LIBRARY_PATH=%s.", libPath);
     419              :         }
     420           20 :         AicpuSchedule::RegCreateCustMc2MaintenanceThreadCallBack();
     421           20 :         AicpuSchedule::RegOpenCustSoCallBack();
     422           20 :         AicpuSchedule::RegCloseMonitorCallBack();
     423              :         // response start message to tsd
     424           20 :         int32_t rspRet = static_cast<int32_t>(tsd::TSD_OK);
     425           20 :         if (startParams.GetGrpNameNum() > 0U) {
     426           19 :             rspRet = StartUpRspAndWaitProcess(deviceId, TSD_CUSTOM_COMPUTE, static_cast<uint32_t>(hostPid), vfId);
     427              :         } else {
     428            1 :             rspRet = StartupResponse(deviceId, TSD_CUSTOM_COMPUTE, static_cast<uint32_t>(hostPid), vfId);
     429              :         }
     430              : 
     431           20 :         if (rspRet != static_cast<int32_t>(tsd::TSD_OK)) {
     432            5 :             aicpusd_err("Cust aicpu startup response return not ok, error_code=%d", rspRet);
     433            5 :             return -1;
     434              :         }
     435           15 :         aicpusd_info("cust aicpu startup response return success");
     436              : 
     437           15 :         (void)SendVfMsgToDrv(VM_QOS_PROCESS_STARTUP, deviceId, vfId);
     438              :         // do group attach
     439           15 :         if (!AttachHostGroup(startParams.GetGrpNameList(), startParams.GetGrpNameNum())) {
     440            4 :             aicpusd_err("AttachHostGroup execute failed.");
     441            4 :             return -1;
     442              :         }
     443              : 
     444           11 :         if (startParams.GetGrpNameNum() > 0U) {
     445           10 :             aicpusd_run_info("Cust Aicpu schedule attach and init success.");
     446           10 :             AicpuSchedule::AicpuEventManager::GetInstance().SendCtrlCpuMsg(
     447              :                 aicpuPid, static_cast<uint32_t>(TsdSubEventType::TSD_EVENT_STOP_AICPU_PROCESS_WAIT), nullptr, 0U);
     448              :         }
     449              : 
     450              :         // wait for shutdown
     451           11 :         const int32_t waitRet = WaitForShutDown(deviceId);
     452           11 :         if (waitRet != static_cast<int32_t>(tsd::TSD_OK)) {
     453            1 :             aicpusd_err("wait for shut down return not ok, error code[%d].", waitRet);
     454            1 :             ret = -1;
     455              :         } else {
     456           10 :             aicpusd_info("wait for shut down success");
     457           10 :             ret = 0;
     458              :         }
     459           11 :         (void)AicpuSchedule::AicpuScheduleInterface::GetInstance().StopAICPUScheduler(deviceId);
     460           11 :         aicpusd_run_info("Aicpu custom schedule stopped.");
     461           11 :         (void)SendVfMsgToDrv(VM_QOS_PROCESS_SUSPEND, deviceId, vfId);
     462           11 :         if (&DlogFlush != nullptr) {
     463           11 :             DlogFlush();
     464              :         }
     465           11 :         return ret;
     466           37 :     } catch (const std::exception& e) {
     467            0 :         aicpusd_err("Execute main failed, reason=%s", e.what());
     468            0 :         return -1;
     469            0 :     }
     470              : }
     471              : } // namespace AicpuSchedule
        

Generated by: LCOV version 2.0-1