• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/mem_address_recorder.h"
17 #include <fstream>
18 #include <sstream>
19 #include <utility>
20 #include "backend/kernel_compiler/kernel.h"
21 
22 namespace mindspore {
23 namespace {
MemInfo2String(const std::string & label,const AddressPtrList & info)24 std::string MemInfo2String(const std::string &label, const AddressPtrList &info) {
25   std::ostringstream ss;
26   ss << label << " " << info.size() << std::endl;
27   for (size_t i = 0; i < info.size(); i++) {
28     if (info[i] != nullptr) {
29       ss << "&" << info[i]->addr << " #" << info[i]->size << std::endl;
30     }
31   }
32   return ss.str();
33 }
34 }  // namespace
35 
SaveMemInfo(const std::string & op_name,const kernel::KernelLaunchInfo & mem_info)36 void MemAddressRecorder::SaveMemInfo(const std::string &op_name, const kernel::KernelLaunchInfo &mem_info) {
37   std::lock_guard<std::mutex> lock(mtx_);
38   if (!printed_) {
39     MS_LOG(INFO) << "RDR update mem info.";
40     printed_ = true;
41   }
42   if (op_names_.count(op_name) != 0) {
43     op_names_.clear();
44     mem_info_stream_.str("");
45   }
46   op_names_.insert(op_name);
47   mem_info_stream_ << op_name << std::endl;
48   mem_info_stream_ << MemInfo2String("kernel_inputs", mem_info.inputs_);
49   mem_info_stream_ << MemInfo2String("kernel_workspaces", mem_info.workspaces_);
50   mem_info_stream_ << MemInfo2String("kernel_outputs", mem_info.outputs_);
51   mem_info_stream_ << std::endl;
52 }
53 
Export()54 void MemAddressRecorder::Export() {
55   auto realpath = GetFileRealPath();
56   if (!realpath.has_value()) {
57     return;
58   }
59   std::lock_guard<std::mutex> lock(mtx_);
60   std::string file_path = realpath.value() + ".txt";
61   ChangeFileMode(file_path, S_IRWXU);
62   std::ofstream fout(file_path);
63   if (!fout.is_open()) {
64     MS_LOG(WARNING) << "Open file for saving memory information failed. File path: '" << file_path << "'.";
65     return;
66   }
67   MS_LOG(INFO) << "RDR export device memory information.";
68   fout << mem_info_stream_.str();
69   fout.close();
70   ChangeFileMode(file_path, S_IRUSR);
71 }
72 
CleanUp()73 void MemAddressRecorder::CleanUp() {
74   std::lock_guard<std::mutex> lock(mtx_);
75   MS_LOG(INFO) << "RDR clean up mem info, kernel size equals " << op_names_.size();
76   op_names_.clear();
77   mem_info_stream_.str("");
78   printed_ = false;
79 }
80 }  // namespace mindspore
81