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/rdr/recorder_manager.h"
17 #include <utility>
18 #include "debug/rdr/base_recorder.h"
19 #include "debug/env_config_parser.h"
20
21 namespace mindspore {
UpdateRdrEnable()22 void RecorderManager::UpdateRdrEnable() {
23 static bool updated = false;
24 if (updated) {
25 return;
26 }
27 auto &config_parser = mindspore::EnvConfigParser::GetInstance();
28 rdr_enable_ = config_parser.RdrEnabled();
29 if (config_parser.HasRdrSetting()) {
30 #ifdef __linux__
31 if (!rdr_enable_) {
32 MS_LOG(WARNING) << "Not enabling RDR. You can enable RDR through configuration file or environment variables.";
33 }
34 #else
35 if (rdr_enable_) {
36 MS_LOG(WARNING) << "The RDR only supports linux os currently.";
37 }
38 rdr_enable_ = false;
39 #endif
40 }
41 updated = true;
42 }
43
RecordObject(const BaseRecorderPtr & recorder)44 bool RecorderManager::RecordObject(const BaseRecorderPtr &recorder) {
45 if (!rdr_enable_) {
46 return false;
47 }
48 if (recorder == nullptr) {
49 MS_LOG(ERROR) << "Register recorder module with nullptr.";
50 return false;
51 }
52 std::string module = recorder->GetModule();
53 std::string name = recorder->GetName();
54 std::pair<std::string, std::string> recorder_key(module, name);
55 std::lock_guard<std::mutex> lock(mtx_);
56 recorder_container_[recorder_key] = recorder;
57 MS_LOG(INFO) << "RDR record object " << name << " in module \"" << module << "\".";
58 return true;
59 }
60
GetRecorder(std::string module,std::string name)61 BaseRecorderPtr RecorderManager::GetRecorder(std::string module, std::string name) {
62 if (!rdr_enable_) {
63 return nullptr;
64 }
65 std::lock_guard<std::mutex> lock(mtx_);
66 std::pair<std::string, std::string> recorder_key(module, name);
67 auto item = recorder_container_.find(recorder_key);
68 if (item != recorder_container_.end()) {
69 return item->second;
70 }
71 return nullptr;
72 }
73
RdrEnable() const74 bool RecorderManager::RdrEnable() const {
75 std::lock_guard<std::mutex> lock(mtx_);
76 return rdr_enable_;
77 }
78
CheckRdrMemIsRecord() const79 bool RecorderManager::CheckRdrMemIsRecord() const {
80 if (!rdr_enable_) {
81 return false;
82 }
83 std::lock_guard<std::mutex> lock(mtx_);
84 return rdr_has_record_mem_;
85 }
86
SetRdrMemIsRecord(bool is_enable)87 void RecorderManager::SetRdrMemIsRecord(bool is_enable) {
88 if (!rdr_enable_) {
89 return;
90 }
91 std::lock_guard<std::mutex> lock(mtx_);
92 rdr_has_record_mem_ = is_enable;
93 }
94
TriggerAll()95 void RecorderManager::TriggerAll() {
96 if (!rdr_enable_) {
97 return;
98 }
99 bool trigger = false;
100 std::lock_guard<std::mutex> lock(mtx_);
101 for (auto iter = recorder_container_.begin(); iter != recorder_container_.end(); ++iter) {
102 iter->second->Export();
103 trigger = true;
104 }
105 if (!trigger) {
106 MS_LOG(WARNING) << "There is no recorder to export.";
107 } else {
108 MS_LOG(INFO) << "RDR export all recorders.";
109 }
110 }
111
ClearAll()112 void RecorderManager::ClearAll() {
113 if (!rdr_enable_) {
114 return;
115 }
116 std::lock_guard<std::mutex> lock(mtx_);
117 recorder_container_.clear();
118 rdr_has_record_mem_ = false;
119 MS_LOG(INFO) << "RDR clear all recorders.";
120 }
121 } // namespace mindspore
122