probe function no longer used anywhere.
[gem5.git] / src / mem / cache / tags / iic.cc
1 /*
2 * Copyright (c) 2002-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: Erik Hallnor
29 */
30
31 /**
32 * @file
33 * Definitions of the Indirect Index Cache tagstore.
34 */
35
36 #include <algorithm>
37 #include <string>
38 #include <vector>
39
40 #include <math.h>
41
42 #include "mem/cache/base.hh"
43 #include "mem/cache/tags/iic.hh"
44 #include "base/intmath.hh"
45 #include "sim/core.hh" // for curTick
46
47 #include "base/trace.hh" // for DPRINTF
48
49
50 using namespace std;
51
52 /** Track the number of accesses to each cache set. */
53 #define PROFILE_IIC 1
54
55 IIC::IIC(IIC::Params &params) :
56 hashSets(params.numSets), blkSize(params.blkSize), assoc(params.assoc),
57 hitLatency(params.hitLatency), subSize(params.subblockSize),
58 numSub(blkSize/subSize),
59 trivialSize((floorLog2(params.size/subSize)*numSub)/8),
60 tagShift(floorLog2(blkSize)), blkMask(blkSize - 1),
61 subShift(floorLog2(subSize)), subMask(numSub - 1),
62 hashDelay(params.hashDelay),
63 numBlocks(params.size/subSize),
64 numTags(hashSets * assoc + params.size/blkSize -1),
65 numSecondary(params.size/blkSize),
66 tagNull(numTags),
67 primaryBound(hashSets * assoc)
68 {
69 int i;
70
71 // Check parameters
72 if (blkSize < 4 || !isPowerOf2(blkSize)) {
73 fatal("Block size must be at least 4 and a power of 2");
74 }
75 if (hashSets <= 0 || !isPowerOf2(hashSets)) {
76 fatal("# of hashsets must be non-zero and a power of 2");
77 }
78 if (assoc <= 0) {
79 fatal("associativity must be greater than zero");
80 }
81 if (hitLatency <= 0) {
82 fatal("access latency must be greater than zero");
83 }
84 if (numSub*subSize != blkSize) {
85 fatal("blocksize must be evenly divisible by subblock size");
86 }
87
88 // debug stuff
89 freeSecond = numSecondary;
90
91 warmedUp = false;
92 warmupBound = params.size/blkSize;
93
94 // Replacement Policy Initialization
95 repl = params.rp;
96 repl->setIIC(this);
97
98 //last_miss_time = 0
99
100 // allocate data reference counters
101 dataReferenceCount = new int[numBlocks];
102 memset(dataReferenceCount, 0, numBlocks*sizeof(int));
103
104 // Allocate storage for both internal data and block fast access data.
105 // We allocate it as one large chunk to reduce overhead and to make
106 // deletion easier.
107 int data_index = 0;
108 dataStore = new uint8_t[(numBlocks + numTags) * blkSize];
109 dataBlks = new uint8_t*[numBlocks];
110 for (i = 0; i < numBlocks; ++i) {
111 dataBlks[i] = &dataStore[data_index];
112 freeDataBlock(i);
113 data_index += subSize;
114 }
115
116 assert(data_index == numBlocks * subSize);
117
118 // allocate and init tag store
119 tagStore = new IICTag[numTags];
120
121 int blkIndex = 0;
122 // allocate and init sets
123 sets = new IICSet[hashSets];
124 for (i = 0; i < hashSets; ++i) {
125 sets[i].assoc = assoc;
126 sets[i].tags = new IICTag*[assoc];
127 sets[i].chain_ptr = tagNull;
128
129 for (int j = 0; j < assoc; ++j) {
130 IICTag *tag = &tagStore[blkIndex++];
131 tag->chain_ptr = tagNull;
132 tag->data_ptr.resize(numSub);
133 tag->size = blkSize;
134 tag->trivialData = new uint8_t[trivialSize];
135 tag->numData = 0;
136 sets[i].tags[j] = tag;
137 tag->set = i;
138 tag->data = &dataStore[data_index];
139 data_index += blkSize;
140 }
141 }
142
143 assert(blkIndex == primaryBound);
144
145 for (i = primaryBound; i < tagNull; i++) {
146 tagStore[i].chain_ptr = i+1;
147 //setup data ptrs to subblocks
148 tagStore[i].data_ptr.resize(numSub);
149 tagStore[i].size = blkSize;
150 tagStore[i].trivialData = new uint8_t[trivialSize];
151 tagStore[i].numData = 0;
152 tagStore[i].set = 0;
153 tagStore[i].data = &dataStore[data_index];
154 data_index += blkSize;
155 }
156 freelist = primaryBound;
157 }
158
159 IIC::~IIC()
160 {
161 delete [] dataReferenceCount;
162 delete [] dataStore;
163 delete [] tagStore;
164 delete [] sets;
165 }
166
167 /* register cache stats */
168 void
169 IIC::regStats(const string &name)
170 {
171 using namespace Stats;
172
173 BaseTags::regStats(name);
174
175 hitHashDepth.init(0, 20, 1);
176 missHashDepth.init(0, 20, 1);
177 setAccess.init(0, hashSets, 1);
178
179 /** IIC Statistics */
180 hitHashDepth
181 .name(name + ".hit_hash_depth_dist")
182 .desc("Dist. of Hash lookup depths")
183 .flags(pdf)
184 ;
185
186 missHashDepth
187 .name(name + ".miss_hash_depth_dist")
188 .desc("Dist. of Hash lookup depths")
189 .flags(pdf)
190 ;
191
192 repl->regStats(name);
193
194 if (PROFILE_IIC)
195 setAccess
196 .name(name + ".set_access_dist")
197 .desc("Dist. of Accesses across sets")
198 .flags(pdf)
199 ;
200
201 missDepthTotal
202 .name(name + ".miss_depth_total")
203 .desc("Total of miss depths")
204 ;
205
206 hashMiss
207 .name(name + ".hash_miss")
208 .desc("Total of misses in hash table")
209 ;
210
211 hitDepthTotal
212 .name(name + ".hit_depth_total")
213 .desc("Total of hit depths")
214 ;
215
216 hashHit
217 .name(name + ".hash_hit")
218 .desc("Total of hites in hash table")
219 ;
220 }
221
222
223 IICTag*
224 IIC::findBlock(Addr addr, int &lat)
225 {
226 Addr tag = extractTag(addr);
227 unsigned set = hash(addr);
228 int set_lat;
229
230 unsigned long chain_ptr = tagNull;
231
232 if (PROFILE_IIC)
233 setAccess.sample(set);
234
235 IICTag *tag_ptr = sets[set].findTag(tag, chain_ptr);
236 set_lat = 1;
237 if (tag_ptr == NULL && chain_ptr != tagNull) {
238 int secondary_depth;
239 tag_ptr = secondaryChain(tag, chain_ptr, &secondary_depth);
240 set_lat += secondary_depth;
241 // set depth for statistics fix this later!!! egh
242 sets[set].depth = set_lat;
243
244 if (tag_ptr != NULL) {
245 /* need to move tag into primary table */
246 // need to preserve chain: fix this egh
247 sets[set].tags[assoc-1]->chain_ptr = tag_ptr->chain_ptr;
248 tagSwap(tag_ptr - tagStore, sets[set].tags[assoc-1] - tagStore);
249 tag_ptr = sets[set].findTag(tag, chain_ptr);
250 assert(tag_ptr!=NULL);
251 }
252
253 }
254 set_lat = set_lat * hashDelay + hitLatency;
255 if (tag_ptr != NULL) {
256 // IIC replacement: if this is not the first element of
257 // list, reorder
258 sets[set].moveToHead(tag_ptr);
259
260 hitHashDepth.sample(sets[set].depth);
261 hashHit++;
262 hitDepthTotal += sets[set].depth;
263 tag_ptr->status |= BlkReferenced;
264 lat = set_lat;
265 if (tag_ptr->whenReady > curTick && tag_ptr->whenReady - curTick > set_lat) {
266 lat = tag_ptr->whenReady - curTick;
267 }
268
269 tag_ptr->refCount += 1;
270 }
271 else {
272 // fall through: cache block not found, not a hit...
273 missHashDepth.sample(sets[set].depth);
274 hashMiss++;
275 missDepthTotal += sets[set].depth;
276 lat = set_lat;
277 }
278 return tag_ptr;
279 }
280
281
282 IICTag*
283 IIC::findBlock(Addr addr) const
284 {
285 Addr tag = extractTag(addr);
286 unsigned set = hash(addr);
287
288 unsigned long chain_ptr = tagNull;
289
290 IICTag *tag_ptr = sets[set].findTag(tag, chain_ptr);
291 if (tag_ptr == NULL && chain_ptr != tagNull) {
292 int secondary_depth;
293 tag_ptr = secondaryChain(tag, chain_ptr, &secondary_depth);
294 }
295 return tag_ptr;
296 }
297
298
299 IICTag*
300 IIC::findReplacement(Addr addr, PacketList &writebacks)
301 {
302 DPRINTF(IIC, "Finding Replacement for %x\n", addr);
303 unsigned set = hash(addr);
304 IICTag *tag_ptr;
305 unsigned long *tmp_data = new unsigned long[numSub];
306
307 // Get a enough subblocks for a full cache line
308 for (int i = 0; i < numSub; ++i){
309 tmp_data[i] = getFreeDataBlock(writebacks);
310 assert(dataReferenceCount[tmp_data[i]]==0);
311 }
312
313 tag_ptr = getFreeTag(set, writebacks);
314
315 tag_ptr->set = set;
316 for (int i=0; i< numSub; ++i) {
317 tag_ptr->data_ptr[i] = tmp_data[i];
318 dataReferenceCount[tag_ptr->data_ptr[i]]++;
319 }
320 tag_ptr->numData = numSub;
321 assert(tag_ptr - tagStore < primaryBound); // make sure it is in primary
322 tag_ptr->chain_ptr = tagNull;
323 sets[set].moveToHead(tag_ptr);
324 delete [] tmp_data;
325
326 list<unsigned long> tag_indexes;
327 repl->doAdvance(tag_indexes);
328 /*
329 while (!tag_indexes.empty()) {
330 if (!tagStore[tag_indexes.front()].isCompressed()) {
331 compress_blocks.push_back(&tagStore[tag_indexes.front()]);
332 }
333 tag_indexes.pop_front();
334 }
335 */
336
337 tag_ptr->re = (void*)repl->add(tag_ptr-tagStore);
338
339 return tag_ptr;
340 }
341
342 void
343 IIC::freeReplacementBlock(PacketList & writebacks)
344 {
345 IICTag *tag_ptr;
346 unsigned long data_ptr;
347 /* consult replacement policy */
348 tag_ptr = &tagStore[repl->getRepl()];
349 assert(tag_ptr->isValid());
350
351 DPRINTF(Cache, "Replacing %x in IIC: %s\n",
352 regenerateBlkAddr(tag_ptr->tag,0),
353 tag_ptr->isDirty() ? "writeback" : "clean");
354 /* write back replaced block data */
355 if (tag_ptr && (tag_ptr->isValid())) {
356 replacements[0]++;
357 totalRefs += tag_ptr->refCount;
358 ++sampledRefs;
359 tag_ptr->refCount = 0;
360
361 if (tag_ptr->isDirty()) {
362 /* PacketPtr writeback =
363 buildWritebackReq(regenerateBlkAddr(tag_ptr->tag, 0),
364 tag_ptr->req->asid, tag_ptr->xc, blkSize,
365 tag_ptr->data,
366 tag_ptr->size);
367 */
368 Request *writebackReq = new Request(regenerateBlkAddr(tag_ptr->tag, 0),
369 blkSize, 0);
370 PacketPtr writeback = new Packet(writebackReq, MemCmd::Writeback,
371 -1);
372 writeback->allocate();
373 memcpy(writeback->getPtr<uint8_t>(), tag_ptr->data, blkSize);
374
375 writebacks.push_back(writeback);
376 }
377 }
378
379 // free the data blocks
380 for (int i = 0; i < tag_ptr->numData; ++i) {
381 data_ptr = tag_ptr->data_ptr[i];
382 assert(dataReferenceCount[data_ptr]>0);
383 if (--dataReferenceCount[data_ptr] == 0) {
384 freeDataBlock(data_ptr);
385 }
386 }
387 freeTag(tag_ptr);
388 }
389
390 unsigned long
391 IIC::getFreeDataBlock(PacketList & writebacks)
392 {
393 struct IICTag *tag_ptr;
394 unsigned long data_ptr;
395
396 tag_ptr = NULL;
397 /* find data block */
398 while (blkFreelist.empty()) {
399 freeReplacementBlock(writebacks);
400 }
401
402 data_ptr = blkFreelist.front();
403 blkFreelist.pop_front();
404 DPRINTF(IICMore,"Found free data at %d\n",data_ptr);
405 return data_ptr;
406 }
407
408
409
410 IICTag*
411 IIC::getFreeTag(int set, PacketList & writebacks)
412 {
413 unsigned long tag_index;
414 IICTag *tag_ptr;
415 // Add new tag
416 tag_ptr = sets[set].findFree();
417 // if no free in primary, and secondary exists
418 if (!tag_ptr && numSecondary) {
419 // need to spill a tag into secondary storage
420 while (freelist == tagNull) {
421 // get replacements until one is in secondary
422 freeReplacementBlock(writebacks);
423 }
424
425 tag_index = freelist;
426 freelist = tagStore[freelist].chain_ptr;
427 freeSecond--;
428
429 assert(tag_index != tagNull);
430 tagSwap(tag_index, sets[set].tags[assoc-1] - tagStore);
431 tagStore[tag_index].chain_ptr = sets[set].chain_ptr;
432 sets[set].chain_ptr = tag_index;
433
434 tag_ptr = sets[set].tags[assoc-1];
435 }
436 DPRINTF(IICMore,"Found free tag at %d\n",tag_ptr - tagStore);
437 tagsInUse++;
438 if (!warmedUp && tagsInUse.value() >= warmupBound) {
439 warmedUp = true;
440 warmupCycle = curTick;
441 }
442
443 return tag_ptr;
444 }
445
446 void
447 IIC::freeTag(IICTag *tag_ptr)
448 {
449 unsigned long tag_index, tmp_index;
450 // Fix tag_ptr
451 if (tag_ptr) {
452 // we have a tag to clear
453 DPRINTF(IICMore,"Freeing Tag for %x\n",
454 regenerateBlkAddr(tag_ptr->tag,0));
455 tagsInUse--;
456 tag_ptr->status = 0;
457 tag_ptr->numData = 0;
458 tag_ptr->re = NULL;
459 tag_index = tag_ptr - tagStore;
460 if (tag_index >= primaryBound) {
461 // tag_ptr points to secondary store
462 assert(tag_index < tagNull); // remove this?? egh
463 if (tag_ptr->chain_ptr == tagNull) {
464 // need to fix chain list
465 unsigned tmp_set = hash(tag_ptr->tag << tagShift);
466 if (sets[tmp_set].chain_ptr == tag_index) {
467 sets[tmp_set].chain_ptr = tagNull;
468 } else {
469 tmp_index = sets[tmp_set].chain_ptr;
470 while (tmp_index != tagNull
471 && tagStore[tmp_index].chain_ptr != tag_index) {
472 tmp_index = tagStore[tmp_index].chain_ptr;
473 }
474 assert(tmp_index != tagNull);
475 tagStore[tmp_index].chain_ptr = tagNull;
476 }
477 tag_ptr->chain_ptr = freelist;
478 freelist = tag_index;
479 freeSecond++;
480 } else {
481 // copy next chained entry to this tag location
482 tmp_index = tag_ptr->chain_ptr;
483 tagSwap(tmp_index, tag_index);
484 tagStore[tmp_index].chain_ptr = freelist;
485 freelist = tmp_index;
486 freeSecond++;
487 }
488 } else {
489 // tag_ptr in primary hash table
490 assert(tag_index < primaryBound);
491 tag_ptr->status = 0;
492 unsigned tmp_set = hash(tag_ptr->tag << tagShift);
493 if (sets[tmp_set].chain_ptr != tagNull) { // collapse chain
494 tmp_index = sets[tmp_set].chain_ptr;
495 tagSwap(tag_index, tmp_index);
496 tagStore[tmp_index].chain_ptr = freelist;
497 freelist = tmp_index;
498 freeSecond++;
499 sets[tmp_set].chain_ptr = tag_ptr->chain_ptr;
500 sets[tmp_set].moveToTail(tag_ptr);
501 }
502 }
503 }
504 }
505
506 void
507 IIC::freeDataBlock(unsigned long data_ptr)
508 {
509 assert(dataReferenceCount[data_ptr] == 0);
510 DPRINTF(IICMore, "Freeing data at %d\n", data_ptr);
511 blkFreelist.push_front(data_ptr);
512 }
513
514 /** Use a simple modulo hash. */
515 #define SIMPLE_HASH 0
516
517 unsigned
518 IIC::hash(Addr addr) const {
519 #if SIMPLE_HASH
520 return extractTag(addr) % iic_hash_size;
521 #else
522 Addr tag, mask, x, y;
523 tag = extractTag(addr);
524 mask = hashSets-1; /* assumes iic_hash_size is a power of 2 */
525 x = tag & mask;
526 y = (tag >> (int)(::log((double)hashSets)/::log((double)2))) & mask;
527 assert (x < hashSets && y < hashSets);
528 return x ^ y;
529 #endif
530 }
531
532
533 void
534 IICSet::moveToHead(IICTag *tag)
535 {
536 if (tags[0] == tag)
537 return;
538
539 // write 'next' block into blks[i], moving up from MRU toward LRU
540 // until we overwrite the block we moved to head.
541
542 // start by setting up to write 'blk' into blks[0]
543 int i = 0;
544 IICTag *next = tag;
545
546 do {
547 assert(i < assoc);
548 // swap blks[i] and next
549 IICTag *tmp = tags[i];
550 tags[i] = next;
551 next = tmp;
552 ++i;
553 } while (next != tag);
554 }
555
556 void
557 IICSet::moveToTail(IICTag *tag)
558 {
559 if (tags[assoc-1] == tag)
560 return;
561
562 // write 'next' block into blks[i], moving up from MRU toward LRU
563 // until we overwrite the block we moved to head.
564
565 // start by setting up to write 'blk' into blks[0]
566 int i = assoc - 1;
567 IICTag *next = tag;
568
569 do {
570 assert(i >= 0);
571 // swap blks[i] and next
572 IICTag *tmp = tags[i];
573 tags[i] = next;
574 next = tmp;
575 --i;
576 } while (next != tag);
577 }
578
579 void
580 IIC::tagSwap(unsigned long index1, unsigned long index2)
581 {
582 DPRINTF(IIC,"Swapping tag[%d]=%x for tag[%d]=%x\n",index1,
583 tagStore[index1].tag<<tagShift, index2,
584 tagStore[index2].tag<<tagShift);
585 IICTag tmp_tag;
586 tmp_tag = tagStore[index1];
587 tagStore[index1] = tagStore[index2];
588 tagStore[index2] = tmp_tag;
589 if (tagStore[index1].isValid())
590 repl->fixTag(tagStore[index1].re, index2, index1);
591 if (tagStore[index2].isValid())
592 repl->fixTag(tagStore[index2].re, index1, index2);
593 }
594
595
596 IICTag *
597 IIC::secondaryChain(Addr tag, unsigned long chain_ptr,
598 int *_depth) const
599 {
600 int depth = 0;
601 while (chain_ptr != tagNull) {
602 DPRINTF(IIC,"Searching secondary at %d for %x\n", chain_ptr,
603 tag<<tagShift);
604 if (tagStore[chain_ptr].tag == tag &&
605 (tagStore[chain_ptr].isValid())) {
606 *_depth = depth;
607 return &tagStore[chain_ptr];
608 }
609 depth++;
610 chain_ptr = tagStore[chain_ptr].chain_ptr;
611 }
612 *_depth = depth;
613 return NULL;
614 }
615
616 void
617 IIC::invalidateBlk(IIC::BlkType *tag_ptr)
618 {
619 if (tag_ptr) {
620 for (int i = 0; i < tag_ptr->numData; ++i) {
621 dataReferenceCount[tag_ptr->data_ptr[i]]--;
622 if (dataReferenceCount[tag_ptr->data_ptr[i]] == 0) {
623 freeDataBlock(tag_ptr->data_ptr[i]);
624 }
625 }
626 repl->removeEntry(tag_ptr->re);
627 freeTag(tag_ptr);
628 }
629 }
630
631 void
632 IIC::readData(IICTag *blk, uint8_t *data)
633 {
634 assert(blk->size <= trivialSize || blk->numData > 0);
635 int data_size = blk->size;
636 if (data_size > trivialSize) {
637 for (int i = 0; i < blk->numData; ++i){
638 memcpy(data+i*subSize,
639 &(dataBlks[blk->data_ptr[i]][0]),
640 (data_size>subSize)?subSize:data_size);
641 data_size -= subSize;
642 }
643 } else {
644 memcpy(data,blk->trivialData,data_size);
645 }
646 }
647
648 void
649 IIC::writeData(IICTag *blk, uint8_t *write_data, int size,
650 PacketList & writebacks)
651 {
652 DPRINTF(IIC, "Writing %d bytes to %x\n", size,
653 blk->tag<<tagShift);
654 // Find the number of subblocks needed, (round up)
655 int num_subs = (size + (subSize -1))/subSize;
656 if (size <= trivialSize) {
657 num_subs = 0;
658 }
659 assert(num_subs <= numSub);
660 if (num_subs > blk->numData) {
661 // need to allocate more data blocks
662 for (int i = blk->numData; i < num_subs; ++i){
663 blk->data_ptr[i] = getFreeDataBlock(writebacks);
664 dataReferenceCount[blk->data_ptr[i]] += 1;
665 }
666 } else if (num_subs < blk->numData){
667 // can free data blocks
668 for (int i=num_subs; i < blk->numData; ++i){
669 // decrement reference count and compare to zero
670 if (--dataReferenceCount[blk->data_ptr[i]] == 0) {
671 freeDataBlock(blk->data_ptr[i]);
672 }
673 }
674 }
675
676 blk->numData = num_subs;
677 blk->size = size;
678 assert(size <= trivialSize || blk->numData > 0);
679 if (size > trivialSize){
680 for (int i = 0; i < blk->numData; ++i){
681 memcpy(&dataBlks[blk->data_ptr[i]][0], write_data + i*subSize,
682 (size>subSize)?subSize:size);
683 size -= subSize;
684 }
685 } else {
686 memcpy(blk->trivialData,write_data,size);
687 }
688 }
689
690
691 void
692 IIC::cleanupRefs()
693 {
694 for (int i = 0; i < numTags; ++i) {
695 if (tagStore[i].isValid()) {
696 totalRefs += tagStore[i].refCount;
697 ++sampledRefs;
698 }
699 }
700 }