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/running_data_recorder.h"
17 #include <utility>
18
19 #include "debug/rdr/graph_exec_order_recorder.h"
20 #include "debug/rdr/recorder_manager.h"
21 #include "debug/rdr/string_recorder.h"
22 #include "debug/rdr/stream_exec_order_recorder.h"
23 #include "debug/rdr/mem_address_recorder.h"
24 #include "mindspore/core/ir/func_graph.h"
25 #include "mindspore/core/ir/anf.h"
26 #include "backend/kernel_compiler/kernel.h"
27 #ifdef ENABLE_D
28 #include "runtime/device/ascend/tasksink/task_generator.h"
29 #include "debug/rdr/task_debug_info_recorder.h"
30 #endif // ENABLE_D
31 namespace mindspore {
32 namespace RDR {
33 #ifdef ENABLE_D
RecordTaskDebugInfo(SubModuleId module,const std::string & name,const std::vector<TaskDebugInfoPtr> & task_debug_info_list)34 bool RecordTaskDebugInfo(SubModuleId module, const std::string &name,
35 const std::vector<TaskDebugInfoPtr> &task_debug_info_list) {
36 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
37 return false;
38 }
39 std::string submodule_name = std::string(GetSubModuleName(module));
40 TaskDebugInfoRecorderPtr task_debug_info_recorder =
41 std::make_shared<TaskDebugInfoRecorder>(submodule_name, name, task_debug_info_list);
42 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(task_debug_info_recorder));
43 return ans;
44 }
45 #endif // ENABLE_D
46
RecordAnfGraph(const SubModuleId module,const std::string & name,const FuncGraphPtr & graph,const DumpGraphParams & info,const std::string & file_type)47 bool RecordAnfGraph(const SubModuleId module, const std::string &name, const FuncGraphPtr &graph,
48 const DumpGraphParams &info, const std::string &file_type) {
49 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
50 return false;
51 }
52 std::string submodule_name = std::string(GetSubModuleName(module));
53 GraphRecorderPtr graph_recorder = std::make_shared<GraphRecorder>(submodule_name, name, graph, file_type);
54 graph_recorder->SetDumpFlag(info);
55 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(graph_recorder));
56 return ans;
57 }
58
RecordGraphExecOrder(const SubModuleId module,const std::string & name,const std::vector<CNodePtr> & final_exec_order)59 bool RecordGraphExecOrder(const SubModuleId module, const std::string &name,
60 const std::vector<CNodePtr> &final_exec_order) {
61 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
62 return false;
63 }
64 std::string submodule_name = std::string(GetSubModuleName(module));
65 GraphExecOrderRecorderPtr graph_exec_order_recorder =
66 std::make_shared<GraphExecOrderRecorder>(submodule_name, name, final_exec_order);
67 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(graph_exec_order_recorder));
68 return ans;
69 }
70
RecordString(SubModuleId module,const std::string & name,const std::string & data)71 bool RecordString(SubModuleId module, const std::string &name, const std::string &data) {
72 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
73 return false;
74 }
75 std::string submodule_name = std::string(GetSubModuleName(module));
76 StringRecorderPtr string_recorder = std::make_shared<StringRecorder>(submodule_name, name, data);
77 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(string_recorder));
78 return ans;
79 }
80
RecordStreamExecOrder(const SubModuleId module,const std::string & name,const std::vector<CNodePtr> & exec_order)81 bool RecordStreamExecOrder(const SubModuleId module, const std::string &name, const std::vector<CNodePtr> &exec_order) {
82 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
83 return false;
84 }
85 std::string submodule_name = std::string(GetSubModuleName(module));
86 StreamExecOrderRecorderPtr stream_exec_order_recorder =
87 std::make_shared<StreamExecOrderRecorder>(submodule_name, name, exec_order);
88 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(stream_exec_order_recorder));
89 return ans;
90 }
91
RecordMemAddressInfo(const SubModuleId module,const std::string & name)92 bool RecordMemAddressInfo(const SubModuleId module, const std::string &name) {
93 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
94 return false;
95 }
96 std::string submodule_name = std::string(GetSubModuleName(module));
97 MemAddressRecorderPtr mem_info_recorder = std::make_shared<MemAddressRecorder>(submodule_name, name);
98 mem_info_recorder->Reset();
99 bool ans = mindspore::RecorderManager::Instance().RecordObject(std::move(mem_info_recorder));
100 return ans;
101 }
102
UpdateMemAddress(const SubModuleId module,const std::string & name,const std::string & op_name,const kernel::KernelLaunchInfo & mem_info)103 bool UpdateMemAddress(const SubModuleId module, const std::string &name, const std::string &op_name,
104 const kernel::KernelLaunchInfo &mem_info) {
105 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
106 return false;
107 }
108 std::string submodule_name = std::string(GetSubModuleName(module));
109 auto recorder = mindspore::RecorderManager::Instance().GetRecorder(submodule_name, name);
110 bool ans = false;
111 if (recorder != nullptr) {
112 auto mem_recorder = std::dynamic_pointer_cast<MemAddressRecorder>(recorder);
113 mem_recorder->SaveMemInfo(op_name, mem_info);
114 ans = true;
115 }
116 return ans;
117 }
118
TriggerAll()119 void TriggerAll() { mindspore::RecorderManager::Instance().TriggerAll(); }
120
ResetRecorder()121 void ResetRecorder() { mindspore::RecorderManager::Instance().ClearAll(); }
122
ClearMemAddressInfo()123 void ClearMemAddressInfo() {
124 if (!mindspore::RecorderManager::Instance().RdrEnable()) {
125 return;
126 }
127 if (RecorderManager::Instance().CheckRdrMemIsRecord()) {
128 std::string name = "mem_address_list";
129 std::string submodule_name = "KERNEL";
130 auto recorder = RecorderManager::Instance().GetRecorder(submodule_name, name);
131 if (recorder != nullptr) {
132 auto mem_recorder = std::dynamic_pointer_cast<MemAddressRecorder>(recorder);
133 mem_recorder->CleanUp();
134 }
135 }
136 }
137 } // namespace RDR
138 } // namespace mindspore
139