Skip to content

Commit

Permalink
separate cache stat out
Browse files Browse the repository at this point in the history
  • Loading branch information
chenhao-ye committed Apr 9, 2024
1 parent 8127545 commit b95fa96
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 57 deletions.
59 changes: 4 additions & 55 deletions include/gcache/ghost_cache.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
#pragma once

#include <bit>
#include <cassert>
#include <cstdint>
#include <functional>
#include <iomanip>
#include <limits>
#include <vector>

#include "gcache/hash.h"
#include "gcache/lru_cache.h"
#include "gcache/node.h"
#include "gcache/table.h"
#include "hash.h"
#include "lru_cache.h"
#include "node.h"
#include "stat.h"

namespace gcache {

Expand All @@ -23,53 +19,6 @@ enum AccessMode : uint8_t {
NOOP, // do not update
};

struct CacheStat {
uint64_t hit_cnt;
uint64_t miss_cnt;

public:
CacheStat() : hit_cnt(0), miss_cnt(0) {}
void add_hit() { ++hit_cnt; }
void add_miss() { ++miss_cnt; }

// we may read an inconsistent version if the reader thread is not the writer,
// but most inaccuracy is tolerable, unless it produces a unreasonable value,
// e.g., hit_rate > 100%.
// we don't use atomic here because we find it is too expensive.
[[nodiscard]] double get_hit_rate() const {
uint64_t acc_cnt = hit_cnt + miss_cnt;
if (acc_cnt == 0) return std::numeric_limits<double>::infinity();
return double(hit_cnt) / double(acc_cnt);
}

[[nodiscard]] double get_miss_rate() const {
uint64_t acc_cnt = hit_cnt + miss_cnt;
if (acc_cnt == 0) return std::numeric_limits<double>::infinity();
return double(miss_cnt) / double(acc_cnt);
}

void reset() {
hit_cnt = 0;
miss_cnt = 0;
}

std::ostream& print(std::ostream& os, int width = 0) const {
uint64_t acc_cnt = hit_cnt + miss_cnt;
if (acc_cnt == 0)
return os << " NAN (" << std::setw(width) << std::fixed << hit_cnt
<< '/' << std::setw(width) << std::fixed << acc_cnt << ')';
return os << std::setw(6) << std::fixed << std::setprecision(2)
<< get_hit_rate() * 100 << "% (" << std::setw(width) << std::fixed
<< hit_cnt << '/' << std::setw(width) << std::fixed << acc_cnt
<< ')';
}

// print for debugging
friend std::ostream& operator<<(std::ostream& os, const CacheStat& s) {
return s.print(os, 0);
}
};

/**
* Simulate a set of page cache.
*/
Expand Down
4 changes: 2 additions & 2 deletions include/gcache/shared_cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
#include <unordered_map>
#include <vector>

#include "gcache/lru_cache.h"
#include "gcache/node.h"
#include "lru_cache.h"
#include "node.h"

namespace gcache {

Expand Down
55 changes: 55 additions & 0 deletions include/gcache/stat.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <cstdint>
#include <iomanip>
#include <limits>

namespace gcache {

struct CacheStat {
uint64_t hit_cnt;
uint64_t miss_cnt;

public:
CacheStat() : hit_cnt(0), miss_cnt(0) {}
void add_hit() { ++hit_cnt; }
void add_miss() { ++miss_cnt; }

// we may read an inconsistent version if the reader thread is not the writer,
// but most inaccuracy is tolerable, unless it produces a unreasonable value,
// e.g., hit_rate > 100%.
// we don't use atomic here because we find it is too expensive.
[[nodiscard]] double get_hit_rate() const {
uint64_t acc_cnt = hit_cnt + miss_cnt;
if (acc_cnt == 0) return std::numeric_limits<double>::infinity();
return double(hit_cnt) / double(acc_cnt);
}

[[nodiscard]] double get_miss_rate() const {
uint64_t acc_cnt = hit_cnt + miss_cnt;
if (acc_cnt == 0) return std::numeric_limits<double>::infinity();
return double(miss_cnt) / double(acc_cnt);
}

void reset() {
hit_cnt = 0;
miss_cnt = 0;
}

std::ostream& print(std::ostream& os, int width = 0) const {
uint64_t acc_cnt = hit_cnt + miss_cnt;
if (acc_cnt == 0)
return os << " NAN (" << std::setw(width) << std::fixed << hit_cnt
<< '/' << std::setw(width) << std::fixed << acc_cnt << ')';
return os << std::setw(6) << std::fixed << std::setprecision(2)
<< get_hit_rate() * 100 << "% (" << std::setw(width) << std::fixed
<< hit_cnt << '/' << std::setw(width) << std::fixed << acc_cnt
<< ')';
}

// print for debugging
friend std::ostream& operator<<(std::ostream& os, const CacheStat& s) {
return s.print(os, 0);
}
};
} // namespace gcache

0 comments on commit b95fa96

Please sign in to comment.