1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
|
/*
* Copyright (C) 2015 Canonical Ltd.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License version 3 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Authored by: Michi Henning <michi.henning@canonical.com>
*/
#pragma once
#include <core/cache_discard_policy.h>
#include <chrono>
#include <memory>
#include <vector>
namespace core
{
namespace internal
{
class PersistentStringCacheImpl;
class PersistentStringCacheStats;
} // namespace internal
/**
\brief Class that provides (read-only) access to cache statistics and settings.
*/
class PersistentCacheStats
{
public:
/** @name Construction, Copy and Assignment
Copy and assignment have the usual value semantics.
The default constructor creates an instance with
an empty cache path, `lru_only` policy, and the
remaining values set to zero.
*/
//{@
PersistentCacheStats();
PersistentCacheStats(PersistentCacheStats const&);
PersistentCacheStats(PersistentCacheStats&&) noexcept;
PersistentCacheStats& operator=(PersistentCacheStats const&);
PersistentCacheStats& operator=(PersistentCacheStats&&);
//@}
// Accessors instead of data members for ABI stability.
/** @name Accessors
*/
//{@
/**
\brief Returns the path to the cache directory.
*/
std::string cache_path() const;
/**
\brief Returns the discard policy (`lru_only` or `lru_ttl`).
*/
CacheDiscardPolicy policy() const noexcept;
/**
\brief Returns the number of entries (including expired ones).
*/
int64_t size() const noexcept;
/**
\brief Returns the size of all entries (including expired ones).
*/
int64_t size_in_bytes() const noexcept;
/**
\brief Returns the maximum size of the cache.
*/
int64_t max_size_in_bytes() const noexcept;
/**
\brief Returns the number of hits since the statistics were last reset.
*/
int64_t hits() const noexcept;
/**
\brief Returns the number of misses since the statistics were last reset.
*/
int64_t misses() const noexcept;
/**
\brief Returns the number of consecutive hits since the last miss.
*/
int64_t hits_since_last_miss() const noexcept;
/**
\brief Returns the number of consecutive misses since the last hit.
*/
int64_t misses_since_last_hit() const noexcept;
/**
\brief Returns the largest number of consecutive hits.
*/
int64_t longest_hit_run() const noexcept;
/**
\brief Returns the largest number of consecutive misses.
*/
int64_t longest_miss_run() const noexcept;
/**
\brief Returns the number of hit runs.
*/
int64_t hit_runs() const noexcept;
/**
\brief Returns the number of miss runs.
*/
int64_t miss_runs() const noexcept;
/**
\brief Returns a rolling average of the hit run length.
*/
double avg_hit_run_length() const noexcept;
/**
\brief Returns a rolling average of the miss run length.
*/
double avg_miss_run_length() const noexcept;
/**
\brief Returns the number of entries that were evicted due to being expired.
*/
int64_t ttl_evictions() const noexcept;
/**
\brief Returns the number of entries that were evicted due to being
least recently used.
*/
int64_t lru_evictions() const noexcept;
/**
\brief Returns the timestamp of the most recent hit.
*/
std::chrono::system_clock::time_point most_recent_hit_time() const noexcept;
/**
\brief Returns the timestamp of the most recent miss.
*/
std::chrono::system_clock::time_point most_recent_miss_time() const noexcept;
/**
\brief Returns the time of the longest hit run.
*/
std::chrono::system_clock::time_point longest_hit_run_time() const noexcept;
/**
\brief Returns the time of the longest miss run.
*/
std::chrono::system_clock::time_point longest_miss_run_time() const noexcept;
/**
\brief Histogram of the size distribution of cache entries.
The histogram uses a logarithmic scale and contains the number of entries in
the cache, with entries grouped by size into a number of bins as follows:
Index | Entry size in bytes
----- | -------------------
0 | 1..9
1 | 10..19
2 | 20..29
... | ...
9 | 90..99
10 | 100..199
11 | 200..200
... | ...
18 | 900..999
19 | 1,000..1,999
20 | 2,000..2,999
... | ...
27 | 9,000..9,999
28 | 10,000..19,999
29 | 20,000..29,999
... | ...
72 | 900,000,000..999,999,999
73 | 1,000,000,000..
Index 0 contains the number of entries < 10 bytes. Thereafter, the histogram
contains 9 bins for each power of 10, plus a final bin at index 73 that contains
the number of entries ≥ 10<sup>9</sup> bytes.
*/
typedef std::vector<uint32_t> Histogram;
/**
\brief Lower and upper bounds for the bins in the histogram.
Each pair contains the lower and upper (inclusive) bound of
the corresponding bin of the values returned by histogram().
*/
typedef std::vector<std::pair<int32_t, int32_t>> HistogramBounds;
/**
\brief The number of bins in a histogram.
*/
static constexpr unsigned NUM_BINS = 74;
/**
\brief Returns a histogram for the entries in the cache.
*/
Histogram const& histogram() const noexcept;
/**
\brief Returns the bounds for each bin a histogram.
This method returns the same vector each time; it is provided
as a convenience method to make it easier to add labels to a
histogram for display. The returned pairs use inclusive ranges,
that is, `pair.second` is the largest possible size of the bin.
*/
static HistogramBounds const& histogram_bounds() noexcept;
//@}
private:
PersistentCacheStats(std::shared_ptr<core::internal::PersistentStringCacheStats> const& p) noexcept;
// We store a shared_ptr for efficiency. When the caller
// retrieves the stats, we set p_ to point at the PersistentStringCacheStats
// inside the cache. If the caller makes a copy or assigns,
// we create a new instance, to provide value semantics. This means
// that we don't have to copy all of the stats each time the caller
// gets them.
std::shared_ptr<internal::PersistentStringCacheStats const> p_;
bool internal_; // True if p_ points at the internal instance.
// @cond
friend class internal::PersistentStringCacheImpl; // For access to constructor
// @endcond
};
} // namespace core
|