LCOV - code coverage report
Current view: top level - acl/aclrt_impl - acl_rt_impl_base.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 94.2 % 120 113
Test Date: 2026-08-06 15:29:52 Functions: 100.0 % 11 11

            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 "acl_rt_impl_base.h"
      11              : #include <mutex>
      12              : #include <string>
      13              : #include <fstream>
      14              : #include <sstream>
      15              : #include <map>
      16              : #include <unordered_set>
      17              : #include <string>
      18              : #include "runtime/dev.h"
      19              : #include "acl_rt_impl.h"
      20              : #include "acl/acl_base.h"
      21              : #include "common/log_inner.h"
      22              : #include "common/error_codes_inner.h"
      23              : #include "platform/platform_info.h"
      24              : #include "runtime/config.h"
      25              : 
      26              : namespace {
      27              : std::mutex aclSocVersionMutex;
      28              : std::string aclSocVersion;
      29              : constexpr size_t SOC_VERSION_LEN = 128U;
      30              : std::recursive_mutex aclInitMutex;
      31              : uint64_t aclInitRefCount = 0;
      32              : std::string aclConfigStr;
      33              : }
      34              : 
      35              : namespace acl {
      36              :     constexpr int32_t MODULE_TYPE_VECTOR_CORE = 7;
      37              :     constexpr int32_t MODULE_TYPE_AICORE = 4;
      38              :     constexpr int32_t INFO_TYPE_CORE_NUM = 3;
      39              :     std::mutex g_platformInfoInitMutex;
      40              :     std::unordered_set<int32_t> g_platformInfoInitSet;
      41              : 
      42           14 :     aclError UpdatePlatformInfoWithDevice(int32_t deviceId)
      43              :     {
      44              : #ifdef __GNUC__
      45              :         // init platform info
      46           14 :         const char *socName = aclrtGetSocNameImpl();
      47           14 :         if (socName == nullptr) {
      48            0 :             ACL_LOG_ERROR("Init SocVersion failed");
      49            0 :             return ACL_ERROR_INTERNAL_ERROR;
      50              :         }
      51           14 :         const std::string socVersion(socName);
      52           14 :         if (fe::PlatformInfoManager::GeInstance().InitRuntimePlatformInfos(socVersion) != 0U) {
      53            2 :             ACL_LOG_WARN("[Init][PlatformInfo]init runtime platform info unsuccessfully, SocVersion = %s",
      54              :                          socVersion.c_str());
      55            2 :             return ACL_ERROR_INTERNAL_ERROR;
      56              :         }
      57              : 
      58           12 :         const std::unique_lock<std::mutex> lk(g_platformInfoInitMutex);
      59           12 :         if (g_platformInfoInitSet.count(deviceId) > 0U) {
      60            5 :             return ACL_SUCCESS;
      61              :         }
      62              : 
      63            7 :         uint32_t aicCnt = 0U;
      64            7 :         auto rtErr = rtGetAiCoreCount(&aicCnt);
      65            7 :         if (rtErr != RT_ERROR_NONE) {
      66            1 :             ACL_LOG_WARN("get aicore count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
      67            1 :             return ACL_GET_ERRCODE_RTS(rtErr);
      68              :         }
      69              : 
      70            6 :         int64_t vecCoreCnt = 0U;
      71              :         // some chips has no vector core
      72            6 :         rtErr = rtGetDeviceInfo(static_cast<uint32_t>(deviceId), MODULE_TYPE_VECTOR_CORE,
      73              :                                 INFO_TYPE_CORE_NUM, &vecCoreCnt);
      74            6 :         if (rtErr != RT_ERROR_NONE) {
      75            1 :             ACL_LOG_WARN("get vector core count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
      76            1 :             return ACL_GET_ERRCODE_RTS(rtErr);
      77              :         }
      78              : 
      79            5 :         int64_t cubeCoreCnt = 0U;
      80            5 :         rtErr = rtGetDeviceInfo(static_cast<uint32_t>(deviceId), MODULE_TYPE_AICORE,
      81              :                                 INFO_TYPE_CUBE_NUM, &cubeCoreCnt);
      82            5 :         if (rtErr != RT_ERROR_NONE) {
      83            1 :             ACL_LOG_WARN("get cube core count unsuccessfully, runtime result = %d", static_cast<int32_t>(rtErr));
      84            1 :             return ACL_GET_ERRCODE_RTS(rtErr);
      85              :         }
      86              : 
      87            4 :         fe::PlatFormInfos platformInfos;
      88              :         uint32_t platformRet =
      89            4 :             fe::PlatformInfoManager::GeInstance().GetRuntimePlatformInfosByDevice(deviceId, platformInfos);
      90            4 :         if (platformRet != 0U) {
      91            1 :             ACL_LOG_WARN("get runtime platformInfos by device unsuccessfully, deviceId = %d", deviceId);
      92            1 :             return ACL_ERROR_INTERNAL_ERROR;
      93              :         }
      94              : 
      95            6 :         const std::string socInfoKey = "SoCInfo";
      96            6 :         const std::string aicCntKey = "ai_core_cnt";
      97            6 :         const std::string vecCoreCntKey = "vector_core_cnt";
      98            3 :         const std::string cubeCoreCntKey = "cube_core_cnt";
      99            3 :         std::map<std::string, std::string> res;
     100            3 :         if (!platformInfos.GetPlatformResWithLock(socInfoKey, res)) {
     101            1 :             ACL_LOG_WARN("unable to get platform result");
     102            1 :             return ACL_ERROR_INTERNAL_ERROR;
     103              :         }
     104              : 
     105            2 :         res[aicCntKey] = std::to_string(aicCnt);
     106            2 :         res[vecCoreCntKey] = std::to_string(vecCoreCnt);
     107            2 :         res[cubeCoreCntKey] = std::to_string(cubeCoreCnt);
     108            2 :         platformInfos.SetPlatformResWithLock(socInfoKey, res);
     109              :         platformRet =
     110            2 :             fe::PlatformInfoManager::GeInstance().UpdateRuntimePlatformInfosByDevice(deviceId, platformInfos);
     111            2 :         if (platformRet != 0U) {
     112            1 :             ACL_LOG_WARN("update runtime platformInfos by device unsuccessfully, deviceId = %d", deviceId);
     113            1 :             return ACL_ERROR_INTERNAL_ERROR;
     114              :         }
     115            1 :         (void)g_platformInfoInitSet.emplace(deviceId);
     116            1 :         ACL_LOG_INFO("Successfully to UpdatePlatformInfoWithDevice, deviceId = %d, aicCnt = %u, vecCoreCnt = %ld, "
     117              :                      "cubeCoreCnt = %ld", deviceId, aicCnt, vecCoreCnt, cubeCoreCnt);
     118              : #endif
     119            1 :         return ACL_SUCCESS;
     120           14 :     }
     121              : 
     122           50 : aclError InitSocVersion()
     123              : {
     124           50 :     const std::unique_lock<std::mutex> lk(aclSocVersionMutex);
     125           50 :     if (aclSocVersion.empty()) {
     126              :         // get socVersion
     127            2 :         char_t socVersion[SOC_VERSION_LEN] = {};
     128            2 :         const auto rtErr = rtGetSocVersion(socVersion, static_cast<uint32_t>(sizeof(socVersion)));
     129            2 :         if (rtErr != RT_ERROR_NONE) {
     130            1 :             ACL_LOG_INFO("Cannot get soc version, runtime errorCode is %d", static_cast<int32_t>(rtErr));
     131            1 :             return ACL_GET_ERRCODE_RTS(rtErr);
     132              :         }
     133            2 :         aclSocVersion = std::string(socVersion);
     134              :     }
     135           49 :     ACL_LOG_INFO("get SocVersion success, SocVersion = %s", aclSocVersion.c_str());
     136           49 :     return ACL_SUCCESS;
     137           50 : }
     138              : 
     139            8 : const std::string &GetSocVersion()
     140              : {
     141            8 :     ACL_LOG_INFO("socVersion is %s", aclSocVersion.c_str());
     142            8 :     return aclSocVersion;
     143              : }
     144              : 
     145           58 : bool GetAclInitFlag()
     146              : {
     147           58 :     return aclInitRefCount > 0UL;
     148              : }
     149              : 
     150           94 : uint64_t &GetAclInitRefCount()
     151              : {
     152           94 :     return aclInitRefCount;
     153              : }
     154              : 
     155           99 : std::recursive_mutex &GetAclInitMutex()
     156              : {
     157           99 :     return aclInitMutex;
     158              : }
     159              : 
     160            4 : std::string &GetConfigPathStr()
     161              : {
     162            4 :     return aclConfigStr;
     163              : }
     164              : 
     165           44 : void SetConfigPathStr(std::string &configStr)
     166              : {
     167           44 :     aclConfigStr = configStr;
     168           44 : }
     169              : 
     170           46 : aclError GetStrFromConfigPath(const char *configPath, std::string &configStr) {
     171              :     // 文件路径为空,按照空文件处理
     172           46 :     if (configPath != nullptr && strlen(configPath) != 0UL) {
     173           28 :         char_t realPath[MMPA_MAX_PATH] = {};
     174           28 :         if (mmRealPath(configPath, realPath, MMPA_MAX_PATH) != EN_OK) {
     175            2 :             const auto formatErrMsg = acl::AclGetErrorFormatMessage(mmGetErrorCode());
     176            2 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PATH_MSG,
     177            4 :                 std::vector<const char*>({"path", "reason"}),
     178            4 :                 std::vector<const char*>({configPath, formatErrMsg.c_str()}));
     179            2 :             ACL_LOG_ERROR("Invalid file: %s", configPath);
     180            2 :             return ACL_ERROR_INVALID_FILE;
     181            2 :         }
     182           26 :         std::ifstream file(realPath, std::ios::binary);
     183           26 :         if (!file.is_open()) {
     184            0 :             acl::AclErrorLogManager::ReportInputError(acl::INVALID_PATH_MSG,
     185            0 :                 std::vector<const char*>({"path", "reason"}),
     186            0 :                 std::vector<const char*>({configPath, "file open failed"}));
     187            0 :             ACL_LOG_ERROR("Failed to open file: %s", configPath);
     188            0 :             return ACL_ERROR_INVALID_FILE;
     189              :         }
     190              : 
     191           26 :         std::stringstream buffer;
     192           26 :         buffer << file.rdbuf();
     193           26 :         configStr = buffer.str();
     194           26 :         file.close();  // 显式关闭文件
     195           26 :     }
     196           44 :     return ACL_SUCCESS;
     197              : }
     198              : } // namespace acl
     199              : 
     200              : #ifdef __cplusplus
     201              : extern "C" {
     202              : #endif
     203              : 
     204           50 : const char *aclrtGetSocNameImpl()
     205              : {
     206           50 :     ACL_LOG_INFO("start to execute aclrtGetSocName.");
     207              :     // get socVersion
     208           50 :     const auto ret = acl::InitSocVersion();
     209           50 :     if (ret != ACL_SUCCESS) {
     210            1 :         ACL_LOG_INFO("Cannot init soc version, errorCode = %d", ret);
     211            1 :         return nullptr;
     212              :     }
     213           49 :     ACL_LOG_INFO("execute aclrtGetSocName successfully");
     214           49 :     return aclSocVersion.c_str();
     215              : }
     216              : 
     217            1 : aclError aclrtGetVersionImpl(int32_t *majorVersion, int32_t *minorVersion, int32_t *patchVersion)
     218              : {
     219            1 :     ACL_LOG_INFO("start to execute aclrtGetVersion.");
     220            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(majorVersion);
     221            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(minorVersion);
     222            1 :     ACL_REQUIRES_NOT_NULL_WITH_INPUT_REPORT(patchVersion);
     223              : 
     224              :     // Acl version is (*majorVersion).(*minorVersion).(*patchVersion)
     225            1 :     *majorVersion = ACL_MAJOR_VERSION;
     226            1 :     *minorVersion = ACL_MINOR_VERSION;
     227            1 :     *patchVersion = ACL_PATCH_VERSION;
     228            1 :     ACL_LOG_INFO("acl version is %d.%d.%d", *majorVersion, *minorVersion, *patchVersion);
     229              : 
     230            1 :     return ACL_SUCCESS;
     231              : }
     232              : #ifdef __cplusplus
     233              : }
     234              : #endif
        

Generated by: LCOV version 2.0-1