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"
38 #include "cpu/base.hh"
42 BaseCache::CachePort::CachePort(const std::string
&_name
, BaseCache
*_cache
,
44 : Port(_name
), cache(_cache
), isCpuSide(_isCpuSide
)
48 waitingOnRetry
= false;
49 //Start ports at null if more than one is created we should panic
55 BaseCache::CachePort::recvStatusChange(Port::Status status
)
57 cache
->recvStatusChange(status
, isCpuSide
);
61 BaseCache::CachePort::getDeviceAddressRanges(AddrRangeList
&resp
,
64 cache
->getAddressRanges(resp
, snoop
, isCpuSide
);
68 BaseCache::CachePort::deviceBlockSize()
70 return cache
->getBlockSize();
74 BaseCache::CachePort::recvTiming(Packet
*pkt
)
77 && !pkt
->req
->isUncacheable()
78 && pkt
->isInvalidate()
79 && !pkt
->isRead() && !pkt
->isWrite()) {
80 //Upgrade or Invalidate
81 //Look into what happens if two slave caches on bus
82 DPRINTF(Cache
, "%s %x ? blk_addr: %x\n", pkt
->cmdString(),
83 pkt
->getAddr() & (((ULL(1))<<48)-1),
84 pkt
->getAddr() & ~((Addr
)cache
->blkSize
- 1));
86 assert(!(pkt
->flags
& SATISFIED
));
87 pkt
->flags
|= SATISFIED
;
88 //Invalidates/Upgrades need no response if they get the bus
92 if (pkt
->isRequest() && blocked
)
94 DPRINTF(Cache
,"Scheduling a retry while blocked\n");
98 return cache
->doTimingAccess(pkt
, this, isCpuSide
);
102 BaseCache::CachePort::recvAtomic(Packet
*pkt
)
104 return cache
->doAtomicAccess(pkt
, isCpuSide
);
108 BaseCache::CachePort::recvFunctional(Packet
*pkt
)
110 cache
->doFunctionalAccess(pkt
, isCpuSide
);
114 BaseCache::CachePort::recvRetry()
117 assert(waitingOnRetry
);
118 if (!drainList
.empty()) {
119 DPRINTF(CachePort
, "%s attempting to send a retry for response\n", name());
120 //We have some responses to drain first
121 if (sendTiming(drainList
.front())) {
122 DPRINTF(CachePort
, "%s sucessful in sending a retry for response\n", name());
123 drainList
.pop_front();
124 if (!drainList
.empty() ||
125 !isCpuSide
&& cache
->doMasterRequest() ||
126 isCpuSide
&& cache
->doSlaveRequest()) {
128 DPRINTF(CachePort
, "%s has more responses/requests\n", name());
129 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
130 reqCpu
->schedule(curTick
+ 1);
132 waitingOnRetry
= false;
137 DPRINTF(CachePort
, "%s attempting to send a retry for MSHR\n", name());
138 if (!cache
->doMasterRequest()) {
139 //This can happen if I am the owner of a block and see an upgrade
140 //while the block was in my WB Buffers. I just remove the
141 //wb and de-assert the masterRequest
142 waitingOnRetry
= false;
145 pkt
= cache
->getPacket();
146 MSHR
* mshr
= (MSHR
*)pkt
->senderState
;
147 bool success
= sendTiming(pkt
);
148 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
149 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
150 cache
->sendResult(pkt
, mshr
, success
);
151 waitingOnRetry
= !success
;
152 if (success
&& cache
->doMasterRequest())
154 DPRINTF(CachePort
, "%s has more requests\n", name());
155 //Still more to issue, rerequest in 1 cycle
157 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
158 reqCpu
->schedule(curTick
+ 1);
164 //pkt = cache->getCoherencePacket();
165 //We save the packet, no reordering on CSHRS
167 bool success
= sendTiming(pkt
);
168 waitingOnRetry
= !success
;
169 if (success
&& cache
->doSlaveRequest())
171 //Still more to issue, rerequest in 1 cycle
173 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
174 reqCpu
->schedule(curTick
+ 1);
178 if (waitingOnRetry
) DPRINTF(CachePort
, "%s STILL Waiting on retry\n", name());
179 else DPRINTF(CachePort
, "%s no longer waiting on retry\n", name());
183 BaseCache::CachePort::setBlocked()
186 DPRINTF(Cache
, "Cache Blocking\n");
188 //Clear the retry flag
189 mustSendRetry
= false;
193 BaseCache::CachePort::clearBlocked()
196 DPRINTF(Cache
, "Cache Unblocking\n");
200 DPRINTF(Cache
, "Cache Sending Retry\n");
201 mustSendRetry
= false;
206 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
)
207 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
)
209 this->setFlags(AutoDelete
);
213 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
, Packet
*_pkt
)
214 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
), pkt(_pkt
)
216 this->setFlags(AutoDelete
);
220 BaseCache::CacheEvent::process()
224 if (cachePort
->waitingOnRetry
) return;
225 //We have some responses to drain first
226 if (!cachePort
->drainList
.empty()) {
227 DPRINTF(CachePort
, "%s trying to drain a response\n", cachePort
->name());
228 if (cachePort
->sendTiming(cachePort
->drainList
.front())) {
229 DPRINTF(CachePort
, "%s drains a response succesfully\n", cachePort
->name());
230 cachePort
->drainList
.pop_front();
231 if (!cachePort
->drainList
.empty() ||
232 !cachePort
->isCpuSide
&& cachePort
->cache
->doMasterRequest() ||
233 cachePort
->isCpuSide
&& cachePort
->cache
->doSlaveRequest()) {
235 DPRINTF(CachePort
, "%s still has outstanding bus reqs\n", cachePort
->name());
236 this->schedule(curTick
+ 1);
240 cachePort
->waitingOnRetry
= true;
241 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
244 else if (!cachePort
->isCpuSide
)
246 DPRINTF(CachePort
, "%s trying to send a MSHR request\n", cachePort
->name());
247 if (!cachePort
->cache
->doMasterRequest()) {
248 //This can happen if I am the owner of a block and see an upgrade
249 //while the block was in my WB Buffers. I just remove the
250 //wb and de-assert the masterRequest
254 pkt
= cachePort
->cache
->getPacket();
255 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
256 bool success
= cachePort
->sendTiming(pkt
);
257 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
258 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
259 cachePort
->cache
->sendResult(pkt
, mshr
, success
);
260 cachePort
->waitingOnRetry
= !success
;
261 if (cachePort
->waitingOnRetry
) DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
262 if (success
&& cachePort
->cache
->doMasterRequest())
264 DPRINTF(CachePort
, "%s still more MSHR requests to send\n", cachePort
->name());
265 //Still more to issue, rerequest in 1 cycle
267 this->schedule(curTick
+1);
272 assert(cachePort
->cache
->doSlaveRequest());
274 pkt
= cachePort
->cache
->getCoherencePacket();
275 bool success
= cachePort
->sendTiming(pkt
);
277 //Need to send on a retry
278 cachePort
->cshrRetry
= pkt
;
279 cachePort
->waitingOnRetry
= true;
281 else if (cachePort
->cache
->doSlaveRequest())
283 //Still more to issue, rerequest in 1 cycle
285 this->schedule(curTick
+1);
291 //Know the packet to send
292 if (pkt
->flags
& NACKED_LINE
)
293 pkt
->result
= Packet::Nacked
;
295 pkt
->result
= Packet::Success
;
296 pkt
->makeTimingResponse();
297 DPRINTF(CachePort
, "%s attempting to send a response\n", cachePort
->name());
298 if (!cachePort
->drainList
.empty() || cachePort
->waitingOnRetry
) {
299 //Already have a list, just append
300 cachePort
->drainList
.push_back(pkt
);
301 DPRINTF(CachePort
, "%s appending response onto drain list\n", cachePort
->name());
303 else if (!cachePort
->sendTiming(pkt
)) {
304 //It failed, save it to list of drain events
305 DPRINTF(CachePort
, "%s now waiting for a retry\n", cachePort
->name());
306 cachePort
->drainList
.push_back(pkt
);
307 cachePort
->waitingOnRetry
= true;
312 BaseCache::CacheEvent::description()
314 return "timing event\n";
318 BaseCache::getPort(const std::string
&if_name
, int idx
)
322 if(cpuSidePort
== NULL
)
323 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
326 else if (if_name
== "functional")
328 if(cpuSidePort
== NULL
)
329 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
332 else if (if_name
== "cpu_side")
334 if(cpuSidePort
== NULL
)
335 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
338 else if (if_name
== "mem_side")
340 if (memSidePort
!= NULL
)
341 panic("Already have a mem side for this cache\n");
342 memSidePort
= new CachePort(name() + "-mem_side_port", this, false);
345 else panic("Port name %s unrecognized\n", if_name
);
351 if (!cpuSidePort
|| !memSidePort
)
352 panic("Cache not hooked up on both sides\n");
353 cpuSidePort
->sendStatusChange(Port::RangeChange
);
357 BaseCache::regStats()
359 Request
temp_req((Addr
) NULL
, 4, 0);
360 Packet::Command temp_cmd
= Packet::ReadReq
;
361 Packet
temp_pkt(&temp_req
, temp_cmd
, 0); //@todo FIx command strings so this isn't neccessary
362 temp_pkt
.allocate(); //Temp allocate, all need data
364 using namespace Stats
;
367 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
368 Packet::Command cmd
= (Packet::Command
)access_idx
;
369 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
372 .init(maxThreadsPerCPU
)
373 .name(name() + "." + cstr
+ "_hits")
374 .desc("number of " + cstr
+ " hits")
375 .flags(total
| nozero
| nonan
)
380 .name(name() + ".demand_hits")
381 .desc("number of demand (read+write) hits")
384 demandHits
= hits
[Packet::ReadReq
] + hits
[Packet::WriteReq
];
387 .name(name() + ".overall_hits")
388 .desc("number of overall hits")
391 overallHits
= demandHits
+ hits
[Packet::SoftPFReq
] + hits
[Packet::HardPFReq
]
392 + hits
[Packet::Writeback
];
395 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
396 Packet::Command cmd
= (Packet::Command
)access_idx
;
397 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
400 .init(maxThreadsPerCPU
)
401 .name(name() + "." + cstr
+ "_misses")
402 .desc("number of " + cstr
+ " misses")
403 .flags(total
| nozero
| nonan
)
408 .name(name() + ".demand_misses")
409 .desc("number of demand (read+write) misses")
412 demandMisses
= misses
[Packet::ReadReq
] + misses
[Packet::WriteReq
];
415 .name(name() + ".overall_misses")
416 .desc("number of overall misses")
419 overallMisses
= demandMisses
+ misses
[Packet::SoftPFReq
] +
420 misses
[Packet::HardPFReq
] + misses
[Packet::Writeback
];
422 // Miss latency statistics
423 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
424 Packet::Command cmd
= (Packet::Command
)access_idx
;
425 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
427 missLatency
[access_idx
]
428 .init(maxThreadsPerCPU
)
429 .name(name() + "." + cstr
+ "_miss_latency")
430 .desc("number of " + cstr
+ " miss cycles")
431 .flags(total
| nozero
| nonan
)
436 .name(name() + ".demand_miss_latency")
437 .desc("number of demand (read+write) miss cycles")
440 demandMissLatency
= missLatency
[Packet::ReadReq
] + missLatency
[Packet::WriteReq
];
443 .name(name() + ".overall_miss_latency")
444 .desc("number of overall miss cycles")
447 overallMissLatency
= demandMissLatency
+ missLatency
[Packet::SoftPFReq
] +
448 missLatency
[Packet::HardPFReq
];
451 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
452 Packet::Command cmd
= (Packet::Command
)access_idx
;
453 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
456 .name(name() + "." + cstr
+ "_accesses")
457 .desc("number of " + cstr
+ " accesses(hits+misses)")
458 .flags(total
| nozero
| nonan
)
461 accesses
[access_idx
] = hits
[access_idx
] + misses
[access_idx
];
465 .name(name() + ".demand_accesses")
466 .desc("number of demand (read+write) accesses")
469 demandAccesses
= demandHits
+ demandMisses
;
472 .name(name() + ".overall_accesses")
473 .desc("number of overall (read+write) accesses")
476 overallAccesses
= overallHits
+ overallMisses
;
478 // miss rate formulas
479 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
480 Packet::Command cmd
= (Packet::Command
)access_idx
;
481 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
484 .name(name() + "." + cstr
+ "_miss_rate")
485 .desc("miss rate for " + cstr
+ " accesses")
486 .flags(total
| nozero
| nonan
)
489 missRate
[access_idx
] = misses
[access_idx
] / accesses
[access_idx
];
493 .name(name() + ".demand_miss_rate")
494 .desc("miss rate for demand accesses")
497 demandMissRate
= demandMisses
/ demandAccesses
;
500 .name(name() + ".overall_miss_rate")
501 .desc("miss rate for overall accesses")
504 overallMissRate
= overallMisses
/ overallAccesses
;
506 // miss latency formulas
507 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
508 Packet::Command cmd
= (Packet::Command
)access_idx
;
509 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
511 avgMissLatency
[access_idx
]
512 .name(name() + "." + cstr
+ "_avg_miss_latency")
513 .desc("average " + cstr
+ " miss latency")
514 .flags(total
| nozero
| nonan
)
517 avgMissLatency
[access_idx
] =
518 missLatency
[access_idx
] / misses
[access_idx
];
522 .name(name() + ".demand_avg_miss_latency")
523 .desc("average overall miss latency")
526 demandAvgMissLatency
= demandMissLatency
/ demandMisses
;
528 overallAvgMissLatency
529 .name(name() + ".overall_avg_miss_latency")
530 .desc("average overall miss latency")
533 overallAvgMissLatency
= overallMissLatency
/ overallMisses
;
535 blocked_cycles
.init(NUM_BLOCKED_CAUSES
);
537 .name(name() + ".blocked_cycles")
538 .desc("number of cycles access was blocked")
539 .subname(Blocked_NoMSHRs
, "no_mshrs")
540 .subname(Blocked_NoTargets
, "no_targets")
544 blocked_causes
.init(NUM_BLOCKED_CAUSES
);
546 .name(name() + ".blocked")
547 .desc("number of cycles access was blocked")
548 .subname(Blocked_NoMSHRs
, "no_mshrs")
549 .subname(Blocked_NoTargets
, "no_targets")
553 .name(name() + ".avg_blocked_cycles")
554 .desc("average number of cycles each access was blocked")
555 .subname(Blocked_NoMSHRs
, "no_mshrs")
556 .subname(Blocked_NoTargets
, "no_targets")
559 avg_blocked
= blocked_cycles
/ blocked_causes
;
562 .name(name() + ".fast_writes")
563 .desc("number of fast writes performed")
567 .name(name() + ".cache_copies")
568 .desc("number of cache copies performed")