-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheToolbox.cpp
More file actions
120 lines (95 loc) · 2.73 KB
/
CacheToolbox.cpp
File metadata and controls
120 lines (95 loc) · 2.73 KB
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
#include <stdexcept>
#include "CacheToolbox.h"
CacheConfig::CacheConfig(unsigned size, unsigned associativity, unsigned cache_line_size) : size(size), assoc(associativity), cache_line_size(cache_line_size)
{
/* For simplicity, we currently enforce that the cache size, associativity and cache line size
* must be power of 2 */
if (!is_power_of_2(size))
throw std::runtime_error("Cache size must be a power of 2");
if (!is_power_of_2(associativity))
throw std::runtime_error("Associativity must be a power of 2");
if (!is_power_of_2(cache_line_size))
throw std::runtime_error("Cache line size must be a power of 2");
nb_sets = size / cache_line_size / associativity;
const unsigned log2_line_size = std::log2(cache_line_size);
set_index_size = std::log2(nb_sets);
set_index_shift = log2_line_size;
tag_size = sizeof(addr_t) * 8 - log2_line_size - set_index_size;
tag_shift = log2_line_size + set_index_size;
}
unsigned CacheConfig::get_size(void) const
{
return size;
}
unsigned CacheConfig::get_associativity(void) const
{
return assoc;
}
unsigned CacheConfig::get_cache_line_size(void) const
{
return cache_line_size;
}
unsigned CacheConfig::get_nb_sets(void) const
{
return nb_sets;
}
unsigned CacheConfig::get_set_index_size(void) const
{
return set_index_size;
}
unsigned CacheConfig::get_set_index_shift(void) const
{
return set_index_shift;
}
unsigned CacheConfig::get_tag_size(void) const
{
return tag_size;
}
unsigned CacheConfig::get_tag_shift(void) const
{
return tag_shift;
}
addr_t get_set_index(addr_t addr, unsigned index_shift, size_t index_size)
{
return extract_bits(addr, index_shift, index_size);
}
addr_t get_set_index(addr_t addr, const CacheConfig& config)
{
return get_set_index(addr, config.get_set_index_shift(), config.get_set_index_size());
}
addr_t get_tag(addr_t addr, unsigned tag_shift, size_t tag_size)
{
return extract_bits(addr, tag_shift, tag_size);
}
addr_t get_tag(addr_t addr, const CacheConfig& config)
{
return get_tag(addr, config.get_tag_shift(), config.get_tag_size());
}
CacheStats::CacheStats(void)
{
accesses = 0;
misses = 0;
}
void CacheStats::log_access(void)
{
++accesses;
}
void CacheStats::log_miss(void)
{
++misses;
}
uint64_t CacheStats::get_accesses(void) const
{
return accesses;
}
uint64_t CacheStats::get_misses(void) const
{
return misses;
}
void print_cache_stats(const CacheStats& stats, std::ostream& os)
{
const uint64_t accesses = stats.get_accesses();
const uint64_t misses = stats.get_misses();
os << "Accesses: " << accesses << std::endl;
os << "Misses: " << misses << " (Miss ratio: " << misses / (float) accesses * 100 << "%)" << std::endl;
}