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 "common/config/configurations.h"
12 : #include "common/config/dflow_deployer_json_parser.h"
13 : #include "common/debug/log.h"
14 : #include "common/config/config_parser.h"
15 : #include "dflow/base/utils/process_utils.h"
16 : #include "common/checker.h"
17 : #include "framework/common/ge_types.h"
18 : #include "graph/ge_context.h"
19 : #include "graph_metadef/graph/utils/file_utils.h"
20 :
21 : namespace ge {
22 : namespace {
23 : const char_t *const kResourceConfigPath = "RESOURCE_CONFIG_PATH";
24 : const char_t *const kHomeEnvName = "HOME";
25 : } // namespace
26 :
27 : Configurations &Configurations::GetInstance() {
28 : static Configurations instance;
29 : return instance;
30 : }
31 :
32 : void Configurations::Finalize() {
33 : information_ = {};
34 : GELOGI("Finalize success, remote node size = %zu", information_.remote_node_config_list.size());
35 : }
36 :
37 : Status Configurations::GetWorkingDir(std::string &working_dir) const {
38 : if (!information_.node_config.deploy_res_path.empty()) {
39 : working_dir = information_.node_config.deploy_res_path;
40 : GELOGI("Get working dir success, path = %s", working_dir.c_str());
41 : return SUCCESS;
42 : }
43 :
44 : std::string ascend_work_path;
45 : GE_ASSERT_SUCCESS(GetAscendWorkPath(ascend_work_path));
46 : if (!ascend_work_path.empty()) {
47 : working_dir = ascend_work_path;
48 : } else {
49 : working_dir = GetHostDirByEnv();
50 : }
51 : GE_CHK_BOOL_RET_STATUS(!working_dir.empty(), ACL_ERROR_GE_PARAM_INVALID, "Env HOME don't exist.");
52 : GELOGI("Get working dir success, path = %s", working_dir.c_str());
53 : return SUCCESS;
54 : }
55 :
56 39 : std::string Configurations::GetDeployResDir() const {
57 : return information_.working_dir + "/runtime/deploy_res/";
58 : }
59 :
60 : std::string Configurations::GetHostDirByEnv() {
61 : const char_t *res_path = nullptr;
62 : MM_SYS_GET_ENV(MM_ENV_HOME, res_path);
63 : if (res_path == nullptr) {
64 : return "";
65 : }
66 : const std::string real_path = RealPath(res_path);
67 : if (real_path.empty()) {
68 : GELOGW("The path[%s] of env[%s] is invalid", res_path, kHomeEnvName);
69 : return "";
70 : }
71 : if (ProcessUtils::IsValidPath(real_path) != SUCCESS) {
72 : GELOGW("env %s config value[%s] real path[%s] is invalid", kHomeEnvName, res_path, real_path.c_str());
73 : return "";
74 : }
75 : GELOGI("Get host dir success from env[%s], path = %s", kHomeEnvName, real_path.c_str());
76 : return real_path;
77 : }
78 :
79 : std::vector<NodeConfig> Configurations::GetAllNodeConfigs() const {
80 : std::vector<NodeConfig> configs{information_.node_config};
81 : configs.insert(configs.cend(), information_.remote_node_config_list.cbegin(),
82 : information_.remote_node_config_list.cend());
83 : std::sort(configs.begin(), configs.end(),
84 : [](const NodeConfig &lhs, const NodeConfig &rhs) -> bool { return (lhs.node_id < rhs.node_id); });
85 : return configs;
86 : }
87 :
88 : const std::vector<NodeConfig> &Configurations::GetRemoteNodeConfigs() const {
89 : return information_.remote_node_config_list;
90 : }
91 :
92 : Status Configurations::GetResourceConfigPath(std::string &config_dir) {
93 : std::string resource_path;
94 : const char_t *file_path = nullptr;
95 : MM_SYS_GET_ENV(MM_ENV_RESOURCE_CONFIG_PATH, file_path);
96 : if (file_path == nullptr) {
97 : (void)ge::GetContext().GetOption(RESOURCE_CONFIG_PATH, resource_path);
98 : GE_CHK_BOOL_RET_STATUS(!resource_path.empty(), ACL_ERROR_GE_PARAM_INVALID,
99 : "Env RESOURCE_CONFIG_PATH or option[%s] does not exist.", RESOURCE_CONFIG_PATH);
100 : GEEVENT("Get config dir[%s] success from option[%s]", resource_path.c_str(), RESOURCE_CONFIG_PATH);
101 : } else {
102 : resource_path = file_path;
103 : GEEVENT("Get config dir[%s] success from env[%s]", resource_path.c_str(), kResourceConfigPath);
104 : }
105 : const std::string real_path = RealPath(resource_path.c_str());
106 : GE_CHK_BOOL_RET_STATUS(!real_path.empty(), ACL_ERROR_GE_PARAM_INVALID, "The path[%s] is invalid",
107 : resource_path.c_str());
108 : config_dir = resource_path;
109 : return SUCCESS;
110 : }
111 :
112 : Status Configurations::InitInformation() {
113 : information_ = {};
114 : std::string file_path;
115 : GE_CHK_STATUS_RET_NOLOG(GetResourceConfigPath(file_path));
116 : GE_CHK_STATUS_RET_NOLOG(ConfigParser::ParseServerInfo(file_path, information_));
117 : GE_CHK_STATUS_RET_NOLOG(GetWorkingDir(information_.working_dir));
118 : return SUCCESS;
119 : }
120 :
121 : const NodeConfig &Configurations::GetLocalNode() const {
122 : return information_.node_config;
123 : }
124 :
125 : std::vector<std::string> Configurations::GetHeterogeneousEnvs() {
126 : static std::vector<std::string> envs = {kResourceConfigPath};
127 : return envs;
128 : }
129 : } // namespace ge
|