Skip to content

Commit 6478e19

Browse files
committed
Add memory usage statistics for slabs and allocation classes
1 parent acdfa0b commit 6478e19

File tree

1 file changed

+73
-0
lines changed

1 file changed

+73
-0
lines changed
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
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+
#pragma once
18+
19+
#include "cachelib/allocator/Cache.h"
20+
#include "cachelib/allocator/BackgroundEvictorStrategy.h"
21+
22+
namespace facebook {
23+
namespace cachelib {
24+
25+
26+
// Base class for background eviction strategy.
27+
class PromotionStrategy : public BackgroundEvictorStrategy {
28+
29+
public:
30+
PromotionStrategy(uint64_t promotionAcWatermark, uint64_t maxPromotionBatch, uint64_t minPromotionBatch):
31+
promotionAcWatermark(promotionAcWatermark), maxPromotionBatch(maxPromotionBatch), minPromotionBatch(minPromotionBatch)
32+
{
33+
34+
}
35+
~PromotionStrategy() {}
36+
37+
std::vector<size_t> calculateBatchSizes(const CacheBase& cache,
38+
std::vector<std::tuple<TierId, PoolId, ClassId>> acVec) {
39+
std::vector<size_t> batches{};
40+
for (auto [tid, pid, cid] : acVec) {
41+
XDCHECK(tid > 0);
42+
auto stats = cache.getAllocationClassStats(tid - 1, pid, cid);
43+
if (stats.approxFreePercent < promotionAcWatermark)
44+
batches.push_back(0);
45+
else {
46+
auto maxPossibleItemsToPromote = static_cast<size_t>((promotionAcWatermark - stats.approxFreePercent) *
47+
stats.memorySize / stats.allocSize);
48+
batches.push_back(maxPossibleItemsToPromote);
49+
}
50+
}
51+
52+
auto maxBatch = *std::max_element(batches.begin(), batches.end());
53+
if (maxBatch == 0)
54+
return batches;
55+
56+
std::transform(batches.begin(), batches.end(), batches.begin(), [&](auto numItems){
57+
auto cappedBatchSize = maxPromotionBatch * numItems / maxBatch;
58+
if (cappedBatchSize < minPromotionBatch)
59+
return 0UL;
60+
else
61+
return cappedBatchSize;
62+
});
63+
64+
return batches;
65+
}
66+
private:
67+
double promotionAcWatermark{4.0};
68+
uint64_t maxPromotionBatch{40};
69+
uint64_t minPromotionBatch{5};
70+
};
71+
72+
} // namespace cachelib
73+
} // namespace facebook

0 commit comments

Comments
 (0)