2018-10-27 02:27:13 +02:00
|
|
|
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
|
|
// This source code is licensed under both the GPLv2 (found in the
|
|
|
|
// COPYING file in the root directory) and Apache 2.0 License
|
|
|
|
// (found in the LICENSE.Apache file in the root directory).
|
|
|
|
|
2019-05-31 02:39:43 +02:00
|
|
|
#include "memory/jemalloc_nodump_allocator.h"
|
2018-10-27 02:27:13 +02:00
|
|
|
|
|
|
|
#include <string>
|
2018-11-20 07:33:39 +01:00
|
|
|
#include <thread>
|
2018-10-27 02:27:13 +02:00
|
|
|
|
2018-11-20 07:33:39 +01:00
|
|
|
#include "port/likely.h"
|
|
|
|
#include "port/port.h"
|
2018-10-27 02:27:13 +02:00
|
|
|
#include "util/string_util.h"
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
namespace ROCKSDB_NAMESPACE {
|
2018-10-27 02:27:13 +02:00
|
|
|
|
|
|
|
#ifdef ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
|
|
|
|
|
|
|
|
std::atomic<extent_alloc_t*> JemallocNodumpAllocator::original_alloc_{nullptr};
|
|
|
|
|
|
|
|
JemallocNodumpAllocator::JemallocNodumpAllocator(
|
2018-11-30 02:30:33 +01:00
|
|
|
JemallocAllocatorOptions& options,
|
2018-11-20 07:33:39 +01:00
|
|
|
std::unique_ptr<extent_hooks_t>&& arena_hooks, unsigned arena_index)
|
2018-11-30 02:30:33 +01:00
|
|
|
: options_(options),
|
|
|
|
arena_hooks_(std::move(arena_hooks)),
|
2018-11-20 07:33:39 +01:00
|
|
|
arena_index_(arena_index),
|
|
|
|
tcache_(&JemallocNodumpAllocator::DestroyThreadSpecificCache) {}
|
|
|
|
|
2018-11-30 02:30:33 +01:00
|
|
|
int JemallocNodumpAllocator::GetThreadSpecificCache(size_t size) {
|
2018-11-20 07:33:39 +01:00
|
|
|
// We always enable tcache. The only corner case is when there are a ton of
|
|
|
|
// threads accessing with low frequency, then it could consume a lot of
|
|
|
|
// memory (may reach # threads * ~1MB) without bringing too much benefit.
|
2018-11-30 02:30:33 +01:00
|
|
|
if (options_.limit_tcache_size && (size <= options_.tcache_size_lower_bound ||
|
|
|
|
size > options_.tcache_size_upper_bound)) {
|
|
|
|
return MALLOCX_TCACHE_NONE;
|
|
|
|
}
|
2018-11-20 07:33:39 +01:00
|
|
|
unsigned* tcache_index = reinterpret_cast<unsigned*>(tcache_.Get());
|
|
|
|
if (UNLIKELY(tcache_index == nullptr)) {
|
|
|
|
// Instantiate tcache.
|
|
|
|
tcache_index = new unsigned(0);
|
|
|
|
size_t tcache_index_size = sizeof(unsigned);
|
|
|
|
int ret =
|
|
|
|
mallctl("tcache.create", tcache_index, &tcache_index_size, nullptr, 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
// No good way to expose the error. Silently disable tcache.
|
|
|
|
delete tcache_index;
|
|
|
|
return MALLOCX_TCACHE_NONE;
|
|
|
|
}
|
|
|
|
tcache_.Reset(static_cast<void*>(tcache_index));
|
|
|
|
}
|
|
|
|
return MALLOCX_TCACHE(*tcache_index);
|
2018-10-27 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void* JemallocNodumpAllocator::Allocate(size_t size) {
|
2018-11-30 02:30:33 +01:00
|
|
|
int tcache_flag = GetThreadSpecificCache(size);
|
2018-11-20 07:33:39 +01:00
|
|
|
return mallocx(size, MALLOCX_ARENA(arena_index_) | tcache_flag);
|
2018-10-27 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
2018-11-20 07:33:39 +01:00
|
|
|
void JemallocNodumpAllocator::Deallocate(void* p) {
|
|
|
|
// Obtain tcache.
|
2018-11-30 02:30:33 +01:00
|
|
|
size_t size = 0;
|
|
|
|
if (options_.limit_tcache_size) {
|
|
|
|
size = malloc_usable_size(p);
|
|
|
|
}
|
|
|
|
int tcache_flag = GetThreadSpecificCache(size);
|
2018-11-20 07:33:39 +01:00
|
|
|
// No need to pass arena index to dallocx(). Jemalloc will find arena index
|
|
|
|
// from its own metadata.
|
|
|
|
dallocx(p, tcache_flag);
|
|
|
|
}
|
2018-10-27 02:27:13 +02:00
|
|
|
|
|
|
|
void* JemallocNodumpAllocator::Alloc(extent_hooks_t* extent, void* new_addr,
|
|
|
|
size_t size, size_t alignment, bool* zero,
|
|
|
|
bool* commit, unsigned arena_ind) {
|
|
|
|
extent_alloc_t* original_alloc =
|
|
|
|
original_alloc_.load(std::memory_order_relaxed);
|
|
|
|
assert(original_alloc != nullptr);
|
|
|
|
void* result = original_alloc(extent, new_addr, size, alignment, zero, commit,
|
|
|
|
arena_ind);
|
|
|
|
if (result != nullptr) {
|
|
|
|
int ret = madvise(result, size, MADV_DONTDUMP);
|
|
|
|
if (ret != 0) {
|
|
|
|
fprintf(
|
|
|
|
stderr,
|
|
|
|
"JemallocNodumpAllocator failed to set MADV_DONTDUMP, error code: %d",
|
|
|
|
ret);
|
|
|
|
assert(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2018-11-20 07:33:39 +01:00
|
|
|
Status JemallocNodumpAllocator::DestroyArena(unsigned arena_index) {
|
|
|
|
assert(arena_index != 0);
|
|
|
|
std::string key = "arena." + ToString(arena_index) + ".destroy";
|
2018-10-27 02:27:13 +02:00
|
|
|
int ret = mallctl(key.c_str(), nullptr, 0, nullptr, 0);
|
|
|
|
if (ret != 0) {
|
2018-11-20 07:33:39 +01:00
|
|
|
return Status::Incomplete("Failed to destroy jemalloc arena, error code: " +
|
|
|
|
ToString(ret));
|
2018-10-27 02:27:13 +02:00
|
|
|
}
|
2018-11-20 07:33:39 +01:00
|
|
|
return Status::OK();
|
|
|
|
}
|
|
|
|
|
|
|
|
void JemallocNodumpAllocator::DestroyThreadSpecificCache(void* ptr) {
|
|
|
|
assert(ptr != nullptr);
|
|
|
|
unsigned* tcache_index = static_cast<unsigned*>(ptr);
|
|
|
|
size_t tcache_index_size = sizeof(unsigned);
|
|
|
|
int ret __attribute__((__unused__)) =
|
|
|
|
mallctl("tcache.destroy", nullptr, 0, tcache_index, tcache_index_size);
|
|
|
|
// Silently ignore error.
|
|
|
|
assert(ret == 0);
|
|
|
|
delete tcache_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
JemallocNodumpAllocator::~JemallocNodumpAllocator() {
|
|
|
|
// Destroy tcache before destroying arena.
|
|
|
|
autovector<void*> tcache_list;
|
|
|
|
tcache_.Scrape(&tcache_list, nullptr);
|
|
|
|
for (void* tcache_index : tcache_list) {
|
|
|
|
DestroyThreadSpecificCache(tcache_index);
|
|
|
|
}
|
|
|
|
// Destroy arena. Silently ignore error.
|
|
|
|
Status s __attribute__((__unused__)) = DestroyArena(arena_index_);
|
|
|
|
assert(s.ok());
|
2018-10-27 02:27:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
size_t JemallocNodumpAllocator::UsableSize(void* p,
|
|
|
|
size_t /*allocation_size*/) const {
|
|
|
|
return malloc_usable_size(static_cast<void*>(p));
|
|
|
|
}
|
|
|
|
#endif // ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
|
|
|
|
|
|
|
|
Status NewJemallocNodumpAllocator(
|
2018-11-30 02:30:33 +01:00
|
|
|
JemallocAllocatorOptions& options,
|
2018-10-27 02:27:13 +02:00
|
|
|
std::shared_ptr<MemoryAllocator>* memory_allocator) {
|
|
|
|
*memory_allocator = nullptr;
|
2019-01-04 01:26:31 +01:00
|
|
|
Status unsupported = Status::NotSupported(
|
2018-10-27 02:27:13 +02:00
|
|
|
"JemallocNodumpAllocator only available with jemalloc version >= 5 "
|
|
|
|
"and MADV_DONTDUMP is available.");
|
2019-01-04 01:26:31 +01:00
|
|
|
#ifndef ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
|
|
|
|
(void)options;
|
|
|
|
return unsupported;
|
2018-10-27 02:27:13 +02:00
|
|
|
#else
|
2019-01-04 01:26:31 +01:00
|
|
|
if (!HasJemalloc()) {
|
|
|
|
return unsupported;
|
|
|
|
}
|
2018-10-27 02:27:13 +02:00
|
|
|
if (memory_allocator == nullptr) {
|
|
|
|
return Status::InvalidArgument("memory_allocator must be non-null.");
|
|
|
|
}
|
2018-11-30 02:30:33 +01:00
|
|
|
if (options.limit_tcache_size &&
|
|
|
|
options.tcache_size_lower_bound >= options.tcache_size_upper_bound) {
|
|
|
|
return Status::InvalidArgument(
|
|
|
|
"tcache_size_lower_bound larger or equal to tcache_size_upper_bound.");
|
|
|
|
}
|
2018-11-20 07:33:39 +01:00
|
|
|
|
2018-10-27 02:27:13 +02:00
|
|
|
// Create arena.
|
|
|
|
unsigned arena_index = 0;
|
|
|
|
size_t arena_index_size = sizeof(arena_index);
|
|
|
|
int ret =
|
|
|
|
mallctl("arenas.create", &arena_index, &arena_index_size, nullptr, 0);
|
|
|
|
if (ret != 0) {
|
|
|
|
return Status::Incomplete("Failed to create jemalloc arena, error code: " +
|
|
|
|
ToString(ret));
|
|
|
|
}
|
|
|
|
assert(arena_index != 0);
|
|
|
|
|
|
|
|
// Read existing hooks.
|
2018-11-20 07:33:39 +01:00
|
|
|
std::string key = "arena." + ToString(arena_index) + ".extent_hooks";
|
2018-10-27 02:27:13 +02:00
|
|
|
extent_hooks_t* hooks;
|
|
|
|
size_t hooks_size = sizeof(hooks);
|
|
|
|
ret = mallctl(key.c_str(), &hooks, &hooks_size, nullptr, 0);
|
|
|
|
if (ret != 0) {
|
2018-11-20 07:33:39 +01:00
|
|
|
JemallocNodumpAllocator::DestroyArena(arena_index);
|
2018-10-27 02:27:13 +02:00
|
|
|
return Status::Incomplete("Failed to read existing hooks, error code: " +
|
|
|
|
ToString(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store existing alloc.
|
|
|
|
extent_alloc_t* original_alloc = hooks->alloc;
|
|
|
|
extent_alloc_t* expected = nullptr;
|
2018-11-20 07:33:39 +01:00
|
|
|
bool success =
|
2018-10-27 02:27:13 +02:00
|
|
|
JemallocNodumpAllocator::original_alloc_.compare_exchange_strong(
|
|
|
|
expected, original_alloc);
|
2018-11-20 07:33:39 +01:00
|
|
|
if (!success && original_alloc != expected) {
|
|
|
|
JemallocNodumpAllocator::DestroyArena(arena_index);
|
|
|
|
return Status::Incomplete("Original alloc conflict.");
|
|
|
|
}
|
2018-10-27 02:27:13 +02:00
|
|
|
|
|
|
|
// Set the custom hook.
|
|
|
|
std::unique_ptr<extent_hooks_t> new_hooks(new extent_hooks_t(*hooks));
|
|
|
|
new_hooks->alloc = &JemallocNodumpAllocator::Alloc;
|
|
|
|
extent_hooks_t* hooks_ptr = new_hooks.get();
|
|
|
|
ret = mallctl(key.c_str(), nullptr, nullptr, &hooks_ptr, sizeof(hooks_ptr));
|
|
|
|
if (ret != 0) {
|
2018-11-20 07:33:39 +01:00
|
|
|
JemallocNodumpAllocator::DestroyArena(arena_index);
|
2018-10-27 02:27:13 +02:00
|
|
|
return Status::Incomplete("Failed to set custom hook, error code: " +
|
|
|
|
ToString(ret));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create cache allocator.
|
|
|
|
memory_allocator->reset(
|
2018-11-30 02:30:33 +01:00
|
|
|
new JemallocNodumpAllocator(options, std::move(new_hooks), arena_index));
|
2018-10-27 02:27:13 +02:00
|
|
|
return Status::OK();
|
|
|
|
#endif // ROCKSDB_JEMALLOC_NODUMP_ALLOCATOR
|
|
|
|
}
|
|
|
|
|
2020-02-20 21:07:53 +01:00
|
|
|
} // namespace ROCKSDB_NAMESPACE
|