From 8f5d24ae68bc3f9993b17104e0a19845495eff85 Mon Sep 17 00:00:00 2001 From: siddontang Date: Thu, 15 Dec 2016 17:14:06 -0800 Subject: [PATCH] C API: support get usage and pinned_usage for cache Summary: Closes https://github.com/facebook/rocksdb/pull/1671 Differential Revision: D4327453 Pulled By: yiwu-arbug fbshipit-source-id: bcdbc65 --- db/c.cc | 13 +++++++++++++ db/c_test.c | 24 +++++++++++++++++++++--- include/rocksdb/c.h | 6 ++++++ 3 files changed, 40 insertions(+), 3 deletions(-) diff --git a/db/c.cc b/db/c.cc index f3c796822b..86c5109309 100644 --- a/db/c.cc +++ b/db/c.cc @@ -2242,6 +2242,11 @@ void rocksdb_readoptions_set_readahead_size( opt->rep.readahead_size = v; } +void rocksdb_readoptions_set_pin_data(rocksdb_readoptions_t* opt, + unsigned char v) { + opt->rep.pin_data = v; +} + rocksdb_writeoptions_t* rocksdb_writeoptions_create() { return new rocksdb_writeoptions_t; } @@ -2309,6 +2314,14 @@ void rocksdb_cache_set_capacity(rocksdb_cache_t* cache, size_t capacity) { cache->rep->SetCapacity(capacity); } +size_t rocksdb_cache_get_usage(rocksdb_cache_t* cache) { + return cache->rep->GetUsage(); +} + +size_t rocksdb_cache_get_pinned_usage(rocksdb_cache_t* cache) { + return cache->rep->GetPinnedUsage(); +} + rocksdb_env_t* rocksdb_create_default_env() { rocksdb_env_t* result = new rocksdb_env_t; result->rep = Env::Default(); diff --git a/db/c_test.c b/db/c_test.c index 5c183eac7f..1b10bd5f85 100644 --- a/db/c_test.c +++ b/db/c_test.c @@ -340,7 +340,7 @@ int main(int argc, char** argv) { roptions = rocksdb_readoptions_create(); rocksdb_readoptions_set_verify_checksums(roptions, 1); - rocksdb_readoptions_set_fill_cache(roptions, 0); + rocksdb_readoptions_set_fill_cache(roptions, 1); woptions = rocksdb_writeoptions_create(); rocksdb_writeoptions_set_sync(woptions, 1); @@ -437,6 +437,24 @@ int main(int argc, char** argv) { rocksdb_compact_range_opt(db, coptions, "a", 1, "z", 1); CheckGet(db, roptions, "foo", "hello"); + // Simple check cache usage + StartPhase("cache_usage"); + { + rocksdb_readoptions_set_pin_data(roptions, 1); + rocksdb_iterator_t* iter = rocksdb_create_iterator(db, roptions); + rocksdb_iter_seek(iter, "foo", 3); + + size_t usage = rocksdb_cache_get_usage(cache); + CheckCondition(usage > 0); + + size_t pin_usage = rocksdb_cache_get_pinned_usage(cache); + CheckCondition(pin_usage > 0); + + rocksdb_iter_next(iter); + rocksdb_iter_destroy(iter); + rocksdb_readoptions_set_pin_data(roptions, 0); + } + StartPhase("writebatch"); { rocksdb_writebatch_t* wb = rocksdb_writebatch_create(); @@ -795,9 +813,9 @@ int main(int argc, char** argv) { { rocksdb_close(db); rocksdb_destroy_db(options, dbname, &err); - CheckNoError(err) + CheckNoError(err); - rocksdb_options_t* db_options = rocksdb_options_create(); + rocksdb_options_t* db_options = rocksdb_options_create(); rocksdb_options_set_create_if_missing(db_options, 1); db = rocksdb_open(db_options, dbname, &err); CheckNoError(err) diff --git a/include/rocksdb/c.h b/include/rocksdb/c.h index bac87554c2..29fe4d885d 100644 --- a/include/rocksdb/c.h +++ b/include/rocksdb/c.h @@ -875,6 +875,8 @@ extern ROCKSDB_LIBRARY_API void rocksdb_readoptions_set_tailing( rocksdb_readoptions_t*, unsigned char); extern ROCKSDB_LIBRARY_API void rocksdb_readoptions_set_readahead_size( rocksdb_readoptions_t*, size_t); +extern ROCKSDB_LIBRARY_API void rocksdb_readoptions_set_pin_data( + rocksdb_readoptions_t*, unsigned char); /* Write options */ @@ -917,6 +919,10 @@ extern ROCKSDB_LIBRARY_API rocksdb_cache_t* rocksdb_cache_create_lru( extern ROCKSDB_LIBRARY_API void rocksdb_cache_destroy(rocksdb_cache_t* cache); extern ROCKSDB_LIBRARY_API void rocksdb_cache_set_capacity( rocksdb_cache_t* cache, size_t capacity); +extern ROCKSDB_LIBRARY_API size_t +rocksdb_cache_get_usage(rocksdb_cache_t* cache); +extern ROCKSDB_LIBRARY_API size_t +rocksdb_cache_get_pinned_usage(rocksdb_cache_t* cache); /* Env */