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

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