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