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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. #ifdef ENABLE_DUMP_IR
  26. constexpr auto ENV_RDR_ENABLE = "MS_RDR_ENABLE";
  27. constexpr auto ENV_RDR_MODE = "MS_RDR_MODE";
  28. constexpr auto ENV_RDR_PATH = "MS_RDR_PATH";
  29. constexpr auto KEY_RDR_SETTINGS = "rdr";
  30. constexpr auto KEY_ENABLE = "enable";
  31. constexpr auto KEY_MODE = "mode";
  32. constexpr auto KEY_PATH = "path";
  33. #endif
  34. constexpr auto KEY_MEM_REUSE_SETTINGS = "sys";
  35. constexpr auto KEY_MEM_REUSE = "mem_reuse";
  36. } // namespace
  37. namespace mindspore {
  38. #ifdef ENABLE_DUMP_IR
  39. std::optional<bool> GetRdrEnableFromEnv() {
  40. // get environment variable to configure RDR
  41. std::string env_enable_str = common::GetEnv(ENV_RDR_ENABLE);
  42. if (!env_enable_str.empty()) {
  43. (void)std::transform(env_enable_str.begin(), env_enable_str.end(), env_enable_str.begin(), ::tolower);
  44. if (env_enable_str != "0" && env_enable_str != "1") {
  45. MS_LOG(WARNING) << "The environment variable '" << ENV_RDR_ENABLE << "' should be 0 or 1.";
  46. }
  47. if (env_enable_str == "1") {
  48. return true;
  49. }
  50. return false;
  51. }
  52. return std::nullopt;
  53. }
  54. std::optional<int> GetRdrModeFromEnv() {
  55. // get environment variable to configure RDR
  56. std::string env_mode_str = common::GetEnv(ENV_RDR_MODE);
  57. if (!env_mode_str.empty()) {
  58. (void)std::transform(env_mode_str.begin(), env_mode_str.end(), env_mode_str.begin(), ::tolower);
  59. if (env_mode_str != "1" && env_mode_str != "2") {
  60. MS_LOG(WARNING) << "The environment variable '" << ENV_RDR_MODE << "' should be 1 or 2.";
  61. }
  62. if (env_mode_str == "2") {
  63. return Normal;
  64. }
  65. return Exceptional;
  66. }
  67. return std::nullopt;
  68. }
  69. std::optional<std::string> GetRdrPathFromEnv() {
  70. // get environment variable to configure RDR
  71. std::string path = common::GetEnv(ENV_RDR_PATH);
  72. if (!path.empty()) {
  73. std::string err_msg = "RDR path parse from environment variable failed. Please check the settings about '" +
  74. std::string(ENV_RDR_PATH) + "' in environment variables.";
  75. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  76. return std::string("");
  77. }
  78. return path;
  79. }
  80. return std::nullopt;
  81. }
  82. #endif
  83. bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key,
  84. const std::string &key) const {
  85. if (!content.is_string()) {
  86. MS_LOG(WARNING) << "Json Parse Failed. The '" << key << "' in '" << setting_key << "' should be string."
  87. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  88. return false;
  89. }
  90. return true;
  91. }
  92. std::optional<nlohmann::detail::iter_impl<const nlohmann::json>> EnvConfigParser::CheckJsonKeyExist(
  93. const nlohmann::json &content, const std::string &setting_key, const std::string &key) const {
  94. auto iter = content.find(key);
  95. if (iter == content.end()) {
  96. MS_LOG(WARNING) << "Check json failed, '" << key << "' not found in '" << setting_key << "'."
  97. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  98. return std::nullopt;
  99. }
  100. return iter;
  101. }
  102. std::string EnvConfigParser::GetIfstreamString(const std::ifstream &ifstream) const {
  103. std::stringstream buffer;
  104. buffer << ifstream.rdbuf();
  105. return buffer.str();
  106. }
  107. void EnvConfigParser::ParseFromEnv() {
  108. #ifdef ENABLE_DUMP_IR
  109. // Get RDR seetings from environment variables
  110. auto rdr_enable_env = GetRdrEnableFromEnv();
  111. if (rdr_enable_env.has_value()) {
  112. has_rdr_setting_ = true;
  113. rdr_enabled_ = rdr_enable_env.value();
  114. }
  115. auto rdr_mode_env = GetRdrModeFromEnv();
  116. if (rdr_mode_env.has_value()) {
  117. has_rdr_setting_ = true;
  118. rdr_mode_ = rdr_mode_env.value();
  119. }
  120. auto path_env = GetRdrPathFromEnv();
  121. if (path_env.has_value()) {
  122. has_rdr_setting_ = true;
  123. std::string path = path_env.value();
  124. if (!path.empty()) {
  125. if (path.back() != '/') {
  126. path += '/';
  127. }
  128. rdr_path_ = path;
  129. }
  130. }
  131. #endif
  132. }
  133. void EnvConfigParser::ParseFromFile() {
  134. auto context = MsContext::GetInstance();
  135. MS_EXCEPTION_IF_NULL(context);
  136. auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  137. if (config_file.empty()) {
  138. MS_LOG(INFO) << "The 'env_config_path' in 'mindspore.context.set_context(env_config_path={path})' is empty.";
  139. return;
  140. }
  141. config_file_ = config_file;
  142. std::ifstream json_file(config_file_);
  143. if (!json_file.is_open()) {
  144. MS_LOG(WARNING) << "Env config file:" << config_file_ << " open failed."
  145. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context."
  146. << ErrnoToString(errno);
  147. return;
  148. }
  149. nlohmann::json j;
  150. try {
  151. json_file >> j;
  152. } catch (nlohmann::json::parse_error &e) {
  153. MS_LOG(WARNING) << "Env config json contents '" << GetIfstreamString(json_file) << "' in config file '"
  154. << config_file_ << "' set by 'env_config_path' in context.";
  155. return;
  156. }
  157. // convert json to string
  158. std::stringstream ss;
  159. ss << j;
  160. std::string cfg = ss.str();
  161. MS_LOG(INFO) << "Env config json:" << cfg;
  162. #ifdef ENABLE_DUMP_IR
  163. ParseRdrSetting(j);
  164. #endif
  165. ParseMemReuseSetting(j);
  166. ConfigToString();
  167. }
  168. void EnvConfigParser::Parse() {
  169. std::lock_guard<std::mutex> guard(lock_);
  170. if (already_parsed_) {
  171. return;
  172. }
  173. already_parsed_ = true;
  174. ParseFromEnv();
  175. ParseFromFile();
  176. }
  177. void EnvConfigParser::ParseMemReuseSetting(const nlohmann::json &content) {
  178. auto sys_setting = content.find(KEY_MEM_REUSE_SETTINGS);
  179. if (sys_setting == content.end()) {
  180. MS_LOG(INFO) << "The '" << KEY_MEM_REUSE_SETTINGS << "' isn't existed. Please check the config file '"
  181. << config_file_ << "' set by 'env_config_path' in context.";
  182. return;
  183. }
  184. auto sys_memreuse = CheckJsonKeyExist(*sys_setting, KEY_MEM_REUSE_SETTINGS, KEY_MEM_REUSE);
  185. if (sys_memreuse.has_value()) {
  186. ParseSysMemReuse(**sys_memreuse);
  187. }
  188. }
  189. void EnvConfigParser::ParseSysMemReuse(const nlohmann::json &content) {
  190. if (!content.is_boolean()) {
  191. MS_LOG(INFO) << "the json object parses failed. 'enable' in " << KEY_MEM_REUSE_SETTINGS << " should be boolean."
  192. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  193. return;
  194. }
  195. sys_memreuse_ = content;
  196. }
  197. #ifdef ENABLE_DUMP_IR
  198. void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
  199. auto rdr_setting = content.find(KEY_RDR_SETTINGS);
  200. if (rdr_setting == content.end()) {
  201. MS_LOG(WARNING) << "The '" << KEY_RDR_SETTINGS << "' not exists. Please check the config file '" << config_file_
  202. << "' set by 'env_config_path' in context.";
  203. return;
  204. }
  205. has_rdr_setting_ = true;
  206. auto rdr_enable = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_ENABLE);
  207. if (rdr_enable.has_value()) {
  208. ParseRdrEnable(**rdr_enable);
  209. }
  210. auto rdr_mode = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_MODE);
  211. if (rdr_mode.has_value()) {
  212. ParseRdrMode(**rdr_mode);
  213. }
  214. auto rdr_path = CheckJsonKeyExist(*rdr_setting, KEY_RDR_SETTINGS, KEY_PATH);
  215. if (rdr_path.has_value()) {
  216. ParseRdrPath(**rdr_path);
  217. }
  218. }
  219. void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
  220. if (!content.is_boolean()) {
  221. MS_LOG(WARNING) << "Json parse failed. 'enable' in " << KEY_RDR_SETTINGS << " should be boolean."
  222. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  223. return;
  224. }
  225. rdr_enabled_ = content;
  226. }
  227. void EnvConfigParser::ParseRdrMode(const nlohmann::json &content) {
  228. if (content != Exceptional && content != Normal) {
  229. MS_LOG(WARNING) << "Json parse failed. 'mode' in " << KEY_RDR_SETTINGS << " should be 1 or 2."
  230. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  231. return;
  232. }
  233. rdr_mode_ = content;
  234. }
  235. void EnvConfigParser::ParseRdrPath(const nlohmann::json &content) {
  236. std::string err_msg = "RDR path parse failed. The RDR path will be a default value: '" + rdr_path_ +
  237. "'. Please check the settings about '" + KEY_RDR_SETTINGS + "' in config file '" +
  238. config_file_ + "' set by 'env_config_path' in context.";
  239. if (!CheckJsonStringType(content, KEY_RDR_SETTINGS, KEY_PATH)) {
  240. MS_LOG(WARNING) << err_msg;
  241. return;
  242. }
  243. std::string path = content;
  244. if (!Common::IsPathValid(path, MAX_DIRECTORY_LENGTH, err_msg)) {
  245. return;
  246. }
  247. if (path.back() != '/') {
  248. path += '/';
  249. }
  250. rdr_path_ = path;
  251. }
  252. #endif
  253. void EnvConfigParser::ConfigToString() {
  254. std::string cur_config;
  255. #ifdef ENABLE_DUMP_IR
  256. cur_config.append("After parsed, ");
  257. cur_config.append("rdr_enable: ");
  258. std::string rdr_enable_flag = rdr_enabled_ ? "1" : "0";
  259. (void)cur_config.append(rdr_enable_flag);
  260. cur_config.append(", rdr mode: ");
  261. cur_config.append(std::to_string(rdr_mode_));
  262. cur_config.append(", rdr path: ");
  263. cur_config.append(rdr_path_);
  264. #endif
  265. MS_LOG(INFO) << cur_config;
  266. }
  267. } // namespace mindspore