Skip to content

Commit

Permalink
[util] extract libnvm_cache from libkudu_util
Browse files Browse the repository at this point in the history
This patch extracts NVM/memkind-specific code from libkudu_util into
its own separate library: libnvm_cache.  The motivation for this
change is to avoid linking NVM-backed LRU/FIFO cache code and libmemkind
into libraries and binaries which do not use that functionality,
but use DRAM-backed LRU/FIFO cache only.

A few follow-up changelists depend on this patch to introduce TTL cache
into DnsResolver.

Change-Id: Ibafea70676f7be9c086f9f195ab215cfff0719fe
Reviewed-on: http://gerrit.cloudera.org:8080/13468
Tested-by: Kudu Jenkins
Reviewed-by: Adar Dembo <[email protected]>
  • Loading branch information
alexeyserbin committed May 31, 2019
1 parent fe0889e commit ff828ac
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
11 changes: 10 additions & 1 deletion src/kudu/cfile/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ add_library(cfile
index_btree.cc
type_encodings.cc)

target_link_libraries(cfile

set(CFILE_LIBS
kudu_common
kudu_fs
kudu_util
Expand All @@ -54,6 +55,14 @@ target_link_libraries(cfile
cfile_proto
bitshuffle)

if(HAVE_LIB_MEMKIND)
set(CFILE_LIBS
${CFILE_LIBS}
nvm_cache)
endif()

target_link_libraries(cfile ${CFILE_LIBS})

# Tests
SET_KUDU_TEST_LINK_LIBS(cfile)
ADD_KUDU_TEST(index-test)
Expand Down
39 changes: 26 additions & 13 deletions src/kudu/util/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -239,12 +239,6 @@ set(UTIL_SRCS
# optimized regardless of the default optimization options.
set_source_files_properties(memory/overwrite.cc PROPERTIES COMPILE_FLAGS "-O3")

if(HAVE_LIB_MEMKIND)
set(UTIL_SRCS
${UTIL_SRCS}
nvm_cache.cc)
endif()

set(UTIL_LIBS
crcutil
gflags
Expand All @@ -269,12 +263,6 @@ if(NOT APPLE)
rt)
endif()

if(HAVE_LIB_MEMKIND)
set(UTIL_LIBS
${UTIL_LIBS}
memkind)
endif()

# We use MallocExtension, but not in the exported version of the library.
set(EXPORTED_UTIL_LIBS ${UTIL_LIBS})
if(${KUDU_TCMALLOC_AVAILABLE})
Expand All @@ -287,6 +275,31 @@ ADD_EXPORTABLE_LIBRARY(kudu_util
NONLINK_DEPS gen_version_info
EXPORTED_DEPS ${EXPORTED_UTIL_LIBS})

#######################################
# nvm_cache
#######################################

if(HAVE_LIB_MEMKIND)
set(NVM_CACHE_SRCS
nvm_cache.cc)

set(NVM_CACHE_LIBS
gflags
glog
gutil
memkind)

if(NOT APPLE)
set(NVM_CACHE_LIBS
${NVM_CACHE_LIBS}
dl
rt)
endif()

add_library(nvm_cache ${NVM_CACHE_SRCS})
target_link_libraries(nvm_cache ${NVM_CACHE_LIBS})
endif()

#######################################
# kudu_util_compression
#######################################
Expand Down Expand Up @@ -342,7 +355,7 @@ target_link_libraries(kudu_test_util

if(HAVE_LIB_MEMKIND)
target_link_libraries(kudu_test_util
memkind)
nvm_cache)
endif()

#######################################
Expand Down
12 changes: 0 additions & 12 deletions src/kudu/util/cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,6 @@
#include "kudu/util/slice.h"
#include "kudu/util/test_util_prod.h"

#if !defined(__APPLE__)
#include "kudu/util/nvm_cache.h"
#endif

// Useful in tests that require accurate cache capacity accounting.
DEFINE_bool(cache_force_single_shard, false,
"Override all cache implementations to use just one shard");
Expand Down Expand Up @@ -697,14 +693,6 @@ Cache* NewCache<Cache::EvictionPolicy::LRU,
return new ShardedCache<Cache::EvictionPolicy::LRU>(capacity, id);
}

#if defined(HAVE_LIB_MEMKIND)
template<>
Cache* NewCache<Cache::EvictionPolicy::LRU,
Cache::MemoryType::NVM>(size_t capacity, const std::string& id) {
return NewLRUNvmCache(capacity, id);
}
#endif

std::ostream& operator<<(std::ostream& os, Cache::MemoryType mem_type) {
switch (mem_type) {
case Cache::MemoryType::DRAM:
Expand Down
8 changes: 0 additions & 8 deletions src/kudu/util/cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,6 @@ template<>
Cache* NewCache<Cache::EvictionPolicy::LRU,
Cache::MemoryType::DRAM>(size_t capacity, const std::string& id);

#if defined(HAVE_LIB_MEMKIND)
// Create a new LRU cache with a fixed size capacity. This implementation
// of Cache uses the least-recently-used eviction policy and stored in NVM.
template<>
Cache* NewCache<Cache::EvictionPolicy::LRU,
Cache::MemoryType::NVM>(size_t capacity, const std::string& id);
#endif

// A helper method to output cache memory type into ostream.
std::ostream& operator<<(std::ostream& os, Cache::MemoryType mem_type);

Expand Down
4 changes: 3 additions & 1 deletion src/kudu/util/nvm_cache.cc
Original file line number Diff line number Diff line change
Expand Up @@ -632,7 +632,9 @@ class ShardedLRUCache : public Cache {

} // end anonymous namespace

Cache* NewLRUNvmCache(size_t capacity, const std::string& id) {
template<>
Cache* NewCache<Cache::EvictionPolicy::LRU,
Cache::MemoryType::NVM>(size_t capacity, const std::string& id) {
// memkind_create_pmem() will fail if the capacity is too small, but with
// an inscrutable error. So, we'll check ourselves.
CHECK_GE(capacity, MEMKIND_PMEM_MIN_SIZE)
Expand Down
15 changes: 8 additions & 7 deletions src/kudu/util/nvm_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,19 @@
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#ifndef KUDU_UTIL_NVM_CACHE_H_
#define KUDU_UTIL_NVM_CACHE_H_
#pragma once

#include <cstddef>
#include <string>

#include "kudu/util/cache.h"

namespace kudu {
class Cache;

// Create a cache in persistent memory with the given capacity.
Cache* NewLRUNvmCache(size_t capacity, const std::string& id);
// Create a new LRU cache with a fixed size capacity. This implementation
// of Cache uses the least-recently-used eviction policy and stored in NVM.
template<>
Cache* NewCache<Cache::EvictionPolicy::LRU,
Cache::MemoryType::NVM>(size_t capacity, const std::string& id);

} // namespace kudu

#endif

0 comments on commit ff828ac

Please sign in to comment.