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.h 2.3 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. #ifndef SUMMARY_EVENT_WRITER_H_
  17. #define SUMMARY_EVENT_WRITER_H_
  18. #include <memory>
  19. #include <string>
  20. #include "pybind11/pybind11.h"
  21. #include "securec/include/securec.h"
  22. #include "utils/system/base.h"
  23. #include "utils/system/file_system.h"
  24. #include "utils/system/crc32c.h"
  25. #include "utils/system/env.h"
  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. class EventWriter {
  35. public:
  36. // The file name = path + file_name
  37. explicit EventWriter(const std::string &file_full_name);
  38. ~EventWriter();
  39. // return the file name
  40. std::string GetFileName() const { return filename_; }
  41. // return the count of write event
  42. int32_t GetWriteEventCount() const;
  43. // Open the file
  44. bool Open();
  45. // write the Serialized "event_str" to file
  46. void Write(const std::string &event_str);
  47. // Flush the cache to disk
  48. bool Flush();
  49. // close the file
  50. bool Close() noexcept;
  51. // Final close: flush and close the event writer and clean
  52. bool Shut() noexcept;
  53. // Summary Record Format:
  54. // 1 uint64 : data length
  55. // 2 uint32 : mask crc value of data length
  56. // 3 bytes : data
  57. // 4 uint32 : mask crc value of data
  58. bool WriteRecord(const std::string &data);
  59. private:
  60. // True: valid / False: closed
  61. bool status_ = false;
  62. std::shared_ptr<FileSystem> fs_;
  63. std::string filename_;
  64. WriteFilePtr event_file_;
  65. int32_t events_write_count_ = 0;
  66. };
  67. } // namespace summary
  68. } // namespace mindspore
  69. #endif // SUMMARY_EVENT_WRITER_H_