~ci-train-bot/thumbnailer/thumbnailer-ubuntu-yakkety-landing-072

« back to all changes in this revision

Viewing changes to include/core/persistent_cache_stats.h

  • Committer: CI Train Bot
  • Author(s): Michi Henning
  • Date: 2015-09-15 11:04:11 UTC
  • mfrom: (125.1.2 landing150915)
  • Revision ID: ci-train-bot@canonical.com-20150915110411-233xw0fljaq7p2o0
Landing changes on devel to trunk.
Approved by: James Henstridge

Show diffs side-by-side

added added

removed removed

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