From b95fa961d4b10e08e0a320ae7215ab52e118aa26 Mon Sep 17 00:00:00 2001 From: Chenhao Ye Date: Tue, 9 Apr 2024 11:41:42 -0500 Subject: [PATCH] separate cache stat out --- include/gcache/ghost_cache.h | 59 +++-------------------------------- include/gcache/shared_cache.h | 4 +-- include/gcache/stat.h | 55 ++++++++++++++++++++++++++++++++ 3 files changed, 61 insertions(+), 57 deletions(-) create mode 100644 include/gcache/stat.h diff --git a/include/gcache/ghost_cache.h b/include/gcache/ghost_cache.h index e83ddc4..a4315b5 100644 --- a/include/gcache/ghost_cache.h +++ b/include/gcache/ghost_cache.h @@ -1,17 +1,13 @@ #pragma once -#include #include #include -#include -#include -#include #include -#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 { @@ -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::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::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. */ diff --git a/include/gcache/shared_cache.h b/include/gcache/shared_cache.h index 310337b..3bd36ae 100644 --- a/include/gcache/shared_cache.h +++ b/include/gcache/shared_cache.h @@ -3,8 +3,8 @@ #include #include -#include "gcache/lru_cache.h" -#include "gcache/node.h" +#include "lru_cache.h" +#include "node.h" namespace gcache { diff --git a/include/gcache/stat.h b/include/gcache/stat.h new file mode 100644 index 0000000..a409bf4 --- /dev/null +++ b/include/gcache/stat.h @@ -0,0 +1,55 @@ +#pragma once + +#include +#include +#include + +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::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::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 \ No newline at end of file