Skip to content

Add extra allocation/eviction policies #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 21 commits into
base: develop
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
b791b77
Run centos and debian workflows on push and PR
igchor Nov 2, 2021
470f563
Introduce 'markedForEviction' state for the Item.
igchor Dec 15, 2022
c1020df
Adds createPutToken and switches findEviction
byrnedj Feb 4, 2023
9bb6775
- Change the cache size calculation to use getCacheSize()
byrnedj Jan 4, 2023
1759e83
- Add memory tier configs to cache allocator based on NUMA bindings
byrnedj Jan 4, 2023
62b0c41
added ability for compressed pointer to use full 32 bits for addressi…
guptask Nov 14, 2022
b7459ee
Add memory usage statistics for allocation classes
igchor Jul 6, 2022
fafface
Initial multi-tier support implementation (rebased with NUMA and cs p…
igchor Sep 28, 2021
369e55b
AC stats multi-tier
byrnedj Jan 17, 2023
713c6d9
This commit contains the additional memory tiers tests
byrnedj Feb 8, 2023
d209d78
This is the additional multi-tier support needed
guptask Nov 14, 2022
e74fa40
added per pool class rolling average latency (upstream PR version)
guptask Jul 21, 2022
bab780a
added per tier pool class rolling average latency (based on upstream PR)
guptask Jul 21, 2022
f0baeb1
MM2Q promotion iterators (#1)
byrnedj Aug 9, 2022
8ab8c75
CS Patch Part 2 for mulit-tier cachelib:
byrnedj Feb 7, 2023
2152639
basic multi-tier test based on numa bindings
igchor Dec 30, 2021
e303104
Aadding new configs to hit_ratio/graph_cache_leader_fobj
vinser52 Jan 27, 2022
1e40a00
Do not block reader if a child item is moving
igchor Dec 19, 2022
b99bb9d
Background data movement (#20)
byrnedj Oct 21, 2022
08bf0b4
fix race in moveRegularItemWith sync where insertOrReplace can cause …
byrnedj Feb 16, 2023
8f88aa6
Add extra allocation/eviction policies
igchor Mar 7, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions .github/workflows/build-cachelib-centos-long.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
name: build-cachelib-centos-latest
on:
schedule:
- cron: '0 7 * * *'

jobs:
build-cachelib-centos8-latest:
name: "CentOS/latest - Build CacheLib with all dependencies"
runs-on: ubuntu-latest
# Docker container image name
container: "centos:latest"
steps:
- name: "update packages"
run: dnf upgrade -y
- name: "install sudo,git"
run: dnf install -y sudo git cmake gcc
- name: "System Information"
run: |
echo === uname ===
uname -a
echo === /etc/os-release ===
cat /etc/os-release
echo === df -hl ===
df -hl
echo === free -h ===
free -h
echo === top ===
top -b -n1 -1 -Eg || timeout 1 top -b -n1
echo === env ===
env
echo === gcc -v ===
gcc -v
- name: "checkout sources"
uses: actions/checkout@v2
- name: "build CacheLib using build script"
run: ./contrib/build.sh -j -v -T
- name: "run tests"
timeout-minutes: 60
run: cd opt/cachelib/tests && ../../../run_tests.sh long
43 changes: 43 additions & 0 deletions .github/workflows/build-cachelib-debian.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: build-cachelib-debian-10
on:
schedule:
- cron: '30 5 * * 0,3'

jobs:
build-cachelib-debian-10:
name: "Debian/Buster - Build CacheLib with all dependencies"
runs-on: ubuntu-latest
# Docker container image name
container: "debian:buster-slim"
steps:
- name: "update packages"
run: apt-get update
- name: "upgrade packages"
run: apt-get -y upgrade
- name: "install sudo,git"
run: apt-get install -y sudo git procps
- name: "System Information"
run: |
echo === uname ===
uname -a
echo === /etc/os-release ===
cat /etc/os-release
echo === df -hl ===
df -hl
echo === free -h ===
free -h
echo === top ===
top -b -n1 -1 -Eg || timeout 1 top -b -n1 ; true
echo === env ===
env
echo === cc -v ===
cc -v || true
echo === g++ -v ===
g++ - || true
- name: "checkout sources"
uses: actions/checkout@v2
- name: "build CacheLib using build script"
run: ./contrib/build.sh -j -v -T
- name: "run tests"
timeout-minutes: 60
run: cd opt/cachelib/tests && ../../../run_tests.sh
49 changes: 49 additions & 0 deletions .github/workflows/build-cachelib-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
name: build-cachelib-docker
on:
push:
pull_request:

jobs:
build-cachelib-docker:
name: "CentOS/latest - Build CacheLib with all dependencies"
runs-on: ubuntu-latest
env:
REPO: cachelib
GITHUB_REPO: intel/CacheLib
CONTAINER_REG: ghcr.io/pmem/cachelib
CONTAINER_REG_USER: ${{ secrets.GH_CR_USER }}
CONTAINER_REG_PASS: ${{ secrets.GH_CR_PAT }}
FORCE_IMAGE_ACTION: ${{ secrets.FORCE_IMAGE_ACTION }}
HOST_WORKDIR: ${{ github.workspace }}
WORKDIR: docker
IMG_VER: devel
strategy:
matrix:
CONFIG: ["OS=centos OS_VER=8streams PUSH_IMAGE=1"]
steps:
- name: "System Information"
run: |
echo === uname ===
uname -a
echo === /etc/os-release ===
cat /etc/os-release
echo === df -hl ===
df -hl
echo === free -h ===
free -h
echo === top ===
top -b -n1 -1 -Eg || timeout 1 top -b -n1
echo === env ===
env
echo === gcc -v ===
gcc -v
- name: "checkout sources"
uses: actions/checkout@v2
with:
fetch-depth: 0

- name: Pull the image or rebuild and push it
run: cd $WORKDIR && ${{ matrix.CONFIG }} ./pull-or-rebuild-image.sh $FORCE_IMAGE_ACTION

- name: Run the build
run: cd $WORKDIR && ${{ matrix.CONFIG }} ./build.sh
2 changes: 1 addition & 1 deletion .github/workflows/clang-format-check.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# From: https://github.com/marketplace/actions/clang-format-check#multiple-paths
name: clang-format Check
on: [pull_request]
on: []
jobs:
formatting-check:
name: Formatting Check
Expand Down
113 changes: 113 additions & 0 deletions MultiTierDataMovement.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
# Background Data Movement

In order to reduce the number of online evictions and support asynchronous
promotion - we have added two periodic workers to handle eviction and promotion.

The diagram below shows a simplified version of how the background evictor
thread (green) is integrated to the CacheLib architecture.

<p align="center">
<img width="640" height="360" alt="BackgroundEvictor" src="cachelib-background-evictor.png">
</p>

## Background Evictors

The background evictors scan each class to see if there are objects to move the next (lower)
tier using a given strategy. Here we document the parameters for the different
strategies and general parameters.

- `backgroundEvictorIntervalMilSec`: The interval that this thread runs for - by default
the background evictor threads will wake up every 10 ms to scan the AllocationClasses. Also,
the background evictor thread will be woken up everytime there is a failed allocation (from
a request handling thread) and the current percentage of free memory for the
AllocationClass is lower than `lowEvictionAcWatermark`. This may render the interval parameter
not as important when there are many allocations occuring from request handling threads.

- `evictorThreads`: The number of background evictors to run - each thread is a assigned
a set of AllocationClasses to scan and evict objects from. Currently, each thread gets
an equal number of classes to scan - but as object size distribution may be unequal - future
versions will attempt to balance the classes among threads. The range is 1 to number of AllocationClasses.
The default is 1.

- `maxEvictionBatch`: The number of objects to remove in a given eviction call. The
default is 40. Lower range is 10 and the upper range is 1000. Too low and we might not
remove objects at a reasonable rate, too high and it might increase contention with user threads.

- `minEvictionBatch`: Minimum number of items to evict at any time (if there are any
candidates)

- `maxEvictionPromotionHotness`: Maximum candidates to consider for eviction. This is similar to `maxEvictionBatch`
but it specifies how many candidates will be taken into consideration, not the actual number of items to evict.
This option can be used to configure duration of critical section on LRU lock.


### FreeThresholdStrategy (default)

- `lowEvictionAcWatermark`: Triggers background eviction thread to run
when this percentage of the AllocationClass is free.
The default is `2.0`, to avoid wasting capacity we don't set this above `10.0`.

- `highEvictionAcWatermark`: Stop the evictions from an AllocationClass when this
percentage of the AllocationClass is free. The default is `5.0`, to avoid wasting capacity we
don't set this above `10`.


## Background Promoters

The background promoters scan each class to see if there are objects to move to a lower
tier using a given strategy. Here we document the parameters for the different
strategies and general parameters.

- `backgroundPromoterIntervalMilSec`: The interval that this thread runs for - by default
the background promoter threads will wake up every 10 ms to scan the AllocationClasses for
objects to promote.

- `promoterThreads`: The number of background promoters to run - each thread is a assigned
a set of AllocationClasses to scan and promote objects from. Currently, each thread gets
an equal number of classes to scan - but as object size distribution may be unequal - future
versions will attempt to balance the classes among threads. The range is `1` to number of AllocationClasses. The default is `1`.

- `maxProtmotionBatch`: The number of objects to promote in a given promotion call. The
default is 40. Lower range is 10 and the upper range is 1000. Too low and we might not
remove objects at a reasonable rate, too high and it might increase contention with user threads.

- `minPromotionBatch`: Minimum number of items to promote at any time (if there are any
candidates)

- `numDuplicateElements`: This allows us to promote items that have existing handles (read-only) since
we won't need to modify the data when a user is done with the data. Therefore, for a short time
the data could reside in both tiers until it is evicted from its current tier. The default is to
not allow this (0). Setting the value to 100 will enable duplicate elements in tiers.

### Background Promotion Strategy (only one currently)

- `promotionAcWatermark`: Promote items if there is at least this
percent of free AllocationClasses. Promotion thread will attempt to move `maxPromotionBatch` number of objects
to that tier. The objects are chosen from the head of the LRU. The default is `4.0`.
This value should correlate with `lowEvictionAcWatermark`, `highEvictionAcWatermark`, `minAcAllocationWatermark`, `maxAcAllocationWatermark`.
- `maxPromotionBatch`: The number of objects to promote in batch during BG promotion. Analogous to
`maxEvictionBatch`. It's value should be lower to decrease contention on hot items.

## Allocation policies

- `maxAcAllocationWatermark`: Item is always allocated in topmost tier if at least this
percentage of the AllocationClass is free.
- `minAcAllocationWatermark`: Item is always allocated in bottom tier if only this percent
of the AllocationClass is free. If percentage of free AllocationClasses is between `maxAcAllocationWatermark`
and `minAcAllocationWatermark`: then extra checks (described below) are performed to decide where to put the element.

By default, allocation will always be performed from the upper tier.

- `acTopTierEvictionWatermark`: If there is less that this percent of free memory in topmost tier, cachelib will attempt to evict from top tier. This option takes precedence before allocationWatermarks.

### Extra policies (used only when percentage of free AllocationClasses is between `maxAcAllocationWatermark`
and `minAcAllocationWatermark`)
- `sizeThresholdPolicy`: If item is smaller than this value, always allocate it in upper tier.
- `defaultTierChancePercentage`: Change (0-100%) of allocating item in top tier

## MMContainer options

- `lruInsertionPointSpec`: Can be set per tier when LRU2Q is used. Determines where new items are
inserted. 0 = insert to hot queue, 1 = insert to warm queue, 2 = insert to cold queue
- `markUsefulChance`: Per-tier, determines chance of moving item to the head of LRU on access

5 changes: 5 additions & 0 deletions cachelib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ set(CMAKE_MODULE_PATH
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED True)

if(COVERAGE_ENABLED)
# Add code coverage
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} --coverage -fprofile-arcs -ftest-coverage")
endif()

# include(fb_cxx_flags)
message(STATUS "Update CXXFLAGS: ${CMAKE_CXX_FLAGS}")

Expand Down
112 changes: 112 additions & 0 deletions cachelib/allocator/BackgroundMover-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
/*
* Copyright (c) Intel and its affiliates.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace facebook {
namespace cachelib {

template <typename CacheT>
BackgroundMover<CacheT>::BackgroundMover(
Cache& cache,
std::shared_ptr<BackgroundMoverStrategy> strategy,
MoverDir direction)
: cache_(cache), strategy_(strategy), direction_(direction) {
if (direction_ == MoverDir::Evict) {
moverFunc = BackgroundMoverAPIWrapper<CacheT>::traverseAndEvictItems;

} else if (direction_ == MoverDir::Promote) {
moverFunc = BackgroundMoverAPIWrapper<CacheT>::traverseAndPromoteItems;
}
}

template <typename CacheT>
BackgroundMover<CacheT>::~BackgroundMover() {
stop(std::chrono::seconds(0));
}

template <typename CacheT>
void BackgroundMover<CacheT>::work() {
try {
checkAndRun();
} catch (const std::exception& ex) {
XLOGF(ERR, "BackgroundMover interrupted due to exception: {}", ex.what());
}
}

template <typename CacheT>
void BackgroundMover<CacheT>::setAssignedMemory(
std::vector<MemoryDescriptorType>&& assignedMemory) {
XLOG(INFO, "Class assigned to background worker:");
for (auto [tid, pid, cid] : assignedMemory) {
XLOGF(INFO, "Tid: {}, Pid: {}, Cid: {}", tid, pid, cid);
}

mutex.lock_combine([this, &assignedMemory] {
this->assignedMemory_ = std::move(assignedMemory);
});
}

// Look for classes that exceed the target memory capacity
// and return those for eviction
template <typename CacheT>
void BackgroundMover<CacheT>::checkAndRun() {
auto assignedMemory = mutex.lock_combine([this] { return assignedMemory_; });

unsigned int moves = 0;
std::set<ClassId> classes{};
auto batches = strategy_->calculateBatchSizes(cache_, assignedMemory);

for (size_t i = 0; i < batches.size(); i++) {
const auto [tid, pid, cid] = assignedMemory[i];
const auto batch = batches[i];

classes.insert(cid);
const auto& mpStats = cache_.getPoolByTid(pid, tid).getStats();

if (!batch) {
continue;
}

// try moving BATCH items from the class in order to reach free target
auto moved = moverFunc(cache_, tid, pid, cid, batch);
moves += moved;
moves_per_class_[tid][pid][cid] += moved;
totalBytesMoved.add(moved * mpStats.acStats.at(cid).allocSize);
}

numTraversals.inc();
numMovedItems.add(moves);
totalClasses.add(classes.size());
}

template <typename CacheT>
BackgroundMoverStats BackgroundMover<CacheT>::getStats() const noexcept {
BackgroundMoverStats stats;
stats.numMovedItems = numMovedItems.get();
stats.runCount = numTraversals.get();
stats.totalBytesMoved = totalBytesMoved.get();
stats.totalClasses = totalClasses.get();

return stats;
}

template <typename CacheT>
std::map<TierId, std::map<PoolId, std::map<ClassId, uint64_t>>>
BackgroundMover<CacheT>::getClassStats() const noexcept {
return moves_per_class_;
}

} // namespace cachelib
} // namespace facebook
Loading