From 5f9cfa46727f3239f0ae1b66dcd15d0ab45d8c64 Mon Sep 17 00:00:00 2001 From: Patrick O'Connor Date: Sun, 25 Oct 2015 22:23:05 -0700 Subject: [PATCH 1/6] Add lru changes --- .gitignore | 1 + src/mem/cache/tags/lru.cc | 42 ++++++++++++++++++++++++++++++++++----- 2 files changed, 38 insertions(+), 5 deletions(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5509140 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.DS_Store diff --git a/src/mem/cache/tags/lru.cc b/src/mem/cache/tags/lru.cc index 744d722..ba7f8f3 100644 --- a/src/mem/cache/tags/lru.cc +++ b/src/mem/cache/tags/lru.cc @@ -100,6 +100,7 @@ LRU::LRU(const Params *p) // locate next cache block BlkType *blk = &blks[blkIndex]; blk->data = &dataBlks[blkSize*blkIndex]; + blk->rrpv = assoc - 1; ++blkIndex; // invalidate new cache block @@ -147,9 +148,9 @@ LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) } if (blk != NULL) { - // move this block to head of the MRU list - sets[set].moveToHead(blk); - DPRINTF(CacheRepl, "set %x: moving blk %x (%s) to MRU\n", + //Decrement this block's RRPV to 0 + sets[set].rrpv = 0; + DPRINTF(CacheRepl, "set %x: setting blk %x (%s) RRPV to 0t\n", set, regenerateBlkAddr(tag, set), is_secure ? "s" : "ns"); if (blk->whenReady > curTick() && cache->ticksToCycles(blk->whenReady - curTick()) > hitLatency) { @@ -176,7 +177,35 @@ LRU::findVictim(Addr addr) { unsigned set = extractSet(addr); // grab a replacement candidate - BlkType *blk = sets[set].blks[assoc-1]; + // BlkType *blk = sets[set].blks[assoc-1]; + + // The replacement will be found from left to right. + // The first blk with RRPV of assoc - 1 will be the replaced blk + int block_index = 0; //This is an int used as an index for the blocks in the set + int loop_index = 0; //This is an int that is incrememnted when no rrpv = assoc - 1 + int rrpv_boundry = assoc - 1; // This value is used to compare to rrpv of blocks + int loop_limit = assoc; // Outer while loop should not exceed this value + + bool found_victim = false; + + while (!found_victim && loop_index < loop_limit){ + while (!found_victim && block_index < assoc){ + BlkType *blk = sets[set].blks[block_index]; + if(blk->rrpv == rrpv_boundry){ + found_victim = true; + } + block_index++; + } + block_index = 0; + // If none of the blocks have an rrpv of assoc - 1, then loop through again + // incrementing all the rrpv's + for (block_index = 0; block_index < assoc && !found_victim; block_index++){ + BlkType *blk = sets[set].blks[block_index]; + blk->rrpv++; + } + loop_index++; + } + if (blk->isValid()) { DPRINTF(CacheRepl, "set %x: selecting blk %x for replacement\n", @@ -253,7 +282,10 @@ LRU::invalidate(BlkType *blk) // should be evicted before valid blocks unsigned set = blk->set; - sets[set].moveToTail(blk); + //sets[set].moveToTail(blk); + + //Invalidating a block should only involve maxing out its rrpv + blk->rrpv = assoc - 1; } void From eecdc3ab2820b6a53ed6b6702f824342806aa100 Mon Sep 17 00:00:00 2001 From: Patrick O'Connor Date: Mon, 26 Oct 2015 22:03:14 -0700 Subject: [PATCH 2/6] These changes successfully build --- src/mem/cache/tags/lru.cc | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/mem/cache/tags/lru.cc b/src/mem/cache/tags/lru.cc index ba7f8f3..773a8b7 100644 --- a/src/mem/cache/tags/lru.cc +++ b/src/mem/cache/tags/lru.cc @@ -149,7 +149,7 @@ LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) if (blk != NULL) { //Decrement this block's RRPV to 0 - sets[set].rrpv = 0; + blk->rrpv = 0; DPRINTF(CacheRepl, "set %x: setting blk %x (%s) RRPV to 0t\n", set, regenerateBlkAddr(tag, set), is_secure ? "s" : "ns"); if (blk->whenReady > curTick() @@ -187,6 +187,7 @@ LRU::findVictim(Addr addr) int loop_limit = assoc; // Outer while loop should not exceed this value bool found_victim = false; + BlkType *blk = NULL; while (!found_victim && loop_index < loop_limit){ while (!found_victim && block_index < assoc){ @@ -260,8 +261,10 @@ LRU::insertBlock(PacketPtr pkt, BlkType *blk) blk->task_id = task_id; blk->tickInserted = curTick(); - unsigned set = extractSet(addr); - sets[set].moveToHead(blk); + //unsigned set = extractSet(addr); + //sets[set].moveToHead(blk); + // At this point do we just set the blks rrpv to 2? + blk->rrpv = assoc - 2; // We only need to write into one tag and one data block. tagAccesses += 1; @@ -281,7 +284,7 @@ LRU::invalidate(BlkType *blk) blk->tickInserted = curTick(); // should be evicted before valid blocks - unsigned set = blk->set; + //unsigned set = blk->set; //sets[set].moveToTail(blk); //Invalidating a block should only involve maxing out its rrpv From 908b11bfa5805799ea4aa738b18b2d56c6654a12 Mon Sep 17 00:00:00 2001 From: Patrick O'Connor Date: Tue, 27 Oct 2015 13:27:26 -0700 Subject: [PATCH 3/6] Add print statements for debugging --- src/mem/cache/tags/lru.cc | 40 ++++++++++++++++++++++++++++++--------- 1 file changed, 31 insertions(+), 9 deletions(-) diff --git a/src/mem/cache/tags/lru.cc b/src/mem/cache/tags/lru.cc index 773a8b7..a50284b 100644 --- a/src/mem/cache/tags/lru.cc +++ b/src/mem/cache/tags/lru.cc @@ -61,6 +61,7 @@ LRU::LRU(const Params *p) numSets(p->size / (p->block_size * p->assoc)), sequentialAccess(p->sequential_access) { + printf("LRU HAS STARTED CONSTRUCTION\n"); // Check parameters if (blkSize < 4 || !isPowerOf2(blkSize)) { fatal("Block size must be at least 4 and a power of 2"); @@ -100,7 +101,7 @@ LRU::LRU(const Params *p) // locate next cache block BlkType *blk = &blks[blkIndex]; blk->data = &dataBlks[blkSize*blkIndex]; - blk->rrpv = assoc - 1; + blk->rrpv = 3; ++blkIndex; // invalidate new cache block @@ -118,6 +119,7 @@ LRU::LRU(const Params *p) blk->set = i; } } + printf("LRU HAS BEEN CONSTRUCTED\n"); } LRU::~LRU() @@ -130,6 +132,7 @@ LRU::~LRU() LRU::BlkType* LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) { + printf("ACCESSING LRU BLOCK\n"); Addr tag = extractTag(addr); unsigned set = extractSet(addr); BlkType *blk = sets[set].findBlk(tag, is_secure); @@ -158,7 +161,7 @@ LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) } blk->refCount += 1; } - + printf("LRU HAS BEEN ACCESSED\n"); return blk; } @@ -166,58 +169,74 @@ LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) LRU::BlkType* LRU::findBlock(Addr addr, bool is_secure) const { + printf("FINDING LRU BLOCK\n"); Addr tag = extractTag(addr); unsigned set = extractSet(addr); BlkType *blk = sets[set].findBlk(tag, is_secure); + printf("RETURNING LRU BLOCK\n"); return blk; } LRU::BlkType* LRU::findVictim(Addr addr) { + printf("FINDING LRU VICTIM\n"); + printf("assoc is %d\n", assoc); unsigned set = extractSet(addr); // grab a replacement candidate // BlkType *blk = sets[set].blks[assoc-1]; // The replacement will be found from left to right. - // The first blk with RRPV of assoc - 1 will be the replaced blk + // The first blk with RRPV of 3 will be the replaced blk int block_index = 0; //This is an int used as an index for the blocks in the set - int loop_index = 0; //This is an int that is incrememnted when no rrpv = assoc - 1 - int rrpv_boundry = assoc - 1; // This value is used to compare to rrpv of blocks + int loop_index = 0; //This is an int that is incrememnted when no rrpv = 3 + int rrpv_boundry = 3; // This value is used to compare to rrpv of blocks int loop_limit = assoc; // Outer while loop should not exceed this value bool found_victim = false; BlkType *blk = NULL; while (!found_victim && loop_index < loop_limit){ + printf("!found_victim && loop_index < loop_limit\n"); while (!found_victim && block_index < assoc){ + printf("!found_victim && block_index < assoc\n"); BlkType *blk = sets[set].blks[block_index]; + printf("Assigned blk\n"); if(blk->rrpv == rrpv_boundry){ + printf("VICTIM FOUND WITH RRPV VALUE OF %d\n", blk->rrpv); found_victim = true; } block_index++; } + printf("BROKE INNER LOOP\n"); + block_index = 0; - // If none of the blocks have an rrpv of assoc - 1, then loop through again + // If none of the blocks have an rrpv of 3, then loop through again // incrementing all the rrpv's - for (block_index = 0; block_index < assoc && !found_victim; block_index++){ + for (block_index = 0; block_index < 4 && !found_victim; block_index++){ + printf("block_index = 0; block_index < 4 && !found_victim; block_index++\n"); BlkType *blk = sets[set].blks[block_index]; blk->rrpv++; } loop_index++; + printf("BROKE FOR LOOP\n"); } + printf("BROKE OUTER LOOP\n"); if (blk->isValid()) { + printf("IS BLOCK VALID?\n"); DPRINTF(CacheRepl, "set %x: selecting blk %x for replacement\n", set, regenerateBlkAddr(blk->tag, set)); } + printf("RETURNING LRU VICTIM\n"); return blk; } void LRU::insertBlock(PacketPtr pkt, BlkType *blk) { + printf("INSERTING BLOCK\n"); Addr addr = pkt->getAddr(); MasterID master_id = pkt->req->masterId(); uint32_t task_id = pkt->req->taskId(); @@ -264,16 +283,18 @@ LRU::insertBlock(PacketPtr pkt, BlkType *blk) //unsigned set = extractSet(addr); //sets[set].moveToHead(blk); // At this point do we just set the blks rrpv to 2? - blk->rrpv = assoc - 2; + blk->rrpv = 2; // We only need to write into one tag and one data block. tagAccesses += 1; dataAccesses += 1; + printf("BLOCK INSERTED\n"); } void LRU::invalidate(BlkType *blk) { + printf("%s\n", "INVALIDATING BLOCK"); assert(blk); assert(blk->isValid()); tagsInUse--; @@ -288,7 +309,8 @@ LRU::invalidate(BlkType *blk) //sets[set].moveToTail(blk); //Invalidating a block should only involve maxing out its rrpv - blk->rrpv = assoc - 1; + blk->rrpv = 3; + printf("%s\n", "BLOCK INVALIDATED"); } void From b43d8f8c2867e0c52b53daeb2d3dd094f0014813 Mon Sep 17 00:00:00 2001 From: Patrick O'Connor Date: Tue, 27 Oct 2015 14:16:43 -0700 Subject: [PATCH 4/6] Update invalidate function to also set rrpv to 3 --- src/mem/cache/blk.hh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/mem/cache/blk.hh b/src/mem/cache/blk.hh index 164bebb..db8f5c3 100644 --- a/src/mem/cache/blk.hh +++ b/src/mem/cache/blk.hh @@ -1,5 +1,4 @@ -/* - * Copyright (c) 2012-2013 ARM Limited + /* Copyright (c) 2012-2013 ARM Limited * All rights reserved. * * The license below extends only to copyright in the software and shall @@ -177,7 +176,7 @@ class CacheBlk set(-1), isTouched(false), refCount(0), srcMasterId(Request::invldMasterId), tickInserted(0), - rrpv(0) + rrpv(3) {} /** @@ -238,6 +237,7 @@ class CacheBlk status = 0; isTouched = false; clearLoadLocks(); + rrpv = 3; } /** From c89175a429744ad77cc8ca8ec9bc5b872195ebd1 Mon Sep 17 00:00:00 2001 From: Patrick O'Connor Date: Tue, 27 Oct 2015 21:16:12 -0700 Subject: [PATCH 5/6] Tried some pointer magic --- src/mem/cache/blk.hh | 1 + src/mem/cache/tags/lru.cc | 17 ++++++++++------- 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/src/mem/cache/blk.hh b/src/mem/cache/blk.hh index db8f5c3..b0f11eb 100644 --- a/src/mem/cache/blk.hh +++ b/src/mem/cache/blk.hh @@ -195,6 +195,7 @@ class CacheBlk set = rhs.set; refCount = rhs.refCount; task_id = rhs.task_id; + rrpv = rhs.rrpv; return *this; } diff --git a/src/mem/cache/tags/lru.cc b/src/mem/cache/tags/lru.cc index a50284b..5a50434 100644 --- a/src/mem/cache/tags/lru.cc +++ b/src/mem/cache/tags/lru.cc @@ -191,16 +191,16 @@ LRU::findVictim(Addr addr) int block_index = 0; //This is an int used as an index for the blocks in the set int loop_index = 0; //This is an int that is incrememnted when no rrpv = 3 int rrpv_boundry = 3; // This value is used to compare to rrpv of blocks - int loop_limit = assoc; // Outer while loop should not exceed this value + int loop_limit = 3; // Outer while loop should not exceed this value bool found_victim = false; - BlkType *blk = NULL; + BlkType *blk = (BlkType*)malloc(sizeof(BlkType)); - while (!found_victim && loop_index < loop_limit){ + while (!found_victim && loop_index < loop_limit && loop_index < assoc){ printf("!found_victim && loop_index < loop_limit\n"); - while (!found_victim && block_index < assoc){ - printf("!found_victim && block_index < assoc\n"); - BlkType *blk = sets[set].blks[block_index]; + while (!found_victim && block_index <= rrpv_boundry){ + printf("!found_victim && block_index <= rrpv_boundry\n"); + blk = sets[set].blks[block_index]; printf("Assigned blk\n"); if(blk->rrpv == rrpv_boundry){ printf("VICTIM FOUND WITH RRPV VALUE OF %d\n", blk->rrpv); @@ -215,7 +215,7 @@ LRU::findVictim(Addr addr) // incrementing all the rrpv's for (block_index = 0; block_index < 4 && !found_victim; block_index++){ printf("block_index = 0; block_index < 4 && !found_victim; block_index++\n"); - BlkType *blk = sets[set].blks[block_index]; + blk = sets[set].blks[block_index]; blk->rrpv++; } loop_index++; @@ -230,6 +230,9 @@ LRU::findVictim(Addr addr) set, regenerateBlkAddr(blk->tag, set)); } printf("RETURNING LRU VICTIM\n"); + // BlkType *blkCopy = NULL; + // memcpy(blkCopy, blk, sizeof(BlkType)); + // free(blk); return blk; } From c37638ecff32d58af5ebf08293539232ade72b63 Mon Sep 17 00:00:00 2001 From: Jace Hensley Date: Thu, 29 Oct 2015 20:34:20 -0700 Subject: [PATCH 6/6] Clean up find victim --- src/mem/cache/tags/lru.cc | 85 ++++++++++----------------------------- 1 file changed, 22 insertions(+), 63 deletions(-) diff --git a/src/mem/cache/tags/lru.cc b/src/mem/cache/tags/lru.cc index 5a50434..dc747c9 100644 --- a/src/mem/cache/tags/lru.cc +++ b/src/mem/cache/tags/lru.cc @@ -61,7 +61,6 @@ LRU::LRU(const Params *p) numSets(p->size / (p->block_size * p->assoc)), sequentialAccess(p->sequential_access) { - printf("LRU HAS STARTED CONSTRUCTION\n"); // Check parameters if (blkSize < 4 || !isPowerOf2(blkSize)) { fatal("Block size must be at least 4 and a power of 2"); @@ -119,7 +118,6 @@ LRU::LRU(const Params *p) blk->set = i; } } - printf("LRU HAS BEEN CONSTRUCTED\n"); } LRU::~LRU() @@ -132,7 +130,6 @@ LRU::~LRU() LRU::BlkType* LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) { - printf("ACCESSING LRU BLOCK\n"); Addr tag = extractTag(addr); unsigned set = extractSet(addr); BlkType *blk = sets[set].findBlk(tag, is_secure); @@ -161,7 +158,6 @@ LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) } blk->refCount += 1; } - printf("LRU HAS BEEN ACCESSED\n"); return blk; } @@ -169,77 +165,49 @@ LRU::accessBlock(Addr addr, bool is_secure, Cycles &lat, int master_id) LRU::BlkType* LRU::findBlock(Addr addr, bool is_secure) const { - printf("FINDING LRU BLOCK\n"); Addr tag = extractTag(addr); unsigned set = extractSet(addr); BlkType *blk = sets[set].findBlk(tag, is_secure); - printf("RETURNING LRU BLOCK\n"); return blk; } LRU::BlkType* LRU::findVictim(Addr addr) { - printf("FINDING LRU VICTIM\n"); - printf("assoc is %d\n", assoc); unsigned set = extractSet(addr); - // grab a replacement candidate - // BlkType *blk = sets[set].blks[assoc-1]; - - // The replacement will be found from left to right. - // The first blk with RRPV of 3 will be the replaced blk - int block_index = 0; //This is an int used as an index for the blocks in the set - int loop_index = 0; //This is an int that is incrememnted when no rrpv = 3 - int rrpv_boundry = 3; // This value is used to compare to rrpv of blocks - int loop_limit = 3; // Outer while loop should not exceed this value - - bool found_victim = false; - BlkType *blk = (BlkType*)malloc(sizeof(BlkType)); - - while (!found_victim && loop_index < loop_limit && loop_index < assoc){ - printf("!found_victim && loop_index < loop_limit\n"); - while (!found_victim && block_index <= rrpv_boundry){ - printf("!found_victim && block_index <= rrpv_boundry\n"); - blk = sets[set].blks[block_index]; - printf("Assigned blk\n"); - if(blk->rrpv == rrpv_boundry){ - printf("VICTIM FOUND WITH RRPV VALUE OF %d\n", blk->rrpv); - found_victim = true; - } - block_index++; - } - printf("BROKE INNER LOOP\n"); - - block_index = 0; - // If none of the blocks have an rrpv of 3, then loop through again - // incrementing all the rrpv's - for (block_index = 0; block_index < 4 && !found_victim; block_index++){ - printf("block_index = 0; block_index < 4 && !found_victim; block_index++\n"); - blk = sets[set].blks[block_index]; - blk->rrpv++; - } - loop_index++; - printf("BROKE FOR LOOP\n"); - } - printf("BROKE OUTER LOOP\n"); + BlkType *blk = NULL; + + bool found_victim = false; + + while(!found_victim) { + for (int block_index = 0; block_index < assoc; block_index++) { + blk = sets[set].blks[block_index]; + if (blk->rrpv == 3) { + found_victim = true; + block_index = assoc; + } + } + + if (!found_victim) { + for(int block_index = 0; block_index < assoc; block_index++){ + BlkType *blk = sets[set].blks[block_index]; + blk->rrpv++; + } + } + } if (blk->isValid()) { - printf("IS BLOCK VALID?\n"); DPRINTF(CacheRepl, "set %x: selecting blk %x for replacement\n", set, regenerateBlkAddr(blk->tag, set)); } - printf("RETURNING LRU VICTIM\n"); - // BlkType *blkCopy = NULL; - // memcpy(blkCopy, blk, sizeof(BlkType)); - // free(blk); + return blk; } void LRU::insertBlock(PacketPtr pkt, BlkType *blk) { - printf("INSERTING BLOCK\n"); Addr addr = pkt->getAddr(); MasterID master_id = pkt->req->masterId(); uint32_t task_id = pkt->req->taskId(); @@ -283,21 +251,17 @@ LRU::insertBlock(PacketPtr pkt, BlkType *blk) blk->task_id = task_id; blk->tickInserted = curTick(); - //unsigned set = extractSet(addr); - //sets[set].moveToHead(blk); - // At this point do we just set the blks rrpv to 2? + // At this point do we just set the blks rrpv to 2 blk->rrpv = 2; // We only need to write into one tag and one data block. tagAccesses += 1; dataAccesses += 1; - printf("BLOCK INSERTED\n"); } void LRU::invalidate(BlkType *blk) { - printf("%s\n", "INVALIDATING BLOCK"); assert(blk); assert(blk->isValid()); tagsInUse--; @@ -307,13 +271,8 @@ LRU::invalidate(BlkType *blk) blk->task_id = ContextSwitchTaskId::Unknown; blk->tickInserted = curTick(); - // should be evicted before valid blocks - //unsigned set = blk->set; - //sets[set].moveToTail(blk); - //Invalidating a block should only involve maxing out its rrpv blk->rrpv = 3; - printf("%s\n", "BLOCK INVALIDATED"); } void