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 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_ENV_CONFIG_PARSER_H_ 17 #define MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_ENV_CONFIG_PARSER_H_ 18 19 #include <string> 20 #include <map> 21 #include <set> 22 #include <mutex> 23 #include <optional> 24 #include "nlohmann/json.hpp" 25 #include "utils/ms_utils.h" 26 #include "include/common/visible.h" 27 28 namespace mindspore { 29 enum RdrModes : int { Exceptional = 1, Normal = 2 }; 30 31 class COMMON_EXPORT EnvConfigParser { 32 public: 33 static EnvConfigParser &GetInstance(); 34 35 void Parse(); ConfigPath()36 std::string ConfigPath() const { return config_file_; } 37 38 #ifdef ENABLE_DUMP_IR HasRdrSetting()39 bool HasRdrSetting() const { return has_rdr_setting_; } RdrEnabled()40 bool RdrEnabled() const { return rdr_enabled_; } RdrMode()41 int RdrMode() const { return rdr_mode_; } RdrPath()42 std::string RdrPath() const { return rdr_path_; } 43 #endif GetSysMemreuse()44 bool GetSysMemreuse() const { return sys_memreuse_; } SetSysMemreuse(bool set_memreuse)45 void SetSysMemreuse(bool set_memreuse) { sys_memreuse_ = set_memreuse; } 46 47 private: EnvConfigParser()48 EnvConfigParser() {} ~EnvConfigParser()49 ~EnvConfigParser() {} 50 51 std::mutex lock_; 52 std::string config_file_{""}; 53 bool already_parsed_{false}; 54 55 #ifdef ENABLE_DUMP_IR 56 // rdr 57 bool has_rdr_setting_{false}; 58 bool rdr_enabled_{false}; 59 int rdr_mode_{1}; 60 std::string rdr_path_{"./"}; 61 #endif 62 63 // memreuse 64 bool sys_memreuse_{true}; 65 66 void ParseFromFile(); 67 void ParseFromEnv(); 68 std::string GetIfstreamString(const std::ifstream &ifstream) const; 69 bool CheckJsonStringType(const nlohmann::json &content, const std::string &setting_key, const std::string &key) const; 70 std::optional<nlohmann::detail::iter_impl<const nlohmann::json>> CheckJsonKeyExist(const nlohmann::json &content, 71 const std::string &setting_key, 72 const std::string &key) const; 73 #ifdef ENABLE_DUMP_IR 74 void ParseRdrSetting(const nlohmann::json &content); 75 void ParseRdrPath(const nlohmann::json &content); 76 void ParseRdrEnable(const nlohmann::json &content); 77 void ParseRdrMode(const nlohmann::json &content); 78 #endif 79 void ParseMemReuseSetting(const nlohmann::json &content); 80 void ParseSysMemReuse(const nlohmann::json &content); 81 82 void ConfigToString(); 83 }; 84 } // namespace mindspore 85 #endif // MINDSPORE_CCSRC_INCLUDE_COMMON_DEBUG_ENV_CONFIG_PARSER_H_ 86