Commit 494eb574 authored by Wu Jiayu's avatar Wu Jiayu Committed by Connor

Cherry-pick jemalloc nodump allocator for block cache, and ffi_try! fixing (#334)

* Adding jemalloc-nodump-allocator option for lru-cache (#328)

Adding LRUCacheOptions::set_no_dump_allocator, so we can use JemallocNoDumpAllocator of rocksdb to exclude some LRU Cache, like block cache, from core dump.
parent db9052e9
......@@ -25,8 +25,9 @@ cc = "1.0.3"
cmake = "0.1"
[dependencies.jemalloc-sys]
version = ">= 0.1.8"
version = "0.1.8"
optional = true
features = ["unprefixed_malloc_on_supported_platforms"]
[dependencies.libz-sys]
version = "1.0.25"
......
......@@ -80,6 +80,7 @@ fn build_rocksdb() -> Build {
let mut cfg = Config::new("rocksdb");
if cfg!(feature = "jemalloc") && NO_JEMALLOC_TARGETS.iter().all(|i| !target.contains(i)) {
cfg.register_dep("JEMALLOC").define("WITH_JEMALLOC", "ON");
println!("cargo:rustc-link-lib=static=jemalloc");
}
if cfg!(feature = "portable") {
cfg.define("PORTABLE", "ON");
......
......@@ -173,6 +173,8 @@ using rocksdb::titandb::TitanOptions;
using rocksdb::titandb::TitanReadOptions;
using rocksdb::titandb::TitanBlobRunMode;
using rocksdb::MemoryAllocator;
using std::shared_ptr;
extern "C" {
......@@ -212,6 +214,7 @@ struct crocksdb_lru_cache_options_t {
LRUCacheOptions rep;
};
struct crocksdb_cache_t { shared_ptr<Cache> rep; };
struct crocksdb_memory_allocator_t { shared_ptr<MemoryAllocator> rep; };
struct crocksdb_livefiles_t { std::vector<LiveFileMetaData> rep; };
struct crocksdb_column_family_handle_t { ColumnFamilyHandle* rep; };
struct crocksdb_envoptions_t { EnvOptions rep; };
......@@ -3288,6 +3291,17 @@ void crocksdb_flushoptions_set_allow_write_stall(
opt->rep.allow_write_stall = v;
}
crocksdb_memory_allocator_t* crocksdb_jemalloc_nodump_allocator_create(char** errptr) {
crocksdb_memory_allocator_t* allocator = new crocksdb_memory_allocator_t;
rocksdb::JemallocAllocatorOptions options;
SaveError(errptr, rocksdb::NewJemallocNodumpAllocator(options, &allocator->rep));
return allocator;
}
void crocksdb_memory_allocator_destroy(crocksdb_memory_allocator_t* allocator) {
delete allocator;
}
crocksdb_lru_cache_options_t* crocksdb_lru_cache_options_create() {
return new crocksdb_lru_cache_options_t;
}
......@@ -3316,6 +3330,11 @@ void crocksdb_lru_cache_options_set_high_pri_pool_ratio(
opt->rep.high_pri_pool_ratio = high_pri_pool_ratio;
}
void crocksdb_lru_cache_options_set_memory_allocator(
crocksdb_lru_cache_options_t* opt, crocksdb_memory_allocator_t* allocator) {
opt->rep.memory_allocator = allocator->rep;
}
crocksdb_cache_t* crocksdb_cache_create_lru(crocksdb_lru_cache_options_t* opt) {
crocksdb_cache_t* c = new crocksdb_cache_t;
c->rep = NewLRUCache(opt->rep);
......@@ -5114,7 +5133,7 @@ void ctitandb_options_set_max_background_gc(ctitandb_options_t* options,
options->rep.max_background_gc = size;
}
void ctitandb_options_set_purge_obsolete_files_period(ctitandb_options_t* options,
void ctitandb_options_set_purge_obsolete_files_period(ctitandb_options_t* options,
unsigned int period) {
options->rep.purge_obsolete_files_period = period;
}
......@@ -5146,12 +5165,12 @@ struct ctitandb_readoptions_t {
TitanReadOptions rep;
};
ctitandb_readoptions_t* ctitandb_readoptions_create() {
return new ctitandb_readoptions_t;
ctitandb_readoptions_t* ctitandb_readoptions_create() {
return new ctitandb_readoptions_t;
}
void ctitandb_readoptions_destroy(ctitandb_readoptions_t* opts) {
delete opts;
void ctitandb_readoptions_destroy(ctitandb_readoptions_t* opts) {
delete opts;
}
bool ctitandb_readoptions_key_only(ctitandb_readoptions_t* opts) {
......
......@@ -76,6 +76,7 @@ typedef struct crocksdb_backup_engine_info_t crocksdb_backup_engine_info_t;
typedef struct crocksdb_restore_options_t crocksdb_restore_options_t;
typedef struct crocksdb_lru_cache_options_t crocksdb_lru_cache_options_t;
typedef struct crocksdb_cache_t crocksdb_cache_t;
typedef struct crocksdb_memory_allocator_t crocksdb_memory_allocator_t;
typedef struct crocksdb_compactionfilter_t crocksdb_compactionfilter_t;
typedef struct crocksdb_compactionfiltercontext_t
crocksdb_compactionfiltercontext_t;
......@@ -1313,6 +1314,12 @@ extern C_ROCKSDB_LIBRARY_API void crocksdb_flushoptions_set_wait(
extern C_ROCKSDB_LIBRARY_API void crocksdb_flushoptions_set_allow_write_stall(
crocksdb_flushoptions_t*, unsigned char);
/* Memory allocator */
extern C_ROCKSDB_LIBRARY_API crocksdb_memory_allocator_t*
crocksdb_jemalloc_nodump_allocator_create(char** errptr);
extern C_ROCKSDB_LIBRARY_API void crocksdb_memory_allocator_destroy(crocksdb_memory_allocator_t*);
/* Cache */
extern C_ROCKSDB_LIBRARY_API crocksdb_lru_cache_options_t*
......@@ -1327,6 +1334,8 @@ extern C_ROCKSDB_LIBRARY_API void crocksdb_lru_cache_options_set_strict_capacity
crocksdb_lru_cache_options_t*, bool);
extern C_ROCKSDB_LIBRARY_API void crocksdb_lru_cache_options_set_high_pri_pool_ratio(
crocksdb_lru_cache_options_t*, double);
extern C_ROCKSDB_LIBRARY_API void crocksdb_lru_cache_options_set_memory_allocator(
crocksdb_lru_cache_options_t*, crocksdb_memory_allocator_t*);
extern C_ROCKSDB_LIBRARY_API crocksdb_cache_t* crocksdb_cache_create_lru(
crocksdb_lru_cache_options_t*);
extern C_ROCKSDB_LIBRARY_API void crocksdb_cache_destroy(crocksdb_cache_t* cache);
......
......@@ -29,6 +29,7 @@ pub enum DBWriteOptions {}
pub enum DBReadOptions {}
pub enum DBMergeOperator {}
pub enum DBBlockBasedTableOptions {}
pub enum DBMemoryAllocator {}
pub enum DBLRUCacheOptions {}
pub enum DBCache {}
pub enum DBFilterPolicy {}
......@@ -292,10 +293,19 @@ pub fn error_message(ptr: *mut c_char) -> String {
#[macro_export]
macro_rules! ffi_try {
($func:ident($($arg:expr),*)) => ({
($func:ident($($arg:expr),+)) => ({
use std::ptr;
let mut err = ptr::null_mut();
let res = $crate::$func($($arg),*, &mut err);
let res = $crate::$func($($arg),+, &mut err);
if !err.is_null() {
return Err($crate::error_message(err));
}
res
});
($func:ident()) => ({
use std::ptr;
let mut err = ptr::null_mut();
let res = $crate::$func(&mut err);
if !err.is_null() {
return Err($crate::error_message(err));
}
......@@ -335,6 +345,12 @@ extern "C" {
cf_descs: *const ColumnFamilyDescriptor,
) -> *mut Options;
// Memory Allocator
pub fn crocksdb_jemalloc_nodump_allocator_create(
err: *mut *mut c_char,
) -> *mut DBMemoryAllocator;
pub fn crocksdb_memory_allocator_destroy(allocator: *mut DBMemoryAllocator);
// Cache
pub fn crocksdb_lru_cache_options_create() -> *mut DBLRUCacheOptions;
pub fn crocksdb_lru_cache_options_destroy(opt: *mut DBLRUCacheOptions);
......@@ -351,6 +367,10 @@ extern "C" {
opt: *mut DBLRUCacheOptions,
high_pri_pool_ratio: c_double,
);
pub fn crocksdb_lru_cache_options_set_memory_allocator(
opt: *mut DBLRUCacheOptions,
allocator: *mut DBMemoryAllocator,
);
pub fn crocksdb_cache_create_lru(opt: *mut DBLRUCacheOptions) -> *mut DBCache;
pub fn crocksdb_cache_destroy(cache: *mut DBCache);
......
......@@ -35,8 +35,8 @@ pub use metadata::{ColumnFamilyMetaData, LevelMetaData, SstFileMetaData};
pub use perf_context::{get_perf_level, set_perf_level, IOStatsContext, PerfContext, PerfLevel};
pub use rocksdb::{
load_latest_options, run_ldb_tool, set_external_sst_file_global_seq_no, BackupEngine, CFHandle,
Cache, DBIterator, DBVector, Env, ExternalSstFileInfo, Kv, Range, SeekKey, SequentialFile,
SstFileWriter, Writable, WriteBatch, DB,
Cache, DBIterator, DBVector, Env, ExternalSstFileInfo, Kv, MemoryAllocator, Range, SeekKey,
SequentialFile, SstFileWriter, Writable, WriteBatch, DB,
};
pub use rocksdb_options::{
BlockBasedOptions, CColumnFamilyDescriptor, ColumnFamilyOptions, CompactOptions,
......
......@@ -18,6 +18,7 @@ use crocksdb_ffi::{
DBWriteBatch,
};
use libc::{self, c_char, c_int, c_void, size_t};
use librocksdb_sys::DBMemoryAllocator;
use metadata::ColumnFamilyMetaData;
use rocksdb_options::{
CColumnFamilyDescriptor, ColumnFamilyDescriptor, ColumnFamilyOptions, CompactOptions,
......@@ -2377,6 +2378,30 @@ impl Drop for Cache {
}
}
pub struct MemoryAllocator {
pub inner: *mut DBMemoryAllocator,
}
impl MemoryAllocator {
#[cfg(feature = "jemalloc")]
pub fn new_jemalloc_memory_allocator() -> Result<MemoryAllocator, String> {
unsafe {
let allocator = MemoryAllocator {
inner: ffi_try!(crocksdb_jemalloc_nodump_allocator_create()),
};
Ok(allocator)
}
}
}
impl Drop for MemoryAllocator {
fn drop(&mut self) {
unsafe {
crocksdb_ffi::crocksdb_memory_allocator_destroy(self.inner);
}
}
}
pub fn set_external_sst_file_global_seq_no(
db: &DB,
cf: &CFHandle,
......
......@@ -26,8 +26,8 @@ use event_listener::{new_event_listener, EventListener};
use libc::{self, c_double, c_int, c_uchar, c_void, size_t};
use merge_operator::MergeFn;
use merge_operator::{self, full_merge_callback, partial_merge_callback, MergeOperatorCallback};
use rocksdb::Cache;
use rocksdb::Env;
use rocksdb::{Cache, MemoryAllocator};
use slice_transform::{new_slice_transform, SliceTransform};
use std::ffi::{CStr, CString};
use std::mem;
......@@ -1842,6 +1842,15 @@ impl LRUCacheOptions {
);
}
}
pub fn set_memory_allocator(&mut self, allocator: MemoryAllocator) {
unsafe {
crocksdb_ffi::crocksdb_lru_cache_options_set_memory_allocator(
self.inner,
allocator.inner,
);
}
}
}
impl Drop for LRUCacheOptions {
......
......@@ -15,6 +15,7 @@ use rocksdb::crocksdb_ffi::{
CompactionPriority, DBCompressionType, DBInfoLogLevel as InfoLogLevel, DBRateLimiterMode,
DBStatisticsHistogramType as HistogramType, DBStatisticsTickerType as TickerType,
};
use rocksdb::rocksdb::MemoryAllocator;
use rocksdb::{
BlockBasedOptions, Cache, ColumnFamilyOptions, CompactOptions, DBOptions, Env,
FifoCompactionOptions, LRUCacheOptions, ReadOptions, SeekKey, SliceTransform, Writable,
......@@ -293,6 +294,22 @@ fn test_set_lru_cache() {
DB::open_cf(opts, path.path().to_str().unwrap(), vec!["default"]).unwrap();
}
#[cfg(feature = "jemalloc")]
#[test]
fn test_set_jemalloc_nodump_allocator_for_lru_cache() {
let path = TempDir::new("_rust_rocksdb_set_jemalloc_nodump_allocator").expect("");
let mut opts = DBOptions::new();
let mut cf_opts = ColumnFamilyOptions::new();
opts.create_if_missing(true);
let mut block_opts = BlockBasedOptions::new();
let mut cache_opts = LRUCacheOptions::new();
cache_opts.set_memory_allocator(MemoryAllocator::new_jemalloc_memory_allocator().unwrap());
cache_opts.set_capacity(8388608);
block_opts.set_block_cache(&Cache::new_lru_cache(cache_opts));
cf_opts.set_block_based_table_factory(&block_opts);
DB::open_cf(opts, path.path().to_str().unwrap(), vec!["default"]).unwrap();
}
#[test]
fn test_set_cache_index_and_filter_blocks_with_high_priority() {
let path = TempDir::new("_rust_rocksdb_set_cache_and_index_with_high_priority").expect("");
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment