fork of https://github.com/oxigraph/rocksdb and https://github.com/facebook/rocksdb for nextgraph and oxigraph
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
93 lines
3.2 KiB
93 lines
3.2 KiB
// Copyright (c) 2011-present, Facebook, Inc. All rights reserved.
|
|
// This source code is licensed under the BSD-style license found in the
|
|
// LICENSE file in the root directory of this source tree. An additional grant
|
|
// of patent rights can be found in the PATENTS file in the same directory.
|
|
// This source code is also licensed under the GPLv2 license found in the
|
|
// COPYING file in the root directory of this source tree.
|
|
//
|
|
// Copyright (c) 2011 The LevelDB Authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file. See the AUTHORS file for names of contributors.
|
|
//
|
|
// WriteBufferManager is for managing memory allocation for one or more
|
|
// MemTables.
|
|
|
|
#pragma once
|
|
|
|
#include <atomic>
|
|
#include <cstddef>
|
|
#include "rocksdb/cache.h"
|
|
|
|
namespace rocksdb {
|
|
|
|
class WriteBufferManager {
|
|
public:
|
|
// _buffer_size = 0 indicates no limit. Memory won't be capped.
|
|
// memory_usage() won't be valid and ShouldFlush() will always return true.
|
|
// if `cache` is provided, we'll put dummy entries in the cache and cost
|
|
// the memory allocated to the cache. It can be used even if _buffer_size = 0.
|
|
explicit WriteBufferManager(size_t _buffer_size,
|
|
std::shared_ptr<Cache> cache = {});
|
|
~WriteBufferManager();
|
|
|
|
bool enabled() const { return buffer_size_ != 0; }
|
|
|
|
// Only valid if enabled()
|
|
size_t memory_usage() const {
|
|
return memory_used_.load(std::memory_order_relaxed);
|
|
}
|
|
size_t mutable_memtable_memory_usage() const {
|
|
return memory_active_.load(std::memory_order_relaxed);
|
|
}
|
|
size_t buffer_size() const { return buffer_size_; }
|
|
|
|
// Should only be called from write thread
|
|
bool ShouldFlush() const {
|
|
// Flush if memory usage hits a hard limit, or total size that hasn't been
|
|
// scheduled to free hits a soft limit, which is 7/8 of the hard limit.
|
|
return enabled() &&
|
|
(memory_usage() >= buffer_size() ||
|
|
mutable_memtable_memory_usage() >= buffer_size() / 8 * 7);
|
|
}
|
|
|
|
void ReserveMem(size_t mem) {
|
|
if (cache_rep_ != nullptr) {
|
|
ReserveMemWithCache(mem);
|
|
} else if (enabled()) {
|
|
memory_used_.fetch_add(mem, std::memory_order_relaxed);
|
|
}
|
|
if (enabled()) {
|
|
memory_active_.fetch_add(mem, std::memory_order_relaxed);
|
|
}
|
|
}
|
|
// We are in the process of freeing `mem` bytes, so it is not considered
|
|
// when checking the soft limit.
|
|
void ScheduleFreeMem(size_t mem) {
|
|
if (enabled()) {
|
|
memory_active_.fetch_sub(mem, std::memory_order_relaxed);
|
|
}
|
|
}
|
|
void FreeMem(size_t mem) {
|
|
if (cache_rep_ != nullptr) {
|
|
FreeMemWithCache(mem);
|
|
} else if (enabled()) {
|
|
memory_used_.fetch_sub(mem, std::memory_order_relaxed);
|
|
}
|
|
}
|
|
|
|
private:
|
|
const size_t buffer_size_;
|
|
std::atomic<size_t> memory_used_;
|
|
// Memory that hasn't been scheduled to free.
|
|
std::atomic<size_t> memory_active_;
|
|
struct CacheRep;
|
|
std::unique_ptr<CacheRep> cache_rep_;
|
|
|
|
void ReserveMemWithCache(size_t mem);
|
|
void FreeMemWithCache(size_t mem);
|
|
|
|
// No copying allowed
|
|
WriteBufferManager(const WriteBufferManager&) = delete;
|
|
WriteBufferManager& operator=(const WriteBufferManager&) = delete;
|
|
};
|
|
} // namespace rocksdb
|
|
|