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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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 <fstream>
  18. #include "nlohmann/json.hpp"
  19. #include "utils/log_adapter.h"
  20. #include "debug/common.h"
  21. #include "utils/ms_context.h"
  22. #include "utils/convert_utils_base.h"
  23. namespace {
  24. constexpr auto kRdrSettings = "rdr";
  25. constexpr auto kPath = "path";
  26. constexpr auto kEnable = "enable";
  27. } // namespace
  28. namespace mindspore {
  29. bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key,
  30. const std::string &key) {
  31. if (!content.is_string()) {
  32. MS_LOG(WARNING) << "Json Parse Failed. The '" << key << "' in '" << setting_key << "' should be string."
  33. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  34. return false;
  35. }
  36. return true;
  37. }
  38. std::optional<nlohmann::detail::iter_impl<const nlohmann::json>> EnvConfigParser::CheckJsonKeyExist(
  39. const nlohmann::json &content, const std::string &setting_key, const std::string &key) {
  40. auto iter = content.find(key);
  41. if (iter == content.end()) {
  42. MS_LOG(WARNING) << "Check json failed, '" << key << "' not found in '" << setting_key << "'."
  43. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  44. return std::nullopt;
  45. }
  46. return iter;
  47. }
  48. std::string EnvConfigParser::GetIfstreamString(const std::ifstream &ifstream) {
  49. std::stringstream buffer;
  50. buffer << ifstream.rdbuf();
  51. return buffer.str();
  52. }
  53. void EnvConfigParser::Parse() {
  54. std::lock_guard<std::mutex> guard(lock_);
  55. if (already_parsed_) {
  56. return;
  57. }
  58. already_parsed_ = true;
  59. auto context = MsContext::GetInstance();
  60. MS_EXCEPTION_IF_NULL(context);
  61. auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
  62. if (config_file.empty()) {
  63. MS_LOG(INFO) << "Get env config file failed. Please check the 'env_config_path' set in context.";
  64. return;
  65. }
  66. config_file_ = config_file;
  67. std::ifstream json_file(config_file_);
  68. if (!json_file.is_open()) {
  69. MS_LOG(WARNING) << "Env config file:" << config_file_ << " open failed."
  70. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  71. return;
  72. }
  73. nlohmann::json j;
  74. try {
  75. json_file >> j;
  76. } catch (nlohmann::json::parse_error &e) {
  77. MS_LOG(WARNING) << "Env config json contents '" << GetIfstreamString(json_file) << "' in config file '"
  78. << config_file_ << "' set by 'env_config_path' in context.";
  79. return;
  80. }
  81. // convert json to string
  82. std::stringstream ss;
  83. ss << j;
  84. std::string cfg = ss.str();
  85. MS_LOG(INFO) << "Env config json:" << cfg;
  86. ParseRdrSetting(j);
  87. ConfigToString();
  88. }
  89. void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
  90. auto rdr_setting = content.find(kRdrSettings);
  91. if (rdr_setting == content.end()) {
  92. MS_LOG(WARNING) << "The '" << kRdrSettings << "' not exists. Please check the config file '" << config_file_
  93. << "' set by 'env_config_path' in context.";
  94. return;
  95. }
  96. auto rdr_enable = CheckJsonKeyExist(*rdr_setting, kRdrSettings, kEnable);
  97. if (rdr_enable.has_value()) {
  98. ParseRdrEnable(**rdr_enable);
  99. }
  100. auto rdr_path = CheckJsonKeyExist(*rdr_setting, kRdrSettings, kPath);
  101. if (rdr_path.has_value()) {
  102. ParseRdrPath(**rdr_path);
  103. }
  104. }
  105. void EnvConfigParser::ParseRdrPath(const nlohmann::json &content) {
  106. std::string err_msg = "RDR path parse failed. The RDR path will be a default value: '" + rdr_path_ +
  107. "'. Please check the settings about '" + kRdrSettings + "' in config file '" + config_file_ +
  108. "' set by 'env_config_path' in context.";
  109. if (!CheckJsonStringType(content, kRdrSettings, kPath)) {
  110. MS_LOG(WARNING) << err_msg;
  111. return;
  112. }
  113. std::string path = content;
  114. if (!Common::IsPathValid(path, maxDirectoryLength, err_msg, false)) {
  115. return;
  116. }
  117. if (path.back() != '/') {
  118. path += '/';
  119. }
  120. rdr_path_ = path;
  121. }
  122. void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
  123. if (!content.is_boolean()) {
  124. MS_LOG(WARNING) << "Json parse failed. 'enable' in " << kRdrSettings << " should be boolean."
  125. << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
  126. rdr_enabled_ = false;
  127. return;
  128. }
  129. rdr_enabled_ = content;
  130. }
  131. void EnvConfigParser::ConfigToString() {
  132. std::string cur_config;
  133. cur_config.append("After parsed, rdr path: ");
  134. cur_config.append(rdr_path_);
  135. cur_config.append(", rdr_enable: ");
  136. cur_config.append(std::to_string(rdr_enabled_));
  137. MS_LOG(INFO) << cur_config;
  138. }
  139. } // namespace mindspore