Updates to make traces work correctly in all circumstances. Add opt set associative...
[gem5.git] / cpu / trace / opt_cpu.hh
1 /*
2 * Copyright (c) 2004 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
29 /**
30 * @file
31 * Declaration of a memory trace CPU object for optimal caches. Uses a memory
32 * trace to access a fully associative cache with optimal replacement.
33 */
34
35 #ifndef __OPT_CPU_HH__
36 #define __OPT_CPU_HH__
37
38 #include <vector>
39
40 #include "cpu/base_cpu.hh"
41 #include "mem/mem_req.hh" // for MemReqPtr
42 #include "sim/eventq.hh" // for Event
43
44 // Forward Declaration
45 class MemTraceReader;
46
47 /**
48 * A CPU object to simulate a fully-associative cache with optimal replacement.
49 */
50 class OptCPU : public BaseCPU
51 {
52 typedef int RefIndex;
53
54 typedef std::vector<RefIndex> L3Table;
55 typedef std::vector<L3Table> L2Table;
56 typedef std::vector<L2Table> L1Table;
57
58 /**
59 * Event to call OptCPU::tick
60 */
61 class TickEvent : public Event
62 {
63 private:
64 /** The associated CPU */
65 OptCPU *cpu;
66
67 public:
68 /**
69 * Construct this event;
70 */
71 TickEvent(OptCPU *c);
72
73 /**
74 * Call the tick function.
75 */
76 void process();
77
78 /**
79 * Return a string description of this event.
80 */
81 const char *description();
82 };
83
84 TickEvent tickEvent;
85
86 class RefInfo
87 {
88 public:
89 RefIndex nextRefTime;
90 Addr addr;
91 };
92
93 /** Reference Information, per set. */
94 std::vector<std::vector<RefInfo> > refInfo;
95
96 /** Lookup table to track blocks in the cache heap */
97 L1Table lookupTable;
98
99 /**
100 * Return the correct value in the lookup table.
101 */
102 RefIndex lookupValue(Addr addr)
103 {
104 int l1_index = (addr >> 32) & 0x0f;
105 int l2_index = (addr >> 16) & 0xffff;
106 int l3_index = addr & 0xffff;
107 assert(l1_index == addr >> 32);
108 return lookupTable[l1_index][l2_index][l3_index];
109 }
110
111 /**
112 * Set the value in the lookup table.
113 */
114 void setValue(Addr addr, RefIndex index)
115 {
116 int l1_index = (addr >> 32) & 0x0f;
117 int l2_index = (addr >> 16) & 0xffff;
118 int l3_index = addr & 0xffff;
119 assert(l1_index == addr >> 32);
120 lookupTable[l1_index][l2_index][l3_index]=index;
121 }
122
123 /**
124 * Initialize the lookup table to the given value.
125 */
126 void initTable(Addr addr, RefIndex index);
127
128 void heapSwap(int set, int a, int b) {
129 RefIndex tmp = cacheHeap[a];
130 cacheHeap[a] = cacheHeap[b];
131 cacheHeap[b] = tmp;
132
133 setValue(refInfo[set][cacheHeap[a]].addr, a);
134 setValue(refInfo[set][cacheHeap[b]].addr, b);
135 }
136
137 int heapLeft(int index) { return index + index + 1; }
138 int heapRight(int index) { return index + index + 2; }
139 int heapParent(int index) { return (index - 1) >> 1; }
140
141 RefIndex heapRank(int set, int index) {
142 return refInfo[set][cacheHeap[index]].nextRefTime;
143 }
144
145 void heapify(int set, int start){
146 int left = heapLeft(start);
147 int right = heapRight(start);
148 int max = start;
149 if (left < assoc && heapRank(set, left) > heapRank(set, start)) {
150 max = left;
151 }
152 if (right < assoc && heapRank(set, right) > heapRank(set, max)) {
153 max = right;
154 }
155
156 if (max != start) {
157 heapSwap(set, start, max);
158 heapify(set, max);
159 }
160 }
161
162 void verifyHeap(int set, int start) {
163 int left = heapLeft(start);
164 int right = heapRight(start);
165
166 if (left < assoc) {
167 assert(heapRank(set, start) >= heapRank(set, left));
168 verifyHeap(set, left);
169 }
170 if (right < assoc) {
171 assert(heapRank(set, start) >= heapRank(set, right));
172 verifyHeap(set, right);
173 }
174 }
175
176 void processRankIncrease(int set, int start) {
177 int parent = heapParent(start);
178 while (start > 0 && heapRank(set,parent) < heapRank(set,start)) {
179 heapSwap(set, parent, start);
180 start = parent;
181 parent = heapParent(start);
182 }
183 }
184
185 void processSet(int set);
186
187 static const RefIndex InfiniteRef = 0x7fffffff;
188
189 /** Memory reference trace. */
190 MemTraceReader *trace;
191
192 /** Cache heap for replacement. */
193 std::vector<RefIndex> cacheHeap;
194
195 /** The number of blocks in the cache. */
196 const int numBlks;
197
198 const int assoc;
199 const int numSets;
200 const int setMask;
201
202
203 int misses;
204 int hits;
205
206 public:
207 /**
208 * Construct a OptCPU object.
209 */
210 OptCPU(const std::string &name,
211 MemTraceReader *_trace,
212 int block_size,
213 int cache_size,
214 int assoc);
215
216 /**
217 * Perform the optimal replacement simulation.
218 */
219 void tick();
220 };
221
222 #endif