LCOV - code coverage report
Current view: top level - src/platform - platform_manager_v2.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 88.8 % 169 150
Test Date: 2026-07-28 10:52:27 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              : 
      11              : #include "platform_manager_v2.h"
      12              : #include <dirent.h>
      13              : #include <string.h>
      14              : #include <algorithm>
      15              : #include <fstream>
      16              : #include "platform_infos_utils.h"
      17              : #include "platform_error_define.h"
      18              : 
      19              : #ifdef __cplusplus
      20              : extern "C" {
      21              : #endif // __cplusplus
      22              : 
      23              : const std::string FIXPIPE_CONFIG_KEY = "Intrinsic_fix_pipe_";
      24              : const std::string INI_FILE_SUFFIX = ".ini";
      25              : 
      26              : const std::string STR_AI_CORE_INTRINSIC_DTYPE_MAP = "AICoreintrinsicDtypeMap";
      27              : const std::string STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP = "VectorCoreintrinsicDtypeMap";
      28              : 
      29              : const std::string SOC_VERSION_ASCEND910 = "Ascend910";
      30              : const std::string SOC_VERSION_ASCEND910A = "Ascend910A";
      31              : 
      32           25 : PlatformManagerV2& PlatformManagerV2::Instance()
      33              : {
      34           25 :     static PlatformManagerV2 platform_info;
      35           25 :     return platform_info;
      36              : }
      37              : 
      38            2 : uint32_t PlatformManagerV2::LoadIniFile(const std::string& ini_file_real_path)
      39              : {
      40            2 :     std::ifstream ifs(ini_file_real_path);
      41            2 :     if (!ifs.is_open()) {
      42            1 :         PF_LOGE("Failed to open conf file, it does not exist or is already opened.");
      43            1 :         return PLATFORM_FAILED;
      44              :     }
      45              : 
      46            1 :     std::map<std::string, std::string> content_map;
      47            1 :     std::map<std::string, std::map<std::string, std::string>> content_info_map;
      48            1 :     content_map.clear();
      49            1 :     content_info_map.clear();
      50            1 :     std::string line;
      51            1 :     std::string map_key;
      52          284 :     while (std::getline(ifs, line)) {
      53          283 :         if (line.empty() || line.find('#') == 0) {
      54           22 :             continue;
      55              :         }
      56              : 
      57          271 :         if (line.find('[') == 0) {
      58           10 :             if (!map_key.empty() && !content_map.empty()) {
      59            9 :                 content_info_map.emplace(make_pair(map_key, content_map));
      60            9 :                 content_map.clear();
      61              :             }
      62           10 :             size_t pos = line.rfind(']');
      63           10 :             if (pos == std::string::npos) {
      64            0 :                 continue;
      65              :             }
      66           10 :             map_key = line.substr(1, pos - 1);
      67           10 :             fe::PlatformInfosUtils::Trim(map_key);
      68           10 :             continue;
      69           10 :         }
      70              : 
      71          261 :         size_t pos_of_equal = line.find('=');
      72          261 :         if (pos_of_equal == std::string::npos) {
      73            0 :             continue;
      74              :         }
      75              : 
      76          261 :         std::string key = line.substr(0, pos_of_equal);
      77          261 :         fe::PlatformInfosUtils::Trim(key);
      78          261 :         std::string value = line.substr(pos_of_equal + 1, line.length() - pos_of_equal - 1);
      79          261 :         fe::PlatformInfosUtils::Trim(value);
      80          261 :         if (!key.empty() && !value.empty()) {
      81          260 :             content_map.emplace(make_pair(key, value));
      82              :         }
      83          261 :     }
      84              : 
      85            1 :     if (!content_map.empty() && !map_key.empty()) {
      86            1 :         content_info_map.emplace(make_pair(map_key, content_map));
      87            1 :         content_map.clear();
      88              :     }
      89              : 
      90            1 :     ifs.close();
      91              : 
      92            1 :     if (AssemblePlatformInfoVector(content_info_map) != PLATFORM_SUCCESS) {
      93            0 :         PF_LOGE("Assemble platform info failed.");
      94            0 :         return PLATFORM_FAILED;
      95              :     }
      96              : 
      97            1 :     return PLATFORM_SUCCESS;
      98            2 : }
      99              : 
     100            1 : uint32_t PlatformManagerV2::AssemblePlatformInfoVector(
     101              :     std::map<std::string, std::map<std::string, std::string>>& content_info_map)
     102              : {
     103            1 :     fe::PlatFormInfos platform_infos;
     104            1 :     if (!platform_infos.Init()) {
     105            0 :         return PLATFORM_FAILED;
     106              :     }
     107              : 
     108            1 :     if (ParsePlatformInfo(content_info_map, platform_infos) != PLATFORM_SUCCESS) {
     109            0 :         PF_LOGE("Parse platform info from content failed.");
     110            0 :         return PLATFORM_FAILED;
     111              :     }
     112            1 :     FillupFixPipeInfo(platform_infos);
     113              : 
     114            2 :     std::string soc_version = "";
     115            3 :     (void)platform_infos.GetPlatformResWithLock("version", "SoC_version", soc_version);
     116            1 :     if (!soc_version.empty()) {
     117            1 :         auto iter = platform_infos_map_.find(soc_version);
     118            1 :         if (iter == platform_infos_map_.end()) {
     119            1 :             platform_infos_map_.emplace(soc_version, platform_infos);
     120            1 :             PF_LOGD("The platform info's os soc version[%s] has been initialized.", soc_version.c_str());
     121              :         }
     122              :     } else {
     123            0 :         PF_LOGE("Failed to initialize platform: %s.", platform_infos.SaveToBuffer().c_str());
     124            0 :         return PLATFORM_FAILED;
     125              :     }
     126              : 
     127            1 :     return PLATFORM_SUCCESS;
     128            1 : }
     129              : 
     130            1 : void PlatformManagerV2::FillupFixPipeInfo(fe::PlatFormInfos& platform_infos)
     131              : {
     132            1 :     std::string is_support_fixpipe_str;
     133            6 :     if (!platform_infos.GetPlatformResWithLock("AICoreSpec", "support_fixpipe", is_support_fixpipe_str) ||
     134            1 :         !static_cast<bool>(std::atoi(is_support_fixpipe_str.c_str()))) {
     135            0 :         return;
     136              :     }
     137            1 :     std::map<std::string, std::vector<std::string>> aicore_map = platform_infos.GetAICoreIntrinsicDtype();
     138            1 :     std::map<std::string, std::vector<std::string>> fixpipe_map;
     139          103 :     for (auto iter = aicore_map.begin(); iter != aicore_map.end(); iter++) {
     140          102 :         if (iter->first.find(FIXPIPE_CONFIG_KEY) != iter->first.npos) {
     141           16 :             fixpipe_map.emplace(make_pair(iter->first, iter->second));
     142              :         }
     143              :     }
     144            1 :     if (fixpipe_map.empty()) {
     145            0 :         return;
     146              :     }
     147            1 :     platform_infos.SetFixPipeDtypeMap(fixpipe_map);
     148            1 : }
     149              : 
     150            1 : void PlatformManagerV2::ParseAICoreintrinsicDtypeMap(
     151              :     std::map<std::string, std::string>& ai_coreintrinsic_dtype_map, fe::PlatFormInfos& platform_info_temp)
     152              : {
     153            1 :     std::map<std::string, std::string>::const_iterator iter;
     154            1 :     std::map<std::string, std::string> platform_res_map;
     155              :     std::map<std::string, std::vector<std::string>> aicore_intrinsic_dtypes =
     156            1 :         platform_info_temp.GetAICoreIntrinsicDtype();
     157          104 :     for (iter = ai_coreintrinsic_dtype_map.begin(); iter != ai_coreintrinsic_dtype_map.end(); iter++) {
     158          103 :         size_t pos = iter->second.find('|');
     159          103 :         if (pos == std::string::npos) {
     160            0 :             return;
     161              :         }
     162          103 :         std::string key = iter->second.substr(0, pos);
     163          103 :         std::string value = iter->second.substr(pos + 1);
     164          103 :         std::vector<std::string> dtype_vector;
     165          103 :         fe::PlatformInfosUtils::Split(value, ',', dtype_vector);
     166          103 :         platform_res_map.emplace(make_pair(key, value));
     167          103 :         aicore_intrinsic_dtypes.emplace(make_pair(key, dtype_vector));
     168          103 :     }
     169              : 
     170            1 :     platform_info_temp.SetPlatformRes(STR_AI_CORE_INTRINSIC_DTYPE_MAP, platform_res_map);
     171            1 :     platform_info_temp.SetAICoreIntrinsicDtype(aicore_intrinsic_dtypes);
     172            1 : }
     173              : 
     174            1 : void PlatformManagerV2::ParseVectorCoreintrinsicDtypeMap(
     175              :     std::map<std::string, std::string>& vector_coreintrinsic_dtype_map, fe::PlatFormInfos& platform_info_temp)
     176              : {
     177            1 :     std::map<std::string, std::string>::const_iterator iter;
     178            1 :     std::map<std::string, std::string> platform_res_map;
     179              :     std::map<std::string, std::vector<std::string>> vector_core_intrinsic_dtype_map =
     180            1 :         platform_info_temp.GetVectorCoreIntrinsicDtype();
     181           75 :     for (iter = vector_coreintrinsic_dtype_map.begin(); iter != vector_coreintrinsic_dtype_map.end(); iter++) {
     182           74 :         size_t pos = iter->second.find('|');
     183           74 :         if (pos == std::string::npos) {
     184            0 :             PF_LOGE("The intrinsic_dtype_map string does not contain '|'.");
     185            0 :             return;
     186              :         }
     187           74 :         std::string key = iter->second.substr(0, pos);
     188           74 :         std::string value = iter->second.substr(pos + 1);
     189           74 :         std::vector<std::string> dtype_vector;
     190           74 :         fe::PlatformInfosUtils::Split(value, ',', dtype_vector);
     191           74 :         platform_res_map.emplace(make_pair(key, value));
     192           74 :         vector_core_intrinsic_dtype_map.emplace(make_pair(key, dtype_vector));
     193           74 :     }
     194              : 
     195            1 :     platform_info_temp.SetPlatformRes(STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP, platform_res_map);
     196            1 :     platform_info_temp.SetVectorCoreIntrinsicDtype(vector_core_intrinsic_dtype_map);
     197            1 : }
     198              : 
     199            8 : void PlatformManagerV2::ParsePlatformRes(
     200              :     const std::string& label, std::map<std::string, std::string>& platform_res_map,
     201              :     fe::PlatFormInfos& platform_info_temp)
     202              : {
     203            8 :     platform_info_temp.SetPlatformRes(label, platform_res_map);
     204            8 : }
     205              : 
     206            1 : uint32_t PlatformManagerV2::ParsePlatformInfo(
     207              :     std::map<std::string, std::map<std::string, std::string>>& content_info_map, fe::PlatFormInfos& platform_info_temp)
     208              : {
     209            1 :     std::map<std::string, std::map<std::string, std::string>>::iterator it;
     210           11 :     for (it = content_info_map.begin(); it != content_info_map.end(); it++) {
     211           10 :         if (it->first == STR_AI_CORE_INTRINSIC_DTYPE_MAP) {
     212            1 :             ParseAICoreintrinsicDtypeMap(it->second, platform_info_temp);
     213            9 :         } else if (it->first == STR_VECTOR_CORE_INTRINSIC_DTYPE_MAP) {
     214            1 :             ParseVectorCoreintrinsicDtypeMap(it->second, platform_info_temp);
     215              :         } else {
     216            8 :             ParsePlatformRes(it->first, it->second, platform_info_temp);
     217              :         }
     218              :     }
     219              : 
     220            1 :     return PLATFORM_SUCCESS;
     221              : }
     222              : 
     223            2 : uint32_t PlatformManagerV2::InitPlatformInfos(const std::string& soc_version)
     224              : {
     225            2 :     std::string cfg_file_real_path = fe::GetConfigFilePath<PlatformManagerV2>();
     226            2 :     if (cfg_file_real_path.empty()) {
     227            0 :         PF_LOGE("File path[%s] is not valid.", cfg_file_real_path.c_str());
     228            0 :         return PLATFORM_FAILED;
     229              :     }
     230              : 
     231            2 :     std::string ini_file_path = cfg_file_real_path + "/" + soc_version + INI_FILE_SUFFIX;
     232            2 :     PF_LOGD("Begin to load ini file[%s].", ini_file_path.c_str());
     233            2 :     if (LoadIniFile(ini_file_path) != PLATFORM_SUCCESS) {
     234            1 :         PF_LOGE("Failed to load ini file[%s].", ini_file_path.c_str());
     235            1 :         return PLATFORM_FAILED;
     236              :     }
     237            1 :     return PLATFORM_SUCCESS;
     238            2 : }
     239              : 
     240            4 : int32_t PlatformManagerV2::GetPlatformInfos(const std::string& soc_version, fe::PlatFormInfos& platform_info)
     241              : {
     242            4 :     std::string realSocVersion = soc_version;
     243            4 :     if (realSocVersion == SOC_VERSION_ASCEND910) {
     244            0 :         realSocVersion = SOC_VERSION_ASCEND910A;
     245              :     }
     246              : 
     247            4 :     std::lock_guard<std::mutex> lock_guard(soc_lock_);
     248            4 :     if (soc_file_status_.count(realSocVersion) == 0) {
     249            2 :         if (InitPlatformInfos(realSocVersion) != PLATFORM_SUCCESS) {
     250            1 :             return PLATFORM_ERROR_PARSE_FILE_FAILED;
     251              :         }
     252              :     }
     253            3 :     soc_file_status_[realSocVersion] = true;
     254            3 :     auto iter = platform_infos_map_.find(realSocVersion);
     255            3 :     if (iter == platform_infos_map_.end()) {
     256            0 :         PF_LOGE("Cannot find platform_info by SoCVersion %s.", realSocVersion.c_str());
     257            0 :         return PLATFORM_ERROR_NOT_FOUND;
     258              :     }
     259            3 :     platform_info = iter->second;
     260            3 :     return PLATFORM_SUCCESS;
     261            4 : }
     262              : 
     263            4 : int32_t PlatformManagerV2::GetSocSpec(
     264              :     const std::string& soc_version, const std::string& label, const std::string& key, std::string& value)
     265              : {
     266            4 :     PF_LOGD("Begin to get soc[%s] info: label[%s], key[%s].", soc_version.c_str(), label.c_str(), key.c_str());
     267            4 :     fe::PlatFormInfos platform_info;
     268            4 :     auto ret = GetPlatformInfos(soc_version, platform_info);
     269            4 :     if (ret != PLATFORM_SUCCESS) {
     270            1 :         return ret;
     271              :     }
     272            3 :     if (!platform_info.GetPlatformResWithLock(label, key, value)) {
     273            2 :         PF_LOGW("Failed to get platform info, label[%s], key[%s].", label.c_str(), key.c_str());
     274            2 :         return PLATFORM_ERROR_NOT_FOUND;
     275              :     }
     276            1 :     PF_LOGD("GetSocSpec label[%s], key[%s], value[%s].", label.c_str(), key.c_str(), value.c_str());
     277            1 :     return PLATFORM_SUCCESS;
     278            4 : }
     279              : 
     280              : #ifdef __cplusplus
     281              : }
     282              : #endif // __cplusplus
        

Generated by: LCOV version 2.0-1