6c03a5b9afa3851a4b5152d32f935b9c9b900008
[platform/core/ml/nnfw.git] / runtime / onert / core / src / util / EventCollectorGlobal.cc
1 /*
2  * Copyright (c) 2019 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 #include "util/EventCollectorGlobal.h"
18
19 #include <cassert>
20 #include <fstream>
21 #include <iostream>
22
23 #include "util/ConfigSource.h"
24 #include "util/EventWriter.h"
25
26 namespace onert
27 {
28 namespace util
29 {
30
31 EventCollectorGlobal::EventCollectorGlobal() : _recorder{}, _collector{&_recorder}
32 {
33   // DO NOTHING
34 }
35
36 EventCollectorGlobal::~EventCollectorGlobal()
37 {
38   if (!_recorder.empty())
39   {
40     try
41     {
42       // TODO Need better way for saved file path than the hardcoded path
43       EventWriter{_recorder}.writeToFile("trace.global.json",
44                                          EventWriter::WriteFormat::CHROME_TRACING);
45     }
46     catch (const std::exception &e)
47     {
48       std::cerr << "E: Fail to record event in EventCollectorGlobal: " << e.what() << std::endl;
49     }
50   }
51 }
52
53 EventCollectorGlobal &EventCollectorGlobal::get()
54 {
55   static EventCollectorGlobal instance;
56   return instance;
57 }
58
59 EventDurationBlock::EventDurationBlock(const std::string &tag) : _tag{tag}
60 {
61   auto &glob = EventCollectorGlobal::get();
62   glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::BEGIN, "0", _tag});
63 }
64 EventDurationBlock::~EventDurationBlock()
65 {
66   auto &glob = EventCollectorGlobal::get();
67   glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::END, "0", _tag});
68 }
69
70 EventDurationManual::EventDurationManual(const std::string &tag) : _tag{tag}, _pair{true} {}
71
72 EventDurationManual::~EventDurationManual()
73 {
74   // Check if it has called begin-end pair
75   assert(_pair);
76 }
77
78 void EventDurationManual::begin()
79 {
80   _pair = false;
81   auto &glob = EventCollectorGlobal::get();
82   glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::BEGIN, "0", _tag});
83 }
84
85 void EventDurationManual::end()
86 {
87   assert(!_pair);
88   _pair = true;
89   auto &glob = EventCollectorGlobal::get();
90   glob.collector().onEvent(EventCollector::Event{EventCollector::Edge::END, "0", _tag});
91 }
92
93 } // namespace util
94 } // namespace onert