/root/doris/be/src/vec/common/arena.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // This file is copied from |
18 | | // https://github.com/ClickHouse/ClickHouse/blob/master/src/Common/Arena.h |
19 | | // and modified by Doris |
20 | | |
21 | | #pragma once |
22 | | |
23 | | #include <common/compiler_util.h> |
24 | | #include <string.h> |
25 | | |
26 | | #include <boost/noncopyable.hpp> |
27 | | #include <memory> |
28 | | #include <vector> |
29 | | #if __has_include(<sanitizer/asan_interface.h>) |
30 | | #include <sanitizer/asan_interface.h> |
31 | | #endif |
32 | | #include "gutil/dynamic_annotations.h" |
33 | | #include "vec/common/allocator.h" |
34 | | #include "vec/common/allocator_fwd.h" |
35 | | #include "vec/common/memcpy_small.h" |
36 | | |
37 | | namespace doris::vectorized { |
38 | | |
39 | | /** Memory pool to append something. For example, short strings. |
40 | | * Usage scenario: |
41 | | * - put lot of strings inside pool, keep their addresses; |
42 | | * - addresses remain valid during lifetime of pool; |
43 | | * - at destruction of pool, all memory is freed; |
44 | | * - memory is allocated and freed by large chunks; |
45 | | * - freeing parts of data is not possible (but look at ArenaWithFreeLists if you need); |
46 | | */ |
47 | | class Arena : private boost::noncopyable { |
48 | | private: |
49 | | /// Padding allows to use 'memcpy_small_allow_read_write_overflow15' instead of 'memcpy'. |
50 | | static constexpr size_t pad_right = 15; |
51 | | |
52 | | /// Contiguous chunk of memory and pointer to free space inside it. Member of single-linked list. |
53 | | struct alignas(16) Chunk : private Allocator<false> /// empty base optimization |
54 | | { |
55 | | char* begin = nullptr; |
56 | | char* pos = nullptr; |
57 | | char* end = nullptr; /// does not include padding. |
58 | | |
59 | | Chunk* prev = nullptr; |
60 | | |
61 | 10.3k | Chunk(size_t size_, Chunk* prev_) { |
62 | 10.3k | begin = reinterpret_cast<char*>(Allocator<false>::alloc(size_)); |
63 | 10.3k | pos = begin; |
64 | 10.3k | end = begin + size_ - pad_right; |
65 | 10.3k | prev = prev_; |
66 | | |
67 | 10.3k | ASAN_POISON_MEMORY_REGION(begin, size_); |
68 | 10.3k | } |
69 | | |
70 | 10.3k | ~Chunk() { |
71 | | /// We must unpoison the memory before returning to the allocator, |
72 | | /// because the allocator might not have asan integration, and the |
73 | | /// memory would stay poisoned forever. If the allocator supports |
74 | | /// asan, it will correctly poison the memory by itself. |
75 | 10.3k | ASAN_UNPOISON_MEMORY_REGION(begin, size()); |
76 | | |
77 | 10.3k | Allocator<false>::free(begin, size()); |
78 | | |
79 | 10.3k | if (prev) delete prev; |
80 | 10.3k | } |
81 | | |
82 | 31.2k | size_t size() const { return end + pad_right - begin; } |
83 | 9 | size_t remaining() const { return end - pos; } |
84 | 192 | size_t used() const { return pos - begin; } |
85 | | }; |
86 | | |
87 | | size_t growth_factor = 2; |
88 | | size_t linear_growth_threshold = 128 * 1024 * 1024; |
89 | | |
90 | | /// Last contiguous chunk of memory. |
91 | | Chunk* head = nullptr; |
92 | | size_t size_in_bytes = 0; |
93 | | size_t _initial_size = 4096; |
94 | | // The memory used by all chunks, excluding head. |
95 | | size_t _used_size_no_head = 0; |
96 | | |
97 | 116 | static size_t round_up_to_page_size(size_t s) { return (s + 4096 - 1) / 4096 * 4096; } |
98 | | |
99 | | /// If chunks size is less than 'linear_growth_threshold', then use exponential growth, otherwise - linear growth |
100 | | /// (to not allocate too much excessive memory). |
101 | 116 | size_t next_size(size_t min_next_size) { |
102 | 116 | DCHECK(head != nullptr); |
103 | 116 | size_t size_after_grow = 0; |
104 | | |
105 | 116 | if (head->size() < linear_growth_threshold) { |
106 | 114 | size_after_grow = std::max(min_next_size, head->size() * growth_factor); |
107 | 114 | } else { |
108 | | // alloc_continue() combined with linear growth results in quadratic |
109 | | // behavior: we append the data by small amounts, and when it |
110 | | // doesn't fit, we create a new chunk and copy all the previous data |
111 | | // into it. The number of times we do this is directly proportional |
112 | | // to the total size of data that is going to be serialized. To make |
113 | | // the copying happen less often, round the next size up to the |
114 | | // linear_growth_threshold. |
115 | 2 | size_after_grow = |
116 | 2 | ((min_next_size + linear_growth_threshold - 1) / linear_growth_threshold) * |
117 | 2 | linear_growth_threshold; |
118 | 2 | } |
119 | | |
120 | 116 | assert(size_after_grow >= min_next_size); |
121 | 0 | return round_up_to_page_size(size_after_grow); |
122 | 116 | } |
123 | | |
124 | | /// Add next contiguous chunk of memory with size not less than specified. |
125 | 116 | void NO_INLINE _add_chunk(size_t min_size) { |
126 | 116 | DCHECK(head != nullptr); |
127 | 116 | _used_size_no_head += head->used(); |
128 | 116 | head = new Chunk(next_size(min_size + pad_right), head); |
129 | 116 | size_in_bytes += head->size(); |
130 | 116 | } |
131 | | |
132 | 184k | void _init_head_if_needed() { |
133 | 184k | if (UNLIKELY(head == nullptr)) { |
134 | 10.2k | head = new Chunk(_initial_size, nullptr); |
135 | 10.2k | size_in_bytes += head->size(); |
136 | 10.2k | } |
137 | 184k | } |
138 | | |
139 | | friend class ArenaAllocator; |
140 | | template <size_t> |
141 | | friend class AlignedArenaAllocator; |
142 | | |
143 | | public: |
144 | | Arena(size_t initial_size_ = 4096, size_t growth_factor_ = 2, |
145 | | size_t linear_growth_threshold_ = 128 * 1024 * 1024) |
146 | | : growth_factor(growth_factor_), |
147 | | linear_growth_threshold(linear_growth_threshold_), |
148 | | _initial_size(initial_size_), |
149 | 31.9k | _used_size_no_head(0) {} |
150 | | |
151 | 31.9k | ~Arena() { delete head; } |
152 | | |
153 | | /// Get piece of memory, without alignment. |
154 | 184k | char* alloc(size_t size) { |
155 | 184k | _init_head_if_needed(); |
156 | | |
157 | 184k | if (UNLIKELY(head->pos + size > head->end)) { |
158 | 116 | _add_chunk(size); |
159 | 116 | } |
160 | | |
161 | 184k | char* res = head->pos; |
162 | 184k | head->pos += size; |
163 | 184k | ASAN_UNPOISON_MEMORY_REGION(res, size + pad_right); |
164 | 184k | return res; |
165 | 184k | } |
166 | | |
167 | | /// Get piece of memory with alignment |
168 | 3 | char* aligned_alloc(size_t size, size_t alignment) { |
169 | 3 | _init_head_if_needed(); |
170 | | |
171 | 3 | do { |
172 | 3 | void* head_pos = head->pos; |
173 | 3 | size_t space = head->end - head->pos; |
174 | | |
175 | 3 | auto res = static_cast<char*>(std::align(alignment, size, head_pos, space)); |
176 | 3 | if (res) { |
177 | 3 | head->pos = static_cast<char*>(head_pos); |
178 | 3 | head->pos += size; |
179 | 3 | ASAN_UNPOISON_MEMORY_REGION(res, size + pad_right); |
180 | 3 | return res; |
181 | 3 | } |
182 | | |
183 | 0 | _add_chunk(size + alignment); |
184 | 0 | } while (true); |
185 | 3 | } |
186 | | |
187 | | template <typename T> |
188 | | T* alloc() { |
189 | | return reinterpret_cast<T*>(aligned_alloc(sizeof(T), alignof(T))); |
190 | | } |
191 | | |
192 | | /** Rollback just performed allocation. |
193 | | * Must pass size not more that was just allocated. |
194 | | * Return the resulting head pointer, so that the caller can assert that |
195 | | * the allocation it intended to roll back was indeed the last one. |
196 | | */ |
197 | 0 | void* rollback(size_t size) { |
198 | 0 | DCHECK(head != nullptr); |
199 | |
|
200 | 0 | head->pos -= size; |
201 | 0 | ASAN_POISON_MEMORY_REGION(head->pos, size + pad_right); |
202 | 0 | return head->pos; |
203 | 0 | } |
204 | | |
205 | | /** Begin or expand a contiguous range of memory. |
206 | | * 'range_start' is the start of range. If nullptr, a new range is |
207 | | * allocated. |
208 | | * If there is no space in the current chunk to expand the range, |
209 | | * the entire range is copied to a new, bigger memory chunk, and the value |
210 | | * of 'range_start' is updated. |
211 | | * If the optional 'start_alignment' is specified, the start of range is |
212 | | * kept aligned to this value. |
213 | | * |
214 | | * NOTE This method is usable only for the last allocation made on this |
215 | | * Arena. For earlier allocations, see 'realloc' method. |
216 | | */ |
217 | | [[nodiscard]] char* alloc_continue(size_t additional_bytes, char const*& range_start, |
218 | 59 | size_t start_alignment = 0) { |
219 | 59 | if (!range_start) { |
220 | | // Start a new memory range. |
221 | 13 | char* result = start_alignment ? aligned_alloc(additional_bytes, start_alignment) |
222 | 13 | : alloc(additional_bytes); |
223 | | |
224 | 13 | range_start = result; |
225 | 13 | return result; |
226 | 13 | } |
227 | | |
228 | 46 | DCHECK(head != nullptr); |
229 | | |
230 | | // Extend an existing memory range with 'additional_bytes'. |
231 | | |
232 | | // This method only works for extending the last allocation. For lack of |
233 | | // original size, check a weaker condition: that 'begin' is at least in |
234 | | // the current Chunk. |
235 | 46 | assert(range_start >= head->begin && range_start < head->end); |
236 | | |
237 | 46 | if (head->pos + additional_bytes <= head->end) { |
238 | | // The new size fits into the last chunk, so just alloc the |
239 | | // additional size. We can alloc without alignment here, because it |
240 | | // only applies to the start of the range, and we don't change it. |
241 | 46 | return alloc(additional_bytes); |
242 | 46 | } |
243 | | |
244 | | // New range doesn't fit into this chunk, will copy to a new one. |
245 | | // |
246 | | // Note: among other things, this method is used to provide a hack-ish |
247 | | // implementation of realloc over Arenas in ArenaAllocators. It wastes a |
248 | | // lot of memory -- quadratically so when we reach the linear allocation |
249 | | // threshold. This deficiency is intentionally left as is, and should be |
250 | | // solved not by complicating this method, but by rethinking the |
251 | | // approach to memory management for aggregate function states, so that |
252 | | // we can provide a proper realloc(). |
253 | 0 | const size_t existing_bytes = head->pos - range_start; |
254 | 0 | const size_t new_bytes = existing_bytes + additional_bytes; |
255 | 0 | const char* old_range = range_start; |
256 | |
|
257 | 0 | char* new_range = |
258 | 0 | start_alignment ? aligned_alloc(new_bytes, start_alignment) : alloc(new_bytes); |
259 | |
|
260 | 0 | memcpy(new_range, old_range, existing_bytes); |
261 | |
|
262 | 0 | range_start = new_range; |
263 | 0 | return new_range + existing_bytes; |
264 | 46 | } |
265 | | |
266 | | /// NOTE Old memory region is wasted. |
267 | 0 | [[nodiscard]] char* realloc(const char* old_data, size_t old_size, size_t new_size) { |
268 | 0 | char* res = alloc(new_size); |
269 | 0 | if (old_data) { |
270 | 0 | memcpy(res, old_data, old_size); |
271 | 0 | ASAN_POISON_MEMORY_REGION(old_data, old_size); |
272 | 0 | } |
273 | 0 | return res; |
274 | 0 | } |
275 | | |
276 | | [[nodiscard]] char* aligned_realloc(const char* old_data, size_t old_size, size_t new_size, |
277 | 0 | size_t alignment) { |
278 | 0 | char* res = aligned_alloc(new_size, alignment); |
279 | 0 | if (old_data) { |
280 | 0 | memcpy(res, old_data, old_size); |
281 | 0 | ASAN_POISON_MEMORY_REGION(old_data, old_size); |
282 | 0 | } |
283 | 0 | return res; |
284 | 0 | } |
285 | | |
286 | | /// Insert string without alignment. |
287 | 25 | [[nodiscard]] const char* insert(const char* data, size_t size) { |
288 | 25 | char* res = alloc(size); |
289 | 25 | memcpy(res, data, size); |
290 | 25 | return res; |
291 | 25 | } |
292 | | |
293 | 0 | [[nodiscard]] const char* aligned_insert(const char* data, size_t size, size_t alignment) { |
294 | 0 | char* res = aligned_alloc(size, alignment); |
295 | 0 | memcpy(res, data, size); |
296 | 0 | return res; |
297 | 0 | } |
298 | | |
299 | | /** |
300 | | * Delete all the chunks before the head, usually the head is the largest chunk in the arena. |
301 | | * considering the scenario of memory reuse: |
302 | | * 1. first time, use arena alloc 64K memory, 4K each time, at this time, there are 4 chunks of 4k 8k 16k 32k in arena. |
303 | | * 2. then, clear arena, only one 32k chunk left in the arena. |
304 | | * 3. second time, same alloc 64K memory, there are 4 chunks of 4k 8k 16k 32k in arena. |
305 | | * 4. then, clear arena, only one 64k chunk left in the arena. |
306 | | * 5. third time, same alloc 64K memory, there is still only one 64K chunk in the arena, and the memory is fully reused. |
307 | | * |
308 | | * special case: if the chunk is larger than 128M, it will no longer be expanded by a multiple of 2. |
309 | | * If alloc 4G memory, 128M each time, then only one 128M chunk will be reserved after clearing, |
310 | | * and only 128M can be reused when you apply for 4G memory again. |
311 | | */ |
312 | 409k | void clear() { |
313 | 409k | if (head == nullptr) { |
314 | 409k | return; |
315 | 409k | } |
316 | | |
317 | 3 | if (head->prev) { |
318 | 0 | delete head->prev; |
319 | 0 | head->prev = nullptr; |
320 | 0 | } |
321 | 3 | head->pos = head->begin; |
322 | 3 | size_in_bytes = head->size(); |
323 | 3 | _used_size_no_head = 0; |
324 | 3 | } |
325 | | |
326 | | /// Size of chunks in bytes. |
327 | 11 | size_t size() const { return size_in_bytes; } |
328 | | |
329 | 120 | size_t used_size() const { |
330 | 120 | if (head == nullptr) { |
331 | 44 | return _used_size_no_head; |
332 | 44 | } |
333 | | |
334 | 76 | return _used_size_no_head + head->used(); |
335 | 120 | } |
336 | | |
337 | 9 | size_t remaining_space_in_current_chunk() const { |
338 | 9 | if (head == nullptr) { |
339 | 0 | return 0; |
340 | 0 | } |
341 | | |
342 | 9 | return head->remaining(); |
343 | 9 | } |
344 | | }; |
345 | | |
346 | | using ArenaPtr = std::shared_ptr<Arena>; |
347 | | using Arenas = std::vector<ArenaPtr>; |
348 | | |
349 | | } // namespace doris::vectorized |