1 // Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
2 // This source code is licensed under both the GPLv2 (found in the
3 // COPYING file in the root directory) and Apache 2.0 License
4 // (found in the LICENSE.Apache file in the root directory).
11 #include "port/jemalloc_helper.h"
12 #include "port/port.h"
13 #include "rocksdb/memory_allocator.h"
14 #include "util/core_local.h"
15 #include "util/thread_local.h"
17 #if defined(ROCKSDB_JEMALLOC) && defined(ROCKSDB_PLATFORM_POSIX)
21 #if (JEMALLOC_VERSION_MAJOR >= 5) && defined(MADV_DONTDUMP)
22 #define ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
26 class JemallocNodumpAllocator
: public MemoryAllocator
{
28 JemallocNodumpAllocator(JemallocAllocatorOptions
& options
,
29 std::unique_ptr
<extent_hooks_t
>&& arena_hooks
,
30 unsigned arena_index
);
31 ~JemallocNodumpAllocator();
33 const char* Name() const override
{ return "JemallocNodumpAllocator"; }
34 void* Allocate(size_t size
) override
;
35 void Deallocate(void* p
) override
;
36 size_t UsableSize(void* p
, size_t allocation_size
) const override
;
39 friend Status
NewJemallocNodumpAllocator(
40 JemallocAllocatorOptions
& options
,
41 std::shared_ptr
<MemoryAllocator
>* memory_allocator
);
43 // Custom alloc hook to replace jemalloc default alloc.
44 static void* Alloc(extent_hooks_t
* extent
, void* new_addr
, size_t size
,
45 size_t alignment
, bool* zero
, bool* commit
,
48 // Destroy arena on destruction of the allocator, or on failure.
49 static Status
DestroyArena(unsigned arena_index
);
51 // Destroy tcache on destruction of the allocator, or thread exit.
52 static void DestroyThreadSpecificCache(void* ptr
);
54 // Get or create tcache. Return flag suitable to use with `mallocx`:
55 // either MALLOCX_TCACHE_NONE or MALLOCX_TCACHE(tc).
56 int GetThreadSpecificCache(size_t size
);
58 // A function pointer to jemalloc default alloc. Use atomic to make sure
59 // NewJemallocNodumpAllocator is thread-safe.
61 // Hack: original_alloc_ needs to be static for Alloc() to access it.
62 // alloc needs to be static to pass to jemalloc as function pointer.
63 static std::atomic
<extent_alloc_t
*> original_alloc_
;
65 const JemallocAllocatorOptions options_
;
67 // Custom hooks has to outlive corresponding arena.
68 const std::unique_ptr
<extent_hooks_t
> arena_hooks_
;
71 const unsigned arena_index_
;
73 // Hold thread-local tcache index.
74 ThreadLocalPtr tcache_
;
77 } // namespace rocksdb
78 #endif // (JEMALLOC_VERSION_MAJOR >= 5) && MADV_DONTDUMP
79 #endif // ROCKSDB_JEMALLOC && ROCKSDB_PLATFORM_POSIX