LCOV - code coverage report
Current view: top level - basic_component/package_manager/src - package_env_info.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 99.4 % 156 155
Test Date: 2026-08-31 10:06:05 Functions: 100.0 % 12 12

            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 "package_env_info.h"
      12              : #include <string>
      13              : #include <vector>
      14              : #include "driver/ascend_hal.h"
      15              : #include "capability_manager.h"
      16              : #include "env_internal_api.h"
      17              : #include "error_manager.h"
      18              : #include "platform_info.h"
      19              : #include "tsd_log.h"
      20              : #include "tsd/status.h"
      21              : #include "tsd_util_func.h"
      22              : #include "package_process_config.h"
      23              : #include "weak_ascend_hal.h"
      24              : 
      25              : namespace {
      26              : const std::string AICPU_PACKAGE_PATTERN = "^Ascend([0-9]{3}(rc)?(P)?)?-aicpu_syskernels\\.tar\\.gz$";
      27              : const std::string EXTEND_PACKAGE_PATTERN = "^Ascend([0-9]{3}(rc)?(P)?)?-aicpu_extend_syskernels\\.tar\\.gz$";
      28              : const std::string ASCENDCPP_PACKAGE_PATTERN = "^transformer_tile_fwk_aicpu_kernel\\.tar\\.gz$";
      29              : constexpr uint32_t SOC_VERSION_LEN = 50U;
      30              : const std::string QUEUE_SCHEDULE_SO = "libqueue_schedule.so";
      31              : constexpr int64_t SUPPORT_MAX_DEVICE_PER_HOST = 8;
      32              : } // namespace
      33              : 
      34              : namespace tsd {
      35              : 
      36          792 : PackageEnvInfo::PackageEnvInfo(uint32_t logicDeviceId, uint32_t platInfoMode, bool isAdcEnv, uint32_t chipType)
      37        76824 :     : platInfoMode_(platInfoMode), isAdcEnv_(isAdcEnv), chipType_(chipType), logicDeviceId_(logicDeviceId)
      38              : {
      39          792 :     packagePattern_[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL)] = AICPU_PACKAGE_PATTERN;
      40              :     packagePattern_[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL)] =
      41          792 :         EXTEND_PACKAGE_PATTERN;
      42          792 :     packagePattern_[static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP)] = ASCENDCPP_PACKAGE_PATTERN;
      43          792 :     hostSoPath_ = tsd::GetHostSoPath();
      44          792 : }
      45              : 
      46           28 : std::string PackageEnvInfo::GetTrustedBasePath(bool useV2) const
      47              : {
      48           28 :     constexpr int32_t peerNode = 0;
      49           28 :     constexpr uint32_t hdcNameMax = 256U;
      50           28 :     char_t path[hdcNameMax] = {};
      51              :     int32_t drvRet;
      52           28 :     if (useV2) {
      53           19 :         drvRet = drvHdcGetTrustedBasePathV2(peerNode, static_cast<int32_t>(logicDeviceId_), &path[0], hdcNameMax);
      54              :     } else {
      55            9 :         drvRet = drvHdcGetTrustedBasePath(peerNode, static_cast<int32_t>(logicDeviceId_), &path[0], hdcNameMax);
      56              :     }
      57           28 :     if (drvRet != DRV_ERROR_NONE) {
      58            2 :         TSD_ERROR("[PackageManager][deviceId_=%u] get trusted base path failed, ret[%d]", logicDeviceId_, drvRet);
      59              :     }
      60           56 :     return std::string(path);
      61              : }
      62              : 
      63           21 : void PackageEnvInfo::GetAscendLatestIntallPath(std::string& pkgBasePath) const
      64              : {
      65           21 :     GetEnvFromMmSys(MM_ENV_ASCEND_LATEST_INSTALL_PATH, "ASCEND_LATEST_INSTALL_PATH", pkgBasePath);
      66           21 :     if (pkgBasePath.empty()) {
      67           20 :         GetEnvFromMmSys(MM_ENV_ASCEND_HOME_PATH, "ASCEND_HOME_PATH", pkgBasePath);
      68           20 :         if (pkgBasePath.empty()) {
      69           20 :             pkgBasePath = "/usr/local/Ascend/latest/";
      70           20 :             TSD_INFO(
      71              :                 "[TsdClient][logicDeviceId_=%u] ASCEND_LATEST_INSTALL_PATH is not set, use default value[%s]",
      72              :                 logicDeviceId_, pkgBasePath.c_str());
      73              :         }
      74              :     }
      75           21 :     TSD_RUN_INFO("latest install path:%s", pkgBasePath.c_str());
      76           21 : }
      77              : 
      78            9 : TSD_StatusT PackageEnvInfo::GetTrustedBasePathFromDevice(int32_t& peerNode, std::string& dstDirPreFix) const
      79              : {
      80            9 :     peerNode = 0;
      81            9 :     dstDirPreFix = GetTrustedBasePath(true);
      82            9 :     return TSD_OK;
      83              : }
      84              : 
      85           20 : std::string PackageEnvInfo::GetCurHostMutexFile(bool useCannPath) const
      86              : {
      87           20 :     if (!useCannPath) {
      88           16 :         return QUEUE_SCHEDULE_SO;
      89              :     }
      90            4 :     int64_t masterId = 0;
      91            4 :     uint32_t phyId = 0U;
      92            4 :     auto drvRet = drvDeviceGetPhyIdByIndex(logicDeviceId_, &phyId);
      93            4 :     if (drvRet != DRV_ERROR_NONE) {
      94            1 :         TSD_RUN_WARN("Getting the physical ID was not successful, retCode[%d] deviceId[%u]", drvRet, logicDeviceId_);
      95            1 :         return QUEUE_SCHEDULE_SO;
      96              :     }
      97            3 :     TSD_INFO("deviceId[%u] physical ID[%u]", logicDeviceId_, phyId);
      98            3 :     drvRet = halGetDeviceInfo(phyId, MODULE_TYPE_SYSTEM, INFO_TYPE_MASTERID, &masterId);
      99            3 :     if (drvRet != DRV_ERROR_NONE) {
     100            1 :         TSD_RUN_WARN(
     101              :             "Calling halGetDeviceInfo was not successful, retCode[%d] deviceId[%u] physical ID[%u]", drvRet,
     102              :             logicDeviceId_, phyId);
     103            1 :         return QUEUE_SCHEDULE_SO;
     104              :     }
     105            2 :     const int64_t devOsId = masterId % SUPPORT_MAX_DEVICE_PER_HOST;
     106            2 :     std::string mutexFile = MUTEX_FILE_PREFIX + std::to_string(devOsId) + ".cfg";
     107            2 :     TSD_RUN_INFO(
     108              :         "get masterId:%lld, logicDeviceId:%u, physicalDeviceId[%u],devOsId:%lld, mutexFile:%s, maxcount:%lld", masterId,
     109              :         logicDeviceId_, phyId, devOsId, mutexFile.c_str(), SUPPORT_MAX_DEVICE_PER_HOST);
     110            2 :     return mutexFile;
     111            2 : }
     112              : 
     113           11 : bool PackageEnvInfo::GetShortSocVersion(std::string& shortSocVersion) const
     114              : {
     115           11 :     char_t socVersion[SOC_VERSION_LEN] = {};
     116           11 :     const auto drvRet = halGetSocVersion(logicDeviceId_, &socVersion[0U], SOC_VERSION_LEN);
     117           11 :     if (drvRet != DRV_ERROR_NONE) {
     118            1 :         TSD_RUN_WARN("get soc_version by halGetSocVersion failed");
     119            1 :         return false;
     120              :     }
     121           10 :     TSD_INFO("get soc_version:%s", &socVersion[0]);
     122           10 :     (void)fe::PlatformInfoManager::Instance().InitializePlatformInfo();
     123           10 :     fe::OptionalInfos optionalInfos;
     124           10 :     fe::PlatFormInfos platformInfos;
     125           30 :     (void)fe::PlatformInfoManager::Instance().GetPlatformInfos(socVersion, platformInfos, optionalInfos);
     126           30 :     const bool ret = platformInfos.GetPlatformRes("version", "Short_SoC_version", shortSocVersion);
     127           10 :     if (!ret) {
     128            1 :         TSD_RUN_WARN("get short_soc_version by fe::PlatFormInfos::GetPlatformRes failed.");
     129            1 :         return false;
     130              :     }
     131            9 :     TSD_RUN_INFO("get short_soc_version:%s", shortSocVersion.c_str());
     132            9 :     return true;
     133           10 : }
     134              : 
     135           93 : bool PackageEnvInfo::ResolvePackageTitle(uint32_t chipType, uint32_t platInfoMode, std::string& packageTitle)
     136              : {
     137           93 :     switch (static_cast<ChipType_t>(chipType)) {
     138           85 :         case CHIP_MINI:
     139           85 :             if (platInfoMode == static_cast<uint32_t>(ModeType::OFFLINE)) {
     140           54 :                 packageTitle = "Ascend310RC";
     141              :             } else {
     142           31 :                 packageTitle = "Ascend310";
     143              :             }
     144           85 :             break;
     145            1 :         case CHIP_ASCEND_910A:
     146            1 :             packageTitle = "Ascend910";
     147            1 :             break;
     148            2 :         case CHIP_DC:
     149            2 :             packageTitle = "Ascend310P";
     150            2 :             break;
     151            4 :         case CHIP_ASCEND_950:
     152              :         case CHIP_ASCEND_910B:
     153              :         case CHIP_CLOUD_V5:
     154              :         case CHIP_MINI_V3:
     155              :         case CHIP_ASCEND_350:
     156              :         case CHIP_CLOUD_V6:
     157            4 :             packageTitle = "Ascend";
     158            4 :             break;
     159            1 :         default:
     160            1 :             packageTitle = "";
     161            1 :             break;
     162              :     }
     163           93 :     return !packageTitle.empty();
     164              : }
     165              : 
     166           92 : bool PackageEnvInfo::GetPackageTitle(std::string& packageTitle) const
     167              : {
     168           92 :     return ResolvePackageTitle(chipType_, platInfoMode_, packageTitle);
     169              : }
     170              : 
     171           80 : bool PackageEnvInfo::GetPackagePath(std::string& packagePath, const uint32_t packageType) const
     172              : {
     173           80 :     std::string ascendAicpuPath;
     174           80 :     GetEnvFromMmSys(MM_ENV_ASCEND_AICPU_PATH, "ASCEND_AICPU_PATH", ascendAicpuPath);
     175           80 :     if (ascendAicpuPath.empty()) {
     176           79 :         ascendAicpuPath = "/usr/local/Ascend/";
     177           79 :         TSD_INFO(
     178              :             "[TsdClient][deviceId=%u] environment variable ASCEND_AICPU_PATH is not set, use default "
     179              :             "value[%s]",
     180              :             logicDeviceId_, ascendAicpuPath.c_str());
     181              :     }
     182           80 :     std::string title = "";
     183           80 :     if (!GetPackageTitle(title)) {
     184            1 :         TSD_RUN_INFO("[TsdClient][deviceId=%u] skip load aicpu opkernel packages", logicDeviceId_);
     185            1 :         return false;
     186              :     }
     187              : 
     188           79 :     if (packageType == static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL) ||
     189              :         packageType == static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL)) {
     190           53 :         packagePath = ascendAicpuPath + "/opp/" + title + "/aicpu/";
     191           26 :     } else if (packageType == static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP)) {
     192           26 :         packagePath = ascendAicpuPath + "/opp/built-in/";
     193              :     } else {
     194            0 :         return false;
     195              :     }
     196              : 
     197           79 :     TSD_INFO("[TsdClient][deviceId=%u] get packagePath[%s]", logicDeviceId_, packagePath.c_str());
     198           79 :     return true;
     199           80 : }
     200              : 
     201           81 : bool PackageEnvInfo::CheckPackageExistsOnce(const uint32_t packageType)
     202              : {
     203           81 :     std::string pkgPath;
     204           81 :     if (!GetPackagePath(pkgPath, packageType)) {
     205            3 :         TSD_INFO(
     206              :             "[TsdClient][deviceId=%u] Package path is invalid, skip load package file, packageType[%u]", logicDeviceId_,
     207              :             packageType);
     208            3 :         return false;
     209              :     }
     210           78 :     if ((mmAccess(pkgPath.c_str()) != EN_OK) || (mmIsDir(pkgPath.c_str()) != EN_OK)) {
     211           54 :         TSD_WARN(
     212              :             "[TsdClient][deviceId=%u] path[%s] does not exist, packageType[%u]", logicDeviceId_, pkgPath.c_str(),
     213              :             packageType);
     214           54 :         return false;
     215              :     }
     216              : 
     217           24 :     std::vector<std::string> packages = ScanAndMatchPackages(pkgPath, packageType);
     218           24 :     packageName_[packageType] = "";
     219           24 :     if (packages.size() != 1U) {
     220           18 :         TSD_RUN_INFO(
     221              :             "[TsdClient][deviceId=%u] pkg size[%zu] in path[%s] must be only one, skip send package, packageType[%u]",
     222              :             logicDeviceId_, packages.size(), pkgPath.c_str(), packageType);
     223           18 :         return false;
     224              :     }
     225            6 :     packageName_[packageType] = packages[0U];
     226            6 :     packagePath_[packageType] = pkgPath;
     227            6 :     TSD_RUN_INFO(
     228              :         "[TsdClient][deviceId=%u] set package path[%s], package name[%s], packageType[%u]", logicDeviceId_,
     229              :         packagePath_[static_cast<uint32_t>(packageType)].c_str(), packageName_[packageType].c_str(), packageType);
     230            6 :     return true;
     231           81 : }
     232              : 
     233           24 : std::vector<std::string> PackageEnvInfo::ScanAndMatchPackages(
     234              :     const std::string& pkgPath, const uint32_t packageType) const
     235              : {
     236           24 :     mmDirent2** fileList = nullptr;
     237           24 :     const int32_t fileCount = mmScandir2(pkgPath.c_str(), &fileList, nullptr, nullptr);
     238           24 :     if (fileCount < 0) {
     239            6 :         TSD_WARN(
     240              :             "[TsdClient][deviceId=%u] scandir path[%s] failed, packageType[%u]", logicDeviceId_, pkgPath.c_str(),
     241              :             packageType);
     242            6 :         return {};
     243              :     }
     244           18 :     std::vector<std::string> files;
     245           39 :     for (int32_t i = 0; i < fileCount; i++) {
     246           21 :         files.emplace_back(fileList[i]->d_name);
     247              :     }
     248           18 :     mmScandirFree2(fileList, fileCount);
     249              : 
     250           18 :     std::vector<std::string> packages;
     251           57 :     for (const std::string& fileName : files) {
     252           21 :         TSD_INFO(
     253              :             "[TsdClient][deviceId=%u] check file[%s], packageType[%u]", logicDeviceId_, fileName.c_str(), packageType);
     254           84 :         if (ValidateStr(fileName.c_str(), packagePattern_[packageType].c_str())) {
     255            6 :             TSD_INFO(
     256              :                 "[TsdClient][deviceId=%u] find package[%s] in path[%s], packageType[%u]", logicDeviceId_,
     257              :                 fileName.c_str(), pkgPath.c_str(), packageType);
     258            6 :             packages.emplace_back(fileName);
     259              :         }
     260              :     }
     261           18 :     return packages;
     262           18 : }
     263              : 
     264           27 : bool PackageEnvInfo::CheckPackageExists(const bool loadAicpuKernelFlag)
     265              : {
     266           27 :     bool hasPackage = false;
     267           27 :     std::vector<uint32_t> packageTypes;
     268           27 :     if (loadAicpuKernelFlag) {
     269           27 :         packageTypes.push_back(static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_KERNEL));
     270           27 :         packageTypes.push_back(static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_AICPU_EXTEND_KERNEL));
     271              :     }
     272           27 :     packageTypes.push_back(static_cast<uint32_t>(TsdLoadPackageType::TSD_PKG_TYPE_ASCENDCPP));
     273          135 :     for (const uint32_t packageType : packageTypes) {
     274           81 :         if (CheckPackageExistsOnce(packageType)) {
     275            6 :             TSD_INFO("[TsdClient][deviceId=%u] get package successfully, packageType[%u]", logicDeviceId_, packageType);
     276            6 :             hasPackage = true;
     277              :         }
     278              :     }
     279              : 
     280           27 :     return hasPackage;
     281           27 : }
     282              : 
     283              : } // namespace tsd
        

Generated by: LCOV version 2.0-1