xref: /aosp_15_r20/external/ComputeLibrary/tests/framework/instruments/OpenCLMemoryUsage.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2018 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_OPENCL_MEMORY_USAGE
25 #define ARM_COMPUTE_TEST_OPENCL_MEMORY_USAGE
26 
27 #include "Instrument.h"
28 
29 #ifdef ARM_COMPUTE_CL
30 #include "arm_compute/core/CL/OpenCL.h"
31 #endif /* ARM_COMPUTE_CL */
32 
33 #include <list>
34 
35 namespace arm_compute
36 {
37 namespace test
38 {
39 namespace framework
40 {
41 /** Instrument collecting memory usage information for OpenCL*/
42 class OpenCLMemoryUsage : public Instrument
43 {
44 public:
45     /** Construct an OpenCL timer.
46      *
47      * @param[in] scale_factor Measurement scale factor.
48      */
49     OpenCLMemoryUsage(ScaleFactor scale_factor);
50     std::string     id() const override;
51     void            test_start() override;
52     void            start() override;
53     void            stop() override;
54     void            test_stop() override;
55     MeasurementsMap test_measurements() const override;
56     MeasurementsMap measurements() const override;
57 #ifdef ARM_COMPUTE_CL
58     std::function<decltype(clCreateBuffer)>     real_clCreateBuffer;
59     std::function<decltype(clRetainMemObject)>  real_clRetainMemObject;
60     std::function<decltype(clReleaseMemObject)> real_clReleaseMemObject;
61     std::function<decltype(clSVMAlloc)>         real_clSVMAlloc;
62     std::function<decltype(clSVMFree)>          real_clSVMFree;
63 
64 private:
65     float _scale_factor{};
66     struct Allocation
67     {
68         Allocation() = default;
AllocationAllocation69         Allocation(size_t alloc_size)
70             : size(alloc_size)
71         {
72         }
73         size_t size{ 0 };
74         int    refcount{ 1 };
75     };
76     std::map<cl_mem, Allocation> _allocations;
77     std::map<void *, size_t>     _svm_allocations;
78     struct Stats
79     {
80         size_t total_allocated{ 0 };
81         size_t max_in_use{ 0 };
82         size_t in_use{ 0 };
83         size_t num_allocations{ 0 };
84     } _start, _end, _now;
85 #endif /* ARM_COMPUTE_CL */
86 };
87 } // namespace framework
88 } // namespace test
89 } // namespace arm_compute
90 #endif /* ARM_COMPUTE_TEST_OPENCL_MEMORY_USAGE */
91