Repository navigation
Expand file tree
/
Copy pathmemory_pool.cc
More file actions
488 lines (409 loc) · 13.6 KB
/
Copy pathmemory_pool.cc
File metadata and controls
488 lines (409 loc) · 13.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#include "arrow/memory_pool.h"
#include <algorithm> // IWYU pragma: keep
#include <cstdlib> // IWYU pragma: keep
#include <cstring> // IWYU pragma: keep
#include <iostream> // IWYU pragma: keep
#include <limits>
#include <memory>
#include "arrow/status.h"
#include "arrow/util/logging.h" // IWYU pragma: keep
#ifdef ARROW_JEMALLOC
// Needed to support jemalloc 3 and 4
#define JEMALLOC_MANGLE
// Explicitly link to our version of jemalloc
#include "jemalloc_ep/dist/include/jemalloc/jemalloc.h"
#endif
#ifdef ARROW_MIMALLOC
#include <mimalloc.h>
#endif
#ifdef ARROW_JEMALLOC
// Compile-time configuration for jemalloc options.
// Note the prefix ("je_arrow_") must match the symbol prefix given when
// building jemalloc.
// See discussion in https://github.com/jemalloc/jemalloc/issues/1621
#ifdef NDEBUG
const char* je_arrow_malloc_conf = "oversize_threshold:0";
#else
// In debug mode, add memory poisoning on alloc / free
const char* je_arrow_malloc_conf = "oversize_threshold:0,junk:true";
#endif
#endif
namespace arrow {
constexpr size_t kAlignment = 64;
namespace {
// A static piece of memory for 0-size allocations, so as to return
// an aligned non-null pointer.
alignas(kAlignment) static uint8_t zero_size_area[1];
// Helper class directing allocations to the standard system allocator.
class SystemAllocator {
public:
// Allocate memory according to the alignment requirements for Arrow
// (as of May 2016 64 bytes)
static Status AllocateAligned(int64_t size, uint8_t** out) {
if (size == 0) {
*out = zero_size_area;
return Status::OK();
}
#ifdef _WIN32
// Special code path for Windows
*out = reinterpret_cast<uint8_t*>(
_aligned_malloc(static_cast<size_t>(size), kAlignment));
if (!*out) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
#else
const int result = posix_memalign(reinterpret_cast<void**>(out), kAlignment,
static_cast<size_t>(size));
if (result == ENOMEM) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
if (result == EINVAL) {
return Status::Invalid("invalid alignment parameter: ", kAlignment);
}
#endif
return Status::OK();
}
static Status ReallocateAligned(int64_t old_size, int64_t new_size, uint8_t** ptr) {
uint8_t* previous_ptr = *ptr;
if (previous_ptr == zero_size_area) {
DCHECK_EQ(old_size, 0);
return AllocateAligned(new_size, ptr);
}
if (new_size == 0) {
DeallocateAligned(previous_ptr, old_size);
*ptr = zero_size_area;
return Status::OK();
}
// Note: We cannot use realloc() here as it doesn't guarantee alignment.
// Allocate new chunk
uint8_t* out = nullptr;
RETURN_NOT_OK(AllocateAligned(new_size, &out));
DCHECK(out);
// Copy contents and release old memory chunk
memcpy(out, *ptr, static_cast<size_t>(std::min(new_size, old_size)));
#ifdef _WIN32
_aligned_free(*ptr);
#else
free(*ptr);
#endif // defined(_WIN32)
*ptr = out;
return Status::OK();
}
static void DeallocateAligned(uint8_t* ptr, int64_t size) {
if (ptr == zero_size_area) {
DCHECK_EQ(size, 0);
} else {
#ifdef _WIN32
_aligned_free(ptr);
#else
free(ptr);
#endif
}
}
};
#ifdef ARROW_JEMALLOC
// Helper class directing allocations to the jemalloc allocator.
class JemallocAllocator {
public:
static Status AllocateAligned(int64_t size, uint8_t** out) {
if (size == 0) {
*out = zero_size_area;
return Status::OK();
}
*out = reinterpret_cast<uint8_t*>(
mallocx(static_cast<size_t>(size), MALLOCX_ALIGN(kAlignment)));
if (*out == NULL) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
return Status::OK();
}
static Status ReallocateAligned(int64_t old_size, int64_t new_size, uint8_t** ptr) {
uint8_t* previous_ptr = *ptr;
if (previous_ptr == zero_size_area) {
DCHECK_EQ(old_size, 0);
return AllocateAligned(new_size, ptr);
}
if (new_size == 0) {
DeallocateAligned(previous_ptr, old_size);
*ptr = zero_size_area;
return Status::OK();
}
*ptr = reinterpret_cast<uint8_t*>(
rallocx(*ptr, static_cast<size_t>(new_size), MALLOCX_ALIGN(kAlignment)));
if (*ptr == NULL) {
*ptr = previous_ptr;
return Status::OutOfMemory("realloc of size ", new_size, " failed");
}
return Status::OK();
}
static void DeallocateAligned(uint8_t* ptr, int64_t size) {
if (ptr == zero_size_area) {
DCHECK_EQ(size, 0);
} else {
dallocx(ptr, MALLOCX_ALIGN(kAlignment));
}
}
};
#endif // defined(ARROW_JEMALLOC)
#ifdef ARROW_MIMALLOC
// Helper class directing allocations to the mimalloc allocator.
class MimallocAllocator {
public:
static Status AllocateAligned(int64_t size, uint8_t** out) {
if (size == 0) {
*out = zero_size_area;
return Status::OK();
}
*out = reinterpret_cast<uint8_t*>(
mi_malloc_aligned(static_cast<size_t>(size), kAlignment));
if (*out == NULL) {
return Status::OutOfMemory("malloc of size ", size, " failed");
}
return Status::OK();
}
static Status ReallocateAligned(int64_t old_size, int64_t new_size, uint8_t** ptr) {
uint8_t* previous_ptr = *ptr;
if (previous_ptr == zero_size_area) {
DCHECK_EQ(old_size, 0);
return AllocateAligned(new_size, ptr);
}
if (new_size == 0) {
DeallocateAligned(previous_ptr, old_size);
*ptr = zero_size_area;
return Status::OK();
}
*ptr = reinterpret_cast<uint8_t*>(
mi_realloc_aligned(previous_ptr, static_cast<size_t>(new_size), kAlignment));
if (*ptr == NULL) {
*ptr = previous_ptr;
return Status::OutOfMemory("realloc of size ", new_size, " failed");
}
return Status::OK();
}
static void DeallocateAligned(uint8_t* ptr, int64_t size) {
if (ptr == zero_size_area) {
DCHECK_EQ(size, 0);
} else {
mi_free(ptr);
}
}
};
#endif // defined(ARROW_MIMALLOC)
} // namespace
MemoryPool::MemoryPool() {}
MemoryPool::~MemoryPool() {}
int64_t MemoryPool::max_memory() const { return -1; }
///////////////////////////////////////////////////////////////////////
// MemoryPool implementation that delegates its core duty
// to an Allocator class.
#ifndef NDEBUG
static constexpr uint8_t kAllocPoison = 0xBC;
static constexpr uint8_t kReallocPoison = 0xBD;
static constexpr uint8_t kDeallocPoison = 0xBE;
#endif
template <typename Allocator>
class BaseMemoryPoolImpl : public MemoryPool {
public:
~BaseMemoryPoolImpl() override {}
Status Allocate(int64_t size, uint8_t** out) override {
if (size < 0) {
return Status::Invalid("negative malloc size");
}
if (static_cast<uint64_t>(size) >= std::numeric_limits<size_t>::max()) {
return Status::CapacityError("malloc size overflows size_t");
}
RETURN_NOT_OK(Allocator::AllocateAligned(size, out));
#ifndef NDEBUG
// Poison data
if (size > 0) {
DCHECK_NE(*out, nullptr);
(*out)[0] = kAllocPoison;
(*out)[size - 1] = kAllocPoison;
}
#endif
stats_.UpdateAllocatedBytes(size);
return Status::OK();
}
Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) override {
if (new_size < 0) {
return Status::Invalid("negative realloc size");
}
if (static_cast<uint64_t>(new_size) >= std::numeric_limits<size_t>::max()) {
return Status::CapacityError("realloc overflows size_t");
}
RETURN_NOT_OK(Allocator::ReallocateAligned(old_size, new_size, ptr));
#ifndef NDEBUG
// Poison data
if (new_size > old_size) {
DCHECK_NE(*ptr, nullptr);
(*ptr)[old_size] = kReallocPoison;
(*ptr)[new_size - 1] = kReallocPoison;
}
#endif
stats_.UpdateAllocatedBytes(new_size - old_size);
return Status::OK();
}
void Free(uint8_t* buffer, int64_t size) override {
#ifndef NDEBUG
// Poison data
if (size > 0) {
DCHECK_NE(buffer, nullptr);
buffer[0] = kDeallocPoison;
buffer[size - 1] = kDeallocPoison;
}
#endif
Allocator::DeallocateAligned(buffer, size);
stats_.UpdateAllocatedBytes(-size);
}
int64_t bytes_allocated() const override { return stats_.bytes_allocated(); }
int64_t max_memory() const override { return stats_.max_memory(); }
protected:
internal::MemoryPoolStats stats_;
};
class SystemMemoryPool : public BaseMemoryPoolImpl<SystemAllocator> {
public:
std::string backend_name() const override { return "system"; }
};
#ifdef ARROW_JEMALLOC
class JemallocMemoryPool : public BaseMemoryPoolImpl<JemallocAllocator> {
public:
std::string backend_name() const override { return "jemalloc"; }
};
#endif
#ifdef ARROW_MIMALLOC
class MimallocMemoryPool : public BaseMemoryPoolImpl<MimallocAllocator> {
public:
std::string backend_name() const override { return "mimalloc"; }
};
#endif
#ifdef ARROW_JEMALLOC
using DefaultMemoryPool = JemallocMemoryPool;
#elif defined(ARROW_MIMALLOC)
using DefaultMemoryPool = MimallocMemoryPool;
#else
using DefaultMemoryPool = SystemMemoryPool;
#endif
std::unique_ptr<MemoryPool> MemoryPool::CreateDefault() {
return std::unique_ptr<MemoryPool>(new DefaultMemoryPool);
}
static SystemMemoryPool system_pool;
#ifdef ARROW_JEMALLOC
static JemallocMemoryPool jemalloc_pool;
#endif
#ifdef ARROW_MIMALLOC
static MimallocMemoryPool mimalloc_pool;
#endif
MemoryPool* system_memory_pool() { return &system_pool; }
Status jemalloc_memory_pool(MemoryPool** out) {
#ifdef ARROW_JEMALLOC
*out = &jemalloc_pool;
return Status::OK();
#else
return Status::NotImplemented("This Arrow build does not enable jemalloc");
#endif
}
Status mimalloc_memory_pool(MemoryPool** out) {
#ifdef ARROW_MIMALLOC
*out = &mimalloc_pool;
return Status::OK();
#else
return Status::NotImplemented("This Arrow build does not enable mimalloc");
#endif
}
MemoryPool* default_memory_pool() {
#ifdef ARROW_JEMALLOC
return &jemalloc_pool;
#elif defined(ARROW_MIMALLOC)
return &mimalloc_pool;
#else
return &system_pool;
#endif
}
///////////////////////////////////////////////////////////////////////
// LoggingMemoryPool implementation
LoggingMemoryPool::LoggingMemoryPool(MemoryPool* pool) : pool_(pool) {}
Status LoggingMemoryPool::Allocate(int64_t size, uint8_t** out) {
Status s = pool_->Allocate(size, out);
std::cout << "Allocate: size = " << size << std::endl;
return s;
}
Status LoggingMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
Status s = pool_->Reallocate(old_size, new_size, ptr);
std::cout << "Reallocate: old_size = " << old_size << " - new_size = " << new_size
<< std::endl;
return s;
}
void LoggingMemoryPool::Free(uint8_t* buffer, int64_t size) {
pool_->Free(buffer, size);
std::cout << "Free: size = " << size << std::endl;
}
int64_t LoggingMemoryPool::bytes_allocated() const {
int64_t nb_bytes = pool_->bytes_allocated();
std::cout << "bytes_allocated: " << nb_bytes << std::endl;
return nb_bytes;
}
int64_t LoggingMemoryPool::max_memory() const {
int64_t mem = pool_->max_memory();
std::cout << "max_memory: " << mem << std::endl;
return mem;
}
std::string LoggingMemoryPool::backend_name() const { return pool_->backend_name(); }
///////////////////////////////////////////////////////////////////////
// ProxyMemoryPool implementation
class ProxyMemoryPool::ProxyMemoryPoolImpl {
public:
explicit ProxyMemoryPoolImpl(MemoryPool* pool) : pool_(pool) {}
Status Allocate(int64_t size, uint8_t** out) {
RETURN_NOT_OK(pool_->Allocate(size, out));
stats_.UpdateAllocatedBytes(size);
return Status::OK();
}
Status Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
RETURN_NOT_OK(pool_->Reallocate(old_size, new_size, ptr));
stats_.UpdateAllocatedBytes(new_size - old_size);
return Status::OK();
}
void Free(uint8_t* buffer, int64_t size) {
pool_->Free(buffer, size);
stats_.UpdateAllocatedBytes(-size);
}
int64_t bytes_allocated() const { return stats_.bytes_allocated(); }
int64_t max_memory() const { return stats_.max_memory(); }
std::string backend_name() const { return pool_->backend_name(); }
private:
MemoryPool* pool_;
internal::MemoryPoolStats stats_;
};
ProxyMemoryPool::ProxyMemoryPool(MemoryPool* pool) {
impl_.reset(new ProxyMemoryPoolImpl(pool));
}
ProxyMemoryPool::~ProxyMemoryPool() {}
Status ProxyMemoryPool::Allocate(int64_t size, uint8_t** out) {
return impl_->Allocate(size, out);
}
Status ProxyMemoryPool::Reallocate(int64_t old_size, int64_t new_size, uint8_t** ptr) {
return impl_->Reallocate(old_size, new_size, ptr);
}
void ProxyMemoryPool::Free(uint8_t* buffer, int64_t size) {
return impl_->Free(buffer, size);
}
int64_t ProxyMemoryPool::bytes_allocated() const { return impl_->bytes_allocated(); }
int64_t ProxyMemoryPool::max_memory() const { return impl_->max_memory(); }
std::string ProxyMemoryPool::backend_name() const { return impl_->backend_name(); }
} // namespace arrow