Add mechanism for caches to handle failure of the fast path on responses.
[gem5.git] / src / mem / cache / base_cache.cc
1 /*
2 * Copyright (c) 2003-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 * Definition of BaseCache functions.
34 */
35
36 #include "mem/cache/base_cache.hh"
37 #include "cpu/smt.hh"
38 #include "cpu/base.hh"
39
40 using namespace std;
41
42 BaseCache::CachePort::CachePort(const std::string &_name, BaseCache *_cache,
43 bool _isCpuSide)
44 : Port(_name), cache(_cache), isCpuSide(_isCpuSide)
45 {
46 blocked = false;
47 //Start ports at null if more than one is created we should panic
48 //cpuSidePort = NULL;
49 //memSidePort = NULL;
50 }
51
52 void
53 BaseCache::CachePort::recvStatusChange(Port::Status status)
54 {
55 cache->recvStatusChange(status, isCpuSide);
56 }
57
58 void
59 BaseCache::CachePort::getDeviceAddressRanges(AddrRangeList &resp,
60 AddrRangeList &snoop)
61 {
62 cache->getAddressRanges(resp, snoop, isCpuSide);
63 }
64
65 int
66 BaseCache::CachePort::deviceBlockSize()
67 {
68 return cache->getBlockSize();
69 }
70
71 bool
72 BaseCache::CachePort::recvTiming(Packet *pkt)
73 {
74 if (blocked)
75 {
76 DPRINTF(Cache,"Scheduling a retry while blocked\n");
77 mustSendRetry = true;
78 return false;
79 }
80 return cache->doTimingAccess(pkt, this, isCpuSide);
81 }
82
83 Tick
84 BaseCache::CachePort::recvAtomic(Packet *pkt)
85 {
86 return cache->doAtomicAccess(pkt, isCpuSide);
87 }
88
89 void
90 BaseCache::CachePort::recvFunctional(Packet *pkt)
91 {
92 cache->doFunctionalAccess(pkt, isCpuSide);
93 }
94
95 void
96 BaseCache::CachePort::recvRetry()
97 {
98 Packet *pkt;
99 if (!drainList.empty()) {
100 //We have some responses to drain first
101 bool result = true;
102 while (result && !drainList.empty()) {
103 result = sendTiming(drainList.front());
104 if (result)
105 drainList.pop_front();
106 }
107 }
108
109 if (!isCpuSide)
110 {
111 pkt = cache->getPacket();
112 bool success = sendTiming(pkt);
113 DPRINTF(Cache, "Address %x was %s in sending the timing request\n",
114 pkt->getAddr(), success ? "succesful" : "unsuccesful");
115 cache->sendResult(pkt, success);
116 if (success && cache->doMasterRequest())
117 {
118 //Still more to issue, rerequest in 1 cycle
119 pkt = NULL;
120 BaseCache::CacheEvent * reqCpu = new BaseCache::CacheEvent(this);
121 reqCpu->schedule(curTick + 1);
122 }
123 }
124 else
125 {
126 pkt = cache->getCoherencePacket();
127 bool success = sendTiming(pkt);
128 if (success && cache->doSlaveRequest())
129 {
130 //Still more to issue, rerequest in 1 cycle
131 pkt = NULL;
132 BaseCache::CacheEvent * reqCpu = new BaseCache::CacheEvent(this);
133 reqCpu->schedule(curTick + 1);
134 }
135
136 }
137 return;
138 }
139 void
140 BaseCache::CachePort::setBlocked()
141 {
142 assert(!blocked);
143 DPRINTF(Cache, "Cache Blocking\n");
144 blocked = true;
145 //Clear the retry flag
146 mustSendRetry = false;
147 }
148
149 void
150 BaseCache::CachePort::clearBlocked()
151 {
152 assert(blocked);
153 DPRINTF(Cache, "Cache Unblocking\n");
154 blocked = false;
155 if (mustSendRetry)
156 {
157 DPRINTF(Cache, "Cache Sending Retry\n");
158 mustSendRetry = false;
159 sendRetry();
160 }
161 }
162
163 BaseCache::CacheEvent::CacheEvent(CachePort *_cachePort)
164 : Event(&mainEventQueue, CPU_Tick_Pri), cachePort(_cachePort)
165 {
166 this->setFlags(AutoDelete);
167 pkt = NULL;
168 }
169
170 BaseCache::CacheEvent::CacheEvent(CachePort *_cachePort, Packet *_pkt)
171 : Event(&mainEventQueue, CPU_Tick_Pri), cachePort(_cachePort), pkt(_pkt)
172 {
173 this->setFlags(AutoDelete);
174 }
175
176 void
177 BaseCache::CacheEvent::process()
178 {
179 if (!pkt)
180 {
181 if (!cachePort->isCpuSide)
182 {
183 //MSHR
184 pkt = cachePort->cache->getPacket();
185 bool success = cachePort->sendTiming(pkt);
186 DPRINTF(Cache, "Address %x was %s in sending the timing request\n",
187 pkt->getAddr(), success ? "succesful" : "unsuccesful");
188 cachePort->cache->sendResult(pkt, success);
189 if (success && cachePort->cache->doMasterRequest())
190 {
191 //Still more to issue, rerequest in 1 cycle
192 pkt = NULL;
193 this->schedule(curTick+1);
194 }
195 }
196 else
197 {
198 //CSHR
199 pkt = cachePort->cache->getCoherencePacket();
200 bool success = cachePort->sendTiming(pkt);
201 if (success && cachePort->cache->doSlaveRequest())
202 {
203 //Still more to issue, rerequest in 1 cycle
204 pkt = NULL;
205 this->schedule(curTick+1);
206 }
207 }
208 return;
209 }
210 //Response
211 //Know the packet to send
212 pkt->result = Packet::Success;
213 pkt->makeTimingResponse();
214 if (!cachePort->sendTiming(pkt)) {
215 //It failed, save it to list of drain events
216 cachePort->drainList.push_back(pkt);
217 }
218 }
219
220 const char *
221 BaseCache::CacheEvent::description()
222 {
223 return "timing event\n";
224 }
225
226 Port*
227 BaseCache::getPort(const std::string &if_name, int idx)
228 {
229 if (if_name == "")
230 {
231 if(cpuSidePort == NULL)
232 cpuSidePort = new CachePort(name() + "-cpu_side_port", this, true);
233 return cpuSidePort;
234 }
235 else if (if_name == "functional")
236 {
237 if(cpuSidePort == NULL)
238 cpuSidePort = new CachePort(name() + "-cpu_side_port", this, true);
239 return cpuSidePort;
240 }
241 else if (if_name == "cpu_side")
242 {
243 if(cpuSidePort == NULL)
244 cpuSidePort = new CachePort(name() + "-cpu_side_port", this, true);
245 return cpuSidePort;
246 }
247 else if (if_name == "mem_side")
248 {
249 if (memSidePort != NULL)
250 panic("Already have a mem side for this cache\n");
251 memSidePort = new CachePort(name() + "-mem_side_port", this, false);
252 return memSidePort;
253 }
254 else panic("Port name %s unrecognized\n", if_name);
255 }
256
257 void
258 BaseCache::init()
259 {
260 if (!cpuSidePort || !memSidePort)
261 panic("Cache not hooked up on both sides\n");
262 cpuSidePort->sendStatusChange(Port::RangeChange);
263 }
264
265 void
266 BaseCache::regStats()
267 {
268 Request temp_req((Addr) NULL, 4, 0);
269 Packet::Command temp_cmd = Packet::ReadReq;
270 Packet temp_pkt(&temp_req, temp_cmd, 0); //@todo FIx command strings so this isn't neccessary
271 temp_pkt.allocate(); //Temp allocate, all need data
272
273 using namespace Stats;
274
275 // Hit statistics
276 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
277 Packet::Command cmd = (Packet::Command)access_idx;
278 const string &cstr = temp_pkt.cmdIdxToString(cmd);
279
280 hits[access_idx]
281 .init(maxThreadsPerCPU)
282 .name(name() + "." + cstr + "_hits")
283 .desc("number of " + cstr + " hits")
284 .flags(total | nozero | nonan)
285 ;
286 }
287
288 demandHits
289 .name(name() + ".demand_hits")
290 .desc("number of demand (read+write) hits")
291 .flags(total)
292 ;
293 demandHits = hits[Packet::ReadReq] + hits[Packet::WriteReq];
294
295 overallHits
296 .name(name() + ".overall_hits")
297 .desc("number of overall hits")
298 .flags(total)
299 ;
300 overallHits = demandHits + hits[Packet::SoftPFReq] + hits[Packet::HardPFReq]
301 + hits[Packet::Writeback];
302
303 // Miss statistics
304 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
305 Packet::Command cmd = (Packet::Command)access_idx;
306 const string &cstr = temp_pkt.cmdIdxToString(cmd);
307
308 misses[access_idx]
309 .init(maxThreadsPerCPU)
310 .name(name() + "." + cstr + "_misses")
311 .desc("number of " + cstr + " misses")
312 .flags(total | nozero | nonan)
313 ;
314 }
315
316 demandMisses
317 .name(name() + ".demand_misses")
318 .desc("number of demand (read+write) misses")
319 .flags(total)
320 ;
321 demandMisses = misses[Packet::ReadReq] + misses[Packet::WriteReq];
322
323 overallMisses
324 .name(name() + ".overall_misses")
325 .desc("number of overall misses")
326 .flags(total)
327 ;
328 overallMisses = demandMisses + misses[Packet::SoftPFReq] +
329 misses[Packet::HardPFReq] + misses[Packet::Writeback];
330
331 // Miss latency statistics
332 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
333 Packet::Command cmd = (Packet::Command)access_idx;
334 const string &cstr = temp_pkt.cmdIdxToString(cmd);
335
336 missLatency[access_idx]
337 .init(maxThreadsPerCPU)
338 .name(name() + "." + cstr + "_miss_latency")
339 .desc("number of " + cstr + " miss cycles")
340 .flags(total | nozero | nonan)
341 ;
342 }
343
344 demandMissLatency
345 .name(name() + ".demand_miss_latency")
346 .desc("number of demand (read+write) miss cycles")
347 .flags(total)
348 ;
349 demandMissLatency = missLatency[Packet::ReadReq] + missLatency[Packet::WriteReq];
350
351 overallMissLatency
352 .name(name() + ".overall_miss_latency")
353 .desc("number of overall miss cycles")
354 .flags(total)
355 ;
356 overallMissLatency = demandMissLatency + missLatency[Packet::SoftPFReq] +
357 missLatency[Packet::HardPFReq];
358
359 // access formulas
360 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
361 Packet::Command cmd = (Packet::Command)access_idx;
362 const string &cstr = temp_pkt.cmdIdxToString(cmd);
363
364 accesses[access_idx]
365 .name(name() + "." + cstr + "_accesses")
366 .desc("number of " + cstr + " accesses(hits+misses)")
367 .flags(total | nozero | nonan)
368 ;
369
370 accesses[access_idx] = hits[access_idx] + misses[access_idx];
371 }
372
373 demandAccesses
374 .name(name() + ".demand_accesses")
375 .desc("number of demand (read+write) accesses")
376 .flags(total)
377 ;
378 demandAccesses = demandHits + demandMisses;
379
380 overallAccesses
381 .name(name() + ".overall_accesses")
382 .desc("number of overall (read+write) accesses")
383 .flags(total)
384 ;
385 overallAccesses = overallHits + overallMisses;
386
387 // miss rate formulas
388 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
389 Packet::Command cmd = (Packet::Command)access_idx;
390 const string &cstr = temp_pkt.cmdIdxToString(cmd);
391
392 missRate[access_idx]
393 .name(name() + "." + cstr + "_miss_rate")
394 .desc("miss rate for " + cstr + " accesses")
395 .flags(total | nozero | nonan)
396 ;
397
398 missRate[access_idx] = misses[access_idx] / accesses[access_idx];
399 }
400
401 demandMissRate
402 .name(name() + ".demand_miss_rate")
403 .desc("miss rate for demand accesses")
404 .flags(total)
405 ;
406 demandMissRate = demandMisses / demandAccesses;
407
408 overallMissRate
409 .name(name() + ".overall_miss_rate")
410 .desc("miss rate for overall accesses")
411 .flags(total)
412 ;
413 overallMissRate = overallMisses / overallAccesses;
414
415 // miss latency formulas
416 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
417 Packet::Command cmd = (Packet::Command)access_idx;
418 const string &cstr = temp_pkt.cmdIdxToString(cmd);
419
420 avgMissLatency[access_idx]
421 .name(name() + "." + cstr + "_avg_miss_latency")
422 .desc("average " + cstr + " miss latency")
423 .flags(total | nozero | nonan)
424 ;
425
426 avgMissLatency[access_idx] =
427 missLatency[access_idx] / misses[access_idx];
428 }
429
430 demandAvgMissLatency
431 .name(name() + ".demand_avg_miss_latency")
432 .desc("average overall miss latency")
433 .flags(total)
434 ;
435 demandAvgMissLatency = demandMissLatency / demandMisses;
436
437 overallAvgMissLatency
438 .name(name() + ".overall_avg_miss_latency")
439 .desc("average overall miss latency")
440 .flags(total)
441 ;
442 overallAvgMissLatency = overallMissLatency / overallMisses;
443
444 blocked_cycles.init(NUM_BLOCKED_CAUSES);
445 blocked_cycles
446 .name(name() + ".blocked_cycles")
447 .desc("number of cycles access was blocked")
448 .subname(Blocked_NoMSHRs, "no_mshrs")
449 .subname(Blocked_NoTargets, "no_targets")
450 ;
451
452
453 blocked_causes.init(NUM_BLOCKED_CAUSES);
454 blocked_causes
455 .name(name() + ".blocked")
456 .desc("number of cycles access was blocked")
457 .subname(Blocked_NoMSHRs, "no_mshrs")
458 .subname(Blocked_NoTargets, "no_targets")
459 ;
460
461 avg_blocked
462 .name(name() + ".avg_blocked_cycles")
463 .desc("average number of cycles each access was blocked")
464 .subname(Blocked_NoMSHRs, "no_mshrs")
465 .subname(Blocked_NoTargets, "no_targets")
466 ;
467
468 avg_blocked = blocked_cycles / blocked_causes;
469
470 fastWrites
471 .name(name() + ".fast_writes")
472 .desc("number of fast writes performed")
473 ;
474
475 cacheCopies
476 .name(name() + ".cache_copies")
477 .desc("number of cache copies performed")
478 ;
479
480 }