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 assert(cache
->doMasterRequest());
139 pkt
= cache
->getPacket();
140 MSHR
* mshr
= (MSHR
*)pkt
->senderState
;
141 bool success
= sendTiming(pkt
);
142 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
143 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
144 cache
->sendResult(pkt
, mshr
, success
);
145 waitingOnRetry
= !success
;
146 if (success
&& cache
->doMasterRequest())
148 DPRINTF(CachePort
, "%s has more requests\n", name());
149 //Still more to issue, rerequest in 1 cycle
151 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
152 reqCpu
->schedule(curTick
+ 1);
158 //pkt = cache->getCoherencePacket();
159 //We save the packet, no reordering on CSHRS
161 bool success
= sendTiming(pkt
);
162 waitingOnRetry
= !success
;
163 if (success
&& cache
->doSlaveRequest())
165 //Still more to issue, rerequest in 1 cycle
167 BaseCache::CacheEvent
* reqCpu
= new BaseCache::CacheEvent(this);
168 reqCpu
->schedule(curTick
+ 1);
172 if (waitingOnRetry
) DPRINTF(CachePort
, "%s STILL Waiting on retry\n", name());
173 else DPRINTF(CachePort
, "%s no longer waiting on retry\n", name());
177 BaseCache::CachePort::setBlocked()
180 DPRINTF(Cache
, "Cache Blocking\n");
182 //Clear the retry flag
183 mustSendRetry
= false;
187 BaseCache::CachePort::clearBlocked()
190 DPRINTF(Cache
, "Cache Unblocking\n");
194 DPRINTF(Cache
, "Cache Sending Retry\n");
195 mustSendRetry
= false;
200 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
)
201 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
)
203 this->setFlags(AutoDelete
);
207 BaseCache::CacheEvent::CacheEvent(CachePort
*_cachePort
, Packet
*_pkt
)
208 : Event(&mainEventQueue
, CPU_Tick_Pri
), cachePort(_cachePort
), pkt(_pkt
)
210 this->setFlags(AutoDelete
);
214 BaseCache::CacheEvent::process()
218 if (cachePort
->waitingOnRetry
) return;
219 //We have some responses to drain first
220 if (!cachePort
->drainList
.empty()) {
221 DPRINTF(CachePort
, "%s trying to drain a response\n", cachePort
->name());
222 if (cachePort
->sendTiming(cachePort
->drainList
.front())) {
223 DPRINTF(CachePort
, "%s drains a response succesfully\n", cachePort
->name());
224 cachePort
->drainList
.pop_front();
225 if (!cachePort
->drainList
.empty() ||
226 !cachePort
->isCpuSide
&& cachePort
->cache
->doMasterRequest() ||
227 cachePort
->isCpuSide
&& cachePort
->cache
->doSlaveRequest()) {
229 DPRINTF(CachePort
, "%s still has outstanding bus reqs\n", cachePort
->name());
230 this->schedule(curTick
+ 1);
234 cachePort
->waitingOnRetry
= true;
235 DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
238 else if (!cachePort
->isCpuSide
)
240 DPRINTF(CachePort
, "%s trying to send a MSHR request\n", cachePort
->name());
241 assert(cachePort
->cache
->doMasterRequest());
243 pkt
= cachePort
->cache
->getPacket();
244 MSHR
* mshr
= (MSHR
*) pkt
->senderState
;
245 bool success
= cachePort
->sendTiming(pkt
);
246 DPRINTF(Cache
, "Address %x was %s in sending the timing request\n",
247 pkt
->getAddr(), success
? "succesful" : "unsuccesful");
248 cachePort
->cache
->sendResult(pkt
, mshr
, success
);
249 cachePort
->waitingOnRetry
= !success
;
250 if (cachePort
->waitingOnRetry
) DPRINTF(CachePort
, "%s now waiting on a retry\n", cachePort
->name());
251 if (success
&& cachePort
->cache
->doMasterRequest())
253 DPRINTF(CachePort
, "%s still more MSHR requests to send\n", cachePort
->name());
254 //Still more to issue, rerequest in 1 cycle
256 this->schedule(curTick
+1);
261 assert(cachePort
->cache
->doSlaveRequest());
263 pkt
= cachePort
->cache
->getCoherencePacket();
264 bool success
= cachePort
->sendTiming(pkt
);
266 //Need to send on a retry
267 cachePort
->cshrRetry
= pkt
;
268 cachePort
->waitingOnRetry
= true;
270 else if (cachePort
->cache
->doSlaveRequest())
272 //Still more to issue, rerequest in 1 cycle
274 this->schedule(curTick
+1);
280 //Know the packet to send
281 if (pkt
->flags
& NACKED_LINE
)
282 pkt
->result
= Packet::Nacked
;
284 pkt
->result
= Packet::Success
;
285 pkt
->makeTimingResponse();
286 DPRINTF(CachePort
, "%s attempting to send a response\n", cachePort
->name());
287 if (!cachePort
->drainList
.empty() || cachePort
->waitingOnRetry
) {
288 //Already have a list, just append
289 cachePort
->drainList
.push_back(pkt
);
290 DPRINTF(CachePort
, "%s appending response onto drain list\n", cachePort
->name());
292 else if (!cachePort
->sendTiming(pkt
)) {
293 //It failed, save it to list of drain events
294 DPRINTF(CachePort
, "%s now waiting for a retry\n", cachePort
->name());
295 cachePort
->drainList
.push_back(pkt
);
296 cachePort
->waitingOnRetry
= true;
301 BaseCache::CacheEvent::description()
303 return "timing event\n";
307 BaseCache::getPort(const std::string
&if_name
, int idx
)
311 if(cpuSidePort
== NULL
)
312 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
315 else if (if_name
== "functional")
317 if(cpuSidePort
== NULL
)
318 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
321 else if (if_name
== "cpu_side")
323 if(cpuSidePort
== NULL
)
324 cpuSidePort
= new CachePort(name() + "-cpu_side_port", this, true);
327 else if (if_name
== "mem_side")
329 if (memSidePort
!= NULL
)
330 panic("Already have a mem side for this cache\n");
331 memSidePort
= new CachePort(name() + "-mem_side_port", this, false);
334 else panic("Port name %s unrecognized\n", if_name
);
340 if (!cpuSidePort
|| !memSidePort
)
341 panic("Cache not hooked up on both sides\n");
342 cpuSidePort
->sendStatusChange(Port::RangeChange
);
346 BaseCache::regStats()
348 Request
temp_req((Addr
) NULL
, 4, 0);
349 Packet::Command temp_cmd
= Packet::ReadReq
;
350 Packet
temp_pkt(&temp_req
, temp_cmd
, 0); //@todo FIx command strings so this isn't neccessary
351 temp_pkt
.allocate(); //Temp allocate, all need data
353 using namespace Stats
;
356 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
357 Packet::Command cmd
= (Packet::Command
)access_idx
;
358 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
361 .init(maxThreadsPerCPU
)
362 .name(name() + "." + cstr
+ "_hits")
363 .desc("number of " + cstr
+ " hits")
364 .flags(total
| nozero
| nonan
)
369 .name(name() + ".demand_hits")
370 .desc("number of demand (read+write) hits")
373 demandHits
= hits
[Packet::ReadReq
] + hits
[Packet::WriteReq
];
376 .name(name() + ".overall_hits")
377 .desc("number of overall hits")
380 overallHits
= demandHits
+ hits
[Packet::SoftPFReq
] + hits
[Packet::HardPFReq
]
381 + hits
[Packet::Writeback
];
384 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
385 Packet::Command cmd
= (Packet::Command
)access_idx
;
386 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
389 .init(maxThreadsPerCPU
)
390 .name(name() + "." + cstr
+ "_misses")
391 .desc("number of " + cstr
+ " misses")
392 .flags(total
| nozero
| nonan
)
397 .name(name() + ".demand_misses")
398 .desc("number of demand (read+write) misses")
401 demandMisses
= misses
[Packet::ReadReq
] + misses
[Packet::WriteReq
];
404 .name(name() + ".overall_misses")
405 .desc("number of overall misses")
408 overallMisses
= demandMisses
+ misses
[Packet::SoftPFReq
] +
409 misses
[Packet::HardPFReq
] + misses
[Packet::Writeback
];
411 // Miss latency statistics
412 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
413 Packet::Command cmd
= (Packet::Command
)access_idx
;
414 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
416 missLatency
[access_idx
]
417 .init(maxThreadsPerCPU
)
418 .name(name() + "." + cstr
+ "_miss_latency")
419 .desc("number of " + cstr
+ " miss cycles")
420 .flags(total
| nozero
| nonan
)
425 .name(name() + ".demand_miss_latency")
426 .desc("number of demand (read+write) miss cycles")
429 demandMissLatency
= missLatency
[Packet::ReadReq
] + missLatency
[Packet::WriteReq
];
432 .name(name() + ".overall_miss_latency")
433 .desc("number of overall miss cycles")
436 overallMissLatency
= demandMissLatency
+ missLatency
[Packet::SoftPFReq
] +
437 missLatency
[Packet::HardPFReq
];
440 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
441 Packet::Command cmd
= (Packet::Command
)access_idx
;
442 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
445 .name(name() + "." + cstr
+ "_accesses")
446 .desc("number of " + cstr
+ " accesses(hits+misses)")
447 .flags(total
| nozero
| nonan
)
450 accesses
[access_idx
] = hits
[access_idx
] + misses
[access_idx
];
454 .name(name() + ".demand_accesses")
455 .desc("number of demand (read+write) accesses")
458 demandAccesses
= demandHits
+ demandMisses
;
461 .name(name() + ".overall_accesses")
462 .desc("number of overall (read+write) accesses")
465 overallAccesses
= overallHits
+ overallMisses
;
467 // miss rate formulas
468 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
469 Packet::Command cmd
= (Packet::Command
)access_idx
;
470 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
473 .name(name() + "." + cstr
+ "_miss_rate")
474 .desc("miss rate for " + cstr
+ " accesses")
475 .flags(total
| nozero
| nonan
)
478 missRate
[access_idx
] = misses
[access_idx
] / accesses
[access_idx
];
482 .name(name() + ".demand_miss_rate")
483 .desc("miss rate for demand accesses")
486 demandMissRate
= demandMisses
/ demandAccesses
;
489 .name(name() + ".overall_miss_rate")
490 .desc("miss rate for overall accesses")
493 overallMissRate
= overallMisses
/ overallAccesses
;
495 // miss latency formulas
496 for (int access_idx
= 0; access_idx
< NUM_MEM_CMDS
; ++access_idx
) {
497 Packet::Command cmd
= (Packet::Command
)access_idx
;
498 const string
&cstr
= temp_pkt
.cmdIdxToString(cmd
);
500 avgMissLatency
[access_idx
]
501 .name(name() + "." + cstr
+ "_avg_miss_latency")
502 .desc("average " + cstr
+ " miss latency")
503 .flags(total
| nozero
| nonan
)
506 avgMissLatency
[access_idx
] =
507 missLatency
[access_idx
] / misses
[access_idx
];
511 .name(name() + ".demand_avg_miss_latency")
512 .desc("average overall miss latency")
515 demandAvgMissLatency
= demandMissLatency
/ demandMisses
;
517 overallAvgMissLatency
518 .name(name() + ".overall_avg_miss_latency")
519 .desc("average overall miss latency")
522 overallAvgMissLatency
= overallMissLatency
/ overallMisses
;
524 blocked_cycles
.init(NUM_BLOCKED_CAUSES
);
526 .name(name() + ".blocked_cycles")
527 .desc("number of cycles access was blocked")
528 .subname(Blocked_NoMSHRs
, "no_mshrs")
529 .subname(Blocked_NoTargets
, "no_targets")
533 blocked_causes
.init(NUM_BLOCKED_CAUSES
);
535 .name(name() + ".blocked")
536 .desc("number of cycles access was blocked")
537 .subname(Blocked_NoMSHRs
, "no_mshrs")
538 .subname(Blocked_NoTargets
, "no_targets")
542 .name(name() + ".avg_blocked_cycles")
543 .desc("average number of cycles each access was blocked")
544 .subname(Blocked_NoMSHRs
, "no_mshrs")
545 .subname(Blocked_NoTargets
, "no_targets")
548 avg_blocked
= blocked_cycles
/ blocked_causes
;
551 .name(name() + ".fast_writes")
552 .desc("number of fast writes performed")
556 .name(name() + ".cache_copies")
557 .desc("number of cache copies performed")