|
| 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 | +namespace facebook { |
| 18 | +namespace cachelib { |
| 19 | +namespace detail { |
| 20 | +template <typename T> |
| 21 | +bool areBytesSame(const T& one, const T& two) { |
| 22 | + return std::memcmp(&one, &two, sizeof(T)) == 0; |
| 23 | +} |
| 24 | +} // namespace detail |
| 25 | + |
| 26 | +/* Container Interface Implementation */ |
| 27 | +template <typename T, MMLruApproxApprox::Hook<T> T::*HookPtr> |
| 28 | +MMLruApprox::Container<T, HookPtr>::Container(serialization::MMLruApproxObject object, |
| 29 | + PtrCompressor compressor) |
| 30 | + : compressor_(std::move(compressor)), |
| 31 | + lru_(*object.lru_ref(), compressor_), |
| 32 | + insertionPoint_(compressor_.unCompress( |
| 33 | + CompressedPtr{*object.compressedInsertionPoint_ref()})), |
| 34 | + tailSize_(*object.tailSize_ref()), |
| 35 | + config_(*object.config_ref()) { |
| 36 | + lruRefreshTime_ = config_.lruRefreshTime; |
| 37 | + nextReconfigureTime_ = config_.mmReconfigureIntervalSecs.count() == 0 |
| 38 | + ? std::numeric_limits<Time>::max() |
| 39 | + : static_cast<Time>(util::getCurrentTimeSec()) + |
| 40 | + config_.mmReconfigureIntervalSecs.count(); |
| 41 | +} |
| 42 | + |
| 43 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 44 | +bool MMLruApprox::Container<T, HookPtr>::recordAccess(T& node, |
| 45 | + AccessMode mode) noexcept { |
| 46 | + if ((mode == AccessMode::kWrite && !config_.updateOnWrite) || |
| 47 | + (mode == AccessMode::kRead && !config_.updateOnRead)) { |
| 48 | + return false; |
| 49 | + } |
| 50 | + |
| 51 | + const auto curr = static_cast<Time>(util::getCurrentTimeSec()); |
| 52 | + // check if the node is still being memory managed |
| 53 | + if (node.isInMMContainer() && |
| 54 | + ((curr >= getUpdateTime(node) + |
| 55 | + lruRefreshTime_.load(std::memory_order_relaxed)) || |
| 56 | + !isAccessed(node))) { |
| 57 | + if (!isAccessed(node)) { |
| 58 | + markAccessed(node); |
| 59 | + } |
| 60 | + |
| 61 | + auto func = [this, &node, curr]() { |
| 62 | + if (node.isInMMContainer()) { |
| 63 | + setUpdateTime(node, curr); |
| 64 | + } |
| 65 | + }; |
| 66 | + |
| 67 | + return true; |
| 68 | + } |
| 69 | + return false; |
| 70 | +} |
| 71 | + |
| 72 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 73 | +cachelib::EvictionAgeStat MMLruApprox::Container<T, HookPtr>::getEvictionAgeStat( |
| 74 | + uint64_t projectedLen) const noexcept { |
| 75 | +} |
| 76 | + |
| 77 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 78 | +cachelib::EvictionAgeStat |
| 79 | +MMLruApprox::Container<T, HookPtr>::getEvictionAgeStatLocked( |
| 80 | + uint64_t projectedLength) const noexcept { |
| 81 | + EvictionAgeStat stat{}; |
| 82 | + const auto currTime = static_cast<Time>(util::getCurrentTimeSec()); |
| 83 | +} |
| 84 | + |
| 85 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 86 | +void MMLruApprox::Container<T, HookPtr>::setConfig(const Config& newConfig) { |
| 87 | +} |
| 88 | + |
| 89 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 90 | +typename MMLruApprox::Config MMLruApprox::Container<T, HookPtr>::getConfig() const { |
| 91 | + return; |
| 92 | +} |
| 93 | + |
| 94 | +//template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 95 | +//bool MMLruApprox::Container<T, HookPtr>::add(T& node) noexcept { |
| 96 | +// const auto currTime = static_cast<Time>(util::getCurrentTimeSec()); |
| 97 | +// |
| 98 | +// if (node.isInMMContainer()) { |
| 99 | +// return false; |
| 100 | +// } |
| 101 | +// node.markInMMContainer(); |
| 102 | +// setUpdateTime(node, currTime); |
| 103 | +// unmarkAccessed(node); |
| 104 | +// return true; |
| 105 | +//} |
| 106 | + |
| 107 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 108 | +typename MMLruApprox::Container<T, HookPtr>::Iterator |
| 109 | +MMLruApprox::Container<T, HookPtr>::getEvictionIterator() const noexcept { |
| 110 | + //LockHolder l(*lruMutex_); |
| 111 | + //return Iterator{std::move(l), lru_.rbegin()}; |
| 112 | + //generate a set of candidates for eviction |
| 113 | + |
| 114 | +} |
| 115 | + |
| 116 | + |
| 117 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 118 | +serialization::MMLruApproxObject MMLruApprox::Container<T, HookPtr>::saveState() |
| 119 | + const noexcept { |
| 120 | + serialization::MMLruApproxConfig configObject; |
| 121 | + *configObject.lruRefreshTime_ref() = |
| 122 | + lruRefreshTime_.load(std::memory_order_relaxed); |
| 123 | + *configObject.lruRefreshRatio_ref() = config_.lruRefreshRatio; |
| 124 | + *configObject.updateOnWrite_ref() = config_.updateOnWrite; |
| 125 | + *configObject.updateOnRead_ref() = config_.updateOnRead; |
| 126 | + *configObject.tryLockUpdate_ref() = config_.tryLockUpdate; |
| 127 | + *configObject.lruInsertionPointSpec_ref() = config_.lruInsertionPointSpec; |
| 128 | + |
| 129 | + serialization::MMLruApproxObject object; |
| 130 | + *object.config_ref() = configObject; |
| 131 | + *object.compressedInsertionPoint_ref() = |
| 132 | + compressor_.compress(insertionPoint_).saveState(); |
| 133 | + *object.tailSize_ref() = tailSize_; |
| 134 | + *object.lru_ref() = lru_.saveState(); |
| 135 | + return object; |
| 136 | +} |
| 137 | + |
| 138 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 139 | +MMContainerStat MMLruApprox::Container<T, HookPtr>::getStats() const noexcept { |
| 140 | +} |
| 141 | + |
| 142 | + |
| 143 | +// Iterator Context Implementation |
| 144 | +template <typename T, MMLruApprox::Hook<T> T::*HookPtr> |
| 145 | +MMLruApprox::Container<T, HookPtr>::Iterator::Iterator( |
| 146 | + LockHolder l, const typename LruList::Iterator& iter) noexcept |
| 147 | + : LruList::Iterator(iter), l_(std::move(l)) {} |
| 148 | +} // namespace cachelib |
| 149 | +} // namespace facebook |
0 commit comments