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 7.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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 kEnableEnv = "MS_RDR_ENABLE";
  26. constexpr auto kPathEnv = "MS_RDR_PATH";
  27. constexpr auto kRdrSettings = "rdr";
  28. constexpr auto kPath = "path";
  29. constexpr auto kEnable = "enable";
  30. } // namespace
  31. namespace mindspore {
  32. std::optional<bool> GetRdrEnableFromEnv() {
  33. // get environment variable to configure RDR
  34. const char *env_enable_char = std::getenv(kEnableEnv);
  35. if (env_enable_char != nullptr) {
  36. std::string env_enable_str = env_enable_char;
  37. (void)std::transform(env_enable_str.begin(), env_enable_str.end(), env_enable_str.begin(), ::tolower);
  38. if (env_enable_str != "0" && env_enable_str != "1") {
  39. MS_LOG(WARNING) << "The environment variable '" << kEnableEnv << "' should be 0 or 1.";
  40. }
  41. if (env_enable_str == "1") {
  42. return true;
  43. }
  44. return false;
  45. }
  46. return std::nullopt;
  47. }
  48. std::optional<std::string> GetRdrPathFromEnv() {
  49. // get environment variable to configure RDR
  50. const char *path_char = std::getenv(kPathEnv);
  51. if (path_char != nullptr) {
  52. std::string err_msg = "RDR path parse from environment variable failed. Please check the settings about '" +
  53. std::string(kPathEnv) + "' in environment variables.";
  54. std::string path = path_char;
  55. if (!Common::IsPathValid(path, maxDirectoryLength, err_msg, false)) {
  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) {
  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) {
  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) {
  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. rdr_path_ = path;
  99. }
  100. }
  101. }
  102. void EnvConfigParser::ParseFromFile() {
  103. auto context = MsContext::GetInstance();
  104. MS_EXCEPTION_IF_NULL(context);
  105. auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  106. if (config_file.empty()) {
  107. MS_LOG(INFO) << "The 'env_config_path' in 'mindspore.context.set_context(env_config_path={path})' is empty.";
  108. return;
  109. }
  110. config_file_ = config_file;
  111. std::ifstream json_file(config_file_);
  112. if (!json_file.is_open()) {
  113. MS_LOG(WARNING) << "Env config file:" << config_file_ << " open failed."
  114. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  115. return;
  116. }
  117. nlohmann::json j;
  118. try {
  119. json_file >> j;
  120. } catch (nlohmann::json::parse_error &e) {
  121. MS_LOG(WARNING) << "Env config json contents '" << GetIfstreamString(json_file) << "' in config file '"
  122. << config_file_ << "' set by 'env_config_path' in context.";
  123. return;
  124. }
  125. // convert json to string
  126. std::stringstream ss;
  127. ss << j;
  128. std::string cfg = ss.str();
  129. MS_LOG(INFO) << "Env config json:" << cfg;
  130. // Parse rdr seetings from file
  131. ParseRdrSetting(j);
  132. ConfigToString();
  133. }
  134. void EnvConfigParser::Parse() {
  135. std::lock_guard<std::mutex> guard(lock_);
  136. if (already_parsed_) {
  137. return;
  138. }
  139. already_parsed_ = true;
  140. ParseFromEnv();
  141. ParseFromFile();
  142. }
  143. void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
  144. auto rdr_setting = content.find(kRdrSettings);
  145. if (rdr_setting == content.end()) {
  146. MS_LOG(WARNING) << "The '" << kRdrSettings << "' not exists. Please check the config file '" << config_file_
  147. << "' set by 'env_config_path' in context.";
  148. return;
  149. }
  150. has_rdr_setting_ = true;
  151. auto rdr_enable = CheckJsonKeyExist(*rdr_setting, kRdrSettings, kEnable);
  152. if (rdr_enable.has_value()) {
  153. ParseRdrEnable(**rdr_enable);
  154. }
  155. auto rdr_path = CheckJsonKeyExist(*rdr_setting, kRdrSettings, kPath);
  156. if (rdr_path.has_value()) {
  157. ParseRdrPath(**rdr_path);
  158. }
  159. }
  160. void EnvConfigParser::ParseRdrPath(const nlohmann::json &content) {
  161. std::string err_msg = "RDR path parse failed. The RDR path will be a default value: '" + rdr_path_ +
  162. "'. Please check the settings about '" + kRdrSettings + "' in config file '" + config_file_ +
  163. "' set by 'env_config_path' in context.";
  164. if (!CheckJsonStringType(content, kRdrSettings, kPath)) {
  165. MS_LOG(WARNING) << err_msg;
  166. return;
  167. }
  168. std::string path = content;
  169. if (!Common::IsPathValid(path, maxDirectoryLength, err_msg, false)) {
  170. return;
  171. }
  172. if (path.back() != '/') {
  173. path += '/';
  174. }
  175. rdr_path_ = path;
  176. }
  177. void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
  178. if (!content.is_boolean()) {
  179. MS_LOG(WARNING) << "Json parse failed. 'enable' in " << kRdrSettings << " should be boolean."
  180. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  181. return;
  182. }
  183. rdr_enabled_ = content;
  184. }
  185. void EnvConfigParser::ConfigToString() {
  186. std::string cur_config;
  187. cur_config.append("After parsed, rdr path: ");
  188. cur_config.append(rdr_path_);
  189. cur_config.append(", rdr_enable: ");
  190. cur_config.append(std::to_string(rdr_enabled_));
  191. MS_LOG(INFO) << cur_config;
  192. }
  193. } // namespace mindspore