Coverage Report

Created: 2025-04-27 02:50

/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
75.5k
        Chunk(size_t size_, Chunk* prev_) {
62
75.5k
            begin = reinterpret_cast<char*>(Allocator<false>::alloc(size_));
63
75.5k
            pos = begin;
64
75.5k
            end = begin + size_ - pad_right;
65
75.5k
            prev = prev_;
66
67
75.5k
            ASAN_POISON_MEMORY_REGION(begin, size_);
68
75.5k
        }
69
70
75.5k
        ~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
75.5k
            ASAN_UNPOISON_MEMORY_REGION(begin, size());
76
77
75.5k
            Allocator<false>::free(begin, size());
78
79
75.5k
            if (prev) delete prev;
80
75.5k
        }
81
82
260k
        size_t size() const { return end + pad_right - begin; }
83
18
        size_t remaining() const { return end - pos; }
84
19.9k
        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
16.9k
    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
16.9k
    size_t next_size(size_t min_next_size) {
102
16.9k
        DCHECK(head != nullptr);
103
16.9k
        size_t size_after_grow = 0;
104
105
16.9k
        if (head->size() < linear_growth_threshold) {
106
16.9k
            size_after_grow = std::max(min_next_size, head->size() * growth_factor);
107
16.9k
        } 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
4
            size_after_grow =
116
4
                    ((min_next_size + linear_growth_threshold - 1) / linear_growth_threshold) *
117
4
                    linear_growth_threshold;
118
4
        }
119
120
16.9k
        assert(size_after_grow >= min_next_size);
121
0
        return round_up_to_page_size(size_after_grow);
122
16.9k
    }
123
124
    /// Add next contiguous chunk of memory with size not less than specified.
125
16.9k
    void NO_INLINE _add_chunk(size_t min_size) {
126
16.9k
        DCHECK(head != nullptr);
127
16.9k
        _used_size_no_head += head->used();
128
16.9k
        head = new Chunk(next_size(min_size + pad_right), head);
129
16.9k
        size_in_bytes += head->size();
130
16.9k
    }
131
132
674k
    void _init_head_if_needed() {
133
674k
        if (UNLIKELY(head == nullptr)) {
134
58.5k
            head = new Chunk(_initial_size, nullptr);
135
58.5k
            size_in_bytes += head->size();
136
58.5k
        }
137
674k
    }
138
139
public:
140
    Arena(size_t initial_size_ = 4096, size_t growth_factor_ = 2,
141
          size_t linear_growth_threshold_ = 128 * 1024 * 1024)
142
            : growth_factor(growth_factor_),
143
              linear_growth_threshold(linear_growth_threshold_),
144
              _initial_size(initial_size_),
145
439k
              _used_size_no_head(0) {}
146
147
439k
    ~Arena() { delete head; }
148
149
    /// Get piece of memory, without alignment.
150
674k
    char* alloc(size_t size) {
151
674k
        _init_head_if_needed();
152
153
674k
        if (UNLIKELY(head->pos + size > head->end)) {
154
16.9k
            _add_chunk(size);
155
16.9k
        }
156
157
674k
        char* res = head->pos;
158
674k
        head->pos += size;
159
674k
        ASAN_UNPOISON_MEMORY_REGION(res, size + pad_right);
160
674k
        return res;
161
674k
    }
162
163
    /// Get piece of memory with alignment
164
84
    char* aligned_alloc(size_t size, size_t alignment) {
165
84
        _init_head_if_needed();
166
167
84
        do {
168
84
            void* head_pos = head->pos;
169
84
            size_t space = head->end - head->pos;
170
171
84
            auto res = static_cast<char*>(std::align(alignment, size, head_pos, space));
172
84
            if (res) {
173
84
                head->pos = static_cast<char*>(head_pos);
174
84
                head->pos += size;
175
84
                ASAN_UNPOISON_MEMORY_REGION(res, size + pad_right);
176
84
                return res;
177
84
            }
178
179
0
            _add_chunk(size + alignment);
180
0
        } while (true);
181
84
    }
182
183
    template <typename T>
184
    T* alloc() {
185
        return reinterpret_cast<T*>(aligned_alloc(sizeof(T), alignof(T)));
186
    }
187
188
    /** Rollback just performed allocation.
189
      * Must pass size not more that was just allocated.
190
    * Return the resulting head pointer, so that the caller can assert that
191
    * the allocation it intended to roll back was indeed the last one.
192
      */
193
0
    void* rollback(size_t size) {
194
0
        DCHECK(head != nullptr);
195
196
0
        head->pos -= size;
197
0
        ASAN_POISON_MEMORY_REGION(head->pos, size + pad_right);
198
0
        return head->pos;
199
0
    }
200
201
    /** Begin or expand a contiguous range of memory.
202
      * 'range_start' is the start of range. If nullptr, a new range is
203
      * allocated.
204
      * If there is no space in the current chunk to expand the range,
205
      * the entire range is copied to a new, bigger memory chunk, and the value
206
      * of 'range_start' is updated.
207
      * If the optional 'start_alignment' is specified, the start of range is
208
      * kept aligned to this value.
209
      *
210
      * NOTE This method is usable only for the last allocation made on this
211
      * Arena. For earlier allocations, see 'realloc' method.
212
      */
