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 "mem/cache/base_cache.hh"
37 #include "mem/cache/miss/mshr.hh"
38 #include "mem/packet_impl.hh"
40 #include "cpu/base.hh"
44 BaseCache::CachePort::CachePort(const std::string
&_name
, BaseCache
*_cache
,
46 : Port(_name
), cache(_cache
), isCpuSide(_isCpuSide
)
49 waitingOnRetry
= false;
50 //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::recvTiming(Packet
*pkt
)
78 && !pkt
->req
->isUncacheable()
79 && pkt
->isInvalidate()
80 && !pkt
->isRead() && !pkt
->isWrite()) {
81 //Upgrade or Invalidate
82 //Look into what happens if two slave caches on bus
83 DPRINTF(Cache
, "%s %x ? blk_addr: %x\n", pkt
->cmdString(),
84 pkt
->getAddr() & (((ULL(1))<<48)-1),
85 pkt
->getAddr() & ~((Addr
)cache
->blkSize
- 1));
87 assert(!(pkt
->flags
& SATISFIED
));
88 pkt
->flags
|= SATISFIED
;
89 //Invalidates/Upgrades need no response if they get the bus
93 if (pkt
->isRequest() && blocked
)
95 DPRINTF(Cache
,"Scheduling a retry while blocked\n");
99 return cache
->doTimingAccess(pkt
, this, isCpuSide
);
103 BaseCache::CachePort::recvAtomic(Packet
*pkt
)
105 return cache
->doAtomicAccess(pkt
, isCpuSide
);
109 BaseCache::CachePort::recvFunctional(Packet
*pkt
)
111 //Check storage here first
112 list
<Packet
*>::iterator i
= drainList
.begin();
113 list
<Packet
*>::iterator end
= drainList
.end();
114 for (; i
!= end
; ++i
) {
115 Packet
* target
= *i
;
116 // If the target contains data, and it overlaps the
117 // probed request, need to update data
118 if (target
->intersect(pkt
)) {
119 fixPacket(pkt
, target
);
122 cache
->doFunctionalAccess(pkt
, isCpuSide
);
126 BaseCache::CachePort::recvRetry()
129 assert(waitingOnRetry
);
130 if (!drainList
.empty()) {
131 DPRINTF(CachePort
, "%s attempting to send a retry for response\n", name());
132 //We have some responses to drain first
133 if (sendTiming(drainList
.front())) {
134 DPRINTF(CachePort
, "%s sucessful in sending a retry for response\n", name());
135 drainList
.pop_front();
136 if (!drainList
.empty() ||
137 !isCpuSide
&& cache
->doMasterRequest() ||
138 isCpuSide
&& cache
->doSlaveRequest()) {
140 DPRINTF(CachePort
, "%s has more responses/requests\n", name());
141 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
142 reqCpu
->schedule(curTick
+ 1);
144 waitingOnRetry
= false;
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 Packet
* 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 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
180 reqCpu
->schedule(curTick
+ 1);
185 assert(cache
->doSlaveRequest());
186 //pkt = cache->getCoherencePacket();
187 //We save the packet, no reordering on CSHRS
188 pkt
= cache
->getCoherencePacket();
189 MSHR
* cshr
= (MSHR
*)pkt
->senderState
;
190 bool success
= sendTiming(pkt
);
191 cache
->sendCoherenceResult(pkt
, cshr
, success
);
192 waitingOnRetry
= !success
;
193 if (success
&& cache
->doSlaveRequest())
195 DPRINTF(CachePort
, "%s has more requests\n", name());
196 //Still more to issue, rerequest in 1 cycle
197 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
198 reqCpu
->schedule(curTick
+ 1);
201 if (waitingOnRetry
) DPRINTF(CachePort
, "%s STILL Waiting on retry\n", name());
202 else DPRINTF(CachePort
, "%s no longer waiting on retry\n", name());
206 BaseCache::CachePort::setBlocked()
209 DPRINTF(Cache
, "Cache Blocking\n");
211 //Clear the retry flag
212 mustSendRetry
= false;
216 BaseCache::CachePort::clearBlocked()
219 DPRINTF(Cache
, "Cache Unblocking\n");
223 DPRINTF(Cache
, "Cache Sending Retry\n");
224 mustSendRetry
= false;
229 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
)
230 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
)
232 this->setFlags(AutoDelete
);
236 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
, Packet
*_pkt
)
237 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
), pkt(_pkt
)
239 this->setFlags(AutoDelete
);
243 BaseCache::CacheEvent::process()
247 if (cachePort
->waitingOnRetry
) return;
248 //We have some responses to drain first
249 if (!cachePort
->drainList
.empty()) {
250 DPRINTF(CachePort
, "%s trying to drain a response\n", cachePort
->name());
251 if (cachePort
->sendTiming(cachePort
->drainList
.front())) {
252 DPRINTF(CachePort
, "%s drains a response succesfully\n", cachePort
->name());
253 cachePort
->drainList
.pop_front();
254 if (!cachePort
->drainList
.empty() ||
255 !cachePort
->isCpuSide
&& cachePort
->cache
->doMasterRequest() ||
256 cachePort
->isCpuSide
&& cachePort
->cache
->doSlaveRequest()) {
258 DPRINTF(CachePort
, "%s still has outstanding bus reqs\n", cachePort
->name());
259 this->schedule(curTick
+ 1);
263 cachePort
->waitingOnRetry
= true;
264 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
267 else if (!cachePort
->isCpuSide
)
269 DPRINTF(CachePort
, "%s trying to send a MSHR request\n", cachePort
->name());
270 if (!cachePort
->cache
->doMasterRequest()) {
271 //This can happen if I am the owner of a block and see an upgrade
272 //while the block was in my WB Buffers. I just remove the
273 //wb and de-assert the masterRequest
277 pkt
= cachePort
->cache
->getPacket();
278 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
279 //Copy the packet, it may be modified/destroyed elsewhere
280 Packet
* copyPkt
= new Packet(*pkt
);
281 copyPkt
->dataStatic
<uint8_t>(pkt
->getPtr
<uint8_t>());
284 bool success
= cachePort
->sendTiming(pkt
);
285 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
286 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
288 cachePort
->waitingOnRetry
= !success
;
289 if (cachePort
->waitingOnRetry
) {
290 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
293 cachePort
->cache
->sendResult(pkt
, mshr
, success
);
294 if (success
&& cachePort
->cache
->doMasterRequest())
296 DPRINTF(CachePort
, "%s still more MSHR requests to send\n",
298 //Still more to issue, rerequest in 1 cycle
300 this->schedule(curTick
+1);
306 assert(cachePort
->cache
->doSlaveRequest());
307 pkt
= cachePort
->cache
->getCoherencePacket();
308 MSHR
* cshr
= (MSHR
*) pkt
->senderState
;
309 bool success
= cachePort
->sendTiming(pkt
);
310 cachePort
->cache
->sendCoherenceResult(pkt
, cshr
, success
);
311 cachePort
->waitingOnRetry
= !success
;
312 if (cachePort
->waitingOnRetry
)
313 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
314 if (success
&& cachePort
->cache
->doSlaveRequest())
316 DPRINTF(CachePort
, "%s still more CSHR requests to send\n",
318 //Still more to issue, rerequest in 1 cycle
320 this->schedule(curTick
+1);
326 //Know the packet to send
327 if (pkt
->flags
& NACKED_LINE
)
328 pkt
->result
= Packet::Nacked
;
330 pkt
->result
= Packet::Success
;
331 pkt
->makeTimingResponse();
332 DPRINTF(CachePort
, "%s attempting to send a response\n", cachePort
->name());
333 if (!cachePort
->drainList
.empty() || cachePort
->waitingOnRetry
) {
334 //Already have a list, just append
335 cachePort
->drainList
.push_back(pkt
);
336 DPRINTF(CachePort
, "%s appending response onto drain list\n", cachePort
->name());
338 else if (!cachePort
->sendTiming(pkt
)) {
339 //It failed, save it to list of drain events
340 DPRINTF(CachePort
, "%s now waiting for a retry\n", cachePort
->name());
341 cachePort
->drainList
.push_back(pkt
);
342 cachePort
->waitingOnRetry
= true;
347 BaseCache::CacheEvent::description()
349 return "timing event\n";
353 BaseCache::getPort(const std::string
&if_name
, int idx
)
357 if(cpuSidePort
== NULL
)
358 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
361 else if (if_name
== "functional")
363 if(cpuSidePort
== NULL
)
364 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
367 else if (if_name
== "cpu_side")
369 if(cpuSidePort
== NULL
)
370 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
373 else if (if_name
== "mem_side")
375 if (memSidePort
!= NULL
)
376 panic("Already have a mem side for this cache\n");
377 memSidePort
= new CachePort(name() + "-mem_side_port", this, false);
380 else panic("Port name %s unrecognized\n", if_name
);
386 if (!cpuSidePort
|| !memSidePort
)
387 panic("Cache not hooked up on both sides\n");
388 cpuSidePort
->sendStatusChange(Port::RangeChange
);
392 BaseCache::regStats()
394 Request
temp_req((Addr
) NULL
, 4, 0);
395 Packet::Command temp_cmd
= Packet::ReadReq
;
396 Packet
temp_pkt(&temp_req
, temp_cmd
, 0); //@todo FIx command strings so this isn't neccessary
397 temp_pkt
.allocate(); //Temp allocate, all need data
399 using namespace Stats
;
402 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
403 Packet::Command cmd
= (Packet::Command
)access_idx
;
404 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
407 .init(maxThreadsPerCPU
)
408 .name(name() + "." + cstr
+ "_hits")
409 .desc("number of " + cstr
+ " hits")
410 .flags(total
| nozero
| nonan
)
415 .name(name() + ".demand_hits")
416 .desc("number of demand (read+write) hits")
419 demandHits
= hits
[Packet::ReadReq
] + hits
[Packet::WriteReq
];
422 .name(name() + ".overall_hits")
423 .desc("number of overall hits")
426 overallHits
= demandHits
+ hits
[Packet::SoftPFReq
] + hits
[Packet::HardPFReq
]
427 + hits
[Packet::Writeback
];
430 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
431 Packet::Command cmd
= (Packet::Command
)access_idx
;
432 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
435 .init(maxThreadsPerCPU
)
436 .name(name() + "." + cstr
+ "_misses")
437 .desc("number of " + cstr
+ " misses")
438 .flags(total
| nozero
| nonan
)
443 .name(name() + ".demand_misses")
444 .desc("number of demand (read+write) misses")
447 demandMisses
= misses
[Packet::ReadReq
] + misses
[Packet::WriteReq
];
450 .name(name() + ".overall_misses")
451 .desc("number of overall misses")
454 overallMisses
= demandMisses
+ misses
[Packet::SoftPFReq
] +
455 misses
[Packet::HardPFReq
] + misses
[Packet::Writeback
];
457 // Miss latency statistics
458 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
459 Packet::Command cmd
= (Packet::Command
)access_idx
;
460 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
462 missLatency
[access_idx
]
463 .init(maxThreadsPerCPU
)
464 .name(name() + "." + cstr
+ "_miss_latency")
465 .desc("number of " + cstr
+ " miss cycles")
466 .flags(total
| nozero
| nonan
)
471 .name(name() + ".demand_miss_latency")
472 .desc("number of demand (read+write) miss cycles")
475 demandMissLatency
= missLatency
[Packet::ReadReq
] + missLatency
[Packet::WriteReq
];
478 .name(name() + ".overall_miss_latency")
479 .desc("number of overall miss cycles")
482 overallMissLatency
= demandMissLatency
+ missLatency
[Packet::SoftPFReq
] +
483 missLatency
[Packet::HardPFReq
];
486 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
487 Packet::Command cmd
= (Packet::Command
)access_idx
;
488 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
491 .name(name() + "." + cstr
+ "_accesses")
492 .desc("number of " + cstr
+ " accesses(hits+misses)")
493 .flags(total
| nozero
| nonan
)
496 accesses
[access_idx
] = hits
[access_idx
] + misses
[access_idx
];
500 .name(name() + ".demand_accesses")
501 .desc("number of demand (read+write) accesses")
504 demandAccesses
= demandHits
+ demandMisses
;
507 .name(name() + ".overall_accesses")
508 .desc("number of overall (read+write) accesses")
511 overallAccesses
= overallHits
+ overallMisses
;
513 // miss rate formulas
514 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
515 Packet::Command cmd
= (Packet::Command
)access_idx
;
516 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
519 .name(name() + "." + cstr
+ "_miss_rate")
520 .desc("miss rate for " + cstr
+ " accesses")
521 .flags(total
| nozero
| nonan
)
524 missRate
[access_idx
] = misses
[access_idx
] / accesses
[access_idx
];
528 .name(name() + ".demand_miss_rate")
529 .desc("miss rate for demand accesses")
532 demandMissRate
= demandMisses
/ demandAccesses
;
535 .name(name() + ".overall_miss_rate")
536 .desc("miss rate for overall accesses")
539 overallMissRate
= overallMisses
/ overallAccesses
;
541 // miss latency formulas
542 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
543 Packet::Command cmd
= (Packet::Command
)access_idx
;
544 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
546 avgMissLatency
[access_idx
]
547 .name(name() + "." + cstr
+ "_avg_miss_latency")
548 .desc("average " + cstr
+ " miss latency")
549 .flags(total
| nozero
| nonan
)
552 avgMissLatency
[access_idx
] =
553 missLatency
[access_idx
] / misses
[access_idx
];
557 .name(name() + ".demand_avg_miss_latency")
558 .desc("average overall miss latency")
561 demandAvgMissLatency
= demandMissLatency
/ demandMisses
;
563 overallAvgMissLatency
564 .name(name() + ".overall_avg_miss_latency")
565 .desc("average overall miss latency")
568 overallAvgMissLatency
= overallMissLatency
/ overallMisses
;
570 blocked_cycles
.init(NUM_BLOCKED_CAUSES
);
572 .name(name() + ".blocked_cycles")
573 .desc("number of cycles access was blocked")
574 .subname(Blocked_NoMSHRs
, "no_mshrs")
575 .subname(Blocked_NoTargets
, "no_targets")
579 blocked_causes
.init(NUM_BLOCKED_CAUSES
);
581 .name(name() + ".blocked")
582 .desc("number of cycles access was blocked")
583 .subname(Blocked_NoMSHRs
, "no_mshrs")
584 .subname(Blocked_NoTargets
, "no_targets")
588 .name(name() + ".avg_blocked_cycles")
589 .desc("average number of cycles each access was blocked")
590 .subname(Blocked_NoMSHRs
, "no_mshrs")
591 .subname(Blocked_NoTargets
, "no_targets")
594 avg_blocked
= blocked_cycles
/ blocked_causes
;
597 .name(name() + ".fast_writes")
598 .desc("number of fast writes performed")
602 .name(name() + ".cache_copies")
603 .desc("number of cache copies performed")