You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

env_config_parser.cc 8.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Copyright 2021 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "debug/env_config_parser.h"
  17. #include <algorithm>
  18. #include <fstream>
  19. #include "nlohmann/json.hpp"
  20. #include "utils/log_adapter.h"
  21. #include "debug/common.h"
  22. #include "utils/ms_context.h"
  23. #include "utils/convert_utils_base.h"
  24. namespace {
  25. constexpr auto ENV_RDR_ENABLE = "MS_RDR_ENABLE";
  26. constexpr auto ENV_RDR_PATH = "MS_RDR_PATH";
  27. constexpr auto KEY_RDR_SETTINGS = "rdr";
  28. constexpr auto KEY_PATH = "path";
  29. constexpr auto KEY_ENABLE = "enable";
  30. constexpr auto KEY_MEM_REUSE_SETTINGS = "sys";
  31. constexpr auto KEY_MEM_REUSE = "mem_reuse";
  32. } // namespace
  33. namespace mindspore {
  34. std::optional<bool> GetRdrEnableFromEnv() {
  35. // get environment variable to configure RDR
  36. std::string env_enable_str = common::GetEnv(ENV_RDR_ENABLE);
  37. if (!env_enable_str.empty()) {
  38. (void)std::transform(env_enable_str.begin(), env_enable_str.end(), env_enable_str.begin(), ::tolower);
  39. if (env_enable_str != "0" && env_enable_str != "1") {
  40. MS_LOG(WARNING) << "The environment variable '" << ENV_RDR_ENABLE << "' should be 0 or 1.";
  41. }
  42. if (env_enable_str == "1") {
  43. return true;
  44. }
  45. return false;
  46. }
  47. return std::nullopt;
  48. }
  49. std::optional<std::string> GetRdrPathFromEnv() {
  50. // get environment variable to configure RDR
  51. std::string path = common::GetEnv(ENV_RDR_PATH);
  52. if (!path.empty()) {
  53. std::string err_msg = "RDR path parse from environment variable failed. Please check the settings about '" +
  54. std::string(ENV_RDR_PATH) + "' in environment variables.";
  55. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  56. return std::string("");
  57. }
  58. return path;
  59. }
  60. return std::nullopt;
  61. }
  62. bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key,
  63. const std::string &key) const {
  64. if (!content.is_string()) {
  65. MS_LOG(WARNING) << "Json Parse Failed. The '" << key << "' in '" << setting_key << "' should be string."
  66. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  67. return false;
  68. }
  69. return true;
  70. }
  71. std::optional<nlohmann::detail::iter_impl<const nlohmann::json>> EnvConfigParser::CheckJsonKeyExist(
  72. const nlohmann::json &content, const std::string &setting_key, const std::string &key) const {
  73. auto iter = content.find(key);
  74. if (iter == content.end()) {
  75. MS_LOG(WARNING) << "Check json failed, '" << key << "' not found in '" << setting_key << "'."
  76. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  77. return std::nullopt;
  78. }
  79. return iter;
  80. }
  81. std::string EnvConfigParser::GetIfstreamString(const std::ifstream &ifstream) const {
  82. std::stringstream buffer;
  83. buffer << ifstream.rdbuf();
  84. return buffer.str();
  85. }
  86. void EnvConfigParser::ParseFromEnv() {
  87. // Get RDR seetings from environment variables
  88. auto rdr_enable_env = GetRdrEnableFromEnv();
  89. if (rdr_enable_env.has_value()) {
  90. has_rdr_setting_ = true;
  91. rdr_enabled_ = rdr_enable_env.value();
  92. }
  93. auto path_env = GetRdrPathFromEnv();
  94. if (path_env.has_value()) {
  95. has_rdr_setting_ = true;
  96. std::string path = path_env.value();
  97. if (!path.empty()) {
  98. if (path.back() != '/') {
  99. path += '/';
  100. }
  101. rdr_path_ = path;
  102. }
  103. }
  104. }
  105. void EnvConfigParser::ParseFromFile() {
  106. auto context = MsContext::GetInstance();
  107. MS_EXCEPTION_IF_NULL(context);
  108. auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  109. if (config_file.empty()) {
  110. MS_LOG(INFO) << "The 'env_config_path' in 'mindspore.context.set_context(env_config_path={path})' is empty.";
  111. return;
  112. }
  113. config_file_ = config_file;
  114. std::ifstream json_file(config_file_);
  115. if (!json_file.is_open()) {
  116. MS_LOG(WARNING) << "Env config file:" << config_file_ << " open failed."
  117. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  118. return;
  119. }
  120. nlohmann::json j;
  121. try {
  122. json_file >> j;
  123. } catch (nlohmann::json::parse_error &e) {
  124. MS_LOG(WARNING) << "Env config json contents '" << GetIfstreamString(json_file) << "' in config file '"
  125. << config_file_ << "' set by 'env_config_path' in context.";
  126. return;
  127. }
  128. // convert json to string
  129. std::stringstream ss;
  130. ss << j;
  131. std::string cfg = ss.str();
  132. MS_LOG(INFO) << "Env config json:" << cfg;
  133. // Parse rdr seetings from file
  134. ParseRdrSetting(j);
  135. ParseMemReuseSetting(j);
  136. ConfigToString();
  137. }
  138. void EnvConfigParser::Parse() {
  139. std::lock_guard<std::mutex> guard(lock_);
  140. if (already_parsed_) {
  141. return;
  142. }
  143. already_parsed_ = true;
  144. ParseFromEnv();
  145. ParseFromFile();
  146. }
  147. void EnvConfigParser::ParseMemReuseSetting(const nlohmann::json &content) {
  148. auto sys_setting = content.find(KEY_MEM_REUSE_SETTINGS);
  149. if (sys_setting == content.end()) {
  150. MS_LOG(INFO) << "The '" << KEY_MEM_REUSE_SETTINGS << "' isn't existed. Please check the config file '"
  151. << config_file_ << "' set by 'env_config_path' in context.";
  152. return;
  153. }
  154. auto sys_memreuse = CheckJsonKeyExist(*sys_setting, KEY_MEM_REUSE_SETTINGS, KEY_MEM_REUSE);
  155. if (sys_memreuse.has_value()) {
  156. ParseSysMemReuse(**sys_memreuse);
  157. }
  158. }
  159. void EnvConfigParser::ParseSysMemReuse(const nlohmann::json &content) {
  160. if (!content.is_boolean()) {
  161. MS_LOG(INFO) << "the json object parses failed. 'enable' in " << KEY_MEM_REUSE_SETTINGS << " should be boolean."
  162. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  163. return;
  164. }
  165. sys_memreuse_ = content;
  166. }
  167. void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
  168. auto rdr_setting = content.find(KEY_RDR_SETTINGS);
  169. if (rdr_setting == content.end()) {
  170. MS_LOG(WARNING) << "The '" << KEY_RDR_SETTINGS << "' not exists. Please check the config file '" << config_file_
  171. << "' set by 'env_config_path' in context.";
  172. return;
  173. }
  174. has_rdr_setting_ = true;
  175. auto rdr_enable = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_ENABLE);
  176. if (rdr_enable.has_value()) {
  177. ParseRdrEnable(**rdr_enable);
  178. }
  179. auto rdr_path = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_PATH);
  180. if (rdr_path.has_value()) {
  181. ParseRdrPath(**rdr_path);
  182. }
  183. }
  184. void EnvConfigParser::ParseRdrPath(const nlohmann::json &content) {
  185. std::string err_msg = "RDR path parse failed. The RDR path will be a default value: '" + rdr_path_ +
  186. "'. Please check the settings about '" + KEY_RDR_SETTINGS + "' in config file '" +
  187. config_file_ + "' set by 'env_config_path' in context.";
  188. if (!CheckJsonStringType(content, KEY_RDR_SETTINGS, KEY_PATH)) {
  189. MS_LOG(WARNING) << err_msg;
  190. return;
  191. }
  192. std::string path = content;
  193. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  194. return;
  195. }
  196. if (path.back() != '/') {
  197. path += '/';
  198. }
  199. rdr_path_ = path;
  200. }
  201. void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
  202. if (!content.is_boolean()) {
  203. MS_LOG(WARNING) << "Json parse failed. 'enable' in " << KEY_RDR_SETTINGS << " should be boolean."
  204. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  205. return;
  206. }
  207. rdr_enabled_ = content;
  208. }
  209. void EnvConfigParser::ConfigToString() {
  210. std::string cur_config;
  211. cur_config.append("After parsed, rdr path: ");
  212. cur_config.append(rdr_path_);
  213. cur_config.append(", rdr_enable: ");
  214. std::string rdr_enable_flag = rdr_enabled_ ? "1" : "0";
  215. (void)cur_config.append(rdr_enable_flag);
  216. MS_LOG(INFO) << cur_config;
  217. }
  218. } // namespace mindspore