!13469 add MS_RDR_ENABLE and MS_RDR_PATH

From: @luopengting
Reviewed-by: @ouwenchang,@yelihua
Signed-off-by: @yelihua
This commit is contained in:
mindspore-ci-bot 2021-03-19 11:07:07 +08:00 committed by Gitee
commit f7ff8e81cd
4 changed files with 73 additions and 14 deletions

View File

@ -14,6 +14,7 @@
* limitations under the License. * limitations under the License.
*/ */
#include "debug/env_config_parser.h" #include "debug/env_config_parser.h"
#include <algorithm>
#include <fstream> #include <fstream>
#include "nlohmann/json.hpp" #include "nlohmann/json.hpp"
#include "utils/log_adapter.h" #include "utils/log_adapter.h"
@ -22,12 +23,46 @@
#include "utils/convert_utils_base.h" #include "utils/convert_utils_base.h"
namespace { namespace {
constexpr auto kEnableEnv = "MS_RDR_ENABLE";
constexpr auto kPathEnv = "MS_RDR_PATH";
constexpr auto kRdrSettings = "rdr"; constexpr auto kRdrSettings = "rdr";
constexpr auto kPath = "path"; constexpr auto kPath = "path";
constexpr auto kEnable = "enable"; constexpr auto kEnable = "enable";
} // namespace } // namespace
namespace mindspore { namespace mindspore {
std::optional<bool> GetRdrEnableFromEnv() {
// get environment variable to configure RDR
const char *env_enable_char = std::getenv(kEnableEnv);
if (env_enable_char != nullptr) {
std::string env_enable_str = env_enable_char;
(void)std::transform(env_enable_str.begin(), env_enable_str.end(), env_enable_str.begin(), ::tolower);
if (env_enable_str != "0" && env_enable_str != "1") {
MS_LOG(WARNING) << "The environment variable '" << kEnableEnv << "' should be 0 or 1.";
}
if (env_enable_str == "1") {
return true;
}
return false;
}
return std::nullopt;
}
std::optional<std::string> GetRdrPathFromEnv() {
// get environment variable to configure RDR
const char *path_char = std::getenv(kPathEnv);
if (path_char != nullptr) {
std::string err_msg = "RDR path parse from environment variable failed. Please check the settings about '" +
std::string(kPathEnv) + "' in environment variables.";
std::string path = path_char;
if (!Common::IsPathValid(path, maxDirectoryLength, err_msg, false)) {
return std::string("");
}
return path;
}
return std::nullopt;
}
bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key, bool EnvConfigParser::CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key,
const std::string &key) { const std::string &key) {
if (!content.is_string()) { if (!content.is_string()) {
@ -55,13 +90,24 @@ std::string EnvConfigParser::GetIfstreamString(const std::ifstream &ifstream) {
return buffer.str(); return buffer.str();
} }
void EnvConfigParser::Parse() { void EnvConfigParser::ParseFromEnv() {
std::lock_guard<std::mutex> guard(lock_); // Get RDR seetings from environment variables
if (already_parsed_) { auto rdr_enable_env = GetRdrEnableFromEnv();
return; if (rdr_enable_env.has_value()) {
has_rdr_setting_ = true;
rdr_enabled_ = rdr_enable_env.value();
}
auto path_env = GetRdrPathFromEnv();
if (path_env.has_value()) {
has_rdr_setting_ = true;
std::string path = path_env.value();
if (!path.empty()) {
rdr_path_ = path;
}
}
} }
already_parsed_ = true;
void EnvConfigParser::ParseFromFile() {
auto context = MsContext::GetInstance(); auto context = MsContext::GetInstance();
MS_EXCEPTION_IF_NULL(context); MS_EXCEPTION_IF_NULL(context);
auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH); auto config_file = context->get_param<std::string>(MS_CTX_ENV_CONFIG_PATH);
@ -92,10 +138,22 @@ void EnvConfigParser::Parse() {
std::string cfg = ss.str(); std::string cfg = ss.str();
MS_LOG(INFO) << "Env config json:" << cfg; MS_LOG(INFO) << "Env config json:" << cfg;
// Parse rdr seetings from file
ParseRdrSetting(j); ParseRdrSetting(j);
ConfigToString(); ConfigToString();
} }
void EnvConfigParser::Parse() {
std::lock_guard<std::mutex> guard(lock_);
if (already_parsed_) {
return;
}
already_parsed_ = true;
ParseFromEnv();
ParseFromFile();
}
void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) { void EnvConfigParser::ParseRdrSetting(const nlohmann::json &content) {
auto rdr_setting = content.find(kRdrSettings); auto rdr_setting = content.find(kRdrSettings);
if (rdr_setting == content.end()) { if (rdr_setting == content.end()) {
@ -142,7 +200,6 @@ void EnvConfigParser::ParseRdrEnable(const nlohmann::json &content) {
if (!content.is_boolean()) { if (!content.is_boolean()) {
MS_LOG(WARNING) << "Json parse failed. 'enable' in " << kRdrSettings << " should be boolean." MS_LOG(WARNING) << "Json parse failed. 'enable' in " << kRdrSettings << " should be boolean."
<< " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context."; << " Please check the config file '" << config_file_ << "' set by 'env_config_path' in context.";
rdr_enabled_ = false;
return; return;
} }
rdr_enabled_ = content; rdr_enabled_ = content;

View File

@ -32,11 +32,11 @@ class EnvConfigParser {
} }
void Parse(); void Parse();
std::string config_path() const { return config_file_; } std::string ConfigPath() const { return config_file_; }
bool has_rdr_setting() const { return has_rdr_setting_; } bool HasRdrSetting() const { return has_rdr_setting_; }
bool rdr_enabled() const { return rdr_enabled_; } bool RdrEnabled() const { return rdr_enabled_; }
std::string rdr_path() const { return rdr_path_; } std::string RdrPath() const { return rdr_path_; }
private: private:
EnvConfigParser() {} EnvConfigParser() {}
@ -50,6 +50,8 @@ class EnvConfigParser {
bool has_rdr_setting_{false}; bool has_rdr_setting_{false};
std::string rdr_path_{"./rdr/"}; std::string rdr_path_{"./rdr/"};
void ParseFromFile();
void ParseFromEnv();
std::string GetIfstreamString(const std::ifstream &ifstream); std::string GetIfstreamString(const std::ifstream &ifstream);
void ParseRdrSetting(const nlohmann::json &content); void ParseRdrSetting(const nlohmann::json &content);

View File

@ -30,7 +30,7 @@ class BaseRecorder {
public: public:
BaseRecorder() : module_(""), name_(""), directory_(""), filename_(""), timestamp_("") {} BaseRecorder() : module_(""), name_(""), directory_(""), filename_(""), timestamp_("") {}
BaseRecorder(const std::string &module, const std::string &name) : module_(module), name_(name), filename_("") { BaseRecorder(const std::string &module, const std::string &name) : module_(module), name_(name), filename_("") {
directory_ = mindspore::EnvConfigParser::GetInstance().rdr_path(); directory_ = mindspore::EnvConfigParser::GetInstance().RdrPath();
if (name.length() > maxNameLength) { if (name.length() > maxNameLength) {
name_ = name.substr(0, maxNameLength); name_ = name.substr(0, maxNameLength);

View File

@ -25,11 +25,11 @@ void RecorderManager::UpdateRdrEnable() {
return; return;
} }
auto &config_parser = mindspore::EnvConfigParser::GetInstance(); auto &config_parser = mindspore::EnvConfigParser::GetInstance();
rdr_enable_ = config_parser.rdr_enabled(); rdr_enable_ = config_parser.RdrEnabled();
if (config_parser.has_rdr_setting()) { if (config_parser.HasRdrSetting()) {
#ifdef __linux__ #ifdef __linux__
if (!rdr_enable_) { if (!rdr_enable_) {
MS_LOG(WARNING) << "Please set the 'enable' as true using 'rdr' setting in file '" << config_parser.config_path() MS_LOG(WARNING) << "Please set the 'enable' as true using 'rdr' setting in file '" << config_parser.ConfigPath()
<< "' if you want to use RDR."; << "' if you want to use RDR.";
} }
#else #else