BuildCache
cache_stats.hpp
1 //--------------------------------------------------------------------------------------------------
2 // Copyright (c) 2019 Alexey Sheplyakov
3 //
4 // This software is provided 'as-is', without any express or implied warranty. In no event will the
5 // authors be held liable for any damages arising from the use of this software.
6 //
7 // Permission is granted to anyone to use this software for any purpose, including commercial
8 // applications, and to alter it and redistribute it freely, subject to the following restrictions:
9 //
10 // 1. The origin of this software must not be misrepresented; you must not claim that you wrote
11 // the original software. If you use this software in a product, an acknowledgment in the
12 // product documentation would be appreciated but is not required.
13 //
14 // 2. Altered source versions must be plainly marked as such, and must not be misrepresented as
15 // being the original software.
16 //
17 // 3. This notice may not be removed or altered from any source distribution.
18 //--------------------------------------------------------------------------------------------------
19 
20 #ifndef BUILDCACHE_CACHE_STATS_HPP_
21 #define BUILDCACHE_CACHE_STATS_HPP_
22 #include <iosfwd>
23 #include <string>
24 
25 struct cJSON;
26 
27 namespace bcache {
28 
30  int m_direct_miss_count{0};
31  int m_direct_hit_count{0};
32  int m_local_miss_count{0};
33  int m_local_hit_count{0};
34  int m_remote_hit_count{0};
35  int m_remote_miss_count{0};
36 
37 public:
38  bool from_file(const std::string& path) noexcept;
39  bool from_json(cJSON const* obj) noexcept;
40  bool to_json(cJSON* obj) const noexcept;
41  bool to_file(const std::string& path) const noexcept;
42 
43  cache_stats_t& operator+=(const cache_stats_t& other) noexcept {
44  m_direct_hit_count += other.m_direct_hit_count;
45  m_direct_miss_count += other.m_direct_miss_count;
46  m_local_hit_count += other.m_local_hit_count;
47  m_local_miss_count += other.m_local_miss_count;
48  m_remote_hit_count += other.m_remote_hit_count;
49  m_remote_miss_count += other.m_remote_miss_count;
50  return *this;
51  }
52 
53  double direct_hit_ratio() const noexcept {
54  int total = m_direct_hit_count + m_direct_miss_count;
55  if (total != 0) {
56  return (100.0 * m_direct_hit_count) / total;
57  } else {
58  return 0.0;
59  }
60  }
61 
62  double local_hit_ratio() const noexcept {
63  int total = m_local_hit_count + m_local_miss_count;
64  if (total != 0) {
65  return (100.0 * m_local_hit_count) / total;
66  } else {
67  return 0.0;
68  }
69  }
70 
71  double remote_hit_ratio() const noexcept {
72  int total = m_remote_hit_count + m_remote_miss_count;
73  if (total != 0) {
74  return (100.0 * m_remote_hit_count) / total;
75  } else {
76  return 0.0;
77  }
78  }
79 
81  int global_hit_count() const noexcept {
82  // buildcache does not use the remote cache on a local hit
83  return m_local_hit_count + m_remote_hit_count;
84  }
85 
87  int global_miss_count() const noexcept {
88  // Every local miss which has been satisfied from the remote cache is a global hit
89  return m_local_miss_count - m_remote_hit_count;
90  }
91 
92  double global_hit_ratio() const noexcept {
93  int total = global_hit_count() + global_miss_count();
94  if (total != 0) {
95  return (100.0 * global_hit_count()) / total;
96  } else {
97  return 0.0;
98  }
99  }
100 
101  static cache_stats_t direct_hit() noexcept {
102  cache_stats_t st;
103  st.m_direct_hit_count = 1;
104  return st;
105  }
106  static cache_stats_t direct_miss() noexcept {
107  cache_stats_t st;
108  st.m_direct_miss_count = 1;
109  return st;
110  }
111  static cache_stats_t local_hit() noexcept {
112  cache_stats_t st;
113  st.m_local_hit_count = 1;
114  return st;
115  }
116  static cache_stats_t local_miss() noexcept {
117  cache_stats_t st;
118  st.m_local_miss_count = 1;
119  return st;
120  }
121  static cache_stats_t remote_miss() noexcept {
122  cache_stats_t st;
123  st.m_remote_miss_count = 1;
124  return st;
125  }
126  static cache_stats_t remote_hit() noexcept {
127  cache_stats_t st;
128  st.m_remote_hit_count = 1;
129  st.m_remote_miss_count = 0;
130  return st;
131  }
132 
133  void dump(std::ostream& os, const std::string& prefix) const;
134 };
135 
136 } // namespace bcache
137 
138 #endif /* BUILDCACHE_CACHE_STATS_HPP_ */
bcache
The top level namespace.
Definition: compressor.cpp:34
bcache::cache_stats_t
Definition: cache_stats.hpp:29
bcache::cache_stats_t::global_miss_count
int global_miss_count() const noexcept
Missed both local and remote cache.
Definition: cache_stats.hpp:87
bcache::cache_stats_t::global_hit_count
int global_hit_count() const noexcept
Hit either the local or the remote cache.
Definition: cache_stats.hpp:81