gem5 v24.0.0.0
Loading...
Searching...
No Matches
associative_cache.hh
Go to the documentation of this file.
1/*
2 * Copyright (c) 2024 Pranith Kumar
3 * Copyright (c) 2018 Metempsy Technology Consulting
4 * All rights reserved
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30#ifndef __BASE_CACHE_ASSOCIATIVE_CACHE_HH__
31#define __BASE_CACHE_ASSOCIATIVE_CACHE_HH__
32
33#include <type_traits>
34#include <vector>
35
37#include "base/intmath.hh"
38#include "base/logging.hh"
39#include "base/named.hh"
40#include "base/types.hh"
44
45namespace gem5
46{
47
48template <typename Entry>
49class AssociativeCache : public Named
50{
51 static_assert(std::is_base_of_v<CacheEntry, Entry>,
52 "Entry should be derived from CacheEntry");
53
55
56 protected:
57
60
63
66
69
70 private:
71
72 void
73 initParams(size_t _num_entries, size_t _assoc)
74 {
75 fatal_if((_num_entries % _assoc) != 0, "The number of entries of an "
76 "AssociativeCache<> must be a multiple of its associativity");
77 for (auto entry_idx = 0; entry_idx < _num_entries; entry_idx++) {
78 Entry *entry = &entries[entry_idx];
79 indexingPolicy->setEntry(entry, entry_idx);
80 entry->replacementData = replPolicy->instantiateEntry();
81 }
82 }
83
84 public:
85
89 AssociativeCache(const char *name) : Named(std::string(name)) {}
90
100 AssociativeCache(const char *name, const size_t num_entries,
101 const size_t associativity_,
102 BaseReplacementPolicy *repl_policy,
103 BaseIndexingPolicy *indexing_policy,
104 Entry const &init_val = Entry())
105 : Named(std::string(name)),
106 associativity(associativity_),
107 replPolicy(repl_policy),
108 indexingPolicy(indexing_policy),
109 entries(num_entries, init_val)
110 {
111 initParams(num_entries, associativity);
112 }
113
117 ~AssociativeCache() = default;
118
124
128 void
130 {
131 for (auto &entry : entries) {
132 invalidate(&entry);
133 }
134 }
135
136 void
137 init(const size_t num_entries,
138 const size_t associativity_,
139 BaseReplacementPolicy *_repl_policy,
140 BaseIndexingPolicy *_indexing_policy,
141 Entry const &init_val = Entry())
142 {
143 associativity = associativity_;
144 replPolicy = _repl_policy;
145 indexingPolicy = _indexing_policy;
146 entries.resize(num_entries, init_val);
147
148 initParams(num_entries, associativity);
149 }
150
156 virtual Addr
157 getTag(const Addr addr) const
158 {
160 }
161
168 virtual Entry*
170 {
171 auto entry = findEntry(addr);
172
173 if (entry) {
174 accessEntry(entry);
175 }
176
177 return entry;
178 }
179
184 virtual void
185 accessEntry(Entry *entry)
186 {
187 replPolicy->touch(entry->replacementData);
188 }
189
196 virtual Entry*
197 findEntry(const Addr addr) const
198 {
199 auto tag = getTag(addr);
200
201 auto candidates = indexingPolicy->getPossibleEntries(addr);
202
203 for (auto candidate : candidates) {
204 Entry *entry = static_cast<Entry*>(candidate);
205 if (entry->matchTag(tag)) {
206 return entry;
207 }
208 }
209
210 return nullptr;
211 }
212
218 virtual Entry*
220 {
221 auto candidates = indexingPolicy->getPossibleEntries(addr);
222
223 auto victim = static_cast<Entry*>(replPolicy->getVictim(candidates));
224
225 invalidate(victim);
226
227 return victim;
228 }
229
235 virtual void
236 invalidate(Entry *entry)
237 {
238 entry->invalidate();
239 replPolicy->invalidate(entry->replacementData);
240 }
241
247 virtual void
248 insertEntry(const Addr addr, Entry *entry)
249 {
250 entry->insert(indexingPolicy->extractTag(addr));
251 replPolicy->reset(entry->replacementData);
252 }
253
262 {
263 std::vector<ReplaceableEntry *> selected_entries =
265
267
268 std::transform(selected_entries.begin(), selected_entries.end(),
269 std::back_inserter(entries), [](auto &entry) {
270 return static_cast<Entry *>(entry);
271 });
272
273 return entries;
274 }
275
279
286 {
287 return entries.begin();
288 }
289
297 {
298 return entries.end();
299 }
300
306 begin() const
307 {
308 return entries.begin();
309 }
310
317 end() const
318 {
319 return entries.end();
320 }
321};
322
323}
324
325#endif
Defines global host-dependent types: Counter, Tick, and (indirectly) {int,uint}{8,...
void clear()
Clear the entries in the cache.
virtual void insertEntry(const Addr addr, Entry *entry)
Indicate that an entry has just been inserted.
AssociativeCache(const char *name)
Empty constructor - need to call init() later with all args.
replacement_policy::Base BaseReplacementPolicy
typename std::vector< Entry >::iterator iterator
typename std::vector< Entry >::const_iterator const_iterator
Iterator types.
size_t associativity
Associativity of the cache.
virtual Entry * accessEntryByAddr(const Addr addr)
Do an access to the entry if it exists.
iterator begin()
Returns an iterator to the first entry of the dictionary.
BaseReplacementPolicy * replPolicy
The replacement policy of the cache.
AssociativeCache(const char *name, const size_t num_entries, const size_t associativity_, BaseReplacementPolicy *repl_policy, BaseIndexingPolicy *indexing_policy, Entry const &init_val=Entry())
Public constructor.
iterator end()
Returns an iterator pointing to the end of the the dictionary (placeholder element,...
~AssociativeCache()=default
Default destructor.
void initParams(size_t _num_entries, size_t _assoc)
std::vector< Entry > entries
The entries.
const_iterator end() const
Returns an iterator pointing to the end of the the dictionary (placeholder element,...
virtual Entry * findVictim(const Addr addr)
Find a victim to be replaced.
BaseIndexingPolicy * indexingPolicy
Indexing policy of the cache.
AssociativeCache & operator=(const AssociativeCache &)=delete
AssociativeCache(const AssociativeCache &)=delete
Disable copy and assignment.
virtual void invalidate(Entry *entry)
Invalidate an entry and its respective replacement data.
virtual void accessEntry(Entry *entry)
Update the replacement information for an entry.
const_iterator begin() const
Returns an iterator to the first entry of the dictionary.
virtual Entry * findEntry(const Addr addr) const
Find an entry within the set.
std::vector< Entry * > getPossibleEntries(const Addr addr) const
Find the set of entries that could be replaced given that we want to add a new entry with the provide...
virtual Addr getTag(const Addr addr) const
Get the tag for the addr.
void init(const size_t num_entries, const size_t associativity_, BaseReplacementPolicy *_repl_policy, BaseIndexingPolicy *_indexing_policy, Entry const &init_val=Entry())
A common base class for indexing table locations.
Definition base.hh:67
void setEntry(ReplaceableEntry *entry, const uint64_t index)
Associate a pointer to an entry to its physical counterpart.
Definition base.cc:81
virtual std::vector< ReplaceableEntry * > getPossibleEntries(const Addr addr) const =0
Find all possible entries for insertion and replacement of an address.
virtual Addr extractTag(const Addr addr) const
Generate the tag from the given address.
Definition base.cc:99
Interface for things with names.
Definition named.hh:39
virtual std::string name() const
Definition named.hh:47
A common base class of cache replacement policy objects.
Definition base.hh:55
virtual void invalidate(const std::shared_ptr< ReplacementData > &replacement_data)=0
Invalidate replacement data to set it as the next probable victim.
virtual void reset(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt)
Reset replacement data.
Definition base.hh:89
virtual void touch(const std::shared_ptr< ReplacementData > &replacement_data, const PacketPtr pkt)
Update replacement data.
Definition base.hh:75
virtual ReplaceableEntry * getVictim(const ReplacementCandidates &candidates) const =0
Find replacement victim among candidates.
virtual std::shared_ptr< ReplacementData > instantiateEntry()=0
Instantiate a replacement data entry.
STL vector class.
Definition stl.hh:37
#define fatal_if(cond,...)
Conditional fatal macro that checks the supplied condition and only causes a fatal error if the condi...
Definition logging.hh:236
Declaration of a common framework for indexing policies.
Bitfield< 3 > addr
Definition types.hh:84
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
uint64_t Addr
Address type This will probably be moved somewhere else in the near future.
Definition types.hh:147
Overload hash function for BasicBlockRange type.
Definition binary32.hh:81

Generated on Tue Jun 18 2024 16:24:00 for gem5 by doxygen 1.11.0