f8c2acca51dad1c4d91c95d4bf7d09d2e5a27680
[platform/core/ml/nnfw.git] / runtime / onert / core / src / exec / ExecutionObservers.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_EXEC_OBSREVERS_H__
18 #define __ONERT_EXEC_OBSREVERS_H__
19
20 #include "exec/IFunction.h"
21 #include "ir/OpSequence.h"
22 #include "ExecTime.h"
23 #include "util/ITimer.h"
24 #include "exec/IExecutor.h"
25 #include "util/EventCollector.h"
26 #include "util/EventRecorder.h"
27
28 namespace onert
29 {
30 namespace exec
31 {
32 class IExecutionObserver
33 {
34 public:
35   /// @brief Invoked just before model (not individual operation) execution begins
36   virtual void handleBegin(IExecutor *) { return; }
37
38   virtual void handleBegin(IExecutor *, const ir::OpSequence *, const backend::Backend *) = 0;
39   virtual void handleEnd(IExecutor *, const ir::OpSequence *, const backend::Backend *) = 0;
40
41   /// @brief Invoked just after model (not individual operation) execution ends
42   virtual void handleEnd(IExecutor *) { return; }
43
44   virtual ~IExecutionObserver() = default;
45 };
46
47 class ProfileObserver : public IExecutionObserver
48 {
49 public:
50   explicit ProfileObserver(std::shared_ptr<ExecTime> et, const ir::Graph &graph)
51       : _et(std::move(et)), _graph(graph)
52   {
53   }
54   void handleBegin(IExecutor *, const ir::OpSequence *, const backend::Backend *) override;
55   void handleEnd(IExecutor *, const ir::OpSequence *, const backend::Backend *) override;
56
57   void handleEnd(IExecutor *) override { _et->uploadOperationsExecTime(); }
58
59 private:
60   std::unique_ptr<util::ITimer> _timer;
61   std::shared_ptr<ExecTime> _et;
62   const ir::Graph &_graph;
63 };
64
65 class ChromeTracingObserver : public IExecutionObserver
66 {
67 public:
68   ChromeTracingObserver(const std::string &filepath, const ir::Graph &graph);
69   ~ChromeTracingObserver();
70   void handleBegin(IExecutor *) override;
71   void handleBegin(IExecutor *, const ir::OpSequence *, const backend::Backend *) override;
72   void handleEnd(IExecutor *, const ir::OpSequence *, const backend::Backend *) override;
73   void handleEnd(IExecutor *) override;
74
75 private:
76   static std::string opSequenceTag(const ir::OpSequence *op_seq, const ir::Operations &operations);
77
78 private:
79   const std::string &_base_filepath;
80   EventRecorder _recorder;
81   EventCollector _collector;
82   const ir::Graph &_graph;
83 };
84
85 } // namespace exec
86 } // namespace onert
87
88 #endif // __ONERT_EXEC_OBSREVERS_H__