• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright 2019-2022 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 #ifndef MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_SUMMARY_EVENT_WRITER_H_
18 #define MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_SUMMARY_EVENT_WRITER_H_
19 
20 #include <memory>
21 #include <string>
22 
23 #include "pybind11/pybind11.h"
24 #include "securec/include/securec.h"
25 #include "include/common/visible.h"
26 
27 namespace mindspore {
28 namespace system {
29 class FileSystem;
30 class WriteFile;
31 }  // namespace system
32 namespace summary {
33 class COMMON_EXPORT EventWriter {
34  public:
35   // The file name = path + file_name
36   explicit EventWriter(const std::string &file_full_name);
37 
38   ~EventWriter();
39 
40   // return the file name
GetFileName()41   std::string GetFileName() const { return filename_; }
42 
43   // return the count of write event
44   int32_t GetWriteEventCount() const;
45 
46   // Open the file
47   bool Open();
48 
49   // write the Serialized "event_str" to file
50   void Write(const std::string &event_str);
51 
52   // Flush the cache to disk
53   bool Flush();
54 
55   // close the file
56   bool Close() noexcept;
57 
58   // Final close: flush and close the event writer and clean
59   bool Shut() noexcept;
60 
61   // Summary Record Format:
62   //  1 uint64 : data length
63   //  2 uint32 : mask crc value of data length
64   //  3 bytes  : data
65   //  4 uint32 : mask crc value of data
66   bool WriteRecord(const std::string &data);
67 
68  private:
69   void CloseFile() noexcept;
70   // True: valid / False: closed
71   bool status_ = false;
72   std::shared_ptr<system::FileSystem> fs_;
73   std::string filename_;
74   std::shared_ptr<system::WriteFile> event_file_;
75   int32_t events_write_count_ = 0;
76 };
77 
78 }  // namespace summary
79 }  // namespace mindspore
80 
81 #endif  // MINDSPORE_CCSRC_INCLUDE_COMMON_UTILS_SUMMARY_EVENT_WRITER_H_
82