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/stream_exec_order_recorder.h" 17 #include <sstream> 18 #include <fstream> 19 #include "mindspore/core/ir/anf.h" 20 #include "mindspore/core/utils/log_adapter.h" 21 #include "backend/session/anf_runtime_algorithm.h" 22 #include "utils/utils.h" 23 24 namespace mindspore { Vector2String(const std::vector<uint32_t> & v)25std::string Vector2String(const std::vector<uint32_t> &v) { 26 std::string str = ""; 27 for (size_t j = 0; j < v.size(); ++j) { 28 str += std::to_string(v[j]) + (j + 1 < v.size() ? "," : ""); 29 } 30 return str; 31 } 32 ExecNode2Json() const33json ExecNode::ExecNode2Json() const { 34 json exec_node; 35 exec_node[kAttrIndex] = index_; 36 exec_node[kAttrNodeName] = node_name_; 37 exec_node[kAttrLogicId] = logic_id_; 38 exec_node[kAttrStreamId] = stream_id_; 39 exec_node[kAttrNodeInfo] = node_info_; 40 exec_node[kAttrEventId] = event_id_; 41 if (!label_ids_.empty()) { 42 exec_node[kAttrLabelId] = Vector2String(label_ids_); 43 } 44 if (!active_stream_ids_.empty()) { 45 exec_node[kAttrActiveStreamId] = Vector2String(active_stream_ids_); 46 } 47 48 return exec_node; 49 } 50 Export()51void StreamExecOrderRecorder::Export() { 52 auto realpath = GetFileRealPath(); 53 if (!realpath.has_value()) { 54 return; 55 } 56 std::string real_file_path = realpath.value() + ".json"; 57 json exec_order_json = json::array(); 58 for (size_t i = 0; i < exec_order_.size(); ++i) { 59 exec_order_json.push_back(exec_order_[i]->ExecNode2Json()); 60 } 61 ChangeFileMode(real_file_path, S_IRWXU); 62 std::ofstream fout(real_file_path, std::ofstream::app); 63 if (!fout.is_open()) { 64 MS_LOG(WARNING) << "Open file for saving stream execute order failed. File path: '" << real_file_path << "'."; 65 return; 66 } 67 const size_t space_num = 2; 68 fout << exec_order_json.dump(space_num); 69 fout.close(); 70 ChangeFileMode(real_file_path, S_IRUSR); 71 } 72 } // namespace mindspore 73