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
, bool &snoop
)
64 cache
->getAddressRanges(resp
, snoop
, isCpuSide
);
68 BaseCache::CachePort::deviceBlockSize()
70 return cache
->getBlockSize();
74 BaseCache::CachePort::checkFunctional(PacketPtr pkt
)
76 //Check storage here first
77 list
<PacketPtr
>::iterator i
= drainList
.begin();
78 list
<PacketPtr
>::iterator iend
= drainList
.end();
80 while (i
!= iend
&& notDone
) {
81 PacketPtr target
= *i
;
82 // If the target contains data, and it overlaps the
83 // probed request, need to update data
84 if (target
->intersect(pkt
)) {
85 DPRINTF(Cache
, "Functional %s access to blk_addr %x intersects a drain\n",
86 pkt
->cmdString(), pkt
->getAddr() & ~(cache
->getBlockSize() - 1));
87 notDone
= fixPacket(pkt
, target
);
91 //Also check the response not yet ready to be on the list
92 std::list
<std::pair
<Tick
,PacketPtr
> >::iterator j
= transmitList
.begin();
93 std::list
<std::pair
<Tick
,PacketPtr
> >::iterator jend
= transmitList
.end();
95 while (j
!= jend
&& notDone
) {
96 PacketPtr target
= j
->second
;
97 // If the target contains data, and it overlaps the
98 // probed request, need to update data
99 if (target
->intersect(pkt
)) {
100 DPRINTF(Cache
, "Functional %s access to blk_addr %x intersects a response\n",
101 pkt
->cmdString(), pkt
->getAddr() & ~(cache
->getBlockSize() - 1));
102 notDone
= fixDelayedResponsePacket(pkt
, target
);
110 BaseCache::CachePort::checkAndSendFunctional(PacketPtr pkt
)
112 bool notDone
= checkFunctional(pkt
);
118 BaseCache::CachePort::recvRetry()
121 assert(waitingOnRetry
);
122 if (!drainList
.empty()) {
123 DPRINTF(CachePort
, "%s attempting to send a retry for response (%i waiting)\n"
124 , name(), drainList
.size());
125 //We have some responses to drain first
126 pkt
= drainList
.front();
127 drainList
.pop_front();
128 if (sendTiming(pkt
)) {
129 DPRINTF(CachePort
, "%s sucessful in sending a retry for"
130 "response (%i still waiting)\n", name(), drainList
.size());
131 if (!drainList
.empty() ||
132 !isCpuSide
&& cache
->doMasterRequest() ||
133 isCpuSide
&& cache
->doSlaveRequest()) {
135 DPRINTF(CachePort
, "%s has more responses/requests\n", name());
136 new BaseCache::RequestEvent(this, curTick
+ 1);
138 waitingOnRetry
= false;
141 drainList
.push_front(pkt
);
143 // Check if we're done draining once this list is empty
144 if (drainList
.empty())
149 DPRINTF(CachePort
, "%s attempting to send a retry for MSHR\n", name());
150 if (!cache
->doMasterRequest()) {
151 //This can happen if I am the owner of a block and see an upgrade
152 //while the block was in my WB Buffers. I just remove the
153 //wb and de-assert the masterRequest
154 waitingOnRetry
= false;
157 pkt
= cache
->getPacket();
158 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
159 //Copy the packet, it may be modified/destroyed elsewhere
160 PacketPtr copyPkt
= new Packet(*pkt
);
161 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
164 bool success
= sendTiming(pkt
);
165 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
166 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
168 waitingOnRetry
= !success
;
169 if (waitingOnRetry
) {
170 DPRINTF(CachePort
, "%s now waiting on a retry\n", name());
173 cache
->sendResult(pkt
, mshr
, success
);
175 if (success
&& cache
->doMasterRequest())
177 DPRINTF(CachePort
, "%s has more requests\n", name());
178 //Still more to issue, rerequest in 1 cycle
179 new BaseCache::RequestEvent(this, curTick
+ 1);
184 assert(cache
->doSlaveRequest());
185 //pkt = cache->getCoherencePacket();
186 //We save the packet, no reordering on CSHRS
187 pkt
= cache
->getCoherencePacket();
188 MSHR
* cshr
= (MSHR
*)pkt
->senderState
;
189 bool success
= sendTiming(pkt
);
190 cache
->sendCoherenceResult(pkt
, cshr
, success
);
191 waitingOnRetry
= !success
;
192 if (success
&& cache
->doSlaveRequest())
194 DPRINTF(CachePort
, "%s has more requests\n", name());
195 //Still more to issue, rerequest in 1 cycle
196 new BaseCache::RequestEvent(this, curTick
+ 1);
199 if (waitingOnRetry
) DPRINTF(CachePort
, "%s STILL Waiting on retry\n", name());
200 else DPRINTF(CachePort
, "%s no longer waiting on retry\n", name());
204 BaseCache::CachePort::setBlocked()
207 DPRINTF(Cache
, "Cache Blocking\n");
209 //Clear the retry flag
210 mustSendRetry
= false;
214 BaseCache::CachePort::clearBlocked()
217 DPRINTF(Cache
, "Cache Unblocking\n");
221 DPRINTF(Cache
, "Cache Sending Retry\n");
222 mustSendRetry
= false;
227 BaseCache::RequestEvent::RequestEvent(CachePort
*_cachePort
, Tick when
)
228 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
)
230 this->setFlags(AutoDelete
);
235 BaseCache::RequestEvent::process()
237 if (cachePort
->waitingOnRetry
) return;
238 //We have some responses to drain first
239 if (!cachePort
->drainList
.empty()) {
240 DPRINTF(CachePort
, "%s trying to drain a response\n", cachePort
->name());
241 if (cachePort
->sendTiming(cachePort
->drainList
.front())) {
242 DPRINTF(CachePort
, "%s drains a response succesfully\n", cachePort
->name());
243 cachePort
->drainList
.pop_front();
244 if (!cachePort
->drainList
.empty() ||
245 !cachePort
->isCpuSide
&& cachePort
->cache
->doMasterRequest() ||
246 cachePort
->isCpuSide
&& cachePort
->cache
->doSlaveRequest()) {
248 DPRINTF(CachePort
, "%s still has outstanding bus reqs\n", cachePort
->name());
249 this->schedule(curTick
+ 1);
253 cachePort
->waitingOnRetry
= true;
254 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
257 else if (!cachePort
->isCpuSide
)
259 DPRINTF(CachePort
, "%s trying to send a MSHR request\n", cachePort
->name());
260 if (!cachePort
->cache
->doMasterRequest()) {
261 //This can happen if I am the owner of a block and see an upgrade
262 //while the block was in my WB Buffers. I just remove the
263 //wb and de-assert the masterRequest
267 PacketPtr pkt
= cachePort
->cache
->getPacket();
268 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
269 //Copy the packet, it may be modified/destroyed elsewhere
270 PacketPtr copyPkt
= new Packet(*pkt
);
271 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
274 bool success
= cachePort
->sendTiming(pkt
);
275 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
276 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
278 cachePort
->waitingOnRetry
= !success
;
279 if (cachePort
->waitingOnRetry
) {
280 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
283 cachePort
->cache
->sendResult(pkt
, mshr
, success
);
284 if (success
&& cachePort
->cache
->doMasterRequest())
286 DPRINTF(CachePort
, "%s still more MSHR requests to send\n",
288 //Still more to issue, rerequest in 1 cycle
289 this->schedule(curTick
+1);
295 assert(cachePort
->cache
->doSlaveRequest());
296 PacketPtr pkt
= cachePort
->cache
->getCoherencePacket();
297 MSHR
* cshr
= (MSHR
*) pkt
->senderState
;
298 bool success
= cachePort
->sendTiming(pkt
);
299 cachePort
->cache
->sendCoherenceResult(pkt
, cshr
, success
);
300 cachePort
->waitingOnRetry
= !success
;
301 if (cachePort
->waitingOnRetry
)
302 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
303 if (success
&& cachePort
->cache
->doSlaveRequest())
305 DPRINTF(CachePort
, "%s still more CSHR requests to send\n",
307 //Still more to issue, rerequest in 1 cycle
308 this->schedule(curTick
+1);
314 BaseCache::RequestEvent::description()
316 return "Cache request event";
319 BaseCache::ResponseEvent::ResponseEvent(CachePort
*_cachePort
)
320 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
)
325 BaseCache::ResponseEvent::process()
327 assert(cachePort
->transmitList
.size());
328 assert(cachePort
->transmitList
.front().first
<= curTick
);
329 PacketPtr pkt
= cachePort
->transmitList
.front().second
;
330 cachePort
->transmitList
.pop_front();
331 if (!cachePort
->transmitList
.empty()) {
332 Tick time
= cachePort
->transmitList
.front().first
;
333 schedule(time
<= curTick
? curTick
+1 : time
);
336 if (pkt
->flags
& NACKED_LINE
)
337 pkt
->result
= Packet::Nacked
;
339 pkt
->result
= Packet::Success
;
340 pkt
->makeTimingResponse();
341 DPRINTF(CachePort
, "%s attempting to send a response\n", cachePort
->name());
342 if (!cachePort
->drainList
.empty() || cachePort
->waitingOnRetry
) {
343 //Already have a list, just append
344 cachePort
->drainList
.push_back(pkt
);
345 DPRINTF(CachePort
, "%s appending response onto drain list\n", cachePort
->name());
347 else if (!cachePort
->sendTiming(pkt
)) {
348 //It failed, save it to list of drain events
349 DPRINTF(CachePort
, "%s now waiting for a retry\n", cachePort
->name());
350 cachePort
->drainList
.push_back(pkt
);
351 cachePort
->waitingOnRetry
= true;
354 // Check if we're done draining once this list is empty
355 if (cachePort
->drainList
.empty() && cachePort
->transmitList
.empty())
356 cachePort
->cache
->checkDrain();
360 BaseCache::ResponseEvent::description()
362 return "Cache response event";
368 if (!cpuSidePort
|| !memSidePort
)
369 panic("Cache not hooked up on both sides\n");
370 cpuSidePort
->sendStatusChange(Port::RangeChange
);
374 BaseCache::regStats()
376 using namespace Stats
;
379 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
380 MemCmd
cmd(access_idx
);
381 const string
&cstr
= cmd
.toString();
384 .init(maxThreadsPerCPU
)
385 .name(name() + "." + cstr
+ "_hits")
386 .desc("number of " + cstr
+ " hits")
387 .flags(total
| nozero
| nonan
)
392 .name(name() + ".demand_hits")
393 .desc("number of demand (read+write) hits")
396 demandHits
= hits
[MemCmd::ReadReq
] + hits
[MemCmd::WriteReq
];
399 .name(name() + ".overall_hits")
400 .desc("number of overall hits")
403 overallHits
= demandHits
+ hits
[MemCmd::SoftPFReq
] + hits
[MemCmd::HardPFReq
]
404 + hits
[MemCmd::Writeback
];
407 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
408 MemCmd
cmd(access_idx
);
409 const string
&cstr
= cmd
.toString();
412 .init(maxThreadsPerCPU
)
413 .name(name() + "." + cstr
+ "_misses")
414 .desc("number of " + cstr
+ " misses")
415 .flags(total
| nozero
| nonan
)
420 .name(name() + ".demand_misses")
421 .desc("number of demand (read+write) misses")
424 demandMisses
= misses
[MemCmd::ReadReq
] + misses
[MemCmd::WriteReq
];
427 .name(name() + ".overall_misses")
428 .desc("number of overall misses")
431 overallMisses
= demandMisses
+ misses
[MemCmd::SoftPFReq
] +
432 misses
[MemCmd::HardPFReq
] + misses
[MemCmd::Writeback
];
434 // Miss latency statistics
435 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
436 MemCmd
cmd(access_idx
);
437 const string
&cstr
= cmd
.toString();
439 missLatency
[access_idx
]
440 .init(maxThreadsPerCPU
)
441 .name(name() + "." + cstr
+ "_miss_latency")
442 .desc("number of " + cstr
+ " miss cycles")
443 .flags(total
| nozero
| nonan
)
448 .name(name() + ".demand_miss_latency")
449 .desc("number of demand (read+write) miss cycles")
452 demandMissLatency
= missLatency
[MemCmd::ReadReq
] + missLatency
[MemCmd::WriteReq
];
455 .name(name() + ".overall_miss_latency")
456 .desc("number of overall miss cycles")
459 overallMissLatency
= demandMissLatency
+ missLatency
[MemCmd::SoftPFReq
] +
460 missLatency
[MemCmd::HardPFReq
];
463 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
464 MemCmd
cmd(access_idx
);
465 const string
&cstr
= cmd
.toString();
468 .name(name() + "." + cstr
+ "_accesses")
469 .desc("number of " + cstr
+ " accesses(hits+misses)")
470 .flags(total
| nozero
| nonan
)
473 accesses
[access_idx
] = hits
[access_idx
] + misses
[access_idx
];
477 .name(name() + ".demand_accesses")
478 .desc("number of demand (read+write) accesses")
481 demandAccesses
= demandHits
+ demandMisses
;
484 .name(name() + ".overall_accesses")
485 .desc("number of overall (read+write) accesses")
488 overallAccesses
= overallHits
+ overallMisses
;
490 // miss rate formulas
491 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
492 MemCmd
cmd(access_idx
);
493 const string
&cstr
= cmd
.toString();
496 .name(name() + "." + cstr
+ "_miss_rate")
497 .desc("miss rate for " + cstr
+ " accesses")
498 .flags(total
| nozero
| nonan
)
501 missRate
[access_idx
] = misses
[access_idx
] / accesses
[access_idx
];
505 .name(name() + ".demand_miss_rate")
506 .desc("miss rate for demand accesses")
509 demandMissRate
= demandMisses
/ demandAccesses
;
512 .name(name() + ".overall_miss_rate")
513 .desc("miss rate for overall accesses")
516 overallMissRate
= overallMisses
/ overallAccesses
;
518 // miss latency formulas
519 for (int access_idx
= 0; access_idx
< MemCmd::NUM_MEM_CMDS
; ++access_idx
) {
520 MemCmd
cmd(access_idx
);
521 const string
&cstr
= cmd
.toString();
523 avgMissLatency
[access_idx
]
524 .name(name() + "." + cstr
+ "_avg_miss_latency")
525 .desc("average " + cstr
+ " miss latency")
526 .flags(total
| nozero
| nonan
)
529 avgMissLatency
[access_idx
] =
530 missLatency
[access_idx
] / misses
[access_idx
];
534 .name(name() + ".demand_avg_miss_latency")
535 .desc("average overall miss latency")
538 demandAvgMissLatency
= demandMissLatency
/ demandMisses
;
540 overallAvgMissLatency
541 .name(name() + ".overall_avg_miss_latency")
542 .desc("average overall miss latency")
545 overallAvgMissLatency
= overallMissLatency
/ overallMisses
;
547 blocked_cycles
.init(NUM_BLOCKED_CAUSES
);
549 .name(name() + ".blocked_cycles")
550 .desc("number of cycles access was blocked")
551 .subname(Blocked_NoMSHRs
, "no_mshrs")
552 .subname(Blocked_NoTargets
, "no_targets")
556 blocked_causes
.init(NUM_BLOCKED_CAUSES
);
558 .name(name() + ".blocked")
559 .desc("number of cycles access was blocked")
560 .subname(Blocked_NoMSHRs
, "no_mshrs")
561 .subname(Blocked_NoTargets
, "no_targets")
565 .name(name() + ".avg_blocked_cycles")
566 .desc("average number of cycles each access was blocked")
567 .subname(Blocked_NoMSHRs
, "no_mshrs")
568 .subname(Blocked_NoTargets
, "no_targets")
571 avg_blocked
= blocked_cycles
/ blocked_causes
;
574 .name(name() + ".fast_writes")
575 .desc("number of fast writes performed")
579 .name(name() + ".cache_copies")
580 .desc("number of cache copies performed")
586 BaseCache::drain(Event
*de
)
592 changeState(SimObject::Draining
);
596 changeState(SimObject::Drained
);