Merge zizzer.eecs.umich.edu:/z/m5/Bitkeeper/newmem
[gem5.git] / src / mem / cache / tags / split.hh
1 /*
2 * Copyright (c) 2004-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are
7 * met: redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer;
9 * redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution;
12 * neither the name of the copyright holders nor the names of its
13 * contributors may be used to endorse or promote products derived from
14 * this software without specific prior written permission.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 *
28 * Authors: Lisa Hsu
29 */
30
31 /**
32 * @file
33 * Declaration of a split/partitioned tag store.
34 */
35
36 #ifndef __SPLIT_HH__
37 #define __SPLIT_HH__
38
39 #include <cstring>
40 #include <list>
41
42 #include "mem/cache/cache_blk.hh" // base class
43 #include "mem/cache/tags/split_blk.hh"
44 #include "mem/packet.hh" // for inlined functions
45 #include <assert.h>
46 #include "mem/cache/tags/base_tags.hh"
47 #include "base/hashmap.hh"
48
49 class BaseCache;
50 class SplitLRU;
51 class SplitLIFO;
52
53 /**
54 * A cache tag store.
55 */
56 class Split : public BaseTags
57 {
58 public:
59 /** Typedef the block type used in this tag store. */
60 typedef SplitBlk BlkType;
61 /** Typedef for a list of pointers to the local block class. */
62 typedef std::list<SplitBlk*> BlkList;
63 protected:
64 /** The number of sets in the cache. */
65 const int numSets;
66 /** The number of bytes in a block. */
67 const int blkSize;
68 /** Whether the 2nd partition (for the nic) is LIFO or not */
69 const bool lifo;
70 /** The hit latency. */
71 const int hitLatency;
72
73 Addr blkMask;
74
75 /** Number of NIC requests that hit in the NIC partition */
76 Stats::Scalar<> NR_NP_hits;
77 /** Number of NIC requests that hit in the CPU partition */
78 Stats::Scalar<> NR_CP_hits;
79 /** Number of CPU requests that hit in the NIC partition */
80 Stats::Scalar<> CR_NP_hits;
81 /** Number of CPU requests that hit in the CPU partition */
82 Stats::Scalar<> CR_CP_hits;
83 /** The number of nic replacements (i.e. misses) */
84 Stats::Scalar<> nic_repl;
85 /** The number of cpu replacements (i.e. misses) */
86 Stats::Scalar<> cpu_repl;
87
88 //For latency studies
89 /** the number of NIC blks that were used before evicted */
90 Stats::Scalar<> nicUsedWhenEvicted;
91 /** the total latency of used NIC blocks in the cache */
92 Stats::Scalar<> nicUsedTotLatency;
93 /** the total number of used NIC blocks evicted */
94 Stats::Scalar<> nicUsedTotEvicted;
95 /** the average number of cycles a used NIC blk is in the cache */
96 Stats::Formula nicUsedAvgLatency;
97 /** the Distribution of used NIC blk eviction times */
98 Stats::Distribution<> usedEvictDist;
99
100 /** the number of NIC blks that were unused before evicted */
101 Stats::Scalar<> nicUnusedWhenEvicted;
102 /** the total latency of unused NIC blks in the cache */
103 Stats::Scalar<> nicUnusedTotLatency;
104 /** the total number of unused NIC blocks evicted */
105 Stats::Scalar<> nicUnusedTotEvicted;
106 /** the average number of cycles an unused NIC blk is in the cache */
107 Stats::Formula nicUnusedAvgLatency;
108 /** the Distribution of unused NIC blk eviction times */
109 Stats::Distribution<> unusedEvictDist;
110
111 /** The total latency of NIC blocks to 1st usage time by CPU */
112 Stats::Scalar<> nicUseByCPUCycleTotal;
113 /** The total number of NIC blocks used */
114 Stats::Scalar<> nicBlksUsedByCPU;
115 /** the average number of cycles before a NIC blk that is used gets used by CPU */
116 Stats::Formula nicAvgUsageByCPULatency;
117 /** the Distribution of cycles time before a NIC blk is used by CPU*/
118 Stats::Distribution<> useByCPUCycleDist;
119
120 /** the number of CPU blks that were used before evicted */
121 Stats::Scalar<> cpuUsedBlks;
122 /** the number of CPU blks that were unused before evicted */
123 Stats::Scalar<> cpuUnusedBlks;
124
125 /** the avg number of cycles before a NIC blk is evicted */
126 Stats::Formula nicAvgLatency;
127
128 typedef m5::hash_map<Addr, int, m5::hash<Addr> > hash_t;
129 typedef hash_t::const_iterator memIter;
130 hash_t memHash;
131
132
133 private:
134 SplitLRU *lru;
135 SplitLRU *lru_net;
136 SplitLIFO *lifo_net;
137
138 public:
139 /**
140 * Construct and initialize this tag store.
141 * @param _numSets The number of sets in the cache.
142 * @param _blkSize The number of bytes in a block.
143 * @param _assoc The associativity of the cache.
144 * @param _hit_latency The latency in cycles for a hit.
145 */
146 Split(int _numSets, int _blkSize, int total_ways, int LRU1_assoc,
147 bool _lifo, bool _two_queue, int _hit_latency);
148
149 /**
150 * Destructor
151 */
152 virtual ~Split();
153
154 /**
155 * Register the stats for this object
156 * @param name The name to prepend to the stats name.
157 */
158 void regStats(const std::string &name);
159
160 /**
161 * Return the block size.
162 * @return the block size.
163 */
164 int getBlockSize()
165 {
166 return blkSize;
167 }
168
169 /**
170 * Return the subblock size. In the case of Split it is always the block
171 * size.
172 * @return The block size.
173 */
174 int getSubBlockSize()
175 {
176 return blkSize;
177 }
178
179 /**
180 * Search for the address in the cache.
181 * @param asid The address space ID.
182 * @param addr The address to find.
183 * @return True if the address is in the cache.
184 */
185 bool probe(Addr addr) const;
186
187 /**
188 * Invalidate the given block.
189 * @param blk The block to invalidate.
190 */
191 void invalidateBlk(BlkType *blk);
192
193 /**
194 * Finds the given address in the cache and update replacement data.
195 * Returns the access latency as a side effect.
196 * @param addr The address to find.
197 * @param asid The address space ID.
198 * @param lat The access latency.
199 * @return Pointer to the cache block if found.
200 */
201 SplitBlk* findBlock(Addr addr, int &lat);
202
203 /**
204 * Finds the given address in the cache, do not update replacement data.
205 * @param addr The address to find.
206 * @param asid The address space ID.
207 * @return Pointer to the cache block if found.
208 */
209 SplitBlk* findBlock(Addr addr) const;
210
211 /**
212 * Find a replacement block for the address provided.
213 * @param pkt The request to a find a replacement candidate for.
214 * @param writebacks List for any writebacks to be performed.
215 * @param compress_blocks List of blocks to compress, for adaptive comp.
216 * @return The block to place the replacement in.
217 */
218 SplitBlk* findReplacement(PacketPtr &pkt, PacketList &writebacks,
219 BlkList &compress_blocks);
220
221
222 /**
223 * Generate the tag from the given address.
224 * @param addr The address to get the tag from.
225 * @param blk The block to find the partition it's in
226 * @return The tag of the address.
227 */
228 Addr extractTag(Addr addr, SplitBlk *blk) const;
229
230 /**
231 * Calculate the set index from the address.
232 * @param addr The address to get the set from.
233 * @return The set index of the address.
234 */
235 int extractSet(Addr addr) const
236 {
237 panic("should never call this!\n");
238 M5_DUMMY_RETURN
239 }
240
241 /**
242 * Get the block offset from an address.
243 * @param addr The address to get the offset of.
244 * @return The block offset.
245 */
246 int extractBlkOffset(Addr addr) const
247 {
248 return (addr & blkMask);
249 }
250
251 /**
252 * Align an address to the block size.
253 * @param addr the address to align.
254 * @return The block address.
255 */
256 Addr blkAlign(Addr addr) const
257 {
258 return (addr & ~(Addr) (blkMask));
259 }
260
261 /**
262 * Regenerate the block address from the tag.
263 * @param tag The tag of the block.
264 * @param set The set of the block.
265 * @return The block address.
266 */
267 Addr regenerateBlkAddr(Addr tag, int set) const;
268
269 /**
270 * Return the hit latency.
271 * @return the hit latency.
272 */
273 int getHitLatency() const
274 {
275 return hitLatency;
276 }
277
278 /**
279 * Read the data out of the internal storage of the given cache block.
280 * @param blk The cache block to read.
281 * @param data The buffer to read the data into.
282 * @return The cache block's data.
283 */
284 void readData(SplitBlk *blk, uint8_t *data)
285 {
286 std::memcpy(data, blk->data, blk->size);
287 }
288
289 /**
290 * Write data into the internal storage of the given cache block. Since in
291 * Split does not store data differently this just needs to update the size.
292 * @param blk The cache block to write.
293 * @param data The data to write.
294 * @param size The number of bytes to write.
295 * @param writebacks A list for any writebacks to be performed. May be
296 * needed when writing to a compressed block.
297 */
298 void writeData(SplitBlk *blk, uint8_t *data, int size,
299 PacketList & writebacks)
300 {
301 assert(size <= blkSize);
302 blk->size = size;
303 }
304
305 /**
306 * Called at end of simulation to complete average block reference stats.
307 */
308 virtual void cleanupRefs();
309 };
310
311 #endif