| 1 |
/// The MIT License (MIT) |
| 2 |
/// Copyright (c) 2016 Peter Goldsborough |
| 3 |
/// |
| 4 |
/// Permission is hereby granted, free of charge, to any person obtaining a copy |
| 5 |
/// of this software and associated documentation files (the "Software"), to |
| 6 |
/// deal in the Software without restriction, including without limitation the |
| 7 |
/// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 8 |
/// sell copies of the Software, and to permit persons to whom the Software is |
| 9 |
/// furnished to do so, subject to the following conditions: |
| 10 |
/// |
| 11 |
/// The above copyright notice and this permission notice shall be included in |
| 12 |
/// all copies or substantial portions of the Software. |
| 13 |
/// |
| 14 |
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 15 |
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 16 |
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 17 |
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 18 |
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 19 |
/// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS |
| 20 |
/// IN THE SOFTWARE. |
| 21 |
|
| 22 |
|
| 23 |
#ifndef LRU_STATISTICS_HPP |
| 24 |
#define LRU_STATISTICS_HPP |
| 25 |
|
| 26 |
#include <cstddef> |
| 27 |
#include <initializer_list> |
| 28 |
#include <iterator> |
| 29 |
#include <type_traits> |
| 30 |
#include <unordered_map> |
| 31 |
#include <utility> |
| 32 |
|
| 33 |
#include <lru/error.hpp> |
| 34 |
#include <lru/internal/utility.hpp> |
| 35 |
#include <lru/key-statistics.hpp> |
| 36 |
|
| 37 |
namespace LRU { |
| 38 |
namespace Internal { |
| 39 |
template <typename> |
| 40 |
class StatisticsMutator; |
| 41 |
} |
| 42 |
|
| 43 |
/// Stores statistics about LRU cache utilization and efficiency. |
| 44 |
/// |
| 45 |
/// The statistics object stores the number of misses and hits were recorded for |
| 46 |
/// a cache in total. Furthemore, it is possibly to register a number of keys |
| 47 |
/// for *monitoring*. For each of these keys, an additional hit and miss count |
| 48 |
/// is maintained, that can keep insight into the utiliization of a particular |
| 49 |
/// cache. Note that accesses only mean lookups -- insertions or erasures will |
| 50 |
/// never signify an "access". |
| 51 |
/// |
| 52 |
/// \tparam Key The type of the keys being monitored. |
| 53 |
template <typename Key> |
| 54 |
class Statistics { |
| 55 |
public: |
| 56 |
using size_t = std::size_t; |
| 57 |
using InitializerList = std::initializer_list<Key>; |
| 58 |
|
| 59 |
/// Constructor. |
| 60 |
Statistics() noexcept : _total_accesses(0), _total_hits(0) { |
| 61 |
} |
| 62 |
|
| 63 |
/// Constructor. |
| 64 |
/// |
| 65 |
/// \param keys Any number of keys to monitor. |
| 66 |
template <typename... Keys, |
| 67 |
typename = std::enable_if_t<Internal::all_of_type<Key, Keys...>>> |
| 68 |
explicit Statistics(Keys&&... keys) : Statistics() { |
| 69 |
// clang-format off |
| 70 |
Internal::for_each([this](auto&& key) { |
| 71 |
this->monitor(std::forward<decltype(key)>(key)); |
| 72 |
}, std::forward<Keys>(keys)...); |
| 73 |
// clang-format on |
| 74 |
} |
| 75 |
|
| 76 |
/// Constructor. |
| 77 |
/// |
| 78 |
/// \param range A range of keys to monitor. |
| 79 |
template <typename Range, typename = Internal::enable_if_range<Range>> |
| 80 |
explicit Statistics(const Range& range) |
| 81 |
: Statistics(std::begin(range), std::end(range)) { |
| 82 |
} |
| 83 |
|
| 84 |
/// Constructor. |
| 85 |
/// |
| 86 |
/// \param begin The start iterator of a range of keys to monitor. |
| 87 |
/// \param end The end iterator of a range of keys to monitor. |
| 88 |
template <typename Iterator, |
| 89 |
typename = Internal::enable_if_iterator<Iterator>> |
| 90 |
Statistics(Iterator begin, Iterator end) : Statistics() { |
| 91 |
for (; begin != end; ++begin) { |
| 92 |
monitor(*begin); |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
/// Constructor. |
| 97 |
/// |
| 98 |
/// \param list A list of keys to monitor. |
| 99 |
Statistics(InitializerList list) // NOLINT(runtime/explicit) |
| 100 |
: Statistics(list.begin(), list.end()) { |
| 101 |
} |
| 102 |
|
| 103 |
/// \returns The total number of accesses (hits + misses) made to the cache. |
| 104 |
size_t total_accesses() const noexcept { |
| 105 |
return _total_accesses; |
| 106 |
} |
| 107 |
|
| 108 |
/// \returns The total number of hits made to the cache. |
| 109 |
size_t total_hits() const noexcept { |
| 110 |
return _total_hits; |
| 111 |
} |
| 112 |
|
| 113 |
/// \returns The total number of misses made to the cache. |
| 114 |
size_t total_misses() const noexcept { |
| 115 |
return total_accesses() - total_hits(); |
| 116 |
} |
| 117 |
|
| 118 |
/// \returns The ratio of hits ($\in [0, 1]$) relative to all accesses. |
| 119 |
double hit_rate() const noexcept { |
| 120 |
return static_cast<double>(total_hits()) / total_accesses(); |
| 121 |
} |
| 122 |
|
| 123 |
/// \returns The ratio of misses ($\in [0, 1]$) relative to all accesses. |
| 124 |
double miss_rate() const noexcept { |
| 125 |
return 1 - hit_rate(); |
| 126 |
} |
| 127 |
|
| 128 |
/// \returns The number of hits for the given key. |
| 129 |
/// \param key The key to retrieve the hits for. |
| 130 |
/// \throws LRU::UnmonitoredKey if the key was not registered for monitoring. |
| 131 |
size_t hits_for(const Key& key) const { |
| 132 |
return stats_for(key).hits; |
| 133 |
} |
| 134 |
|
| 135 |
/// \returns The number of misses for the given key. |
| 136 |
/// \param key The key to retrieve the misses for. |
| 137 |
/// \throws LRU::UnmonitoredKey if the key was not registered for monitoring. |
| 138 |
size_t misses_for(const Key& key) const { |
| 139 |
return stats_for(key).misses; |
| 140 |
} |
| 141 |
|
| 142 |
/// \returns The number of accesses (hits + misses) for the given key. |
| 143 |
/// \param key The key to retrieve the accesses for. |
| 144 |
/// \throws LRU::UnmonitoredKey if the key was not registered for monitoring. |
| 145 |
size_t accesses_for(const Key& key) const { |
| 146 |
return stats_for(key).accesses(); |
| 147 |
} |
| 148 |
|
| 149 |
/// \returns A `KeyStatistics` object for the given key. |
| 150 |
/// \param key The key to retrieve the stats for. |
| 151 |
/// \throws LRU::UnmonitoredKey if the key was not registered for monitoring. |
| 152 |
const KeyStatistics& stats_for(const Key& key) const { |
| 153 |
auto iterator = _key_map.find(key); |
| 154 |
if (iterator == _key_map.end()) { |
| 155 |
throw LRU::Error::UnmonitoredKey(); |
| 156 |
} |
| 157 |
|
| 158 |
return iterator->second; |
| 159 |
} |
| 160 |
|
| 161 |
/// \copydoc stats_for() |
| 162 |
const KeyStatistics& operator[](const Key& key) const { |
| 163 |
return stats_for(key); |
| 164 |
} |
| 165 |
|
| 166 |
/// Registers the key for monitoring. |
| 167 |
/// |
| 168 |
/// If the key was already registered, this is a no-op (most importantly, the |
| 169 |
/// old statistics are __not__ wiped). |
| 170 |
/// |
| 171 |
/// \param key The key to register. |
| 172 |
void monitor(const Key& key) { |
| 173 |
// emplace does nothing if the key is already present |
| 174 |
_key_map.emplace(key, KeyStatistics()); |
| 175 |
} |
| 176 |
|
| 177 |
/// Unregisters the given key from monitoring. |
| 178 |
/// |
| 179 |
/// \param key The key to unregister. |
| 180 |
/// \throws LRU::Error::UnmonitoredKey if the key was never registered for |
| 181 |
/// monitoring. |
| 182 |
void unmonitor(const Key& key) { |
| 183 |
auto iterator = _key_map.find(key); |
| 184 |
if (iterator == _key_map.end()) { |
| 185 |
throw LRU::Error::UnmonitoredKey(); |
| 186 |
} else { |
| 187 |
_key_map.erase(iterator); |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
/// Unregisters all keys from monitoring. |
| 192 |
void unmonitor_all() { |
| 193 |
_key_map.clear(); |
| 194 |
} |
| 195 |
|
| 196 |
/// Clears all statistics for the given key, but keeps on monitoring it. |
| 197 |
/// |
| 198 |
/// \param key The key to reset. |
| 199 |
void reset_key(const Key& key) { |
| 200 |
auto iterator = _key_map.find(key); |
| 201 |
if (iterator == _key_map.end()) { |
| 202 |
throw LRU::Error::UnmonitoredKey(); |
| 203 |
} else { |
| 204 |
iterator->second.reset(); |
| 205 |
} |
| 206 |
} |
| 207 |
|
| 208 |
/// Clears the statistics of all keys, but keeps on monitoring it them. |
| 209 |
void reset_all() { |
| 210 |
for (auto& pair : _key_map) { |
| 211 |
_key_map.second.reset(); |
| 212 |
} |
| 213 |
} |
| 214 |
|
| 215 |
/// \returns True if the given key is currently registered for monitoring, |
| 216 |
/// else false. |
| 217 |
/// \param key The key to check for. |
| 218 |
bool is_monitoring(const Key& key) const noexcept { |
| 219 |
return _key_map.count(key); |
| 220 |
} |
| 221 |
|
| 222 |
/// \returns The number of keys currnetly being monitored. |
| 223 |
size_t number_of_monitored_keys() const noexcept { |
| 224 |
return _key_map.size(); |
| 225 |
} |
| 226 |
|
| 227 |
/// \returns True if currently any keys at all are being monitored, else |
| 228 |
/// false. |
| 229 |
bool is_monitoring_keys() const noexcept { |
| 230 |
return !_key_map.empty(); |
| 231 |
} |
| 232 |
|
| 233 |
private: |
| 234 |
template <typename> |
| 235 |
friend class Internal::StatisticsMutator; |
| 236 |
|
| 237 |
using HitMap = std::unordered_map<Key, KeyStatistics>; |
| 238 |
|
| 239 |
/// The total number of accesses made for any key. |
| 240 |
size_t _total_accesses; |
| 241 |
|
| 242 |
/// The total number of htis made for any key. |
| 243 |
size_t _total_hits; |
| 244 |
|
| 245 |
/// The map to keep track of statistics for monitored keys. |
| 246 |
HitMap _key_map; |
| 247 |
}; |
| 248 |
|
| 249 |
namespace Lowercase { |
| 250 |
template <typename... Ts> |
| 251 |
using statistics = Statistics<Ts...>; |
| 252 |
} // namespace Lowercase |
| 253 |
|
| 254 |
} // namespace LRU |
| 255 |
|
| 256 |
#endif // LRU_STATISTICS_HPP |