2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #ifndef __ONERT_UTIL_EVENT_WRITER_H__
18 #define __ONERT_UTIL_EVENT_WRITER_H__
20 #include "EventRecorder.h"
24 #include <unordered_map>
28 class EventFormatWriter
31 EventFormatWriter(const std::string &filepath) : _os{filepath, std::ofstream::out} {}
32 virtual ~EventFormatWriter() { /* empty */}
34 virtual void flush(const std::vector<std::unique_ptr<EventRecorder>> &) = 0;
40 class SNPEWriter : public EventFormatWriter
43 SNPEWriter(const std::string &filepath) : EventFormatWriter(filepath) { /* empty */}
44 void flush(const std::vector<std::unique_ptr<EventRecorder>> &) override;
47 class ChromeTracingWriter : public EventFormatWriter
50 ChromeTracingWriter(const std::string &filepath) : EventFormatWriter(filepath) { /* empty */}
51 void flush(const std::vector<std::unique_ptr<EventRecorder>> &) override;
54 void flushOneRecord(const EventRecorder &);
57 class MDTableWriter : public EventFormatWriter
60 MDTableWriter(const std::string &filepath) : EventFormatWriter(filepath) { /* empty */}
61 void flush(const std::vector<std::unique_ptr<EventRecorder>> &) override;
64 void flushOneRecord(const EventRecorder &);
70 enum class WriteFormat
78 * @brief Retuens a singleton object
80 static EventWriter *get(const std::string &filename)
82 std::unique_lock<std::mutex> lock{_mutex};
84 static EventWriter singleton(filename);
89 * @brief Call this when observer which use EventWriter starts
93 std::unique_lock<std::mutex> lock{_mutex};
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.
102 void readyToFlush(std::unique_ptr<EventRecorder> &&recorder);
105 EventWriter(const std::string &filepath) : _ref_count(0)
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");
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);
117 void flush(WriteFormat write_format);
120 static std::mutex _mutex;
122 // number of observer of an executor that want to write profiling data
125 // one recorder object per executor
126 std::vector<std::unique_ptr<EventRecorder>> _recorders;
128 std::unordered_map<WriteFormat, std::unique_ptr<EventFormatWriter>> _actual_writers;
131 #endif // __ONERT_UTIL_EVENT_WRITER_H__