2004-09-28 22:55:55 +02:00
|
|
|
/*
|
2005-06-05 11:16:00 +02:00
|
|
|
* Copyright (c) 2004-2005 The Regents of The University of Michigan
|
2004-09-28 22:55:55 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met: redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer;
|
|
|
|
* redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution;
|
|
|
|
* neither the name of the copyright holders nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2006-06-01 01:26:56 +02:00
|
|
|
*
|
|
|
|
* Authors: Erik Hallnor
|
2004-09-28 22:55:55 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Declaration of a memory trace CPU object for optimal caches. Uses a memory
|
|
|
|
* trace to access a fully associative cache with optimal replacement.
|
|
|
|
*/
|
|
|
|
|
2005-02-19 17:46:41 +01:00
|
|
|
#ifndef __CPU_TRACE_OPT_CPU_HH__
|
|
|
|
#define __CPU_TRACE_OPT_CPU_HH__
|
2004-09-28 22:55:55 +02:00
|
|
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "mem/mem_req.hh" // for MemReqPtr
|
|
|
|
#include "sim/eventq.hh" // for Event
|
2005-02-19 17:46:41 +01:00
|
|
|
#include "sim/sim_object.hh"
|
2004-09-28 22:55:55 +02:00
|
|
|
|
|
|
|
// Forward Declaration
|
|
|
|
class MemTraceReader;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A CPU object to simulate a fully-associative cache with optimal replacement.
|
|
|
|
*/
|
2005-02-19 17:46:41 +01:00
|
|
|
class OptCPU : public SimObject
|
2004-09-28 22:55:55 +02:00
|
|
|
{
|
2005-02-19 17:46:41 +01:00
|
|
|
private:
|
2004-09-28 22:55:55 +02:00
|
|
|
typedef int RefIndex;
|
|
|
|
|
|
|
|
typedef std::vector<RefIndex> L3Table;
|
|
|
|
typedef std::vector<L3Table> L2Table;
|
|
|
|
typedef std::vector<L2Table> L1Table;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Event to call OptCPU::tick
|
|
|
|
*/
|
|
|
|
class TickEvent : public Event
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/** The associated CPU */
|
|
|
|
OptCPU *cpu;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct this event;
|
|
|
|
*/
|
|
|
|
TickEvent(OptCPU *c);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Call the tick function.
|
|
|
|
*/
|
|
|
|
void process();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return a string description of this event.
|
|
|
|
*/
|
2008-02-06 22:32:40 +01:00
|
|
|
const char *description() const;
|
2004-09-28 22:55:55 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
TickEvent tickEvent;
|
|
|
|
|
|
|
|
class RefInfo
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RefIndex nextRefTime;
|
|
|
|
Addr addr;
|
|
|
|
};
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
/** Reference Information, per set. */
|
|
|
|
std::vector<std::vector<RefInfo> > refInfo;
|
2004-09-28 22:55:55 +02:00
|
|
|
|
|
|
|
/** Lookup table to track blocks in the cache heap */
|
|
|
|
L1Table lookupTable;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the correct value in the lookup table.
|
|
|
|
*/
|
|
|
|
RefIndex lookupValue(Addr addr)
|
|
|
|
{
|
|
|
|
int l1_index = (addr >> 32) & 0x0f;
|
|
|
|
int l2_index = (addr >> 16) & 0xffff;
|
|
|
|
int l3_index = addr & 0xffff;
|
|
|
|
assert(l1_index == addr >> 32);
|
|
|
|
return lookupTable[l1_index][l2_index][l3_index];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Set the value in the lookup table.
|
|
|
|
*/
|
|
|
|
void setValue(Addr addr, RefIndex index)
|
|
|
|
{
|
|
|
|
int l1_index = (addr >> 32) & 0x0f;
|
|
|
|
int l2_index = (addr >> 16) & 0xffff;
|
|
|
|
int l3_index = addr & 0xffff;
|
|
|
|
assert(l1_index == addr >> 32);
|
|
|
|
lookupTable[l1_index][l2_index][l3_index]=index;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Initialize the lookup table to the given value.
|
|
|
|
*/
|
|
|
|
void initTable(Addr addr, RefIndex index);
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
void heapSwap(int set, int a, int b) {
|
2004-09-28 22:55:55 +02:00
|
|
|
RefIndex tmp = cacheHeap[a];
|
|
|
|
cacheHeap[a] = cacheHeap[b];
|
|
|
|
cacheHeap[b] = tmp;
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
setValue(refInfo[set][cacheHeap[a]].addr, a);
|
|
|
|
setValue(refInfo[set][cacheHeap[b]].addr, b);
|
2004-09-28 22:55:55 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int heapLeft(int index) { return index + index + 1; }
|
|
|
|
int heapRight(int index) { return index + index + 2; }
|
|
|
|
int heapParent(int index) { return (index - 1) >> 1; }
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
RefIndex heapRank(int set, int index) {
|
|
|
|
return refInfo[set][cacheHeap[index]].nextRefTime;
|
2004-09-28 22:55:55 +02:00
|
|
|
}
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
void heapify(int set, int start){
|
2004-09-28 22:55:55 +02:00
|
|
|
int left = heapLeft(start);
|
|
|
|
int right = heapRight(start);
|
|
|
|
int max = start;
|
2004-09-30 07:46:48 +02:00
|
|
|
if (left < assoc && heapRank(set, left) > heapRank(set, start)) {
|
2004-09-28 22:55:55 +02:00
|
|
|
max = left;
|
|
|
|
}
|
2004-09-30 07:46:48 +02:00
|
|
|
if (right < assoc && heapRank(set, right) > heapRank(set, max)) {
|
2004-09-28 22:55:55 +02:00
|
|
|
max = right;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (max != start) {
|
2004-09-30 07:46:48 +02:00
|
|
|
heapSwap(set, start, max);
|
|
|
|
heapify(set, max);
|
2004-09-28 22:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
void verifyHeap(int set, int start) {
|
2004-09-28 22:55:55 +02:00
|
|
|
int left = heapLeft(start);
|
|
|
|
int right = heapRight(start);
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
if (left < assoc) {
|
|
|
|
assert(heapRank(set, start) >= heapRank(set, left));
|
|
|
|
verifyHeap(set, left);
|
2004-09-28 22:55:55 +02:00
|
|
|
}
|
2004-09-30 07:46:48 +02:00
|
|
|
if (right < assoc) {
|
|
|
|
assert(heapRank(set, start) >= heapRank(set, right));
|
|
|
|
verifyHeap(set, right);
|
2004-09-28 22:55:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
void processRankIncrease(int set, int start) {
|
2004-09-28 22:55:55 +02:00
|
|
|
int parent = heapParent(start);
|
2004-09-30 07:46:48 +02:00
|
|
|
while (start > 0 && heapRank(set,parent) < heapRank(set,start)) {
|
|
|
|
heapSwap(set, parent, start);
|
2004-09-28 22:55:55 +02:00
|
|
|
start = parent;
|
|
|
|
parent = heapParent(start);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
void processSet(int set);
|
|
|
|
|
2004-09-28 22:55:55 +02:00
|
|
|
static const RefIndex InfiniteRef = 0x7fffffff;
|
|
|
|
|
|
|
|
/** Memory reference trace. */
|
|
|
|
MemTraceReader *trace;
|
|
|
|
|
|
|
|
/** Cache heap for replacement. */
|
|
|
|
std::vector<RefIndex> cacheHeap;
|
|
|
|
|
|
|
|
/** The number of blocks in the cache. */
|
|
|
|
const int numBlks;
|
|
|
|
|
2004-09-30 07:46:48 +02:00
|
|
|
const int assoc;
|
|
|
|
const int numSets;
|
|
|
|
const int setMask;
|
|
|
|
|
2004-09-28 22:55:55 +02:00
|
|
|
|
|
|
|
int misses;
|
|
|
|
int hits;
|
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* Construct a OptCPU object.
|
|
|
|
*/
|
|
|
|
OptCPU(const std::string &name,
|
|
|
|
MemTraceReader *_trace,
|
2004-09-30 07:46:48 +02:00
|
|
|
int block_size,
|
|
|
|
int cache_size,
|
|
|
|
int assoc);
|
2004-09-28 22:55:55 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Perform the optimal replacement simulation.
|
|
|
|
*/
|
|
|
|
void tick();
|
|
|
|
};
|
|
|
|
|
2005-02-19 17:46:41 +01:00
|
|
|
#endif // __CPU_TRACE_OPT_CPU_HH__
|