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(),
84 : jsonRead.dump().c_str());
85 1 : return AICPU_SCHEDULE_OK;
86 3 : }
87 :
88 3 : StatusCode AicpuMemInfoProcess::ParseCfgData(const nlohmann::json &input, BuffCfg &output)
89 : {
90 3 : aicpusd_info("Start parse cfg data!");
91 3 : const auto jsonSize = (input.size() < BUFF_MAX_CFG_NUM) ? input.size() : static_cast<size_t>(BUFF_MAX_CFG_NUM);
92 4 : for (size_t i = 0UL; i < jsonSize; ++i) {
93 3 : std::string key = std::to_string(i);
94 3 : if (!input.contains(key)) {
95 1 : aicpusd_run_info("The key [%s] is not in json content, please check the json file", key.c_str());
96 1 : return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
97 : }
98 : try {
99 2 : (void)input[key].at("cfg_id").get_to(output.cfg[i].cfg_id);
100 2 : (void)input[key].at("total_size").get_to(output.cfg[i].total_size);
101 1 : (void)input[key].at("blk_size").get_to(output.cfg[i].blk_size);
102 1 : (void)input[key].at("max_buf_size").get_to(output.cfg[i].max_buf_size);
103 1 : (void)input[key].at("page_type").get_to(output.cfg[i].page_type);
104 1 : } catch (nlohmann::json::exception &e) {
105 1 : aicpusd_run_info("The keys of json is not correct: [%s]", e.what());
106 1 : return AICPU_SCHEDULE_ERROR_READ_JSON_FAILED;
107 1 : }
108 3 : }
109 1 : return AICPU_SCHEDULE_OK;
110 : }
111 :
112 5 : StatusCode AicpuMemInfoProcess::CheckPathValid(const std::string &cfgFullPath)
113 : {
114 5 : if (cfgFullPath.length() >= static_cast<size_t>(PATH_MAX)) {
115 0 : aicpusd_run_info(
116 : "cfgFullPath file length[%zu] must less than PATH_MAX[%u]", cfgFullPath.length(), PATH_MAX);
117 0 : return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
118 : }
119 :
120 5 : std::unique_ptr<char_t []> path(new (std::nothrow) char_t[PATH_MAX]);
121 5 : if (path == nullptr) {
122 0 : aicpusd_run_info("Alloc memory for path failed.");
123 0 : return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
124 : }
125 :
126 5 : const auto eRet = memset_s(path.get(), PATH_MAX, 0, PATH_MAX);
127 5 : if (eRet != EOK) {
128 1 : aicpusd_run_info("Mem set was not successful, ret=%d", eRet);
129 1 : return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
130 : }
131 :
132 4 : if (realpath(cfgFullPath.data(), path.get()) == nullptr) {
133 3 : aicpusd_run_info("Check cfg file full path:[%s], path:[%s]", cfgFullPath.c_str(), path.get());
134 3 : return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
135 : }
136 1 : const std::string normalPath(path.get());
137 1 : if (normalPath != cfgFullPath) {
138 1 : aicpusd_run_info("Invalid mem cfg file:[%s], should be [%s]", cfgFullPath.c_str(), normalPath.c_str());
139 1 : return AICPU_SCHEDULE_ERROR_GET_PATH_FAILED;
140 : }
141 0 : aicpusd_info("Check mem cfg file [%s] success.", cfgFullPath.c_str());
142 0 : return AICPU_SCHEDULE_OK;
143 5 : }
144 :
145 16 : StatusCode AicpuMemInfoProcess::CheckRunMode()
146 : {
147 : uint32_t runMode;
148 16 : const aicpu::status_t status = aicpu::GetAicpuRunMode(runMode);
149 16 : if (status != aicpu::AICPU_ERROR_NONE) {
150 1 : aicpusd_err("GetAicpuRunMode returned [%u]", status);
151 1 : return AICPU_SCHEDULE_ERROR_GET_RUN_MODE_FAILED;
152 : }
153 15 : if (runMode != static_cast<uint32_t>(aicpu::AicpuRunMode::PROCESS_SOCKET_MODE)) {
154 8 : aicpusd_run_info("Current aicpu mode is not socket_mode, please check!");
155 8 : return AICPU_SCHEDULE_OK;
156 : }
157 7 : return AICPU_SCHEDULE_OK;
158 : }
159 : }
|