Skip to content

added stats #2

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
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
87 changes: 87 additions & 0 deletions BackgroundMovers.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# 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 (higher)
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 thead will be woken up everytime there is a failed allocation (from
a request handling thread) and the current percentage of allocated slabs for the
AllocationClass exceed the `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 4, so each thread will get 6 classes by default.

- `evictionHotnessThreshold`: The number of objects to remove in a given eviction call. The
default is 200. 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 we hold the locks for copying data
between tiers for too long.


### FreeThresholdStrategy (default)

- `evictionSlabWatermark`: Allows background eviction once this total percentage of slabs
allocated has been reached. This is precondition for background eviction to occur at all since
if there are unallocated slabs then those should be allocated before running any background
evictions. The default is `100` and probably shouldn't be changed much without good reason.

- `lowEvictionAcWatermark`: Triggers background eviction thread to run
when this percentage of the AllocationClass is allocated.
The default is `98.0`, to avoid wasting capacity we don't set this below `90`.

- `highEvictionAcWatermark`: Stop the evictions from an AllocationClass when this
percentage of the AllocationClass is allocated. The default is `95.0`, to avoid wasting capacity we
don't set this below `90`.


## Background Promoters

The background promotes 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 4, so each thread will get 6 classes by default.

- `evictionHotnessThreshold`: The number of objects to remove in a given eviction call. The
default is 200. 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 we hold the locks for copying data
between tiers for too long.

- `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`: If the class for the next lower tier has at least this percentage of free
slabs, then the promotion thread will attempt to move `evictionHotnessThreshold` number of objects
to the next lower tier. The objects are chosen from the head of the LRU. The default is `97` so
promotion will only occur when there is at least 3% of the slabs for this class free.




Binary file added cachelib-background-evictor.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
114 changes: 114 additions & 0 deletions cachelib/allocator/BackgroundEvictor-inl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
/*
* 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>
BackgroundEvictor<CacheT>::BackgroundEvictor(Cache& cache,
std::shared_ptr<BackgroundEvictorStrategy> strategy)
: cache_(cache),
strategy_(strategy)
{
}

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

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

template <typename CacheT>
void BackgroundEvictor<CacheT>::setAssignedMemory(std::vector<std::tuple<TierId, PoolId, ClassId>> &&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 BackgroundEvictor<CacheT>::checkAndRun() {
auto assignedMemory = mutex.lock_combine([this]{
return assignedMemory_;
});

unsigned int evictions = 0;
std::set<ClassId> classes{};

for (const auto [tid, pid, cid] : assignedMemory) {
classes.insert(cid);
const auto& mpStats = cache_.getPoolByTid(pid,tid).getStats();
auto batch = strategy_->calculateBatchSize(cache_,tid,pid,cid);
if (!batch) {
continue;
}

stats.evictionSize.add(batch * mpStats.acStats.at(cid).allocSize);

//try evicting BATCH items from the class in order to reach free target
auto evicted =
BackgroundEvictorAPIWrapper<CacheT>::traverseAndEvictItems(cache_,
tid,pid,cid,batch);
evictions += evicted;

const size_t cid_id = (size_t)mpStats.acStats.at(cid).allocSize;
auto it = evictions_per_class_.find(cid_id);
if (it != evictions_per_class_.end()) {
it->second += evicted;
} else {
evictions_per_class_[cid_id] = 0;
}
}

stats.numTraversals.inc();
stats.numEvictedItems.add(evictions);
stats.totalClasses.add(classes.size());
}

template <typename CacheT>
BackgroundEvictionStats BackgroundEvictor<CacheT>::getStats() const noexcept {
BackgroundEvictionStats evicStats;
evicStats.numEvictedItems = stats.numEvictedItems.get();
evicStats.runCount = stats.numTraversals.get();
evicStats.evictionSize = stats.evictionSize.get();
evicStats.totalClasses = stats.totalClasses.get();

return evicStats;
}

template <typename CacheT>
std::map<uint32_t,uint64_t> BackgroundEvictor<CacheT>::getClassStats() const noexcept {
return evictions_per_class_;
}

} // namespace cachelib
} // namespace facebook
99 changes: 99 additions & 0 deletions cachelib/allocator/BackgroundEvictor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/*
* 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 <gtest/gtest_prod.h>
#include <folly/concurrency/UnboundedQueue.h>

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


namespace facebook {
namespace cachelib {

// wrapper that exposes the private APIs of CacheType that are specifically
// needed for the eviction.
template <typename C>
struct BackgroundEvictorAPIWrapper {

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

struct BackgroundEvictorStats {
// items evicted
AtomicCounter numEvictedItems{0};

// traversals
AtomicCounter numTraversals{0};

// total class size
AtomicCounter totalClasses{0};

// item eviction size
AtomicCounter evictionSize{0};
};

// 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 BackgroundEvictor : public PeriodicWorker {
public:
using Cache = CacheT;
// @param cache the cache interface
// @param target_free the target amount of memory to keep free in
// this tier
// @param tier id memory tier to perform eviction on
BackgroundEvictor(Cache& cache,
std::shared_ptr<BackgroundEvictorStrategy> strategy);

~BackgroundEvictor() override;

BackgroundEvictionStats getStats() const noexcept;
std::map<uint32_t,uint64_t> getClassStats() const noexcept;

void setAssignedMemory(std::vector<std::tuple<TierId, PoolId, ClassId>> &&assignedMemory);

private:
std::map<uint32_t,uint64_t> evictions_per_class_;

// cache allocator's interface for evicting

using Item = typename Cache::Item;

Cache& cache_;
std::shared_ptr<BackgroundEvictorStrategy> strategy_;

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

BackgroundEvictorStats stats;

std::vector<std::tuple<TierId, PoolId, ClassId>> assignedMemory_;
folly::DistributedMutex mutex;
};
} // namespace cachelib
} // namespace facebook

#include "cachelib/allocator/BackgroundEvictor-inl.h"
36 changes: 36 additions & 0 deletions cachelib/allocator/BackgroundEvictorStrategy.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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 {


// Base class for background eviction strategy.
class BackgroundEvictorStrategy {

public:
virtual size_t calculateBatchSize(const CacheBase& cache,
unsigned int tid,
PoolId pid,
ClassId cid ) = 0;
};

} // namespace cachelib
} // namespace facebook
Loading