jemalloc_nodump_allocator.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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. #pragma once
  6. #include <atomic>
  7. #include <vector>
  8. #include "port/jemalloc_helper.h"
  9. #include "port/port.h"
  10. #include "rocksdb/memory_allocator.h"
  11. #include "util/thread_local.h"
  12. #if defined(ROCKSDB_JEMALLOC) && defined(ROCKSDB_PLATFORM_POSIX)
  13. #include <sys/mman.h>
  14. #if (JEMALLOC_VERSION_MAJOR >= 5) && defined(MADV_DONTDUMP)
  15. #define ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
  16. namespace ROCKSDB_NAMESPACE {
  17. class JemallocNodumpAllocator : public MemoryAllocator {
  18. public:
  19. JemallocNodumpAllocator(JemallocAllocatorOptions& options,
  20. std::unique_ptr<extent_hooks_t>&& arena_hooks,
  21. unsigned arena_index);
  22. ~JemallocNodumpAllocator();
  23. const char* Name() const override { return "JemallocNodumpAllocator"; }
  24. void* Allocate(size_t size) override;
  25. void Deallocate(void* p) override;
  26. size_t UsableSize(void* p, size_t allocation_size) const override;
  27. private:
  28. friend Status NewJemallocNodumpAllocator(
  29. JemallocAllocatorOptions& options,
  30. std::shared_ptr<MemoryAllocator>* memory_allocator);
  31. // Custom alloc hook to replace jemalloc default alloc.
  32. static void* Alloc(extent_hooks_t* extent, void* new_addr, size_t size,
  33. size_t alignment, bool* zero, bool* commit,
  34. unsigned arena_ind);
  35. // Destroy arena on destruction of the allocator, or on failure.
  36. static Status DestroyArena(unsigned arena_index);
  37. // Destroy tcache on destruction of the allocator, or thread exit.
  38. static void DestroyThreadSpecificCache(void* ptr);
  39. // Get or create tcache. Return flag suitable to use with `mallocx`:
  40. // either MALLOCX_TCACHE_NONE or MALLOCX_TCACHE(tc).
  41. int GetThreadSpecificCache(size_t size);
  42. // A function pointer to jemalloc default alloc. Use atomic to make sure
  43. // NewJemallocNodumpAllocator is thread-safe.
  44. //
  45. // Hack: original_alloc_ needs to be static for Alloc() to access it.
  46. // alloc needs to be static to pass to jemalloc as function pointer.
  47. static std::atomic<extent_alloc_t*> original_alloc_;
  48. const JemallocAllocatorOptions options_;
  49. // Custom hooks has to outlive corresponding arena.
  50. const std::unique_ptr<extent_hooks_t> arena_hooks_;
  51. // Arena index.
  52. const unsigned arena_index_;
  53. // Hold thread-local tcache index.
  54. ThreadLocalPtr tcache_;
  55. };
  56. } // namespace ROCKSDB_NAMESPACE
  57. #endif // (JEMALLOC_VERSION_MAJOR >= 5) && MADV_DONTDUMP
  58. #endif // ROCKSDB_JEMALLOC && ROCKSDB_PLATFORM_POSIX