]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/util/jemalloc_nodump_allocator.h
import 14.2.4 nautilus point release
[ceph.git] / ceph / src / rocksdb / util / jemalloc_nodump_allocator.h
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).
5
6 #pragma once
7
8 #include <atomic>
9 #include <vector>
10
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"
16
17 #if defined(ROCKSDB_JEMALLOC) && defined(ROCKSDB_PLATFORM_POSIX)
18
19 #include <sys/mman.h>
20
21 #if (JEMALLOC_VERSION_MAJOR >= 5) && defined(MADV_DONTDUMP)
22 #define ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
23
24 namespace rocksdb {
25
26 class JemallocNodumpAllocator : public MemoryAllocator {
27 public:
28 JemallocNodumpAllocator(JemallocAllocatorOptions& options,
29 std::unique_ptr<extent_hooks_t>&& arena_hooks,
30 unsigned arena_index);
31 ~JemallocNodumpAllocator();
32
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;
37
38 private:
39 friend Status NewJemallocNodumpAllocator(
40 JemallocAllocatorOptions& options,
41 std::shared_ptr<MemoryAllocator>* memory_allocator);
42
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,
46 unsigned arena_ind);
47
48 // Destroy arena on destruction of the allocator, or on failure.
49 static Status DestroyArena(unsigned arena_index);
50
51 // Destroy tcache on destruction of the allocator, or thread exit.
52 static void DestroyThreadSpecificCache(void* ptr);
53
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);
57
58 // A function pointer to jemalloc default alloc. Use atomic to make sure
59 // NewJemallocNodumpAllocator is thread-safe.
60 //
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_;
64
65 const JemallocAllocatorOptions options_;
66
67 // Custom hooks has to outlive corresponding arena.
68 const std::unique_ptr<extent_hooks_t> arena_hooks_;
69
70 // Arena index.
71 const unsigned arena_index_;
72
73 // Hold thread-local tcache index.
74 ThreadLocalPtr tcache_;
75 };
76
77 } // namespace rocksdb
78 #endif // (JEMALLOC_VERSION_MAJOR >= 5) && MADV_DONTDUMP
79 #endif // ROCKSDB_JEMALLOC && ROCKSDB_PLATFORM_POSIX