xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/instruments/SchedulerTimer.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2017-2019,2021 Arm Limited.
3  *
4  * SPDX-License-Identifier: MIT
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to
8  * deal in the Software without restriction, including without limitation the
9  * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10  * sell copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in all
14  * copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22  * SOFTWARE.
23  */
24 #ifndef ARM_COMPUTE_TEST_SCHEDULER_TIMER
25 #define ARM_COMPUTE_TEST_SCHEDULER_TIMER
26 
27 #include "Instrument.h"
28 #include "arm_compute/graph/Workload.h"
29 #include "arm_compute/runtime/Scheduler.h"
30 
31 #include <list>
32 #include <memory>
33 #include <vector>
34 
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace framework
40 {
41 /** Scheduler user interface  */
42 class ISchedulerUser
43 {
44 public:
45     /** Default Destructor */
46     virtual ~ISchedulerUser() = default;
47     /** Intercept the scheduler used by
48      *
49      * @param interceptor Intercept the scheduler used by the scheduler user.
50      */
51     virtual void intercept_scheduler(std::unique_ptr<IScheduler> interceptor) = 0;
52     /** Restore the original scheduler */
53     virtual void restore_scheduler() = 0;
54     /** Real scheduler accessor
55      *
56      * @return The real scheduler
57      */
58     virtual IScheduler *scheduler() = 0;
59 };
60 
61 /** Instrument creating measurements based on the information returned by clGetEventProfilingInfo for each OpenCL kernel executed*/
62 template <bool output_timestamps>
63 class SchedulerClock : public Instrument
64 {
65 public:
66     using LayerData = std::map<std::string, std::string>;
67     /** Construct a Scheduler timer.
68      *
69      * @param[in] scale_factor Measurement scale factor.
70      */
71     SchedulerClock(ScaleFactor scale_factor);
72     /** Prevent instances of this class from being copy constructed */
73     SchedulerClock(const SchedulerClock &) = delete;
74     /** Prevent instances of this class from being copied */
75     SchedulerClock &operator=(const SchedulerClock &) = delete;
76     /** Use the default move assignment operator */
77     SchedulerClock &operator=(SchedulerClock &&) = default;
78     /** Use the default move constructor */
79     SchedulerClock(SchedulerClock &&) = default;
80     /** Use the default destructor */
81     ~SchedulerClock() = default;
82 
83     // Inherited overridden methods
84     std::string                 id() const override;
85     void                        test_start() override;
86     void                        start() override;
87     void                        test_stop() override;
88     Instrument::MeasurementsMap measurements() const override;
89     std::string                 instrument_header() const override;
90 
91     /** Kernel information */
92     struct kernel_info
93     {
94         Instrument::MeasurementsMap measurements{}; /**< Time it took the kernel to run */
95         std::string                 name{};         /**< Kernel name */
96         std::string                 prefix{};       /**< Kernel prefix */
97     };
98 
99 private:
100     std::list<kernel_info> _kernels;
101     std::map<std::string, LayerData> _layer_data_map;
102     IScheduler     *_real_scheduler;
103     Scheduler::Type _real_scheduler_type;
104 #ifdef ARM_COMPUTE_GRAPH_ENABLED
105     std::function<decltype(graph::execute_task)> _real_graph_function;
106 #endif /* ARM_COMPUTE_GRAPH_ENABLED */
107     ScaleFactor                   _scale_factor;
108     std::shared_ptr<IScheduler>   _interceptor;
109     std::vector<ISchedulerUser *> _scheduler_users;
110 };
111 
112 using SchedulerTimer      = SchedulerClock<false>;
113 using SchedulerTimestamps = SchedulerClock<true>;
114 
115 } // namespace framework
116 } // namespace test
117 } // namespace arm_compute
118 #endif /* ARM_COMPUTE_TEST_SCHEDULER_TIMER */
119