213
    [[nodiscard]] char* alloc_continue(size_t additional_bytes, char const*& range_start,
214
118k
                                       size_t start_alignment = 0) {
215
118k
        if (!range_start) {
216
            // Start a new memory range.
217
934
            char* result = start_alignment ? aligned_alloc(additional_bytes, start_alignment)
218
934
                                           : alloc(additional_bytes);
219
220
934
            range_start = result;
221
934
            return result;
222
934
        }
223
224
117k
        DCHECK(head != nullptr);
225
226
        // Extend an existing memory range with 'additional_bytes'.
227
228
        // This method only works for extending the last allocation. For lack of
229
        // original size, check a weaker condition: that 'begin' is at least in
230
        // the current Chunk.
231
117k
        assert(range_start >= head->begin && range_start < head->end);
232
233
117k
        if (head->pos + additional_bytes <= head->end) {
234
            // The new size fits into the last chunk, so just alloc the
235
            // additional size. We can alloc without alignment here, because it
236
            // only applies to the start of the range, and we don't change it.
237
117k
            return alloc(additional_bytes);
238
117k
        }
239
240
        // New range doesn't fit into this chunk, will copy to a new one.
241
        //
242
        // Note: among other things, this method is used to provide a hack-ish
243
        // implementation of realloc over Arenas in ArenaAllocators. It wastes a
244
        // lot of memory -- quadratically so when we reach the linear allocation
245
        // threshold. This deficiency is intentionally left as is, and should be
246
        // solved not by complicating this method, but by rethinking the
247
        // approach to memory management for aggregate function states, so that
248
        // we can provide a proper realloc().
249
56
        const size_t existing_bytes = head->pos - range_start;
250
56
        const size_t new_bytes = existing_bytes + additional_bytes;
251
56
        const char* old_range = range_start;
252
253
56
        char* new_range =
254
56
                start_alignment ? aligned_alloc(new_bytes, start_alignment) : alloc(new_bytes);
255
256
56
        memcpy(new_range, old_range, existing_bytes);
257
258
56
        range_start = new_range;
259
56
        return new_range + existing_bytes;
260
117k
    }
261
262
    /// NOTE Old memory region is wasted.
263
0
    [[nodiscard]] char* realloc(const char* old_data, size_t old_size, size_t new_size) {
264
0
        char* res = alloc(new_size);
265
0
        if (old_data) {
266
0
            memcpy(res, old_data, old_size);
267
0
            ASAN_POISON_MEMORY_REGION(old_data, old_size);
268
0
        }
269
0
        return res;
270
0
    }
271
272
    [[nodiscard]] char* aligned_realloc(const char* old_data, size_t old_size, size_t new_size,
273
0
                                        size_t alignment) {
274
0
        char* res = aligned_alloc(new_size, alignment);
275
0
        if (old_data) {
276
0
            memcpy(res, old_data, old_size);
277
0
            ASAN_POISON_MEMORY_REGION(old_data, old_size);
278
0
        }
279
0
        return res;
280
0
    }
281
282
    /// Insert string without alignment.
283
140
    [[nodiscard]] const char* insert(const char* data, size_t size) {
284
140
        char* res = alloc(size);
285
140
        memcpy(res, data, size);
286
140
        return res;
287
140
    }
288
289
0
    [[nodiscard]] const char* aligned_insert(const char* data, size_t size, size_t alignment) {
290
0
        char* res = aligned_alloc(size, alignment);
291
0
        memcpy(res, data, size);
292
0
        return res;
293
0
    }
294
295
    /**
296
    * Delete all the chunks before the head, usually the head is the largest chunk in the arena.
297
    * considering the scenario of memory reuse:
298
    * 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.
299
    * 2. then, clear arena, only one 32k chunk left in the arena.
300
    * 3. second time, same alloc 64K memory, there are 4 chunks of 4k 8k 16k 32k in arena.
301
    * 4. then, clear arena, only one 64k chunk left in the arena.
302
    * 5. third time, same alloc 64K memory, there is still only one 64K chunk in the arena, and the memory is fully reused.
303
    *
304
    * special case: if the chunk is larger than 128M, it will no longer be expanded by a multiple of 2.
305
    * If alloc 4G memory, 128M each time, then only one 128M chunk will be reserved after clearing,
306
    * and only 128M can be reused when you apply for 4G memory again.
307
    */
308
847k
    void clear() {
309
847k
        if (head == nullptr) {
310
847k
            return;
311
847k
        }
312
313
4
        if (head->prev) {
314
0
            delete head->prev;
315
0
            head->prev = nullptr;
316
0
        }
317
4
        head->pos = head->begin;
318
4
        size_in_bytes = head->size();
319
4
        _used_size_no_head = 0;
320
4
    }
321
322
    /// Size of chunks in bytes.
323
59.3k
    size_t size() const { return size_in_bytes; }
324
325
5.74k
    size_t used_size() const {
326
5.74k
        if (head == nullptr) {
327
2.72k
            return _used_size_no_head;
328
2.72k
        }
329
330
3.02k
        return _used_size_no_head + head->used();
331
5.74k
    }
332
333
18
    size_t remaining_space_in_current_chunk() const {
334
18
        if (head == nullptr) {
335
0
            return 0;
336
0
        }
337
338
18
        return head->remaining();
339
18
    }
340
};
341
342
using ArenaPtr = std::shared_ptr<Arena>;
343
using Arenas = std::vector<ArenaPtr>;
344
345
} // namespace doris::vectorized