LCOV - code coverage report
Current view: top level - aicpu_schedule/core - aicpusd_drv_manager.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 92.5 % 389 360
Test Date: 2026-07-28 10:54:05 Functions: 100.0 % 25 25

            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 "aicpusd_drv_manager.h"
      11              : #include <sys/time.h>
      12              : #include <sstream>
      13              : #include "driver/ascend_hal.h"
      14              : #include "aicpusd_status.h"
      15              : #include "aicpusd_context.h"
      16              : #include "aicpusd_resource_manager.h"
      17              : #include "securec.h"
      18              : #include "type_def.h"
      19              : #include "aicpusd_hal_interface_ref.h"
      20              : #include "aicpusd_feature_ctrl.h"
      21              : 
      22              : #ifdef __cplusplus
      23              : extern "C" {
      24              : #endif
      25              : #ifdef __cplusplus
      26              : }
      27              : #endif
      28              : 
      29              : namespace {
      30              : // device num
      31              : constexpr uint32_t DEVICE_NUM = 64U;
      32              : // first array index
      33              : constexpr size_t FIRST_INDEX = 0LU;
      34              : // max number of eatch device can split
      35              : constexpr const uint32_t DEVICE_MAX_SPLIT_NUM = 16U;
      36              : // max number of eatch device cpu
      37              : constexpr const uint32_t DEVICE_MAX_CPU_NUM = 16U;
      38              : // 查询bind信息间隔
      39              : constexpr uint32_t QUERY_BIND_HOST_PID_INTERVBALE = 10000U;
      40              : // 查询bind超时时间
      41              : #ifndef aicpusd_UT
      42              : constexpr uint32_t QUERY_BIND_HOST_PID_TIME = 120000000U;
      43              : #else
      44              : constexpr uint32_t QUERY_BIND_HOST_PID_TIME = 100000U;
      45              : #endif
      46              : // bind结果的日志输出周期
      47              : constexpr uint32_t QUERY_BIND_HOST_PID_LOG_INTERVAL = 1000U;
      48              : }
      49              : 
      50              : namespace AicpuSchedule {
      51         2048 :     AicpuDrvManager &AicpuDrvManager::GetInstance()
      52              :     {
      53         2048 :         static AicpuDrvManager instance;
      54         2048 :         return instance;
      55              :     }
      56              : 
      57           59 :     int32_t AicpuDrvManager::GetNormalAicpuDCpuInfo(const std::vector<uint32_t> &deviceVec)
      58              :     {
      59           59 :         uint32_t deviceIdTmp = deviceVec[FIRST_INDEX];
      60              :         drvError_t ret;
      61           59 :         if (FeatureCtrl::IsVfModeDie1(deviceVec[FIRST_INDEX])) {
      62            2 :             int64_t pfDeviceId = 0;
      63            2 :             ret = halGetDeviceInfo(deviceVec[FIRST_INDEX], MODULE_TYPE_SYSTEM, INFO_TYPE_PHY_DIE_ID, &pfDeviceId);
      64            2 :             if (ret != DRV_ERROR_NONE) {
      65            1 :                 aicpusd_err("halGetDeviceInfo get pfDeviceId fail in device[%d], ret[%d]", deviceVec[FIRST_INDEX], ret);
      66            1 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
      67              :             }
      68            1 :             deviceIdTmp = static_cast<uint32_t>(pfDeviceId);
      69              :         }
      70           58 :         int64_t aicpuNum = 0;
      71           58 :         int64_t aicpuOsSched = 0;
      72           58 :         int64_t ccpuNum = 0;
      73           58 :         int64_t ccpuOsSched = 0;
      74           58 :         int64_t dcpuNum = 0;
      75           58 :         int64_t dcpuOsSched = 0;
      76           58 :         int64_t tsCpuNum = 0;
      77           58 :         int64_t tsCpuOsSched = 0;
      78           58 :         uint32_t retDrv = static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_AICPU,
      79           58 :             INFO_TYPE_CORE_NUM, &aicpuNum));
      80           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_AICPU,
      81              :             INFO_TYPE_OS_SCHED, &aicpuOsSched));
      82           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_CCPU,
      83              :             INFO_TYPE_CORE_NUM, &ccpuNum));
      84           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_CCPU,
      85              :             INFO_TYPE_OS_SCHED, &ccpuOsSched));
      86           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_DCPU,
      87              :             INFO_TYPE_CORE_NUM, &dcpuNum));
      88           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_DCPU,
      89              :             INFO_TYPE_OS_SCHED, &dcpuOsSched));
      90           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_TSCPU,
      91              :             INFO_TYPE_CORE_NUM, &tsCpuNum));
      92           58 :         retDrv |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceIdTmp, MODULE_TYPE_TSCPU,
      93              :             INFO_TYPE_OS_SCHED, &tsCpuOsSched));
      94           58 :         if (retDrv != 0U) {
      95            0 :             aicpusd_err("AicpuGetDeviceInfo failed, ret[%u]", retDrv);
      96            0 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
      97              :         }
      98              : 
      99           58 :         if (FeatureCtrl::IsAosCore()) {
     100            2 :             aicpuNumPerDev_ = aicpuNum;
     101            2 :             if (ccpuOsSched != 0) {
     102            1 :                 aicpuBaseId_ += ccpuNum;
     103              :             }
     104            2 :             if (dcpuOsSched != 0) {
     105            1 :                 aicpuBaseId_ += dcpuNum;
     106              :             }
     107              :         }
     108              :         // connot overflow
     109           58 :         coreNumPerDev_ = (ccpuNum * ccpuOsSched) + (dcpuNum * dcpuOsSched) + (aicpuNum * aicpuOsSched) +
     110              :                          (tsCpuNum * tsCpuOsSched);
     111           58 :         aicpusd_run_info("GetNormalAicpuDCpuInfo, deviceId[%u], aicpu_num[%u], aicpu_os_sched[%u], ccpu_num[%u], "
     112              :             "ccpu_os_sched[%u], dcpu_num[%u], dcpu_os_sched[%u], tscpu_num[%u], tscpu_os_sched[%u].",
     113              :             deviceIdTmp, aicpuNum, aicpuOsSched, ccpuNum, ccpuOsSched, dcpuNum, dcpuOsSched, tsCpuNum, tsCpuOsSched);
     114           58 :         dcpuBase_ = (deviceIdTmp * coreNumPerDev_) + ccpuNum;
     115           58 :         dcpuNum_ = dcpuNum;
     116           58 :         return AICPU_SCHEDULE_OK;
     117              :     }
     118          728 :     int32_t AicpuDrvManager::AicpuGetDeviceInfo(const uint32_t deviceId,
     119              :                                                 const DEV_MODULE_TYPE moduleType,
     120              :                                                 const DEV_INFO_TYPE infoType,
     121              :                                                 int64_t *value) const
     122              :     {
     123          728 :         if (value == nullptr) {
     124            0 :             aicpusd_err("halGetDeviceInfo value is null, deviceId[%u], DEV_MODULE_TYPE[%d], DEV_INFO_TYPE[%d]",
     125              :                         deviceId, static_cast<int32_t>(moduleType), static_cast<int32_t>(infoType));
     126            0 :             return -1;
     127              :         }
     128          728 :         const auto ret = halGetDeviceInfo(deviceId, static_cast<int32_t>(moduleType),
     129              :                                           static_cast<int32_t>(infoType), value);
     130          728 :         if (ret != DRV_ERROR_NONE) {
     131            4 :             aicpusd_err("halGetDeviceInfo failed, ret[%d], deviceId[%u] DEV_MODULE_TYPE[%d], DEV_INFO_TYPE[%d], "
     132              :                 "value[%lld]", ret, deviceId, static_cast<int32_t>(moduleType), static_cast<int32_t>(infoType), *value);
     133              :         }
     134          728 :         return static_cast<int32_t>(ret);
     135              :     }
     136              : 
     137           66 :     int32_t AicpuDrvManager::GetNormalAicpuInfo(const std::vector<uint32_t> &deviceVec)
     138              :     {
     139           66 :         int64_t aicpuNum = 0;
     140           66 :         int64_t aicpuBitMap = 0;
     141           66 :         int32_t ret = 0;
     142           66 :         if (FeatureCtrl::IsVfModeCheckedByDeviceId(deviceVec[0])) {
     143            4 :             ret = AicpuGetDeviceInfo(deviceVec[FIRST_INDEX], MODULE_TYPE_AICPU, INFO_TYPE_PF_CORE_NUM, &aicpuNum);
     144            4 :             if (ret != DRV_ERROR_NONE) {
     145            1 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     146              :             }
     147            3 :             ret = AicpuGetDeviceInfo(deviceVec[FIRST_INDEX], MODULE_TYPE_AICPU, INFO_TYPE_PF_OCCUPY, &aicpuBitMap);
     148            3 :             if (ret != DRV_ERROR_NONE) {
     149            0 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     150              :             }
     151              :         } else {
     152           62 :             ret = AicpuGetDeviceInfo(deviceVec[FIRST_INDEX], MODULE_TYPE_AICPU, INFO_TYPE_CORE_NUM, &aicpuNum);
     153           62 :             if (ret != DRV_ERROR_NONE) {
     154            1 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     155              :             }
     156           61 :             ret = AicpuGetDeviceInfo(deviceVec[FIRST_INDEX], MODULE_TYPE_AICPU, INFO_TYPE_OCCUPY, &aicpuBitMap);
     157           61 :             if (ret != DRV_ERROR_NONE) {
     158            0 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     159              :             }
     160              :         }
     161              : 
     162           64 :         aicpusd_run_info("aicpuBitMap[%lld], aicpuNum[%lld].", aicpuBitMap, aicpuNum);
     163           64 :         aicpuNumPerDev_ = static_cast<uint32_t>(aicpuNum);
     164           64 :         aicpuIdVec_.clear();
     165              : 
     166           64 :         DeployContext deployCtx = DeployContext::DEVICE;
     167           64 :         (void)GetAicpuDeployContext(deployCtx);
     168           64 :         if (deployCtx == DeployContext::HOST) {
     169           65 :             for (int64_t i = 0; i < aicpuNum; ++i) {
     170           64 :                 aicpuIdVec_.push_back(i);
     171              :             }
     172            1 :             return AICPU_SCHEDULE_OK;
     173              :         }
     174              : 
     175         1071 :         for (uint32_t i = 0U; i < DEVICE_MAX_CPU_NUM; i++) {
     176         1008 :             if ((aicpuBitMap & 0x1LL) != 0LL) {
     177          106 :                 aicpuIdVec_.push_back(i);
     178              :             }
     179         1008 :             aicpuBitMap = aicpuBitMap >> 1;
     180              :         }
     181           63 :         if (static_cast<uint32_t>(aicpuNum) != aicpuIdVec_.size()) {
     182            1 :             aicpusd_err("aicpunum bitmap error, aicpuNum[%lld],aicpuIdVecSize[%zu]", aicpuNum, aicpuIdVec_.size());
     183            1 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     184              :         }
     185           62 :         return AICPU_SCHEDULE_OK;
     186              :     }
     187              : 
     188           58 :     int32_t AicpuDrvManager::GetCcpuInfo(const std::vector<uint32_t> &deviceVec)
     189              :     {
     190           58 :         int64_t ccpuBitMap = 0;
     191           58 :         const int32_t ret = AicpuGetDeviceInfo(deviceVec[FIRST_INDEX], MODULE_TYPE_CCPU, INFO_TYPE_OCCUPY, &ccpuBitMap);
     192           58 :         if (ret != DRV_ERROR_NONE) {
     193            0 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     194              :         }
     195           58 :         aicpusd_run_info("ccpuBitMap[%lld].", ccpuBitMap);
     196          986 :         for (uint32_t i = 0U; i < DEVICE_MAX_CPU_NUM; i++) {
     197          928 :             if ((ccpuBitMap & 0x1LL) != 0LL) {
     198           57 :                 ccpuIdVec_.push_back(i);
     199              :             }
     200          928 :             ccpuBitMap = ccpuBitMap >> 1;
     201              :         }
     202           58 :         return AICPU_SCHEDULE_OK;
     203              :     }
     204              : 
     205            2 :     int32_t AicpuDrvManager::GetThreadVfAicpuDCpuInfo(const std::vector<uint32_t> &deviceVec)
     206              :     {
     207            2 :         const uint32_t deviceId = deviceVec[FIRST_INDEX];
     208            2 :         int64_t aicpuNum = 0L;
     209            2 :         uint32_t ret = static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_AICPU,
     210            2 :                                                                 INFO_TYPE_CORE_NUM, &aicpuNum));
     211            2 :         int64_t aicpuOsSched = 0;
     212            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_AICPU,
     213              :                                                         INFO_TYPE_OS_SCHED, &aicpuOsSched));
     214            2 :         int64_t ccpuNum = 0;
     215            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_CCPU,
     216              :                                                         INFO_TYPE_CORE_NUM, &ccpuNum));
     217            2 :         int64_t ccpuOsSched = 0;
     218            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_CCPU,
     219              :                                                         INFO_TYPE_OS_SCHED, &ccpuOsSched));
     220            2 :         int64_t dcpuNum = 0;
     221            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_DCPU,
     222              :                                                         INFO_TYPE_CORE_NUM, &dcpuNum));
     223            2 :         int64_t dcpuOsSched = 0;
     224            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_DCPU,
     225              :                                                         INFO_TYPE_OS_SCHED, &dcpuOsSched));
     226            2 :         int64_t tsCpuNum = 0;
     227            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_TSCPU,
     228              :                                                         INFO_TYPE_CORE_NUM, &tsCpuNum));
     229            2 :         int64_t tsCpuOsSched = 0;
     230            2 :         ret |= static_cast<uint32_t>(AicpuGetDeviceInfo(deviceId, MODULE_TYPE_TSCPU,
     231              :                                                         INFO_TYPE_OS_SCHED, &tsCpuOsSched));
     232            2 :         if (ret != 0U) {
     233            0 :             aicpusd_err("AicpuGetDeviceInfo failed, ret[%u]", ret);
     234            0 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     235              :         }
     236              : 
     237            2 :         if (FeatureCtrl::IsAosCore()) {
     238            1 :             aicpuNumPerDev_ = static_cast<uint32_t>(aicpuNum);
     239            1 :             if (ccpuOsSched != 0L) {
     240            1 :                 aicpuBaseId_ += static_cast<uint32_t>(ccpuNum);
     241              :             }
     242            1 :             if (dcpuOsSched != 0L) {
     243            1 :                 aicpuBaseId_ += static_cast<uint32_t>(dcpuNum);
     244              :             }
     245              :         }
     246              :         // connot overflow
     247            2 :         coreNumPerDev_ = (static_cast<uint32_t>(ccpuNum) * static_cast<uint32_t>(ccpuOsSched)) +
     248            2 :                          (static_cast<uint32_t>(dcpuNum) * static_cast<uint32_t>(dcpuOsSched)) +
     249            2 :                          (static_cast<uint32_t>(aicpuNum) * static_cast<uint32_t>(aicpuOsSched)) +
     250            2 :                          (static_cast<uint32_t>(tsCpuNum) * static_cast<uint32_t>(tsCpuOsSched));
     251            2 :         aicpusd_info("GetThreadVfAicpuDCpuInfo, deviceId[%u], aicpuNum[%llu], aicpuOsSched[%llu], ccpuNum[%llu], "
     252              :             "ccpuOsSched[%llu], dcpuNum[%llu], dcpuOsSched[%llu], tsCpuNum[%llu], tsCpuOsSched[%llu].",
     253              :             deviceId, aicpuNum, aicpuOsSched, ccpuNum, ccpuOsSched, dcpuNum, dcpuOsSched, tsCpuNum, tsCpuOsSched);
     254            2 :         dcpuBase_ = (deviceVec[FIRST_INDEX] * coreNumPerDev_) + static_cast<uint32_t>(ccpuNum);
     255            2 :         dcpuNum_ = static_cast<uint32_t>(dcpuNum);
     256            2 :         return AICPU_SCHEDULE_OK;
     257              :     }
     258              : 
     259           57 :     void AicpuDrvManager::InitDrvMgrCaluniqueVfId(const std::vector<uint32_t> &deviceVec, const uint32_t vfId)
     260              :     {
     261           57 :         if (FeatureCtrl::IsVfModeCheckedByDeviceId(deviceVec[0])) {
     262            1 :             uniqueVfId_ = deviceVec[0];
     263              :         } else {
     264           56 :             uniqueVfId_ = vfId;
     265           56 :             if (FeatureCtrl::IsAosCore()) {
     266            1 :                 return;
     267              :             }
     268           55 :             if ((!deviceVec.empty()) && (deviceVec[FIRST_INDEX] != 0U) && (vfId != 0U)) {
     269            2 :                 uint32_t maxNumSpDev = 0U;
     270            2 :                 const auto vfMaxRet = halGetDeviceVfMax(deviceVec[FIRST_INDEX], &maxNumSpDev);
     271            2 :                 if ((vfMaxRet != DRV_ERROR_NONE) || (maxNumSpDev > DEVICE_MAX_SPLIT_NUM)) {
     272            0 :                     aicpusd_err("Failed to get device cat vf number, result[%d], max num[%u].", vfMaxRet, maxNumSpDev);
     273            0 :                     return;
     274              :                 }
     275            2 :                 uniqueVfId_ = (maxNumSpDev * deviceVec[FIRST_INDEX]) + vfId;
     276              :             }
     277              :         }
     278           56 :         aicpusd_run_info("InitDrvMgr uniqueVfId=%u, deviceId=%u", uniqueVfId_, deviceVec[0]);
     279              :     }
     280              : 
     281           60 :     void AicpuDrvManager::InitSocType(const uint32_t deviceId)
     282              :     {
     283           60 :         DeployContext deployCtx = DeployContext::DEVICE;
     284           60 :         (void)GetAicpuDeployContext(deployCtx);
     285           60 :         if (deployCtx == DeployContext::HOST) {
     286            0 :             return;
     287              :         }
     288              : 
     289           60 :         int64_t hardwareVersion = 0L;
     290           60 :         const int32_t ret = AicpuGetDeviceInfo(deviceId, MODULE_TYPE_SYSTEM, INFO_TYPE_VERSION, &hardwareVersion);
     291           60 :         if (ret != AICPU_SCHEDULE_OK) {
     292            2 :             aicpusd_err("Get device info failed, deviceId=%u", deviceId);
     293            2 :             return;
     294              :         }
     295              : 
     296           58 :         FeatureCtrl::Init(hardwareVersion, deviceId);
     297              : 
     298           58 :         return;
     299              :     }
     300              : 
     301           60 :     int32_t AicpuDrvManager::InitDrvMgr(const std::vector<uint32_t> &deviceVec, const pid_t hostPid,
     302              :                                         const uint32_t vfId, const bool hasThread, const aicpu::AicpuRunMode runMode,
     303              :                                         const std::string &hostProcName)
     304              :     {
     305           60 :         if (deviceVec.size() == 0LU) {
     306            1 :             aicpusd_err("deviceList is empty, deviceNum[%zu]", deviceVec.size());
     307            1 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     308              :         }
     309              : 
     310           59 :         InitSocType(deviceVec[0U]);
     311              : 
     312           59 :         auto ret = 0;
     313           59 :         if (!FeatureCtrl::IsAosCore()) {
     314           59 :             ret = GetNormalAicpuInfo(deviceVec);
     315           59 :             if (ret != 0) {
     316            1 :                 aicpusd_err("GetNormalAicpuInfo error, ret[%d]", ret);
     317            1 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     318              :             }
     319           58 :             ret = GetCcpuInfo(deviceVec);
     320           58 :             if (ret != 0) {
     321            1 :                 aicpusd_err("GetCcpuInfo error, ret[%d]", ret);
     322            1 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     323              :             }
     324              :         }
     325              : 
     326           57 :         deviceNum_ = deviceVec.size();
     327          114 :         for (size_t i = 0LU; i < deviceNum_; i++) {
     328           58 :             if (deviceVec[i] >= DEVICE_NUM) {
     329            1 :                 aicpusd_err("invalid device id[%u]", deviceVec[i]);
     330            1 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     331              :             }
     332              :         }
     333              : 
     334           56 :         if ((runMode == aicpu::AicpuRunMode::THREAD_MODE) && (vfId > 0)) {
     335            0 :             ret = GetThreadVfAicpuDCpuInfo(deviceVec);
     336              :         } else {
     337           56 :             ret = GetNormalAicpuDCpuInfo(deviceVec);
     338              :         }
     339           56 :         if (ret != AICPU_SCHEDULE_OK) {
     340            0 :             aicpusd_err("get cpu info failed, ret[%d], vfId[%u], runMode[%u]", ret, vfId, runMode);
     341            0 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     342              :         }
     343              : 
     344           56 :         InitDrvMgrCaluniqueVfId(deviceVec, vfId);
     345           56 :         aicpusd_run_info("host pid[%d], host proc name[%s], vf id[%u], first aicpu index[%u], aicpu num[%u],"
     346              :                          " dcpu base index[%u], dcpu num[%u].",
     347              :                          hostPid, hostProcName.c_str(), vfId, aicpuBaseId_, aicpuNumPerDev_, dcpuBase_, dcpuNum_);
     348              : 
     349           56 :         deviceVec_ = deviceVec;
     350           56 :         hostPid_ = hostPid;
     351           56 :         vfId_ = vfId;
     352           56 :         hasThread_ = hasThread;
     353           56 :         isInit_ = true;
     354           56 :         hostProcName_ = hostProcName;
     355              : 
     356           56 :         return AICPU_SCHEDULE_OK;
     357              :     }
     358              : 
     359            3 :     void AicpuDrvManager::DettachAllDevice(const size_t deviceIndex) const
     360              :     {
     361            6 :         for (size_t i = 0LU; i <= deviceIndex; i++) {
     362            3 :             (void)halEschedDettachDevice(deviceVec_[i]);
     363              :         }
     364            3 :     }
     365              : 
     366           15 :     int32_t AicpuDrvManager::InitDrvSchedModule(const uint32_t grpId,
     367              :                                                 const std::map<EVENT_ID, SCHEDULE_PRIORITY> &priority)
     368              :     {
     369              :         // use driver event means has thread
     370           15 :         hasThread_ = true;
     371           15 :         int32_t ret = InitDrvZone();
     372           15 :         if (ret != DRV_ERROR_NONE) {
     373            0 :             aicpusd_err("Failed to init the drv zone, ret[%d].", ret);
     374            0 :             return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     375              :         }
     376           24 :         for (size_t i = 0LU; i < deviceNum_; i++) {
     377           14 :             aicpusd_info("Attach device[%u] to drv scheduler", deviceVec_[i]);
     378           14 :             ret = halEschedAttachDevice(deviceVec_[i]);
     379           14 :             if ((ret != DRV_ERROR_NONE) && (ret != DRV_ERROR_PROCESS_REPEAT_ADD)) {
     380            2 :                 aicpusd_err("Failed to attach device in drv, result[%d], device[%u].", ret, deviceVec_[i]);
     381            5 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     382              :             }
     383              : 
     384           12 :             GROUP_TYPE cpuGrpType = GRP_TYPE_BIND_DP_CPU;
     385           12 :             DeployContext deployCtx = DeployContext::DEVICE;
     386           12 :             const StatusCode status = GetAicpuDeployContext(deployCtx);
     387           12 :             if (status != AICPU_SCHEDULE_OK) {
     388            0 :                 aicpusd_err("Get current deploy ctx failed.");
     389            0 :                 return status;
     390              :             }
     391           12 :             const uint32_t aicpuNum = GetAicpuNumPerDevice();
     392           12 :             if ((deployCtx == DeployContext::HOST) || (aicpuNum == 0U)) {
     393            0 :                 cpuGrpType = GRP_TYPE_BIND_CP_CPU;
     394              :             }
     395           12 :             aicpusd_info("Create group[%u], device[%d], type[%d]", grpId, deviceVec_[i], cpuGrpType);
     396           12 :             ret = halEschedCreateGrp(deviceVec_[i], grpId, cpuGrpType);
     397           12 :             if (ret != DRV_ERROR_NONE) {
     398            2 :                 (void)DettachAllDevice(i);
     399            2 :                 aicpusd_err("Failed to create group in drv, result[%d], group[%u], device[%d], type[%d].",
     400              :                     ret, grpId, deviceVec_[i], cpuGrpType);
     401            2 :                 return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     402              :             }
     403           91 :             for (const auto &item : priority) {
     404           82 :                 ret = halEschedSetEventPriority(deviceVec_[i], item.first, item.second);
     405           82 :                 if (ret != DRV_ERROR_NONE) {
     406            1 :                     (void)DettachAllDevice(i);
     407            1 :                     aicpusd_err("Failed to set event[%d] priority, result[%d].", item.first, ret);
     408            1 :                     return AICPU_SCHEDULE_ERROR_INIT_FAILED;
     409              :                 }
     410           81 :                 aicpusd_info("Set event[%d] priority[%d] success.", item.first, item.second);
     411              :             }
     412              : 
     413            9 :             if (FeatureCtrl::ShouldInitDrvThread() && (deployCtx == DeployContext::DEVICE) && (&halDrvEventThreadInit != nullptr)) {
     414              :                 // call driver interface
     415            0 :                 ret = halDrvEventThreadInit(deviceVec_[i]);
     416            0 :                 aicpusd_run_info("Enable process drv queue msg on ccpu, ret is %d.", static_cast<int32_t>(ret));
     417              :             }
     418              :         }
     419           10 :         return AICPU_SCHEDULE_OK;
     420              :     }
     421              : 
     422           16 :     int32_t AicpuDrvManager::InitDrvZone() const
     423              :     {
     424              :         // init bufManager/eventManager
     425           16 :         if (hasThread_) {
     426           15 :             (void) BufManager::GetInstance().InitBufManager();
     427           15 :             (void) EventWaitManager::NotifyWaitManager(MAX_NOTIFY_COUNT);
     428           15 :             (void) EventWaitManager::EndGraphWaitManager(MAX_MODEL_COUNT);
     429           15 :             (void) ModelStreamManager::GetInstance();
     430           15 :             (void) EventWaitManager::QueueNotEmptyWaitManager(DEFAULT_QUEUE_COUNT);
     431           15 :             (void) EventWaitManager::QueueNotFullWaitManager(DEFAULT_QUEUE_COUNT);
     432           15 :             (void) EventWaitManager::PrepareMemWaitManager(MAX_MODEL_COUNT);
     433           15 :             (void) EventWaitManager::AnyQueNotEmptyWaitManager(MAX_MODEL_COUNT);
     434           15 :             (void) EventWaitManager::TableUnlockWaitManager(MAX_MODEL_COUNT);
     435              :         }
     436           16 :         return AICPU_SCHEDULE_OK;
     437              :     }
     438              : 
     439            9 :     int32_t AicpuDrvManager::SubscribeQueueNotEmptyEvent(const uint32_t queueId) const
     440              :     {
     441            9 :         const uint32_t deviceId = GetDeviceId();
     442            9 :         aicpusd_info("Begin to subscribe non-empty event, deviceId[%u], queueId[%u], groupId[%u].",
     443              :                      deviceId, queueId, groupId_);
     444            9 :         const drvError_t ret = halQueueSubscribe(deviceId, queueId, groupId_, QUEUE_TYPE_SINGLE);
     445            9 :         if (ret != DRV_ERROR_NONE) {
     446            1 :             aicpusd_err("Failed to subscribe event for queue[%u], ret[%d].", queueId, ret);
     447            1 :             return AICPU_SCHEDULE_ERROR_FROM_DRV;
     448              :         }
     449              : 
     450            8 :         aicpusd_info("End to subscribe non-empty event, deviceId[%u], queueId[%u], groupId[%u].",
     451              :                      deviceId, queueId, groupId_);
     452            8 :         return AICPU_SCHEDULE_OK;
     453              :     }
     454              : 
     455            5 :     int32_t AicpuDrvManager::UnSubscribeQueueNotEmptyEvent(const uint32_t queueId) const
     456              :     {
     457            5 :         const uint32_t deviceId = GetDeviceId();
     458            5 :         aicpusd_info("Begin to unsubscribe non-empty event, deviceId[%u], queueId[%u].", deviceId, queueId);
     459            5 :         const drvError_t ret = halQueueUnsubscribe(deviceId, queueId);
     460            5 :         if (ret != DRV_ERROR_NONE) {
     461            1 :             aicpusd_warn("Failed to unsubscribe event for queue[%u], ret[%d].", queueId, ret);
     462            1 :             return AICPU_SCHEDULE_ERROR_FROM_DRV;
     463              :         }
     464              : 
     465            4 :         aicpusd_info("End to unsubscribe non-empty event, deviceId[%u], queueId[%u].", deviceId, queueId);
     466            4 :         return AICPU_SCHEDULE_OK;
     467              :     }
     468              : 
     469            5 :     int32_t AicpuDrvManager::SubscribeQueueNotFullEvent(const uint32_t queueId) const
     470              :     {
     471            5 :         const uint32_t deviceId = GetDeviceId();
     472            5 :         aicpusd_info("Begin to subscribe not full event, deviceId[%u], queueId[%u], groupId[%u].",
     473              :                      deviceId, queueId, groupId_);
     474            5 :         const drvError_t ret = halQueueSubF2NFEvent(deviceId, queueId, groupId_);
     475            5 :         if (ret != DRV_ERROR_NONE) {
     476            1 :             aicpusd_err("Failed to subscribe the event of output queue[%u], ret[%d].", queueId, ret);
     477            1 :             return AICPU_SCHEDULE_ERROR_FROM_DRV;
     478              :         }
     479            4 :         aicpusd_info("End to subscribe not full event, deviceId[%u], queueId[%u], groupId[%u].",
     480              :                      deviceId, queueId, groupId_);
     481            4 :         return AICPU_SCHEDULE_OK;
     482              :     }
     483              : 
     484            4 :     int32_t AicpuDrvManager::UnSubscribeQueueNotFullEvent(const uint32_t queueId) const
     485              :     {
     486            4 :         const uint32_t deviceId = GetDeviceId();
     487            4 :         aicpusd_info("Begin to unsubscribe not full event, deviceId[%u], queueId[%u].", deviceId, queueId);
     488            4 :         const drvError_t ret = halQueueUnsubF2NFEvent(deviceId, queueId);
     489            4 :         if (ret != DRV_ERROR_NONE) {
     490            1 :             aicpusd_warn("Failed to unsubscribe not full event for queue[%u], ret[%d].", queueId, ret);
     491            1 :             return AICPU_SCHEDULE_ERROR_FROM_DRV;
     492              :         }
     493            3 :         aicpusd_info("End to unsubscribe not full event, deviceId[%u], queueId[%u].", deviceId, queueId);
     494            3 :         return AICPU_SCHEDULE_OK;
     495              :     }
     496              : 
     497           14 :     int32_t AicpuDrvManager::BindHostPid(const std::string &signStr,
     498              :                                          const int32_t mode,
     499              :                                          const uint32_t vfId,
     500              :                                          const enum devdrv_process_type cpType) const
     501              :     {
     502           14 :         aicpusd_info("Begin to bind host pid, device id[%u], host pid[%d], mode[%d], vfId[%u], cp type[%d]",
     503              :             deviceVec_[FIRST_INDEX], hostPid_, mode, vfId, cpType);
     504           14 :         if (mode >= static_cast<int32_t>(AicpuPlat::AICPU_MAX_PLAT)) {
     505            1 :             aicpusd_err("plat mode[%d] should less than %d", mode, AicpuPlat::AICPU_MAX_PLAT);
     506            1 :             return AICPU_SCHEDULE_ERROR_INNER_ERROR;
     507              :         }
     508              : 
     509           13 :         if (cpType >= DEVDRV_PROCESS_CPTYPE_MAX) {
     510            1 :             aicpusd_err("cp type[%d] should less than %d", cpType, DEVDRV_PROCESS_CPTYPE_MAX);
     511            1 :             return AICPU_SCHEDULE_ERROR_INNER_ERROR;
     512              :         }
     513              : 
     514           12 :         struct drvBindHostpidInfo para = {};
     515           23 :         for (size_t i = 0LU; i < deviceNum_; i++) {
     516           12 :             para.host_pid = hostPid_;
     517           12 :             para.vfid = vfId;
     518           12 :             para.len = static_cast<uint32_t>(PROCESS_SIGN_LENGTH);
     519           12 :             para.mode = mode;
     520           12 :             para.cp_type = cpType;
     521           12 :             para.chip_id = deviceVec_[i];
     522           12 :             const errno_t memRet = strcpy_s(para.sign, static_cast<size_t>(PROCESS_SIGN_LENGTH), signStr.c_str());
     523           12 :             if (memRet != EOK) {
     524            0 :                 aicpusd_err("memcpy ret[%d], pid[%d], device id[%u], cp type[%d], mode[%d]",
     525              :                     memRet, hostPid_, deviceVec_[i], cpType, mode);
     526            0 :                 return AICPU_SCHEDULE_ERROR_INNER_ERROR;
     527              :             }
     528              : 
     529           12 :             const drvError_t drvRet = drvBindHostPid(para);
     530           12 :             if (drvRet != DRV_ERROR_NONE) {
     531            1 :                 aicpusd_err("Call drvBindHostPid failed, ret[%d] device id[%u], host pid[%d], mode[%d], cp type[%d],"
     532              :                     " vf id[%u]", drvRet, deviceVec_[i], hostPid_, mode, cpType, vfId);
     533            1 :                 return AICPU_SCHEDULE_ERROR_DRV_ERR;
     534              :             }
     535              :         }
     536              : 
     537           11 :         aicpusd_run_info("Bind host pid success, hostPid=%u, vfId=%u, mode=%d", hostPid_, vfId, mode);
     538           11 :         return AICPU_SCHEDULE_OK;
     539              :     }
     540              : 
     541            5 :     int32_t AicpuDrvManager::CheckBindHostPid() const
     542              :     {
     543            5 :         aicpusd_run_info("Start query process host pid");
     544            5 :         if (&drvQueryProcessHostPid == nullptr) {
     545            0 :             aicpusd_run_info("drvQueryProcessHostPid does not exist");
     546            0 :             return AICPU_SCHEDULE_OK;
     547              :         }
     548            5 :         drvError_t ret = DRV_ERROR_NONE;
     549            5 :         unsigned int hostpid = 0;
     550            5 :         unsigned int cpType = DEVDRV_PROCESS_CPTYPE_MAX;
     551            5 :         const int pid = static_cast<int>(getpid());
     552           16 :         for (uint32_t i = 0; i < QUERY_BIND_HOST_PID_TIME/QUERY_BIND_HOST_PID_INTERVBALE; i++) {
     553           15 :             ret = drvQueryProcessHostPid(pid, nullptr, nullptr, &hostpid, &cpType);
     554           15 :             if ((i % QUERY_BIND_HOST_PID_LOG_INTERVAL) == 0U) {
     555            5 :                 aicpusd_run_info("Query process host pid end, ret=%d, hostpid=%u, expect=%u, cpType=%u",
     556              :                                  static_cast<int32_t>(ret), hostpid, hostPid_, cpType);
     557              :             }
     558           15 :             if (ret == DRV_ERROR_NO_PROCESS) {
     559           11 :                 aicpusd_info("call drvQueryProcessHostPid try again");
     560           11 :                 (void)usleep(QUERY_BIND_HOST_PID_INTERVBALE);
     561           11 :                 continue;
     562              :             }
     563              : 
     564            4 :             if (ret != DRV_ERROR_NONE) {
     565            2 :                 aicpusd_err("call drvQueryProcessHostPid failed, ret[%d]", static_cast<int32_t>(ret));
     566            2 :                 return AICPU_SCHEDULE_ERROR_DRV_ERR;
     567              :             }
     568              : 
     569            2 :             aicpusd_info("call drvQueryProcessHostPid result, hostpid[%d], cpType[%d]", hostpid, cpType);
     570            2 :             if (hostPid_ == static_cast<pid_t>(hostpid)) {
     571            1 :                 aicpusd_run_info("call drvQueryProcessHostPid successfully, hostpid[%d], cpType[%d].", hostpid, cpType);
     572            1 :                 return AICPU_SCHEDULE_OK;
     573              :             } else {
     574            1 :                 aicpusd_err("CheckBindHostPid failed, hostpid not right. ret[%d], pid[%d], hostpid[%d], cpType[%d]",
     575              :                             static_cast<int32_t>(ret), pid, hostpid, cpType);
     576            1 :                 return AICPU_SCHEDULE_ERROR_DRV_ERR;
     577              :             }
     578              :         }
     579            1 :         aicpusd_err("CheckBindHostPid failed, try timeout. ret[%d], pid[%d], hostpid[%d], cpType[%d]",
     580              :                     static_cast<int32_t>(ret), pid, hostpid, cpType);
     581            1 :         return AICPU_SCHEDULE_ERROR_DRV_ERR;
     582              :     }
     583              : 
     584           70 :     uint32_t AicpuDrvManager::GetAicpuNum() const
     585              :     {
     586           70 :         aicpusd_info("Get aicpu core num[%u], device num[%zu]", aicpuNumPerDev_, deviceNum_);
     587           70 :         return aicpuNumPerDev_ * static_cast<uint32_t>(deviceNum_);
     588              :     }
     589              : 
     590           34 :     std::vector<uint32_t> AicpuDrvManager::GetDeviceList() const
     591              :     {
     592           34 :         aicpusd_info("Get device list");
     593           34 :         return deviceVec_;
     594              :     }
     595              : 
     596           60 :     uint32_t AicpuDrvManager::GetAicpuNumPerDevice() const
     597              :     {
     598           60 :         aicpusd_info("Get aicpu core num[%u]", aicpuNumPerDev_);
     599           60 :         return aicpuNumPerDev_;
     600              :     }
     601              : 
     602           26 :     int32_t AicpuDrvManager::QueryProcBuffInfo(uint32_t procId,
     603              :                                                std::map<std::string, GroupShareAttr> &buffGrpInfo) const
     604              :     {
     605           26 :         std::unique_ptr<GroupQueryOutput> groupInfoPtr(new (std::nothrow) GroupQueryOutput());
     606           26 :         if (groupInfoPtr == nullptr) {
     607            0 :             aicpusd_err("groupInfoPtr malloc failed.");
     608            0 :             return AICPU_SCHEDULE_ERROR_DRV_ERR;
     609              :         }
     610           26 :         GroupQueryOutput &groupInfo = *(groupInfoPtr.get());
     611           26 :         uint32_t groupInfoLen = 0U;
     612           26 :         const auto drvRet = halGrpQuery(GRP_QUERY_GROUPS_OF_PROCESS, &procId,
     613              :             static_cast<uint32_t>(sizeof(procId)), PtrToPtr<GroupQueryOutput, void>(&groupInfo), &groupInfoLen);
     614           26 :         if (drvRet != DRV_ERROR_NONE) {
     615            2 :             aicpusd_err("Get group info of process[%u] failed, ret[%d]", procId, drvRet);
     616            2 :             return drvRet;
     617              :         }
     618           24 :         if ((static_cast<size_t>(groupInfoLen) % sizeof(groupInfo.grpQueryGroupsOfProcInfo[0])) != 0UL) {
     619            1 :             aicpusd_err("Output buffer size[%d] in halGrpQuery is invalid", groupInfoLen);
     620            1 :             return AICPU_SCHEDULE_ERROR_DRV_ERR;
     621              :         }
     622           23 :         const size_t groupNum = static_cast<size_t>(groupInfoLen) / sizeof(groupInfo.grpQueryGroupsOfProcInfo[0]);
     623           23 :         if (groupNum == 0UL) {
     624           12 :             buffGrpInfo.clear();
     625           12 :             aicpusd_info("Query proc[%u] buff succ, group number[%zu]", procId, groupNum);
     626           12 :             return AICPU_SCHEDULE_OK;
     627              :         }
     628           29 :         for (size_t i = 0UL; i < groupNum; ++i) {
     629           18 :             const std::string grpName(groupInfo.grpQueryGroupsOfProcInfo[i].groupName);
     630           18 :             if (!IsSvmShareGrp(grpName)) {
     631           18 :                 buffGrpInfo[grpName] = groupInfo.grpQueryGroupsOfProcInfo[i].attr;
     632              :             }
     633           18 :         }
     634           11 :         aicpusd_info("Query proc[%u] buff succ, group number[%zu], size[%lu]", procId, groupNum, buffGrpInfo.size());
     635           11 :         return AICPU_SCHEDULE_OK;
     636           26 :     }
     637              : 
     638          128 :     uint32_t AicpuDrvManager::GetAicpuPhysIndex(const uint32_t aicpuLogIndex, const uint32_t deviceId) const
     639              :     {
     640          128 :         if (FeatureCtrl::IsAosCore()) {
     641              :             // connot overflow
     642            8 :             return ((aicpuBaseId_ + aicpuNumPerDev_) * deviceId) + aicpuBaseId_ + aicpuLogIndex;
     643              :         }
     644          120 :         if (FeatureCtrl::BindCpuOnlyOneDevice()) {
     645              :             // 只有一个device,vf切分到同一容器时会产生多个deviceId。
     646            0 :             return aicpuIdVec_[aicpuLogIndex];
     647              :         }
     648          120 :         return (coreNumPerDev_ * deviceId) + aicpuIdVec_[aicpuLogIndex];
     649              :     }
     650              : 
     651           13 :     std::vector<uint32_t> AicpuDrvManager::GetAicpuPhysIndexs(const uint32_t deviceId)
     652              :     {
     653           13 :         if (!aicpuPhyIds_.empty()) {
     654           11 :             return aicpuPhyIds_;
     655              :         }
     656              : 
     657            2 :         const uint32_t aicpuNum = GetAicpuNum();
     658            5 :         for (uint32_t i = 0U; i < aicpuNum; ++i) {
     659            3 :             const uint32_t aicpuLogicIdx = i % aicpuNumPerDev_;
     660            3 :             aicpuPhyIds_.emplace_back(GetAicpuPhysIndex(aicpuLogicIdx, deviceId));
     661              :         }
     662              : 
     663            2 :         return aicpuPhyIds_;
     664              :     }
     665              : 
     666           18 :     bool AicpuDrvManager::IsSvmShareGrp(const std::string &grpName) const
     667              :     {
     668           18 :         return grpName.find("svm_share_grp") != std::string::npos;
     669              :     }
     670              : }  // namespace AicpuSchedule
        

Generated by: LCOV version 2.0-1