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.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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. const char *env_enable_char = std::getenv(ENV_RDR_ENABLE);
  37. if (env_enable_char != nullptr) {
  38. std::string env_enable_str = std::string(env_enable_char);
  39. (void)std::transform(env_enable_str.begin(), env_enable_str.end(), env_enable_str.begin(), ::tolower);
  40. if (env_enable_str != "0" && env_enable_str != "1") {
  41. MS_LOG(WARNING) << "The environment variable '" << ENV_RDR_ENABLE << "' should be 0 or 1.";
  42. }
  43. if (env_enable_str == "1") {
  44. return true;
  45. }
  46. return false;
  47. }
  48. return std::nullopt;
  49. }
  50. std::optional<std::string> GetRdrPathFromEnv() {
  51. // get environment variable to configure RDR
  52. const char *path_char = std::getenv(ENV_RDR_PATH);
  53. if (path_char != nullptr) {
  54. std::string err_msg = "RDR path parse from environment variable failed. Please check the settings about '" +
  55. std::string(ENV_RDR_PATH) + "' in environment variables.";
  56. std::string path = std::string(path_char);
  57. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  58. return std::string("");
  59. }
  60. return path;
  61. }
  62. return std::nullopt;
  63. }
  64. bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key,
  65. const std::string &key) {
  66. if (!content.is_string()) {
  67. MS_LOG(WARNING) << "Json Parse Failed. The '" << key << "' in '" << setting_key << "' should be string."
  68. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  69. return false;
  70. }
  71. return true;
  72. }
  73. std::optional<nlohmann::detail::iter_impl<const nlohmann::json>> EnvConfigParser::CheckJsonKeyExist(
  74. const nlohmann::json &content, const std::string &setting_key, const std::string &key) {
  75. auto iter = content.find(key);
  76. if (iter == content.end()) {
  77. MS_LOG(WARNING) << "Check json failed, '" << key << "' not found in '" << setting_key << "'."
  78. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  79. return std::nullopt;
  80. }
  81. return iter;
  82. }
  83. std::string EnvConfigParser::GetIfstreamString(const std::ifstream &ifstream) {
  84. std::stringstream buffer;
  85. buffer << ifstream.rdbuf();
  86. return buffer.str();
  87. }
  88. void EnvConfigParser::ParseFromEnv() {
  89. // Get RDR seetings from environment variables
  90. auto rdr_enable_env = GetRdrEnableFromEnv();
  91. if (rdr_enable_env.has_value()) {
  92. has_rdr_setting_ = true;
  93. rdr_enabled_ = rdr_enable_env.value();
  94. }
  95. auto path_env = GetRdrPathFromEnv();
  96. if (path_env.has_value()) {
  97. has_rdr_setting_ = true;
  98. std::string path = path_env.value();
  99. if (!path.empty()) {
  100. if (path.back() != '/') {
  101. path += '/';
  102. }
  103. rdr_path_ = path;
  104. }
  105. }
  106. }
  107. void EnvConfigParser::ParseFromFile() {
  108. auto context = MsContext::GetInstance();
  109. MS_EXCEPTION_IF_NULL(context);
  110. auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  111. if (config_file.empty()) {
  112. MS_LOG(INFO) << "The 'env_config_path' in 'mindspore.context.set_context(env_config_path={path})' is empty.";
  113. return;
  114. }
  115. config_file_ = config_file;
  116. std::ifstream json_file(config_file_);
  117. if (!json_file.is_open()) {
  118. MS_LOG(WARNING) << "Env config file:" << config_file_ << " open failed."
  119. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  120. return;
  121. }
  122. nlohmann::json j;
  123. try {
  124. json_file >> j;
  125. } catch (nlohmann::json::parse_error &e) {
  126. MS_LOG(WARNING) << "Env config json contents '" << GetIfstreamString(json_file) << "' in config file '"
  127. << config_file_ << "' set by 'env_config_path' in context.";
  128. return;
  129. }
  130. // convert json to string
  131. std::stringstream ss;
  132. ss << j;
  133. std::string cfg = ss.str();
  134. MS_LOG(INFO) << "Env config json:" << cfg;
  135. // Parse rdr seetings from file
  136. ParseRdrSetting(j);
  137. ParseMemReuseSetting(j);
  138. ConfigToString();
  139. }
  140. void EnvConfigParser::Parse() {
  141. std::lock_guard<std::mutex> guard(lock_);
  142. if (already_parsed_) {
  143. return;
  144. }
  145. already_parsed_ = true;
  146. ParseFromEnv();
  147. ParseFromFile();
  148. }
  149. void EnvConfigParser::ParseMemReuseSetting(const nlohmann::json &content) {
  150. auto sys_setting = content.find(KEY_MEM_REUSE_SETTINGS);
  151. if (sys_setting == content.end()) {
  152. MS_LOG(INFO) << "The '" << KEY_MEM_REUSE_SETTINGS << "' isn't existed. Please check the config file '"
  153. << config_file_ << "' set by 'env_config_path' in context.";
  154. return;
  155. }
  156. auto sys_memreuse = CheckJsonKeyExist(*sys_setting, KEY_MEM_REUSE_SETTINGS, KEY_MEM_REUSE);
  157. if (sys_memreuse.has_value()) {
  158. ParseSysMemReuse(**sys_memreuse);
  159. }
  160. }
  161. void EnvConfigParser::ParseSysMemReuse(const nlohmann::json &content) {
  162. if (!content.is_boolean()) {
  163. MS_LOG(INFO) << "the json object parses failed. 'enable' in " << KEY_MEM_REUSE_SETTINGS << " should be boolean."
  164. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  165. return;
  166. }
  167. sys_memreuse_ = content;
  168. }
  169. void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
  170. auto rdr_setting = content.find(KEY_RDR_SETTINGS);
  171. if (rdr_setting == content.end()) {
  172. MS_LOG(WARNING) << "The '" << KEY_RDR_SETTINGS << "' not exists. Please check the config file '" << config_file_
  173. << "' set by 'env_config_path' in context.";
  174. return;
  175. }
  176. has_rdr_setting_ = true;
  177. auto rdr_enable = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_ENABLE);
  178. if (rdr_enable.has_value()) {
  179. ParseRdrEnable(**rdr_enable);
  180. }
  181. auto rdr_path = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_PATH);
  182. if (rdr_path.has_value()) {
  183. ParseRdrPath(**rdr_path);
  184. }
  185. }
  186. void EnvConfigParser::ParseRdrPath(const nlohmann::json &content) {
  187. std::string err_msg = "RDR path parse failed. The RDR path will be a default value: '" + rdr_path_ +
  188. "'. Please check the settings about '" + KEY_RDR_SETTINGS + "' in config file '" +
  189. config_file_ + "' set by 'env_config_path' in context.";
  190. if (!CheckJsonStringType(content, KEY_RDR_SETTINGS, KEY_PATH)) {
  191. MS_LOG(WARNING) << err_msg;
  192. return;
  193. }
  194. std::string path = content;
  195. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  196. return;
  197. }
  198. if (path.back() != '/') {
  199. path += '/';
  200. }
  201. rdr_path_ = path;
  202. }
  203. void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
  204. if (!content.is_boolean()) {
  205. MS_LOG(WARNING) << "Json parse failed. 'enable' in " << KEY_RDR_SETTINGS << " should be boolean."
  206. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  207. return;
  208. }
  209. rdr_enabled_ = content;
  210. }
  211. void EnvConfigParser::ConfigToString() {
  212. std::string cur_config;
  213. cur_config.append("After parsed, rdr path: ");
  214. cur_config.append(rdr_path_);
  215. cur_config.append(", rdr_enable: ");
  216. cur_config.append(std::to_string(rdr_enabled_));
  217. MS_LOG(INFO) << cur_config;
  218. }
  219. } // namespace mindspore