Skip to content

Commit

Permalink
return cache stat instead miss rate for ghost kv cache API
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhao-ye committed Oct 14, 2024
1 parent abcae8f commit 58403d9
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 12 deletions.
15 changes: 7 additions & 8 deletions include/gcache/ghost_kv_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -64,22 +64,21 @@ class SampledGhostKvCache {

void reset_stat() { ghost_cache.reset_stat(); }

[[nodiscard]] const std::vector<
std::tuple</*count*/ uint32_t, /*size*/ uint32_t, /*miss_rate*/ double>>
get_miss_rate_curve() const {
std::vector<std::tuple<uint32_t, uint32_t, double>> mrc;
[[nodiscard]] const std::vector<std::tuple<
/*count*/ uint32_t, /*size*/ uint32_t, /*miss_rate*/ CacheStat>>
get_cache_stat_curve() const {
std::vector<std::tuple<uint32_t, uint32_t, CacheStat>> curve;
uint32_t curr_count = 0;
uint32_t curr_size = 0;
ghost_cache.unsafe_for_each_mru([&](Handle_t h) {
curr_size += h->kv_size;
++curr_count;
if ((curr_count - ghost_cache.min_size) % ghost_cache.tick == 0) {
mrc.emplace_back(
curr_count << SampleShift, curr_size << SampleShift,
ghost_cache.get_stat_shifted(curr_count).get_miss_rate());
curve.emplace_back(curr_count << SampleShift, curr_size << SampleShift,
ghost_cache.get_stat_shifted(curr_count));
}
});
return mrc;
return curve;
// should be implicitly moved by compiler
// avoid explict move for Return Value Optimization (RVO)
}
Expand Down
8 changes: 4 additions & 4 deletions tests/test_ghost_kv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,20 +61,20 @@ void bench1() {
<< "-------------------------------------------------------------"
<< "-----------------------\n";

auto mrc = sampled_ghost_kv_cache.get_miss_rate_curve();
auto curve = sampled_ghost_kv_cache.get_cache_stat_curve();
for (uint32_t s = tick; s <= bench_size; s += tick) {
std::cout << std::setw(5) << s / 1024 << "K|";
ghost_cache.get_stat(s).print(std::cout, 8);
std::cout << '|';
sampled_ghost_kv_cache.get_stat(s).print(std::cout, 8);
std::cout << '|';
auto [count, size, miss_rate] = mrc[s / tick - 1];
auto [count, size, cache_stat] = curve[s / tick - 1];
assert(count == s);
if (miss_rate == std::numeric_limits<double>::infinity()) {
if (cache_stat.hit_cnt == 0) {
std::cout << " NAN";
} else {
std::cout << std::setw(5) << std::fixed << std::setprecision(1)
<< (1 - miss_rate) * 100 << '%';
<< cache_stat.get_hit_rate() * 100 << '%';
}
std::cout << " @" << std::setw(7) << std::fixed << size / 1024 / 1024 << 'M'
<< std::setw(5) << std::fixed << size / count << std::endl;
Expand Down

0 comments on commit 58403d9

Please sign in to comment.