Imported Upstream version 1.12.0
[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/Index.h"
22 #include "ir/OpSequence.h"
23 #include "ExecTime.h"
24 #include "util/ITimer.h"
25 #include "exec/IExecutor.h"
26 #include "util/EventCollector.h"
27 #include "util/EventRecorder.h"
28 #include "util/EventWriter.h"
29 #include "util/TracingCtx.h"
30 #include "util/EventWriter.h"
31
32 namespace onert
33 {
34 namespace exec
35 {
36 class IExecutionObserver
37 {
38 public:
39   /// @brief Invoked just before model (not individual operation) execution begins
40   virtual void handleSubgraphBegin(ir::SubgraphIndex) { return; }
41
42   virtual void handleJobBegin(IExecutor *, ir::SubgraphIndex, const ir::OpSequence *,
43                               const backend::Backend *) = 0;
44   virtual void handleJobEnd(IExecutor *, ir::SubgraphIndex, const ir::OpSequence *,
45                             const backend::Backend *) = 0;
46
47   /// @brief Invoked just after model (not individual operation) execution ends
48   virtual void handleSubgraphEnd(ir::SubgraphIndex) { return; }
49
50   virtual ~IExecutionObserver() = default;
51 };
52
53 class ProfileObserver : public IExecutionObserver
54 {
55 public:
56   explicit ProfileObserver(std::shared_ptr<ExecTime> et, const ir::Graph &graph)
57       : _et(std::move(et)), _graph(graph)
58   {
59   }
60   void handleJobBegin(IExecutor *, ir::SubgraphIndex, const ir::OpSequence *,
61                       const backend::Backend *) override;
62   void handleJobEnd(IExecutor *, ir::SubgraphIndex, const ir::OpSequence *,
63                     const backend::Backend *) override;
64
65   void handleSubgraphEnd(ir::SubgraphIndex) override { _et->storeOperationsExecTime(); }
66
67 private:
68   std::unique_ptr<util::ITimer> _timer;
69   std::shared_ptr<ExecTime> _et;
70   const ir::Graph &_graph;
71 };
72
73 class TracingObserver : public IExecutionObserver
74 {
75 public:
76   TracingObserver(const std::string &filepath, const ir::Graph &graph,
77                   const util::TracingCtx *tracing_ctx);
78   ~TracingObserver();
79   void handleSubgraphBegin(ir::SubgraphIndex) override;
80   void handleJobBegin(IExecutor *, ir::SubgraphIndex, const ir::OpSequence *,
81                       const backend::Backend *) override;
82   void handleJobEnd(IExecutor *, ir::SubgraphIndex, const ir::OpSequence *,
83                     const backend::Backend *) override;
84   void handleSubgraphEnd(ir::SubgraphIndex) override;
85
86 private:
87   static std::string opSequenceTag(const ir::OpSequence *op_seq, const ir::Operations &operations);
88
89 private:
90   std::unique_ptr<EventRecorder> _recorder;
91   EventCollector _collector;
92   const ir::Graph &_graph;
93   EventWriter *_event_writer;
94   const util::TracingCtx *_tracing_ctx;
95 };
96
97 } // namespace exec
98 } // namespace onert
99
100 #endif // __ONERT_EXEC_OBSREVERS_H__