Skip to content

Commit edd034d

Browse files
committed
Single tier cache example with NUMA bindings
1 parent bf5b7dc commit edd034d

File tree

3 files changed

+172
-0
lines changed

3 files changed

+172
-0
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Copyright (c) Facebook, Inc. and its affiliates.
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# http://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
cmake_minimum_required (VERSION 3.12)
16+
17+
project (cachelib-cmake-test-project VERSION 0.1)
18+
19+
find_package(cachelib CONFIG REQUIRED)
20+
21+
add_executable(single-tier-cache-example main.cpp)
22+
23+
target_link_libraries(single-tier-cache-example cachelib)

examples/single_tier_cache/build.sh

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/sh
2+
3+
# Copyright (c) Facebook, Inc. and its affiliates.
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
set -e
18+
19+
# Root directory for the CacheLib project
20+
CLBASE="$PWD/../.."
21+
22+
# Additional "FindXXX.cmake" files are here (e.g. FindSodium.cmake)
23+
CLCMAKE="$CLBASE/cachelib/cmake"
24+
25+
# After ensuring we are in the correct directory, set the installation prefix"
26+
PREFIX="$CLBASE/opt/cachelib/"
27+
28+
CMAKE_PARAMS="-DCMAKE_INSTALL_PREFIX='$PREFIX' -DCMAKE_MODULE_PATH='$CLCMAKE'"
29+
30+
CMAKE_PREFIX_PATH="$PREFIX/lib/cmake:$PREFIX/lib64/cmake:$PREFIX/lib:$PREFIX/lib64:$PREFIX:${CMAKE_PREFIX_PATH:-}"
31+
export CMAKE_PREFIX_PATH
32+
PKG_CONFIG_PATH="$PREFIX/lib/pkgconfig:$PREFIX/lib64/pkgconfig:${PKG_CONFIG_PATH:-}"
33+
export PKG_CONFIG_PATH
34+
LD_LIBRARY_PATH="$PREFIX/lib:$PREFIX/lib64:${LD_LIBRARY_PATH:-}"
35+
export LD_LIBRARY_PATH
36+
37+
mkdir -p build
38+
cd build
39+
cmake $CMAKE_PARAMS ..
40+
make

examples/single_tier_cache/main.cpp

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
/*
2+
* Copyright (c) Facebook, Inc. and its affiliates.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "cachelib/allocator/CacheAllocator.h"
18+
#include "cachelib/allocator/MemoryTierCacheConfig.h"
19+
#include "folly/init/Init.h"
20+
21+
namespace facebook {
22+
namespace cachelib_examples {
23+
using Cache = cachelib::LruAllocator; // or Lru2QAllocator, or TinyLFUAllocator
24+
using CacheConfig = typename Cache::Config;
25+
using CacheKey = typename Cache::Key;
26+
using CacheReadHandle = typename Cache::ReadHandle;
27+
using MemoryTierCacheConfig = typename cachelib::MemoryTierCacheConfig;
28+
using NumaBitMask = typename cachelib::NumaBitMask;
29+
30+
// Global cache object and a default cache pool
31+
std::unique_ptr<Cache> gCache_;
32+
cachelib::PoolId defaultPool_;
33+
34+
void initializeCache() {
35+
CacheConfig config;
36+
config
37+
.setCacheSize(48 * 1024 * 1024) // 48 MB
38+
.setCacheName("SingleTier Cache")
39+
.enableCachePersistence("/tmp/simple-tier-cache")
40+
.setAccessConfig(
41+
{25 /* bucket power */, 10 /* lock power */}) // assuming caching 20
42+
// million items
43+
.configureMemoryTiers({
44+
MemoryTierCacheConfig::fromShm()
45+
.setRatio(1)
46+
.setMemBind(NumaBitMask().setBit(0))}) // allocate only from NUMA node 0
47+
.validate(); // will throw if bad config
48+
gCache_ = std::make_unique<Cache>(Cache::SharedMemNew, config);
49+
defaultPool_ =
50+
gCache_->addPool("default", gCache_->getCacheMemoryStats().cacheSize);
51+
}
52+
53+
void destroyCache() { gCache_.reset(); }
54+
55+
CacheReadHandle get(CacheKey key) { return gCache_->find(key); }
56+
57+
bool put(CacheKey key, const std::string& value) {
58+
auto handle = gCache_->allocate(defaultPool_, key, value.size());
59+
if (!handle) {
60+
return false; // cache may fail to evict due to too many pending writes
61+
}
62+
std::memcpy(handle->getMemory(), value.data(), value.size());
63+
gCache_->insertOrReplace(handle);
64+
return true;
65+
}
66+
} // namespace cachelib_examples
67+
} // namespace facebook
68+
69+
using namespace facebook::cachelib_examples;
70+
71+
int main(int argc, char** argv) {
72+
folly::init(&argc, &argv);
73+
74+
initializeCache();
75+
76+
std::string value(4*1024, 'X'); // 4 KB value
77+
const size_t NUM_ITEMS = 13000;
78+
79+
// Use cache
80+
{
81+
for(size_t i = 0; i < NUM_ITEMS; ++i) {
82+
std::string key = "key" + std::to_string(i);
83+
auto res = put(key, value);
84+
85+
std::ignore = res;
86+
assert(res);
87+
}
88+
89+
size_t nFound = 0;
90+
size_t nNotFound = 0;
91+
for(size_t i = 0; i < NUM_ITEMS; ++i) {
92+
std::string key = "key" + std::to_string(i);
93+
auto item = get(key);
94+
if(item) {
95+
++nFound;
96+
folly::StringPiece sp{reinterpret_cast<const char*>(item->getMemory()),
97+
item->getSize()};
98+
std::ignore = sp;
99+
assert(sp == value);
100+
} else {
101+
++nNotFound;
102+
}
103+
}
104+
std::cout << "Found:\t\t" << nFound << " items\n"
105+
<< "Not found:\t" << nNotFound << " items" << std::endl;
106+
}
107+
108+
destroyCache();
109+
}

0 commit comments

Comments
 (0)