blob: b090bf1e1e9e2d1a0d9c6ff49ba2271076c4795c [file] [log] [blame]
Anthony Barbier35aa6a32018-04-23 16:12:12 +01001/*
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#include "OpenCLMemoryUsage.h"
25
26#include "../Framework.h"
27#include "../Utils.h"
28
29#ifndef ARM_COMPUTE_CL
30#error "You can't use OpenCLMemoryUsage without OpenCL"
31#endif /* ARM_COMPUTE_CL */
32
33#include "arm_compute/core/CL/CLKernelLibrary.h"
34
35namespace arm_compute
36{
37namespace test
38{
39namespace framework
40{
41std::string OpenCLMemoryUsage::id() const
42{
43 return "OpenCLMemoryUsage";
44}
45
46OpenCLMemoryUsage::OpenCLMemoryUsage(ScaleFactor scale_factor)
47 : real_clCreateBuffer(CLSymbols::get().clCreateBuffer_ptr), real_clRetainMemObject(CLSymbols::get().clRetainMemObject_ptr), real_clReleaseMemObject(CLSymbols::get().clReleaseMemObject_ptr),
48 real_clSVMAlloc(CLSymbols::get().clSVMAlloc_ptr), real_clSVMFree(CLSymbols::get().clSVMFree_ptr), _allocations(), _svm_allocations(), _start(), _end(), _now()
49{
50 switch(scale_factor)
51 {
52 case ScaleFactor::NONE:
53 _scale_factor = 1;
54 _unit = "";
55 break;
56 case ScaleFactor::SCALE_1K:
57 _scale_factor = 1000;
58 _unit = "K ";
59 break;
60 case ScaleFactor::SCALE_1M:
61 _scale_factor = 1000000;
62 _unit = "M ";
63 break;
64 default:
65 ARM_COMPUTE_ERROR("Invalid scale");
66 }
67}
68
69void OpenCLMemoryUsage::test_start()
70{
71 _now = Stats();
72
73 ARM_COMPUTE_ERROR_ON(CLSymbols::get().clCreateBuffer_ptr == nullptr);
74 CLSymbols::get().clCreateBuffer_ptr = [this](
75 cl_context context,
76 cl_mem_flags flags,
77 size_t size,
78 void *host_ptr,
79 cl_int * errcode_ret)
80 {
81 cl_mem retval = this->real_clCreateBuffer(context, flags, size, host_ptr, errcode_ret);
82 if(host_ptr != nullptr)
83 {
84 // If it's an SVM / external allocation;
85 size = 0;
86 }
87 else
88 {
89 _now.num_allocations++;
90 _now.in_use += size;
91 _now.total_allocated += size;
92 if(_now.in_use > _now.max_in_use)
93 {
94 _now.max_in_use = _now.in_use;
95 }
96 }
97 this->_allocations[retval] = Allocation(size);
98 return retval;
99 };
100 ARM_COMPUTE_ERROR_ON(CLSymbols::get().clRetainMemObject_ptr == nullptr);
101 CLSymbols::get().clRetainMemObject_ptr = [this](cl_mem memobj)
102 {
103 cl_int retval = this->real_clRetainMemObject(memobj);
104 this->_allocations[memobj].refcount++;
105 return retval;
106 };
107 ARM_COMPUTE_ERROR_ON(CLSymbols::get().clReleaseMemObject_ptr == nullptr);
108 CLSymbols::get().clReleaseMemObject_ptr = [this](cl_mem memobj)
109 {
110 cl_int retval = this->real_clRetainMemObject(memobj);
111 Allocation &alloc = this->_allocations[memobj];
112 if(--alloc.refcount == 0)
113 {
114 _now.in_use -= alloc.size;
115 }
116 return retval;
117 };
118
119 //Only intercept the function if it exists:
120 if(CLSymbols::get().clSVMAlloc_ptr != nullptr)
121 {
122 CLSymbols::get().clSVMAlloc_ptr = [this](cl_context context, cl_svm_mem_flags flags, size_t size, cl_uint alignment)
123 {
124 void *retval = this->real_clSVMAlloc(context, flags, size, alignment);
125 if(retval != nullptr)
126 {
127 _now.num_allocations++;
128 _now.in_use += size;
129 _now.total_allocated += size;
130 if(_now.in_use > _now.max_in_use)
131 {
132 _now.max_in_use = _now.in_use;
133 }
134 }
135 return retval;
136 };
137 }
138
139 //Only intercept the function if it exists:
140 if(CLSymbols::get().clSVMFree_ptr != nullptr)
141 {
142 CLSymbols::get().clSVMFree_ptr = [this](cl_context context, void *svm_pointer)
143 {
144 this->real_clSVMFree(context, svm_pointer);
145 auto iterator = _svm_allocations.find(svm_pointer);
146 if(iterator != _svm_allocations.end())
147 {
148 size_t size = iterator->second;
149 _svm_allocations.erase(iterator);
150 _now.in_use -= size;
151 }
152 };
153 }
154}
155
156void OpenCLMemoryUsage::start()
157{
158 _start = _now;
159}
160void OpenCLMemoryUsage::stop()
161{
162 _end = _now;
163}
164
165void OpenCLMemoryUsage::test_stop()
166{
167 // Restore real function
168 CLSymbols::get().clCreateBuffer_ptr = real_clCreateBuffer;
169 CLSymbols::get().clRetainMemObject_ptr = real_clRetainMemObject;
170 CLSymbols::get().clReleaseMemObject_ptr = real_clReleaseMemObject;
171 CLSymbols::get().clSVMAlloc_ptr = real_clSVMAlloc;
172 CLSymbols::get().clSVMFree_ptr = real_clSVMFree;
173}
174
175Instrument::MeasurementsMap OpenCLMemoryUsage::measurements() const
176{
177 MeasurementsMap measurements;
178 measurements.emplace("Num buffers allocated per run", Measurement(_end.num_allocations - _start.num_allocations, ""));
179 measurements.emplace("Total memory allocated per run", Measurement((_end.total_allocated - _start.total_allocated) / _scale_factor, _unit));
180 measurements.emplace("Memory in use at start of run", Measurement(_start.in_use / _scale_factor, _unit));
181
182 return measurements;
183}
184Instrument::MeasurementsMap OpenCLMemoryUsage::test_measurements() const
185{
186 MeasurementsMap measurements;
187 measurements.emplace("Num buffers", Measurement(_now.num_allocations, ""));
188 measurements.emplace("Total memory allocated", Measurement(_now.total_allocated / _scale_factor, _unit));
189 measurements.emplace("Max memory allocated", Measurement(_now.max_in_use / _scale_factor, _unit));
190 measurements.emplace("Memory leaked", Measurement(_now.in_use / _scale_factor, _unit));
191
192 size_t num_programs = CLKernelLibrary::get().get_built_programs().size();
193 size_t total_size = 0;
194 for(auto it : CLKernelLibrary::get().get_built_programs())
195 {
196 std::vector<size_t> binary_sizes = it.second.getInfo<CL_PROGRAM_BINARY_SIZES>();
197 total_size = std::accumulate(binary_sizes.begin(), binary_sizes.end(), total_size);
198 }
199
200 measurements.emplace("Num programs in cache", Measurement(num_programs, ""));
201 measurements.emplace("Total programs memory in cache", Measurement(total_size / _scale_factor, _unit));
202
203 return measurements;
204}
205} // namespace framework
206} // namespace test
207} // namespace arm_compute