You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

event_writer.cc 5.6 kB

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