Skip to content

[upstream] Initial framework for background item eviction/promotion #43

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

Closed
wants to merge 44 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
160bb77
Updating submodules
Feb 16, 2023
ba8d6b0
Updating submodules
Feb 16, 2023
ba9dd68
Change some of the stats to rate
Feb 17, 2023
ad8bfd6
Updating submodules
Feb 17, 2023
bdad762
Updating submodules
Feb 18, 2023
b42327a
Updating submodules
Feb 19, 2023
0d49b53
Updating submodules
Feb 20, 2023
bb2d1c6
Updating submodules
Feb 21, 2023
c167a83
Updating submodules
Feb 21, 2023
32ed439
Return replacedPtr from InsertOrReplace API
Feb 21, 2023
dd0af61
Updating submodules
Feb 21, 2023
cce19ea
Updating submodules
Feb 22, 2023
6357906
Pin fmt version at 8.0.1 (#196)
wonglkd Feb 24, 2023
a925737
KVReplayGenerator: parse GET_LEASE and SET_LEASE operations
Feb 25, 2023
f7e13a4
Update @braintree/sanitize-url version
Feb 27, 2023
70ff91f
Add missing numa deps for fedora, rocky, arch (#197)
wonglkd Feb 27, 2023
6ad7a31
fbcode/cachelib/allocator/datastruct/serialize
Feb 27, 2023
df5b9f6
Run Github Actions on pull requests (#198)
wonglkd Feb 28, 2023
e8151ad
Add a custom deleter class to access the Item Handle
Feb 28, 2023
9827818
fix flaky tests in NvmCacheTests
Feb 28, 2023
9447a8a
Added PM9A3 support for Cachebench Write Bytes Calculations
Mar 1, 2023
293118b
Fix XDCHECK regression in lambda for gcc-8.x (#201)
wonglkd Mar 1, 2023
f853a42
Get or update object TTL via object shared_ptr
Mar 1, 2023
c120a53
Remove isWriteHandle() API
Mar 2, 2023
7785d24
Edit Cachebench_FB_HW_eval.md using inpage editor
Mar 2, 2023
185bbe6
Fix Debian GitHub build & zstd CMake error (#200)
wonglkd Mar 2, 2023
968533f
fix broken installation page link
Mar 3, 2023
496ed37
fix another broken installation page link in index.js
Mar 3, 2023
67d2d8b
temporarily disable PackIt (#205)
michel-slm Mar 5, 2023
115732a
Edit Overview_A_random_walk_down_the_Cache_Library.md using inpage ed…
Mar 6, 2023
17bdc18
fbcode/cachelib/experimental/objcache2/persistence
Mar 7, 2023
e679b72
README.md: added build status badges to main README.md
Mar 7, 2023
43a7ce3
Fix Bug in ItemValue handling in CacheBench
Mar 8, 2023
872d61a
fbcode/cachelib/benchmarks
Mar 9, 2023
ffa5470
fbcode/cachelib/allocator/datastruct/tests
Mar 9, 2023
1031b13
fbcode/cachelib/experimental/objcache2/tests
Mar 9, 2023
d92910b
fbcode/cachelib/experimental/objcache/tests
Mar 9, 2023
1e05a16
fbcode/cachelib/navy/serialization
Mar 9, 2023
0bc9f66
Introduce 'markedForEviction' state for the Item. (#183)
igchor Mar 9, 2023
0bdb935
fbcode/cachelib/allocator/serialize
Mar 9, 2023
99a05d3
fbcode/cachelib/experimental/objcache
Mar 9, 2023
d1ff29c
fbcode/cachelib/
Mar 10, 2023
3a8d8b4
Add extra notes for object-cache find APIs
Mar 13, 2023
c6daa0a
Initial framework for background item eviction/promotion
byrnedj Aug 9, 2022
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
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-centos-8-1.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name: build-cachelib-centos-8-1
on:
# push:
pull_request:
schedule:
- cron: '0 11 * * 1,3,5'
jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-centos-8-5.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name: build-cachelib-centos-8.5
on:
# push:
pull_request:
schedule:
- cron: '0 9 * * *'
jobs:
Expand Down
4 changes: 4 additions & 0 deletions .github/workflows/build-cachelib-debian-10.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name: build-cachelib-debian-10
on:
# push:
pull_request:
schedule:
- cron: '0 13 * * *'
jobs:
Expand Down Expand Up @@ -51,6 +52,9 @@ jobs:
g++ - || true
- name: "checkout sources"
uses: actions/checkout@v2
- name: "Add Git safe directory"
# Workaround for Docker image bug (GitHub issue #199).
run: git config --system --add safe.directory $GITHUB_WORKSPACE
- name: "Install Prerequisites"
run: ./contrib/build.sh -S -B
- name: "Test: update-submodules"
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-fedora-36.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name: build-cachelib-fedora-36
on:
# push:
pull_request:
schedule:
- cron: '0 19 * * *'
jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-rockylinux-8.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name: build-cachelib-rockylinux-8.6
on:
# push:
pull_request:
schedule:
- cron: '0 15 * * 2,4,6'
jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-rockylinux-9.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
name: build-cachelib-rockylinux-9.0
on:
# push:
pull_request:
schedule:
- cron: '0 17 * * *'
jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-ubuntu-18.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
name: build-cachelib-ubuntu-18
on:
# push:
pull_request:
schedule:
- cron: '0 5 * * 2,4,6'
jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-ubuntu-20.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
name: build-cachelib-ubuntu-20
on:
# push:
pull_request:
schedule:
- cron: '0 5 * * 1,3,5'
jobs:
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/build-cachelib-ubuntu-22.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
name: build-cachelib-ubuntu-22
on:
# push:
pull_request:
schedule:
- cron: '0 7 * * *'
jobs:
Expand Down
25 changes: 0 additions & 25 deletions .packit.yaml

This file was deleted.

17 changes: 16 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ cd CacheLib
Re-running `./contrib/build.sh` will update CacheLib and its dependencies
to their latest versions and rebuild them.

See [build](https://cachelib.org/docs/installation/installation) for more details about
See [build](https://cachelib.org/docs/installation/) for more details about
the building and installation process.


Expand All @@ -77,3 +77,18 @@ https://www.facebook.com/whitehat

Facebook's security team will triage your report and determine whether or not is
it eligible for a bounty under our program.


## Build status

Clicking on a badge will show you the recent builds for that OS. If your target OS's build is failing, you may wish to check recent issues and PRs for known workarounds.

- [![CentOS 8.1](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-centos-8-1.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-centos-8-1.yml?query=event%3Aschedule)
- [![CentOS 8.5](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-centos-8-5.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-centos-8-5.yml?query=event%3Aschedule)
- [![Debian 10](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-debian-10.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-debian-10.yml?query=event%3Aschedule)
- [![Fedora 36](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-fedora-36.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-fedora-36.yml?query=event%3Aschedule)
- [![Rocky Linux 8](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-rockylinux-8.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-rockylinux-8.yml?query=event%3Aschedule)
- [![Rocky Linux 9](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-rockylinux-9.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-rockylinux-9.yml?query=event%3Aschedule)
- [![Ubuntu 18](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-ubuntu-18.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-ubuntu-18.yml?query=event%3Aschedule)
- [![Ubuntu 20](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-ubuntu-20.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-ubuntu-20.yml?query=event%3Aschedule)
- [![Ubuntu 22](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-ubuntu-22.yml/badge.svg?event=schedule)](https://github.com/facebook/cachelib/actions/workflows/build-cachelib-ubuntu-22.yml?query=event%3Aschedule)
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 [pid, cid] : assignedMemory) {
XLOGF(INFO, "Pid: {}, Cid: {}", 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 [pid, cid] = assignedMemory[i];
const auto batch = batches[i];

classes.insert(cid);
const auto& mpStats = cache_.getPool(pid).getStats();

if (!batch) {
continue;
}

// try moving BATCH items from the class in order to reach free target
auto moved = moverFunc(cache_, pid, cid, batch);
moves += moved;
moves_per_class_[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<PoolId, std::map<ClassId, uint64_t>>
BackgroundMover<CacheT>::getClassStats() const noexcept {
return moves_per_class_;
}

} // namespace cachelib
} // namespace facebook
96 changes: 96 additions & 0 deletions cachelib/allocator/BackgroundMover.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
/*
* 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.
*/

#pragma once

#include "cachelib/allocator/BackgroundMoverStrategy.h"
#include "cachelib/allocator/CacheStats.h"
#include "cachelib/common/AtomicCounter.h"
#include "cachelib/common/PeriodicWorker.h"

namespace facebook {
namespace cachelib {

// wrapper that exposes the private APIs of CacheType that are specifically
// needed for the cache api
template <typename C>
struct BackgroundMoverAPIWrapper {
static size_t traverseAndEvictItems(C& cache,
unsigned int pid,
unsigned int cid,
size_t batch) {
return cache.traverseAndEvictItems(pid, cid, batch);
}

static size_t traverseAndPromoteItems(C& cache,
unsigned int pid,
unsigned int cid,
size_t batch) {
return cache.traverseAndPromoteItems(pid, cid, batch);
}
};

enum class MoverDir { Evict = 0, Promote };

// Periodic worker that evicts items from tiers in batches
// The primary aim is to reduce insertion times for new items in the
// cache
template <typename CacheT>
class BackgroundMover : public PeriodicWorker {
public:
using Cache = CacheT;
// @param cache the cache interface
// @param strategy the stragey class that defines how objects are
// moved,
// (promoted vs. evicted and how much)
BackgroundMover(Cache& cache,
std::shared_ptr<BackgroundMoverStrategy> strategy,
MoverDir direction_);

~BackgroundMover() override;

BackgroundMoverStats getStats() const noexcept;
std::map<PoolId, std::map<ClassId, uint64_t>> getClassStats() const noexcept;

void setAssignedMemory(std::vector<MemoryDescriptorType>&& assignedMemory);

private:
std::map<PoolId, std::map<ClassId, uint64_t>> moves_per_class_;
// cache allocator's interface for evicting
using Item = typename Cache::Item;

Cache& cache_;
std::shared_ptr<BackgroundMoverStrategy> strategy_;
MoverDir direction_;

std::function<size_t(Cache&, unsigned int, unsigned int, size_t)> moverFunc;

// implements the actual logic of running the background evictor
void work() override final;
void checkAndRun();

AtomicCounter numMovedItems{0};
AtomicCounter numTraversals{0};
AtomicCounter totalClasses{0};
AtomicCounter totalBytesMoved{0};

std::vector<MemoryDescriptorType> assignedMemory_;
folly::DistributedMutex mutex;
};
} // namespace cachelib
} // namespace facebook

#include "cachelib/allocator/BackgroundMover-inl.h"
38 changes: 38 additions & 0 deletions cachelib/allocator/BackgroundMoverStrategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* Copyright (c) Facebook, Inc. 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.
*/

#pragma once

#include "cachelib/allocator/Cache.h"

namespace facebook {
namespace cachelib {

struct MemoryDescriptorType {
MemoryDescriptorType(PoolId pid, ClassId cid) : pid_(pid), cid_(cid) {}
PoolId pid_;
ClassId cid_;
};

// Base class for background eviction strategy.
class BackgroundMoverStrategy {
public:
virtual std::vector<size_t> calculateBatchSizes(
const CacheBase& cache, std::vector<MemoryDescriptorType> acVec) = 0;
};

} // namespace cachelib
} // namespace facebook
1 change: 1 addition & 0 deletions cachelib/allocator/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ add_library (cachelib_allocator
CCacheManager.cpp
ContainerTypes.cpp
FreeMemStrategy.cpp
FreeThresholdStrategy.cpp
HitsPerSlabStrategy.cpp
LruTailAgeStrategy.cpp
MarginalHitsOptimizeStrategy.cpp
Expand Down
Loading