2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
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.
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.
28 * Authors: Erik Hallnor
33 * Definition of BaseCache functions.
36 #include "cpu/base.hh"
38 #include "mem/cache/base_cache.hh"
39 #include "mem/cache/miss/mshr.hh"
43 BaseCache::CachePort::CachePort(const std::string
&_name
, BaseCache
*_cache
,
45 : Port(_name
, _cache
), cache(_cache
), isCpuSide(_isCpuSide
)
48 waitingOnRetry
= false;
49 //Start ports at null if more than one is created we should panic
56 BaseCache::CachePort::recvStatusChange(Port::Status status
)
58 cache
->recvStatusChange(status
, isCpuSide
);
62 BaseCache::CachePort::getDeviceAddressRanges(AddrRangeList
&resp
,
65 cache
->getAddressRanges(resp
, snoop
, isCpuSide
);
69 BaseCache::CachePort::deviceBlockSize()
71 return cache
->getBlockSize();
75 BaseCache::CachePort::checkFunctional(PacketPtr pkt
)
77 //Check storage here first
78 list
<PacketPtr
>::iterator i
= drainList
.begin();
79 list
<PacketPtr
>::iterator iend
= drainList
.end();
81 while (i
!= iend
&& notDone
) {
82 PacketPtr target
= *i
;
83 // If the target contains data, and it overlaps the
84 // probed request, need to update data
85 if (target
->intersect(pkt
)) {
86 DPRINTF(Cache
, "Functional %s access to blk_addr %x intersects a drain\n",
87 pkt
->cmdString(), pkt
->getAddr() & ~(cache
->getBlockSize() - 1));
88 notDone
= fixPacket(pkt
, target
);
92 //Also check the response not yet ready to be on the list
93 std::list
<std::pair
<Tick
,PacketPtr
> >::iterator j
= transmitList
.begin();
94 std::list
<std::pair
<Tick
,PacketPtr
> >::iterator jend
= transmitList
.end();
96 while (j
!= jend
&& notDone
) {
97 PacketPtr target
= j
->second
;
98 // If the target contains data, and it overlaps the
99 // probed request, need to update data
100 if (target
->intersect(pkt
)) {
101 DPRINTF(Cache
, "Functional %s access to blk_addr %x intersects a response\n",
102 pkt
->cmdString(), pkt
->getAddr() & ~(cache
->getBlockSize() - 1));
103 notDone
= fixDelayedResponsePacket(pkt
, target
);
111 BaseCache::CachePort::checkAndSendFunctional(PacketPtr pkt
)
113 bool notDone
= checkFunctional(pkt
);
119 BaseCache::CachePort::recvRetry()
122 assert(waitingOnRetry
);
123 if (!drainList
.empty()) {
124 DPRINTF(CachePort
, "%s attempting to send a retry for response (%i waiting)\n"
125 , name(), drainList
.size());
126 //We have some responses to drain first
127 pkt
= drainList
.front();
128 drainList
.pop_front();
129 if (sendTiming(pkt
)) {
130 DPRINTF(CachePort
, "%s sucessful in sending a retry for"
131 "response (%i still waiting)\n", name(), drainList
.size());
132 if (!drainList
.empty() ||
133 !isCpuSide
&& cache
->doMasterRequest() ||
134 isCpuSide
&& cache
->doSlaveRequest()) {
136 DPRINTF(CachePort
, "%s has more responses/requests\n", name());
137 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this, false);
138 reqCpu
->schedule(curTick
+ 1);
140 waitingOnRetry
= false;
143 drainList
.push_front(pkt
);
145 // Check if we're done draining once this list is empty
146 if (drainList
.empty())
151 DPRINTF(CachePort
, "%s attempting to send a retry for MSHR\n", name());
152 if (!cache
->doMasterRequest()) {
153 //This can happen if I am the owner of a block and see an upgrade
154 //while the block was in my WB Buffers. I just remove the
155 //wb and de-assert the masterRequest
156 waitingOnRetry
= false;
159 pkt
= cache
->getPacket();
160 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
161 //Copy the packet, it may be modified/destroyed elsewhere
162 PacketPtr copyPkt
= new Packet(*pkt
);
163 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
166 bool success
= sendTiming(pkt
);
167 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
168 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
170 waitingOnRetry
= !success
;
171 if (waitingOnRetry
) {
172 DPRINTF(CachePort
, "%s now waiting on a retry\n", name());
175 cache
->sendResult(pkt
, mshr
, success
);
177 if (success
&& cache
->doMasterRequest())
179 DPRINTF(CachePort
, "%s has more requests\n", name());
180 //Still more to issue, rerequest in 1 cycle
181 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this, false);
182 reqCpu
->schedule(curTick
+ 1);
187 assert(cache
->doSlaveRequest());
188 //pkt = cache->getCoherencePacket();
189 //We save the packet, no reordering on CSHRS
190 pkt
= cache
->getCoherencePacket();
191 MSHR
* cshr
= (MSHR
*)pkt
->senderState
;
192 bool success
= sendTiming(pkt
);
193 cache
->sendCoherenceResult(pkt
, cshr
, success
);
194 waitingOnRetry
= !success
;
195 if (success
&& cache
->doSlaveRequest())
197 DPRINTF(CachePort
, "%s has more requests\n", name());
198 //Still more to issue, rerequest in 1 cycle
199 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this, false);
200 reqCpu
->schedule(curTick
+ 1);
203 if (waitingOnRetry
) DPRINTF(CachePort
, "%s STILL Waiting on retry\n", name());
204 else DPRINTF(CachePort
, "%s no longer waiting on retry\n", name());
208 BaseCache::CachePort::setBlocked()
211 DPRINTF(Cache
, "Cache Blocking\n");
213 //Clear the retry flag
214 mustSendRetry
= false;
218 BaseCache::CachePort::clearBlocked()
221 DPRINTF(Cache
, "Cache Unblocking\n");
225 DPRINTF(Cache
, "Cache Sending Retry\n");
226 mustSendRetry
= false;
231 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
, bool _newResponse
)
232 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
),
233 newResponse(_newResponse
)
236 this->setFlags(AutoDelete
);
241 BaseCache::CacheEvent::process()
245 if (cachePort
->waitingOnRetry
) return;
246 //We have some responses to drain first
247 if (!cachePort
->drainList
.empty()) {
248 DPRINTF(CachePort
, "%s trying to drain a response\n", cachePort
->name());
249 if (cachePort
->sendTiming(cachePort
->drainList
.front())) {
250 DPRINTF(CachePort
, "%s drains a response succesfully\n", cachePort
->name());
251 cachePort
->drainList
.pop_front();
252 if (!cachePort
->drainList
.empty() ||
253 !cachePort
->isCpuSide
&& cachePort
->cache
->doMasterRequest() ||
254 cachePort
->isCpuSide
&& cachePort
->cache
->doSlaveRequest()) {
256 DPRINTF(CachePort
, "%s still has outstanding bus reqs\n", cachePort
->name());
257 this->schedule(curTick
+ 1);
261 cachePort
->waitingOnRetry
= true;
262 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
265 else if (!cachePort
->isCpuSide
)
267 DPRINTF(CachePort
, "%s trying to send a MSHR request\n", cachePort
->name());
268 if (!cachePort
->cache
->doMasterRequest()) {
269 //This can happen if I am the owner of a block and see an upgrade
270 //while the block was in my WB Buffers. I just remove the
271 //wb and de-assert the masterRequest
275 pkt
= cachePort
->cache
->getPacket();
276 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
277 //Copy the packet, it may be modified/destroyed elsewhere
278 PacketPtr copyPkt
= new Packet(*pkt
);
279 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
282 bool success
= cachePort
->sendTiming(pkt
);
283 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
284 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
286 cachePort
->waitingOnRetry
= !success
;
287 if (cachePort
->waitingOnRetry
) {
288 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
291 cachePort
->cache
->sendResult(pkt
, mshr
, success
);
292 if (success
&& cachePort
->cache
->doMasterRequest())
294 DPRINTF(CachePort
, "%s still more MSHR requests to send\n",
296 //Still more to issue, rerequest in 1 cycle
298 this->schedule(curTick
+1);
304 assert(cachePort
->cache
->doSlaveRequest());
305 pkt
= cachePort
->cache
->getCoherencePacket();
306 MSHR
* cshr
= (MSHR
*) pkt
->senderState
;
307 bool success
= cachePort
->sendTiming(pkt
);
308 cachePort
->cache
->sendCoherenceResult(pkt
, cshr
, success
);
309 cachePort
->waitingOnRetry
= !success
;
310 if (cachePort
->waitingOnRetry
)
311 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
312 if (success
&& cachePort
->cache
->doSlaveRequest())
314 DPRINTF(CachePort
, "%s still more CSHR requests to send\n",
316 //Still more to issue, rerequest in 1 cycle
318 this->schedule(curTick
+1);
323 //Else it's a response
324 assert(cachePort
->transmitList
.size());
325 assert(cachePort
->transmitList
.front().first
<= curTick
);
326 pkt
= cachePort
->transmitList
.front().second
;
327 cachePort
->transmitList
.pop_front();
328 if (!cachePort
->transmitList
.empty()) {
329 Tick time
= cachePort
->transmitList
.front().first
;
330 schedule(time
<= curTick
? curTick
+1 : time
);
333 if (pkt
->flags
& NACKED_LINE
)
334 pkt
->result
= Packet::Nacked
;
336 pkt
->result
= Packet::Success
;
337 pkt
->makeTimingResponse();
338 DPRINTF(CachePort
, "%s attempting to send a response\n", cachePort
->name());
339 if (!cachePort
->drainList
.empty() || cachePort
->waitingOnRetry
) {
340 //Already have a list, just append
341 cachePort
->drainList
.push_back(pkt
);
342 DPRINTF(CachePort
, "%s appending response onto drain list\n", cachePort
->name());
344 else if (!cachePort
->sendTiming(pkt
)) {
345 //It failed, save it to list of drain events
346 DPRINTF(CachePort
, "%s now waiting for a retry\n", cachePort
->name());
347 cachePort
->drainList
.push_back(pkt
);
348 cachePort
->waitingOnRetry
= true;
351 // Check if we're done draining once this list is empty
352 if (cachePort
->drainList
.empty() && cachePort
->transmitList
.empty())
353 cachePort
->cache
->checkDrain();
357 BaseCache::CacheEvent::description()
359 return "BaseCache timing event";
365 if (!cpuSidePort
|| !memSidePort
)
366 panic("Cache not hooked up on both sides\n");
367 cpuSidePort
->sendStatusChange(Port::RangeChange
);
371 BaseCache::regStats()
373 using namespace Stats
;
376 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
377 MemCmd
cmd(access_idx
);
378 const string
&cstr
= cmd
.toString();
381 .init(maxThreadsPerCPU
)
382 .name(name() + "." + cstr
+ "_hits")
383 .desc("number of " + cstr
+ " hits")
384 .flags(total
| nozero
| nonan
)
389 .name(name() + ".demand_hits")
390 .desc("number of demand (read+write) hits")
393 demandHits
= hits
[MemCmd::ReadReq
] + hits
[MemCmd::WriteReq
];
396 .name(name() + ".overall_hits")
397 .desc("number of overall hits")
400 overallHits
= demandHits
+ hits
[MemCmd::SoftPFReq
] + hits
[MemCmd::HardPFReq
]
401 + hits
[MemCmd::Writeback
];
404 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
405 MemCmd
cmd(access_idx
);
406 const string
&cstr
= cmd
.toString();
409 .init(maxThreadsPerCPU
)
410 .name(name() + "." + cstr
+ "_misses")
411 .desc("number of " + cstr
+ " misses")
412 .flags(total
| nozero
| nonan
)
417 .name(name() + ".demand_misses")
418 .desc("number of demand (read+write) misses")
421 demandMisses
= misses
[MemCmd::ReadReq
] + misses
[MemCmd::WriteReq
];
424 .name(name() + ".overall_misses")
425 .desc("number of overall misses")
428 overallMisses
= demandMisses
+ misses
[MemCmd::SoftPFReq
] +
429 misses
[MemCmd::HardPFReq
] + misses
[MemCmd::Writeback
];
431 // Miss latency statistics
432 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
433 MemCmd
cmd(access_idx
);
434 const string
&cstr
= cmd
.toString();
436 missLatency
[access_idx
]
437 .init(maxThreadsPerCPU
)
438 .name(name() + "." + cstr
+ "_miss_latency")
439 .desc("number of " + cstr
+ " miss cycles")
440 .flags(total
| nozero
| nonan
)
445 .name(name() + ".demand_miss_latency")
446 .desc("number of demand (read+write) miss cycles")
449 demandMissLatency
= missLatency
[MemCmd::ReadReq
] + missLatency
[MemCmd::WriteReq
];
452 .name(name() + ".overall_miss_latency")
453 .desc("number of overall miss cycles")
456 overallMissLatency
= demandMissLatency
+ missLatency
[MemCmd::SoftPFReq
] +
457 missLatency
[MemCmd::HardPFReq
];
460 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
461 MemCmd
cmd(access_idx
);
462 const string
&cstr
= cmd
.toString();
465 .name(name() + "." + cstr
+ "_accesses")
466 .desc("number of " + cstr
+ " accesses(hits+misses)")
467 .flags(total
| nozero
| nonan
)
470 accesses
[access_idx
] = hits
[access_idx
] + misses
[access_idx
];
474 .name(name() + ".demand_accesses")
475 .desc("number of demand (read+write) accesses")
478 demandAccesses
= demandHits
+ demandMisses
;
481 .name(name() + ".overall_accesses")
482 .desc("number of overall (read+write) accesses")
485 overallAccesses
= overallHits
+ overallMisses
;
487 // miss rate formulas
488 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
489 MemCmd
cmd(access_idx
);
490 const string
&cstr
= cmd
.toString();
493 .name(name() + "." + cstr
+ "_miss_rate")
494 .desc("miss rate for " + cstr
+ " accesses")
495 .flags(total
| nozero
| nonan
)
498 missRate
[access_idx
] = misses
[access_idx
] / accesses
[access_idx
];
502 .name(name() + ".demand_miss_rate")
503 .desc("miss rate for demand accesses")
506 demandMissRate
= demandMisses
/ demandAccesses
;
509 .name(name() + ".overall_miss_rate")
510 .desc("miss rate for overall accesses")
513 overallMissRate
= overallMisses
/ overallAccesses
;
515 // miss latency formulas
516 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
517 MemCmd
cmd(access_idx
);
518 const string
&cstr
= cmd
.toString();
520 avgMissLatency
[access_idx
]
521 .name(name() + "." + cstr
+ "_avg_miss_latency")
522 .desc("average " + cstr
+ " miss latency")
523 .flags(total
| nozero
| nonan
)
526 avgMissLatency
[access_idx
] =
527 missLatency
[access_idx
] / misses
[access_idx
];
531 .name(name() + ".demand_avg_miss_latency")
532 .desc("average overall miss latency")
535 demandAvgMissLatency
= demandMissLatency
/ demandMisses
;
537 overallAvgMissLatency
538 .name(name() + ".overall_avg_miss_latency")
539 .desc("average overall miss latency")
542 overallAvgMissLatency
= overallMissLatency
/ overallMisses
;
544 blocked_cycles
.init(NUM_BLOCKED_CAUSES
);
546 .name(name() + ".blocked_cycles")
547 .desc("number of cycles access was blocked")
548 .subname(Blocked_NoMSHRs
, "no_mshrs")
549 .subname(Blocked_NoTargets
, "no_targets")
553 blocked_causes
.init(NUM_BLOCKED_CAUSES
);
555 .name(name() + ".blocked")
556 .desc("number of cycles access was blocked")
557 .subname(Blocked_NoMSHRs
, "no_mshrs")
558 .subname(Blocked_NoTargets
, "no_targets")
562 .name(name() + ".avg_blocked_cycles")
563 .desc("average number of cycles each access was blocked")
564 .subname(Blocked_NoMSHRs
, "no_mshrs")
565 .subname(Blocked_NoTargets
, "no_targets")
568 avg_blocked
= blocked_cycles
/ blocked_causes
;
571 .name(name() + ".fast_writes")
572 .desc("number of fast writes performed")
576 .name(name() + ".cache_copies")
577 .desc("number of cache copies performed")
583 BaseCache::drain(Event
*de
)
589 changeState(SimObject::Draining
);
593 changeState(SimObject::Drained
);