• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019 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 
17 #include "include/common/utils/summary/event_writer.h"
18 #include <string>
19 #include "utils/system/base.h"
20 #include "utils/system/file_system.h"
21 #include "utils/system/crc32c.h"
22 #include "utils/system/env.h"
23 #include "utils/log_adapter.h"
24 #include "include/common/utils/convert_utils.h"
25 
26 namespace mindspore {
27 namespace summary {
28 namespace py = pybind11;
29 using string = std::string;
30 using Env = system::Env;
31 using WriteFile = system::WriteFile;
32 using WriteFilePtr = std::shared_ptr<WriteFile>;
33 using FileSystem = system::FileSystem;
34 
35 // implement the EventWriter
EventWriter(const std::string & file_full_name)36 EventWriter::EventWriter(const std::string &file_full_name) : filename_(file_full_name), events_write_count_(0) {
37   fs_ = Env::GetFileSystem();
38   if (fs_ == nullptr) {
39     MS_LOG(EXCEPTION) << "Get the file system failed.";
40   }
41   event_file_ = fs_->CreateWriteFile(filename_);
42   if (event_file_ == nullptr) {
43     MS_LOG(EXCEPTION) << "Create the event file(" << file_full_name << ") failed.";
44   }
45   // set the event writer status
46   status_ = true;
47 }
48 
~EventWriter()49 EventWriter::~EventWriter() { CloseFile(); }
50 
CloseFile()51 void EventWriter::CloseFile() noexcept {
52   if (event_file_ != nullptr) {
53     bool result = Close();
54     if (!result) {
55       MS_LOG(ERROR) << "Close file(" << filename_ << ") failed.";
56     }
57   }
58 }
59 
60 // get the write event count
GetWriteEventCount() const61 int32_t EventWriter::GetWriteEventCount() const { return events_write_count_; }
62 
63 // Open the file
Open()64 bool EventWriter::Open() {
65   if (event_file_ == nullptr) {
66     MS_LOG(ERROR) << "Open the file(" << filename_ << ") failed.";
67     return false;
68   }
69   bool result = event_file_->Open();
70   if (!result) {
71     MS_LOG(ERROR) << "Open the file(" << filename_ << ") failed.";
72   }
73   return result;
74 }
75 
76 // write the event serialization string to file
Write(const std::string & event_str)77 void EventWriter::Write(const std::string &event_str) {
78   if (event_file_ == nullptr) {
79     MS_LOG(ERROR) << "Write failed because file could not be opened.";
80     return;
81   }
82   events_write_count_++;
83   bool result = WriteRecord(event_str);
84   if (!result) {
85     MS_LOG(ERROR) << "Event write failed.";
86   }
87 }
88 
Flush()89 bool EventWriter::Flush() {
90   // Confirm the event file is exist?
91   if (!fs_->FileExist(filename_)) {
92     MS_LOG(ERROR) << "Failed to flush to file(" << filename_ << ") because the file not exist.";
93     return false;
94   }
95   if (event_file_ == nullptr) {
96     MS_LOG(ERROR) << "Can't flush because the event file is null.";
97     return false;
98   }
99   // Sync the file
100   if (!event_file_->Flush()) {
101     MS_LOG(ERROR) << "Failed to sync to file(" << filename_ << "), the event count(" << events_write_count_ << ").";
102     return false;
103   }
104   MS_LOG(DEBUG) << "Flush " << events_write_count_ << " events to disk file(" << filename_ << ").";
105   return true;
106 }
107 
Close()108 bool EventWriter::Close() noexcept {
109   MS_LOG(DEBUG) << "Close the event writer.";
110   bool result = true;
111   if (!status_) {
112     MS_LOG(INFO) << "The event writer is closed.";
113     return result;
114   }
115   if (event_file_ != nullptr) {
116     result = event_file_->Close();
117     if (!result) {
118       MS_LOG(ERROR) << "Close the file(" << filename_ << ") failed.";
119     }
120   }
121   return result;
122 }
123 
Shut()124 bool EventWriter::Shut() noexcept {
125   MS_LOG(DEBUG) << "ShutDown the event writer.";
126   if (!status_) {
127     MS_LOG(INFO) << "The event writer is closed.";
128     return true;
129   }
130   bool result = Flush();
131   if (!result) {
132     MS_LOG(ERROR) << "Flush failed when close the file.";
133   }
134   if (event_file_ != nullptr) {
135     bool _close = event_file_->Close();
136     if (!_close) {
137       MS_LOG(ERROR) << "Close the file(" << filename_ << ") failed.";
138       result = _close;
139     }
140   }
141   events_write_count_ = 0;
142   status_ = false;
143   return result;
144 }
145 
WriteRecord(const std::string & data)146 bool EventWriter::WriteRecord(const std::string &data) {
147   if (event_file_ == nullptr) {
148     MS_LOG(ERROR) << "Writer not initialized or previously closed.";
149     return false;
150   }
151   // Write the data to event file
152   const unsigned int kArrayLen = sizeof(uint64_t);
153   char data_len_array[kArrayLen];
154   char crc_array[sizeof(uint32_t)];
155 
156   // step 1: write the data length
157   system::EncodeFixed64(data_len_array, kArrayLen, static_cast<int64_t>(data.size()));
158   bool result = event_file_->Write(string(data_len_array, sizeof(data_len_array)));
159   if (!result) {
160     MS_LOG(ERROR) << "Write the Summary data length failed.";
161     return false;
162   }
163 
164   // step 2: write the crc of data length
165   system::EncodeFixed64(data_len_array, kArrayLen, SizeToInt(data.size()));
166   uint32_t crc = system::Crc32c::GetMaskCrc32cValue(data_len_array, sizeof(data_len_array));
167   system::EncodeFixed32(crc_array, crc);
168   result = event_file_->Write(string(crc_array, sizeof(crc_array)));
169   if (!result) {
170     MS_LOG(ERROR) << "Write the Summary data length crc failed.";
171     return false;
172   }
173 
174   // step 3: write the data
175   result = event_file_->Write(data);
176   if (!result) {
177     MS_LOG(ERROR) << "Write the Summary data failed.";
178     return false;
179   }
180 
181   // step 4: write data crc
182   crc = system::Crc32c::GetMaskCrc32cValue(data.data(), data.size());
183   system::EncodeFixed32(crc_array, crc);
184   result = event_file_->Write(string(crc_array, sizeof(crc_array)));
185   if (!result) {
186     MS_LOG(ERROR) << "Write the Summary footer failed.";
187     return false;
188   }
189 
190   return true;
191 }
192 
193 }  // namespace summary
194 }  // namespace mindspore
195