File: persistent_string_cache_stats.h

package info (click to toggle)
persistent-cache-cpp 1.0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 648 kB
  • sloc: cpp: 7,754; python: 183; ansic: 91; sh: 34; makefile: 7
file content (249 lines) | stat: -rw-r--r-- 7,766 bytes parent folder | download | duplicates (2)
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
/*
 * 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 <core/persistent_cache_stats.h>

#include <cassert>
#include <cmath>
#include <cstring>
#include <sstream>

namespace core
{

namespace internal
{

// Simple stats class to keep track of accesses.

class PersistentStringCacheStats
{
public:
    PersistentStringCacheStats() noexcept
        : policy_(CacheDiscardPolicy::lru_only)
        , max_cache_size_(0)
        , num_entries_(0)
        , cache_size_(0)
    {
        clear();
        hist_.resize(PersistentCacheStats::NUM_BINS, 0);
    }

    PersistentStringCacheStats(PersistentStringCacheStats const&) = default;
    PersistentStringCacheStats(PersistentStringCacheStats&&) = default;
    PersistentStringCacheStats& operator=(PersistentStringCacheStats const&) = default;
    PersistentStringCacheStats& operator=(PersistentStringCacheStats&&) = default;

    std::string cache_path_;           // Immutable
    core::CacheDiscardPolicy policy_;  // Immutable
    int64_t max_cache_size_;

    int64_t num_entries_;
    int64_t cache_size_;
    PersistentCacheStats::Histogram hist_;

    // Values below are reset by a call to clear().
    int64_t hits_;
    int64_t misses_;
    int64_t hits_since_last_miss_;
    int64_t misses_since_last_hit_;
    int64_t longest_hit_run_;
    int64_t longest_miss_run_;
    int64_t num_hit_runs_;
    int64_t num_miss_runs_;
    int64_t ttl_evictions_;
    int64_t lru_evictions_;
    std::chrono::system_clock::time_point most_recent_hit_time_;
    std::chrono::system_clock::time_point most_recent_miss_time_;
    std::chrono::system_clock::time_point longest_hit_run_time_;
    std::chrono::system_clock::time_point longest_miss_run_time_;

    enum State
    {
        Initialized,
        LastAccessWasHit,
        LastAccessWasMiss
    };
    State state_;

    void inc_hits() noexcept
    {
        ++hits_since_last_miss_;
        ++hits_;
        most_recent_hit_time_ = std::chrono::system_clock::now();
        if (state_ != LastAccessWasHit)
        {
            ++num_hit_runs_;
            state_ = LastAccessWasHit;
            misses_since_last_hit_ = 0;
        }
        if (state_ != LastAccessWasMiss && hits_since_last_miss_ > longest_hit_run_)
        {
            longest_hit_run_ = hits_since_last_miss_;
            longest_hit_run_time_ = most_recent_hit_time_;
        }
    }

    void inc_misses() noexcept
    {
        ++misses_since_last_hit_;
        ++misses_;
        most_recent_miss_time_ = std::chrono::system_clock::now();
        if (state_ != LastAccessWasMiss)
        {
            ++num_miss_runs_;
            state_ = LastAccessWasMiss;
            hits_since_last_miss_ = 0;
        }
        if (state_ != LastAccessWasHit && misses_since_last_hit_ > longest_miss_run_)
        {
            longest_miss_run_ = misses_since_last_hit_;
            longest_miss_run_time_ = most_recent_miss_time_;
        }
    }

    void hist_decrement(int64_t size) noexcept
    {
        assert(size > 0);
        --hist_[size_to_index(size)];
    }

    void hist_increment(int64_t size) noexcept
    {
        assert(size > 0);
        ++hist_[size_to_index(size)];
    }

    void hist_clear() noexcept
    {
        memset(&hist_[0], 0, hist_.size() * sizeof(PersistentCacheStats::Histogram::value_type));
    }

    void clear() noexcept
    {
        state_ = Initialized;
        hits_ = 0;
        misses_ = 0;
        hits_since_last_miss_ = 0;
        misses_since_last_hit_ = 0;
        longest_hit_run_ = 0;
        longest_miss_run_ = 0;
        num_hit_runs_ = 0;
        num_miss_runs_ = 0;
        ttl_evictions_ = 0;
        lru_evictions_ = 0;
        most_recent_hit_time_ = std::chrono::system_clock::time_point();
        most_recent_miss_time_ = std::chrono::system_clock::time_point();
        longest_hit_run_time_ = std::chrono::system_clock::time_point();
        longest_miss_run_time_ = std::chrono::system_clock::time_point();
    }

    // Serialize the stats.

    std::string serialize() const
    {
        using namespace std;
        using namespace std::chrono;

        ostringstream os;
        os << state_ << " "
           << num_entries_ << " "
           << cache_size_ << " "
           << hits_ << " "
           << misses_ << " "
           << hits_since_last_miss_ << " "
           << misses_since_last_hit_ << " "
           << longest_hit_run_ << " "
           << longest_miss_run_ << " "
           << num_hit_runs_ << " "
           << num_miss_runs_ << " "
           << ttl_evictions_ << " "
           << lru_evictions_ << " "
           << duration_cast<milliseconds>(most_recent_hit_time_.time_since_epoch()).count() << " "
           << duration_cast<milliseconds>(most_recent_miss_time_.time_since_epoch()).count() << " "
           << duration_cast<milliseconds>(longest_hit_run_time_.time_since_epoch()).count() << " "
           << duration_cast<milliseconds>(longest_miss_run_time_.time_since_epoch()).count();
        for (auto d : hist_)
        {
            os << " " << d;
        }
        return os.str();
    }

    // De-serialize the stats.

    void deserialize(const std::string& s) noexcept
    {
        using namespace std;
        using namespace std::chrono;

        istringstream is(s);
        int64_t state;
        int64_t mrht;
        int64_t mrmt;
        int64_t lhrt;
        int64_t lmrt;
        is >> state
           >> num_entries_
           >> cache_size_
           >> hits_
           >> misses_
           >> hits_since_last_miss_
           >> misses_since_last_hit_
           >> longest_hit_run_
           >> longest_miss_run_
           >> num_hit_runs_
           >> num_miss_runs_
           >> ttl_evictions_
           >> lru_evictions_
           >> mrht
           >> mrmt
           >> lhrt
           >> lmrt;
        for (unsigned i = 0; i < PersistentCacheStats::NUM_BINS; ++i)
        {
            is >> hist_[i];
        }
        assert(!is.bad());
        state_ = static_cast<State>(state);
        most_recent_hit_time_ = system_clock::time_point(milliseconds(mrht));
        most_recent_miss_time_ = system_clock::time_point(milliseconds(mrmt));
        longest_hit_run_time_ = system_clock::time_point(milliseconds(lhrt));
        longest_miss_run_time_ = system_clock::time_point(milliseconds(lmrt));
    }

private:
    unsigned size_to_index(int64_t size) const noexcept
    {
        using namespace std;
        assert(size > 0);
        unsigned log = floor(log10(size));     // 0..9 = 0, 10..99 = 1, 100..199 = 2, etc.
        unsigned exp = pow(10, log);           // 0..9 = 1, 10..99 = 10, 100..199 = 100, etc.
        unsigned div = size / exp;             // Extracts first decimal digit of size.
        int index = log * 10 + div - log - 1;  // Partition each power of 10 into 9 bins.
        index -= 8;                            // Sizes < 10 all go into bin 0;
        return index < 0 ? 0 : (index > int(hist_.size()) - 1 ? hist_.size() - 1 : index);
    }
};

}  // namespace internal

}  // namespace core