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