1027ec84d46220e4b47a1e535a2edb38ad52a381
[platform/core/ml/nnfw.git] / runtime / onert / core / src / util / EventCollectorGlobal.h
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 #ifndef __ONERT_UTIL_EVENT_COLLECTOR_GLOBAL_H__
18 #define __ONERT_UTIL_EVENT_COLLECTOR_GLOBAL_H__
19
20 #include "util/EventRecorder.h"
21 #include "util/EventCollector.h"
22
23 namespace onert
24 {
25 namespace util
26 {
27
28 /**
29  * @brief Singleton class for event collection from anywhere in code
30  *
31  */
32 class EventCollectorGlobal
33 {
34 public:
35   /**
36    * @brief Get the singleton object of this class
37    *
38    * @return EventCollectorGlobal& Singleton object
39    */
40   static EventCollectorGlobal &get();
41
42 public:
43   /**
44    * @brief Getter for event collector object
45    *
46    * @return EventCollector& Collector object
47    */
48   EventCollector &collector() { return _collector; }
49
50 private:
51   EventCollectorGlobal();
52   ~EventCollectorGlobal();
53
54 private:
55   EventRecorder _recorder;
56   EventCollector _collector;
57 };
58
59 /**
60  * @brief Helper class for emitting duration event which is handled automatically with ctor/dtor
61  *
62  */
63 class EventDurationBlock
64 {
65 public:
66   /**
67    * @brief Raise a duration event with type of BEGIN
68    *
69    * @param tag A label for the duration event
70    */
71   EventDurationBlock(const std::string &tag);
72   /**
73    * @brief Raise a duration event with type of END
74    *
75    */
76   ~EventDurationBlock();
77
78 private:
79   std::string _tag;
80 };
81
82 /**
83  * @brief Helper class for emitting duration event which is handled manually
84  *
85  *        Usage:
86  *        {
87  *          ...
88  *          EventDurationManual duration("some tag");
89  *          duration.begin();
90  *          ...
91  *          ... // Code for duration
92  *          ...
93  *          duration.end();
94  *        }
95  *
96  */
97 class EventDurationManual
98 {
99 public:
100   /**
101    * @brief Construct a new Event Duration Manual object
102    *
103    * @param tag A label for the duration object
104    */
105   EventDurationManual(const std::string &tag);
106   /**
107    * @brief Destroy the Event Duration Manual object
108    *
109    */
110   ~EventDurationManual();
111
112   /**
113    * @brief Raise a duration event with type of BEGIN
114    *
115    */
116   void begin();
117   /**
118    * @brief Raise a duration event with type of END
119    *
120    */
121   void end();
122
123 private:
124   std::string _tag;
125   bool _pair;
126 };
127
128 } // namespace util
129 } // namespace onert
130
131 /**
132  * Helper Macro Definitions
133  *
134  * HOW TO USE
135  *
136  * void f(args)
137  * {
138  *   EVENT_DURATION_FUNCTION();
139  *   ...
140  *   if(cond)
141  *   {
142  *     EVENT_DURATION_REGION("if branch");
143  *     ...
144  *   }
145  *   ...
146  * }
147  */
148
149 #define EVENT_DURATION_FUNCTION() \
150   ::onert::util::EventDurationBlock __event_duration__##__LINE__ { __FUNCTION__ }
151
152 #define EVENT_DURATION_REGION(tag) \
153   ::onert::util::EventDurationBlock __event_duration__##__LINE__ { tag }
154
155 #endif // __ONERT_UTIL_EVENT_COLLECTOR_GLOBAL_H__