LCOV - code coverage report
Current view: top level - aicpu_schedule/core - aicpusd_meminfo_process.cpp (source / functions) Coverage Total Hit
Test: coverage.info Lines: 93.8 % 97 91
Test Date: 2026-08-07 10:45:47 Functions: 100.0 % 5 5

            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 <fstream>
      12              : #include <climits>
      13              : #include <cstdlib>
      14              : #include <securec.h>
      15              : #include "ascend_hal_define.h"
      16              : #include "aicpusd_util.h"
      17              : #include "aicpusd_drv_manager.h"
      18              : #include "aicpu_context.h"
      19              : #include "aicpusd_meminfo_process.h"
      20              : 
      21              : namespace AicpuSchedule {
      22           21 : StatusCode AicpuMemInfoProcess::GetMemZoneInfo(BuffCfg& buffCfg)
      23              : {
      24           21 :     aicpusd_info("Start get memzone info!");
      25           21 :     buffCfg = {}; // default
      26           21 :     auto ret = CheckRunMode();
      27           21 :     if (ret != AICPU_SCHEDULE_OK) {
      28            1 :         return ret;
      29              :     }
      30              : 
      31           20 :     std::string blockModePath = "";
      32           20 :     const bool envRet = AicpuUtil::GetEnvVal(ENV_NAME_BLOCK_CFG_PATH, blockModePath);
      33           20 :     if (!envRet) {
      34           13 :         aicpusd_run_info("The pointer of BLOCK_CFG_PATH is nullptr");
      35           13 :         return AICPU_SCHEDULE_OK;
      36              :     }
      37              : 
      38            7 :     if ((blockModePath.size() > 0U) && (blockModePath[blockModePath.size() - 1U] != '/')) {
      39            7 :         (void)blockModePath.append("/");
      40              :     }
      41              : 
      42            7 :     const std::string procName = AicpuDrvManager::GetInstance().GetHostProcName();
      43            7 :     const std::string memBuffCfgFile = blockModePath + "aifmk/" + procName + ".json";
      44            7 :     ret = CheckPathValid(memBuffCfgFile);
      45            7 :     if (ret != AICPU_SCHEDULE_OK) {
      46            3 :         aicpusd_run_info("The memBufCfgFile path is invalid: [%s]!", memBuffCfgFile.c_str());
      47            3 :         return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
      48              :     }
      49              : 
      50            4 :     nlohmann::json jsonRead;
      51            4 :     ret = ReadJsonFile(memBuffCfgFile, jsonRead);
      52            4 :     if (ret != AICPU_SCHEDULE_OK) {
      53            2 :         aicpusd_run_info("Execute ReadJsonFile returned [%d].", ret);
      54            2 :         return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
      55              :     }
      56            2 :     ret = ParseCfgData(jsonRead, buffCfg);
      57            2 :     if (ret != AICPU_SCHEDULE_OK) {
      58            1 :         aicpusd_run_info("Execute ParseCfgData returned [%d].", ret);
      59            1 :         return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
      60              :     }
      61            1 :     return AICPU_SCHEDULE_OK;
      62           20 : }
      63              : 
      64            3 : StatusCode AicpuMemInfoProcess::ReadJsonFile(const std::string& filePath, nlohmann::json& jsonRead)
      65              : {
      66            3 :     aicpusd_info("Read [%s] file", filePath.c_str());
      67            3 :     std::ifstream ifs(filePath);
      68            3 :     if (!ifs.is_open()) {
      69            1 :         aicpusd_run_info("Cant not open [%s], please check!", filePath.c_str());
      70            1 :         return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
      71              :     }
      72              :     try {
      73            2 :         ifs >> jsonRead;
      74            1 :         ifs.close();
      75            1 :     } catch (const nlohmann::json::exception& e) {
      76            1 :         if (ifs.is_open()) {
      77            1 :             ifs.close();
      78              :         }
      79            1 :         aicpusd_run_info("Exception: [%s]", e.what());
      80            1 :         return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
      81            1 :     }
      82              : 
      83            1 :     aicpusd_info("Read [%s] file successfully, content is: [%s].", filePath.c_str(), jsonRead.dump().c_str());
      84            1 :     return AICPU_SCHEDULE_OK;
      85            3 : }
      86              : 
      87            3 : StatusCode AicpuMemInfoProcess::ParseCfgData(const nlohmann::json& input, BuffCfg& output)
      88              : {
      89            3 :     aicpusd_info("Start parse cfg data!");
      90            3 :     const auto jsonSize = (input.size() < BUFF_MAX_CFG_NUM) ? input.size() : static_cast<size_t>(BUFF_MAX_CFG_NUM);
      91            4 :     for (size_t i = 0UL; i < jsonSize; ++i) {
      92            3 :         std::string key = std::to_string(i);
      93            3 :         if (!input.contains(key)) {
      94            1 :             aicpusd_run_info("The key [%s] is not in json content, please check the json file", key.c_str());
      95            1 :             return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
      96              :         }
      97              :         try {
      98            2 :             (void)input[key].at("cfg_id").get_to(output.cfg[i].cfg_id);
      99            2 :             (void)input[key].at("total_size").get_to(output.cfg[i].total_size);
     100            1 :             (void)input[key].at("blk_size").get_to(output.cfg[i].blk_size);
     101            1 :             (void)input[key].at("max_buf_size").get_to(output.cfg[i].max_buf_size);
     102            1 :             (void)input[key].at("page_type").get_to(output.cfg[i].page_type);
     103            1 :         } catch (nlohmann::json::exception& e) {
     104            1 :             aicpusd_run_info("The keys of json is not correct: [%s]", e.what());
     105            1 :             return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
     106            1 :         }
     107            3 :     }
     108            1 :     return AICPU_SCHEDULE_OK;
     109              : }
     110              : 
     111            5 : StatusCode AicpuMemInfoProcess::CheckPathValid(const std::string& cfgFullPath)
     112              : {
     113            5 :     if (cfgFullPath.length() >= static_cast<size_t>(PATH_MAX)) {
     114            0 :         aicpusd_run_info("cfgFullPath file length[%zu] must less than PATH_MAX[%u]", cfgFullPath.length(), PATH_MAX);
     115            0 :         return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
     116              :     }
     117              : 
     118            5 :     std::unique_ptr<char_t[]> path(new (std::nothrow) char_t[PATH_MAX]);
     119            5 :     if (path == nullptr) {
     120            0 :         aicpusd_run_info("Alloc memory for path failed.");
     121            0 :         return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
     122              :     }
     123              : 
     124            5 :     const auto eRet = memset_s(path.get(), PATH_MAX, 0, PATH_MAX);
     125            5 :     if (eRet != EOK) {
     126            1 :         aicpusd_run_info("Mem set was not successful, ret=%d", eRet);
     127            1 :         return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
     128              :     }
     129              : 
     130            4 :     if (realpath(cfgFullPath.data(), path.get()) == nullptr) {
     131            3 :         aicpusd_run_info("Check cfg file full path:[%s], path:[%s]", cfgFullPath.c_str(), path.get());
     132            3 :         return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
     133              :     }
     134            1 :     const std::string normalPath(path.get());
     135            1 :     if (normalPath != cfgFullPath) {
     136            1 :         aicpusd_run_info("Invalid mem cfg file:[%s], should be [%s]", cfgFullPath.c_str(), normalPath.c_str());
     137            1 :         return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
     138              :     }
     139            0 :     aicpusd_info("Check mem cfg file [%s] success.", cfgFullPath.c_str());
     140            0 :     return AICPU_SCHEDULE_OK;
     141            5 : }
     142              : 
     143           16 : StatusCode AicpuMemInfoProcess::CheckRunMode()
     144              : {
     145              :     uint32_t runMode;
     146           16 :     const aicpu::status_t status = aicpu::GetAicpuRunMode(runMode);
     147           16 :     if (status != aicpu::AICPU_ERROR_NONE) {
     148            1 :         aicpusd_err("GetAicpuRunMode returned [%u]", status);
     149            1 :         return AICPU_SCHEDULE_ERROR_GET_RUN_MODE_FAILED;
     150              :     }
     151           15 :     if (runMode != static_cast<uint32_t>(aicpu::AicpuRunMode::PROCESS_SOCKET_MODE)) {
     152            8 :         aicpusd_run_info("Current aicpu mode is not socket_mode, please check!");
     153            8 :         return AICPU_SCHEDULE_OK;
     154              :     }
     155            7 :     return AICPU_SCHEDULE_OK;
     156              : }
     157              : } // namespace AicpuSchedule
        

Generated by: LCOV version 2.0-1