|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 "ReuseTimeReinsertionPolicy.h" |
| 18 | + |
| 19 | +#include "cachelib/allocator/nvmcache/NvmItem.h" |
| 20 | + |
| 21 | +namespace facebook::cachelib::navy { |
| 22 | + |
| 23 | +ReuseTimeReinsertionPolicy::ReuseTimeReinsertionPolicy( |
| 24 | + const cachelib::navy::Index& index, |
| 25 | + size_t numBuckets, |
| 26 | + size_t bucketSize, |
| 27 | + uint32_t reinsertionThreshold, |
| 28 | + std::shared_ptr<Ticker> ticker) |
| 29 | + : index_(index), |
| 30 | + numBuckets_(numBuckets), |
| 31 | + bucketSize_(bucketSize), |
| 32 | + reuseTimeThreshold_(reinsertionThreshold) { |
| 33 | + auto config = AccessTracker::Config(); |
| 34 | + config.numBuckets = numBuckets_; |
| 35 | + config.useCounts = false; |
| 36 | + config.numTicksPerBucket = bucketSize_; |
| 37 | + config.ticker = std::move(ticker); |
| 38 | + tracker_ = std::make_unique<AccessTracker>(std::move(config)); |
| 39 | +} |
| 40 | + |
| 41 | +ReuseTimeReinsertionPolicy::ReuseTimeReinsertionPolicy( |
| 42 | + const cachelib::navy::Index& index, |
| 43 | + size_t numBuckets, |
| 44 | + size_t bucketSize, |
| 45 | + uint32_t reinsertionThreshold) |
| 46 | + : ReuseTimeReinsertionPolicy( |
| 47 | + index, numBuckets, bucketSize, reinsertionThreshold, nullptr) {} |
| 48 | + |
| 49 | +bool ReuseTimeReinsertionPolicy::shouldReinsert(folly::StringPiece key, |
| 50 | + folly::StringPiece value) { |
| 51 | + reinsertAttempts_.inc(); |
| 52 | + |
| 53 | + const auto lr = index_.peek( |
| 54 | + makeHK(cachelib::navy::BufferView{ |
| 55 | + key.size(), reinterpret_cast<const uint8_t*>(key.data())}) |
| 56 | + .keyHash()); |
| 57 | + |
| 58 | + if (!(lr.found())) { |
| 59 | + keyNotFound_.inc(); |
| 60 | + return false; |
| 61 | + } |
| 62 | + |
| 63 | + if (isExpired(value)) { |
| 64 | + expired_.inc(); |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + auto reuseTime = getReuseTime(key); |
| 69 | + if ((reuseTime == 0) || (reuseTime >= reuseTimeThreshold_)) { |
| 70 | + return false; |
| 71 | + } |
| 72 | + reinserted_.inc(); |
| 73 | + return true; |
| 74 | +} |
| 75 | + |
| 76 | +void ReuseTimeReinsertionPolicy::onLookup(folly::StringPiece key) { |
| 77 | + tracker_->recordAccess(key); |
| 78 | +} |
| 79 | + |
| 80 | +void ReuseTimeReinsertionPolicy::getCounters( |
| 81 | + const util::CounterVisitor& visitor) const { |
| 82 | + visitor("bc_reinsert_reuse_time_attempts", |
| 83 | + reinsertAttempts_.get(), |
| 84 | + cachelib::util::CounterVisitor::CounterType::RATE); |
| 85 | + visitor("bc_reinsert_reuse_time_key_not_found", |
| 86 | + keyNotFound_.get(), |
| 87 | + cachelib::util::CounterVisitor::CounterType::RATE); |
| 88 | + visitor("bc_reinsert_reuse_time_success", |
| 89 | + reinserted_.get(), |
| 90 | + cachelib::util::CounterVisitor::CounterType::RATE); |
| 91 | + visitor("bc_reinsert_expired", |
| 92 | + expired_.get(), |
| 93 | + cachelib::util::CounterVisitor::CounterType::RATE); |
| 94 | + visitor("bc_reinsert_no_prev_access", |
| 95 | + noPrevAccess_.get(), |
| 96 | + cachelib::util::CounterVisitor::CounterType::RATE); |
| 97 | + // Report percentile stats for reuse time values |
| 98 | + reuseTimeStats_.visitQuantileEstimator(visitor, "bc_reinsert_reuse_time"); |
| 99 | +} |
| 100 | + |
| 101 | +size_t ReuseTimeReinsertionPolicy::getReuseTime(folly::StringPiece key) { |
| 102 | + /* |
| 103 | + * Compute the reuse time for the key. The reuse time is the maximum of |
| 104 | + * time since last access and the most recent reuse time. If we only have |
| 105 | + * a history of access in a single bucket, then we only have time since |
| 106 | + * access. If we have a history of access in multiple buckets, then we |
| 107 | + * have both time since last access and the most recent reuse time. |
| 108 | + * |
| 109 | + * The granularity of reuse time is based on bucket size. Even if the key |
| 110 | + * was accessed in last few seconds, if there is access in the most recent |
| 111 | + * bucket, the reuse time will be equal to bucket size. |
| 112 | + */ |
| 113 | + std::tuple<int64_t, int64_t> reuseTuple = getPrevAccessBuckets(key); |
| 114 | + auto reuseTime = 0; |
| 115 | + if (std::get<0>(reuseTuple) >= 0) { |
| 116 | + size_t timeSinceLastAccess = bucketSize_ * (std::get<0>(reuseTuple) + 1); |
| 117 | + size_t latestReuseTime = 0; |
| 118 | + if (std::get<1>(reuseTuple) >= 0) { |
| 119 | + latestReuseTime = |
| 120 | + (std::get<1>(reuseTuple) - std::get<0>(reuseTuple)) * bucketSize_; |
| 121 | + } |
| 122 | + reuseTime = std::max(timeSinceLastAccess, latestReuseTime); |
| 123 | + reuseTimeStats_.trackValue(static_cast<double>(reuseTime)); |
| 124 | + } else { |
| 125 | + noPrevAccess_.inc(); |
| 126 | + } |
| 127 | + return reuseTime; |
| 128 | +} |
| 129 | + |
| 130 | +std::tuple<int64_t, int64_t> ReuseTimeReinsertionPolicy::getPrevAccessBuckets( |
| 131 | + folly::StringPiece key) { |
| 132 | + /* |
| 133 | + * Get the index of the latest and second latest access buckets for the key. |
| 134 | + * This information allows us to calculate time since last access and the most |
| 135 | + * recent reuse time. |
| 136 | + * If we can't find the latest or second latest access bucket, |
| 137 | + * we assign -1 as the bucket index. The key that was not |
| 138 | + * accessed in the time window being tracked will return a tuple of (-1, -1). |
| 139 | + */ |
| 140 | + std::vector<double> accesses = tracker_->getAccesses(key); |
| 141 | + int64_t latestReuseBucket = -1; |
| 142 | + int64_t secondLatestReuseBucket = -1; |
| 143 | + |
| 144 | + for (size_t bucketIdx = 0; bucketIdx < accesses.size(); bucketIdx++) { |
| 145 | + if (accesses.at(bucketIdx) > 0) { |
| 146 | + if (latestReuseBucket == -1) { |
| 147 | + latestReuseBucket = bucketIdx; |
| 148 | + } else if (secondLatestReuseBucket == -1) { |
| 149 | + secondLatestReuseBucket = bucketIdx; |
| 150 | + } else { |
| 151 | + break; |
| 152 | + } |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + return std::make_tuple(latestReuseBucket, secondLatestReuseBucket); |
| 157 | +} |
| 158 | + |
| 159 | +uint32_t ReuseTimeReinsertionPolicy::isExpired(folly::StringPiece value) { |
| 160 | + if (value.size() < sizeof(cachelib::NvmItem)) { |
| 161 | + return 0; |
| 162 | + } |
| 163 | + cachelib::NvmItem nvmItem{0, 0, 0, std::vector<cachelib::Blob>{}}; |
| 164 | + ::memcpy(&nvmItem, value.data(), sizeof(cachelib::NvmItem)); |
| 165 | + |
| 166 | + return nvmItem.isExpired(); |
| 167 | +} |
| 168 | + |
| 169 | +} // namespace facebook::cachelib::navy |
0 commit comments