Imported Upstream version 1.12.0
[platform/core/ml/nnfw.git] / runtime / onert / core / src / util / EventWriter.h
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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 __ONERT_UTIL_EVENT_WRITER_H__
18 #define __ONERT_UTIL_EVENT_WRITER_H__
19
20 #include "EventRecorder.h"
21
22 #include <string>
23 #include <vector>
24 #include <unordered_map>
25 #include <mutex>
26 #include <fstream>
27
28 class EventFormatWriter
29 {
30 public:
31   EventFormatWriter(const std::string &filepath) : _os{filepath, std::ofstream::out} {}
32   virtual ~EventFormatWriter() { /* empty */}
33
34   virtual void flush(const std::vector<std::unique_ptr<EventRecorder>> &) = 0;
35
36 protected:
37   std::ofstream _os;
38 };
39
40 class SNPEWriter : public EventFormatWriter
41 {
42 public:
43   SNPEWriter(const std::string &filepath) : EventFormatWriter(filepath) { /* empty */}
44   void flush(const std::vector<std::unique_ptr<EventRecorder>> &) override;
45 };
46
47 class ChromeTracingWriter : public EventFormatWriter
48 {
49 public:
50   ChromeTracingWriter(const std::string &filepath) : EventFormatWriter(filepath) { /* empty */}
51   void flush(const std::vector<std::unique_ptr<EventRecorder>> &) override;
52
53 private:
54   void flushOneRecord(const EventRecorder &);
55 };
56
57 class MDTableWriter : public EventFormatWriter
58 {
59 public:
60   MDTableWriter(const std::string &filepath) : EventFormatWriter(filepath) { /* empty */}
61   void flush(const std::vector<std::unique_ptr<EventRecorder>> &) override;
62
63 private:
64   void flushOneRecord(const EventRecorder &);
65 };
66
67 class EventWriter
68 {
69 public:
70   enum class WriteFormat
71   {
72     CHROME_TRACING,
73     SNPE_BENCHMARK,
74     MD_TABLE,
75   };
76
77   /**
78    * @brief Retuens a singleton object
79    */
80   static EventWriter *get(const std::string &filename)
81   {
82     std::unique_lock<std::mutex> lock{_mutex};
83
84     static EventWriter singleton(filename);
85     return &singleton;
86   }
87
88   /**
89    * @brief Call this when observer which use EventWriter starts
90    */
91   void startToUse()
92   {
93     std::unique_lock<std::mutex> lock{_mutex};
94     _ref_count++;
95   }
96
97   /**
98    * @brief Call this when observer which use EventWriter finishes.
99    *        After multiple observers calls this method, the reference count will eventually be 0.
100    *        Then, EventWriter will write profiling result file.
101    */
102   void readyToFlush(std::unique_ptr<EventRecorder> &&recorder);
103
104 private:
105   EventWriter(const std::string &filepath) : _ref_count(0)
106   {
107     std::string snpe_log_name(filepath);
108     std::string chrome_tracing_log_name(filepath + ".chrome.json");
109     std::string md_table_log_name(filepath + ".table.md");
110
111     _actual_writers[WriteFormat::SNPE_BENCHMARK] = std::make_unique<SNPEWriter>(snpe_log_name);
112     _actual_writers[WriteFormat::CHROME_TRACING] =
113         std::make_unique<ChromeTracingWriter>(chrome_tracing_log_name);
114     _actual_writers[WriteFormat::MD_TABLE] = std::make_unique<MDTableWriter>(md_table_log_name);
115   };
116
117   void flush(WriteFormat write_format);
118
119 private:
120   static std::mutex _mutex;
121
122   // number of observer of an executor that want to write profiling data
123   int32_t _ref_count;
124
125   // one recorder object per executor
126   std::vector<std::unique_ptr<EventRecorder>> _recorders;
127
128   std::unordered_map<WriteFormat, std::unique_ptr<EventFormatWriter>> _actual_writers;
129 };
130
131 #endif // __ONERT_UTIL_EVENT_WRITER_H__