Coverage Report

Created: 2025-04-25 17:29

/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
37.7k
        Chunk(size_t size_, Chunk* prev_) {
62
37.7k
            begin = reinterpret_cast<char*>(Allocator<false>::alloc(size_));
63
37.7k
            pos = begin;
64
37.7k
            end = begin + size_ - pad_right;
65
37.7k
            prev = prev_;
66
67
37.7k
            ASAN_POISON_MEMORY_REGION(begin, size_);
68
37.7k
        }
69
70
37.7k
        ~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
37.7k
            ASAN_UNPOISON_MEMORY_REGION(begin, size());
76
77
37.7k
            Allocator<false>::free(begin, size());
78
79
37.7k
            if (prev) delete prev;
80
37.7k
        }
81
82
130k
        size_t size() const { return end + pad_right - begin; }
83
9
        size_t remaining() const { return end - pos; }
84
9.96k
        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
8.45k
    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
8.45k
    size_t next_size(size_t min_next_size) {
102
8.45k
        DCHECK(head != nullptr);
103
8.45k
        size_t size_after_grow = 0;
104
105
8.45k
        if (head->size() < linear_growth_threshold) {
106
8.45k
            size_after_grow = std::max(min_next_size, head->size() * growth_factor);
107
8.45k
        } 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
8.45k
        assert(size_after_grow >= min_next_size);
121
0
        return round_up_to_page_size(size_after_grow);
122
8.45k
    }
123
124
    /// Add next contiguous chunk of memory with size not less than specified.
125
8.45k
    void NO_INLINE _add_chunk(size_t min_size) {
126
8.45k
        DCHECK(head != nullptr);
127
8.45k
        _used_size_no_head += head->used();
128
8.45k
        head = new Chunk(next_size(min_size + pad_right), head);
129
8.45k
        size_in_bytes += head->size();
130
8.45k
    }
131
132
337k
    void _init_head_if_needed() {
133
337k
        if (UNLIKELY(head == nullptr)) {
134
29.2k
            head = new Chunk(_initial_size, nullptr);
135
29.2k
            size_in_bytes += head->size();
136
29.2k
        }
137
337k
    }
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
219k
              _used_size_no_head(0) {}
146
147
219k
    ~Arena() { delete head; }
148
149
    /// Get piece of memory, without alignment.
150
337k
    char* alloc(size_t size) {
151
337k
        _init_head_if_needed();
152
153
337k
        if (UNLIKELY(head->pos + size > head->end)) {
154
8.45k
            _add_chunk(size);
155
8.45k
        }
156
157
337k
        char* res = head->pos;
158
337k
        head->pos += size;
159
337k
        ASAN_UNPOISON_MEMORY_REGION(res, size + pad_right);
160
337k
        return res;
161
337k
    }
162
163
    /// Get piece of memory with alignment
164
42
    char* aligned_alloc(size_t size, size_t alignment) {
165
42
        _init_head_if_needed();
166
167
42
        do {
168
42
            void* head_pos = head->pos;
169
42
            size_t space = head->end - head->pos;
170
171
42
            auto res = static_cast<char*>(std::align(alignment, size, head_pos, space));
172
42
            if (res) {
173
42
                head->pos = static_cast<char*>(head_pos);
174
42
                head->pos += size;
175
42
                ASAN_UNPOISON_MEMORY_REGION(res, size + pad_right);
176
42
                return res;
177
42
            }
178
179
0
            _add_chunk(size + alignment);
180
0
        } while (true);
181
42
    }
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
59.3k
                                       size_t start_alignment = 0) {
215
59.3k
        if (!range_start) {
216
            // Start a new memory range.
217
467
            char* result = start_alignment ? aligned_alloc(additional_bytes, start_alignment)
218
467
                                           : alloc(additional_bytes);
219
220
467
            range_start = result;
221
467
            return result;
222
467
        }
223
224
58.8k
        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
58.8k
        assert(range_start >= head->begin && range_start < head->end);
232
233
58.8k
        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
58.8k
            return alloc(additional_bytes);
238
58.8k
        }
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
28
        const size_t existing_bytes = head->pos - range_start;
250
28
        const size_t new_bytes = existing_bytes + additional_bytes;
251
28
        const char* old_range = range_start;
252
253
28
        char* new_range =
254
28
                start_alignment ? aligned_alloc(new_bytes, start_alignment) : alloc(new_bytes);
255
256
28
        memcpy(new_range, old_range, existing_bytes);
257
258
28
        range_start = new_range;
259
28
        return new_range + existing_bytes;
260
58.8k
    }
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
70
    [[nodiscard]] const char* insert(const char* data, size_t size) {
284
70
        char* res = alloc(size);
285
70
        memcpy(res, data, size);
286
70
        return res;
287
70
    }
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
423k
    void clear() {
309
423k
        if (head == nullptr) {
310
423k
            return;
311
423k
        }
312
313
2
        if (head->prev) {
314
0
            delete head->prev;
315
0
            head->prev = nullptr;
316
0
        }
317
2
        head->pos = head->begin;
318
2
        size_in_bytes = head->size();
319
2
        _used_size_no_head = 0;
320
2
    }
321
322
    /// Size of chunks in bytes.
323
29.6k
    size_t size() const { return size_in_bytes; }
324
325
2.87k
    size_t used_size() const {
326
2.87k
        if (head == nullptr) {
327
1.36k
            return _used_size_no_head;
328
1.36k
        }
329
330
1.51k
        return _used_size_no_head + head->used();
331
2.87k
    }
332
333
9
    size_t remaining_space_in_current_chunk() const {
334
9
        if (head == nullptr) {
335
0
            return 0;
336
0
        }
337
338
9
        return head->remaining();
339
9
    }
340
};
341
342
using ArenaPtr = std::shared_ptr<Arena>;
343
using Arenas = std::vector<ArenaPtr>;
344
345
} // namespace doris::vectorized