First pass at snooping stuff that compiles and doesn't break.
[gem5.git] / src / mem / cache / base_cache.cc
1 /*
2 * Copyright (c) 2003-2005 The Regents of The University of Michigan
3 * All rights reserved.
4 *
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.
15 *
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.
27 *
28 * Authors: Erik Hallnor
29 */
30
31 /**
32 * @file
33 * Definition of BaseCache functions.
34 */
35
36 #include "mem/cache/base_cache.hh"
37 #include "cpu/smt.hh"
38 #include "cpu/base.hh"
39
40 using namespace std;
41
42 BaseCache::CachePort::CachePort(const std::string &_name, BaseCache *_cache,
43 bool _isCpuSide)
44 : Port(_name), cache(_cache), isCpuSide(_isCpuSide)
45 {
46 blocked = false;
47 //Start ports at null if more than one is created we should panic
48 //cpuSidePort = NULL;
49 //memSidePort = NULL;
50 }
51
52 void
53 BaseCache::CachePort::recvStatusChange(Port::Status status)
54 {
55 cache->recvStatusChange(status, isCpuSide);
56 }
57
58 void
59 BaseCache::CachePort::getDeviceAddressRanges(AddrRangeList &resp,
60 AddrRangeList &snoop)
61 {
62 cache->getAddressRanges(resp, snoop, isCpuSide);
63 }
64
65 int
66 BaseCache::CachePort::deviceBlockSize()
67 {
68 return cache->getBlockSize();
69 }
70
71 bool
72 BaseCache::CachePort::recvTiming(Packet *pkt)
73 {
74 if (blocked)
75 {
76 DPRINTF(Cache,"Scheduling a retry while blocked\n");
77 mustSendRetry = true;
78 return false;
79 }
80 return cache->doTimingAccess(pkt, this, isCpuSide);
81 }
82
83 Tick
84 BaseCache::CachePort::recvAtomic(Packet *pkt)
85 {
86 return cache->doAtomicAccess(pkt, isCpuSide);
87 }
88
89 void
90 BaseCache::CachePort::recvFunctional(Packet *pkt)
91 {
92 cache->doFunctionalAccess(pkt, isCpuSide);
93 }
94
95 void
96 BaseCache::CachePort::recvRetry()
97 {
98 Packet *pkt;
99
100 if (!isCpuSide)
101 {
102 pkt = cache->getPacket();
103 bool success = sendTiming(pkt);
104 DPRINTF(Cache, "Address %x was %s in sending the timing request\n",
105 pkt->getAddr(), success ? "succesful" : "unsuccesful");
106 cache->sendResult(pkt, success);
107 if (success && cache->doMasterRequest())
108 {
109 //Still more to issue, rerequest in 1 cycle
110 pkt = NULL;
111 BaseCache::CacheEvent * reqCpu = new BaseCache::CacheEvent(this);
112 reqCpu->schedule(curTick + 1);
113 }
114 }
115 else
116 {
117 pkt = cache->getCoherencePacket();
118 bool success = sendTiming(pkt);
119 if (success && cache->doSlaveRequest())
120 {
121 //Still more to issue, rerequest in 1 cycle
122 pkt = NULL;
123 BaseCache::CacheEvent * reqCpu = new BaseCache::CacheEvent(this);
124 reqCpu->schedule(curTick + 1);
125 }
126
127 }
128 return;
129 }
130 void
131 BaseCache::CachePort::setBlocked()
132 {
133 assert(!blocked);
134 DPRINTF(Cache, "Cache Blocking\n");
135 blocked = true;
136 //Clear the retry flag
137 mustSendRetry = false;
138 }
139
140 void
141 BaseCache::CachePort::clearBlocked()
142 {
143 assert(blocked);
144 DPRINTF(Cache, "Cache Unblocking\n");
145 blocked = false;
146 if (mustSendRetry)
147 {
148 DPRINTF(Cache, "Cache Sending Retry\n");
149 mustSendRetry = false;
150 sendRetry();
151 }
152 }
153
154 BaseCache::CacheEvent::CacheEvent(CachePort *_cachePort)
155 : Event(&mainEventQueue, CPU_Tick_Pri), cachePort(_cachePort)
156 {
157 this->setFlags(AutoDelete);
158 pkt = NULL;
159 }
160
161 BaseCache::CacheEvent::CacheEvent(CachePort *_cachePort, Packet *_pkt)
162 : Event(&mainEventQueue, CPU_Tick_Pri), cachePort(_cachePort), pkt(_pkt)
163 {
164 this->setFlags(AutoDelete);
165 }
166
167 void
168 BaseCache::CacheEvent::process()
169 {
170 if (!pkt)
171 {
172 if (!cachePort->isCpuSide)
173 {
174 //MSHR
175 pkt = cachePort->cache->getPacket();
176 bool success = cachePort->sendTiming(pkt);
177 DPRINTF(Cache, "Address %x was %s in sending the timing request\n",
178 pkt->getAddr(), success ? "succesful" : "unsuccesful");
179 cachePort->cache->sendResult(pkt, success);
180 if (success && cachePort->cache->doMasterRequest())
181 {
182 //Still more to issue, rerequest in 1 cycle
183 pkt = NULL;
184 this->schedule(curTick+1);
185 }
186 }
187 else
188 {
189 //CSHR
190 pkt = cachePort->cache->getCoherencePacket();
191 bool success = cachePort->sendTiming(pkt);
192 if (success && cachePort->cache->doSlaveRequest())
193 {
194 //Still more to issue, rerequest in 1 cycle
195 pkt = NULL;
196 this->schedule(curTick+1);
197 }
198 }
199 return;
200 }
201 //Response
202 //Know the packet to send
203 pkt->result = Packet::Success;
204 pkt->makeTimingResponse();
205 assert(cachePort->sendTiming(pkt));
206 }
207
208 const char *
209 BaseCache::CacheEvent::description()
210 {
211 return "timing event\n";
212 }
213
214 Port*
215 BaseCache::getPort(const std::string &if_name, int idx)
216 {
217 if (if_name == "")
218 {
219 if(cpuSidePort == NULL)
220 cpuSidePort = new CachePort(name() + "-cpu_side_port", this, true);
221 return cpuSidePort;
222 }
223 else if (if_name == "functional")
224 {
225 if(cpuSidePort == NULL)
226 cpuSidePort = new CachePort(name() + "-cpu_side_port", this, true);
227 return cpuSidePort;
228 }
229 else if (if_name == "cpu_side")
230 {
231 if(cpuSidePort == NULL)
232 cpuSidePort = new CachePort(name() + "-cpu_side_port", this, true);
233 return cpuSidePort;
234 }
235 else if (if_name == "mem_side")
236 {
237 if (memSidePort != NULL)
238 panic("Already have a mem side for this cache\n");
239 memSidePort = new CachePort(name() + "-mem_side_port", this, false);
240 return memSidePort;
241 }
242 else panic("Port name %s unrecognized\n", if_name);
243 }
244
245 void
246 BaseCache::init()
247 {
248 if (!cpuSidePort || !memSidePort)
249 panic("Cache not hooked up on both sides\n");
250 cpuSidePort->sendStatusChange(Port::RangeChange);
251 }
252
253 void
254 BaseCache::regStats()
255 {
256 Request temp_req((Addr) NULL, 4, 0);
257 Packet::Command temp_cmd = Packet::ReadReq;
258 Packet temp_pkt(&temp_req, temp_cmd, 0); //@todo FIx command strings so this isn't neccessary
259 temp_pkt.allocate(); //Temp allocate, all need data
260
261 using namespace Stats;
262
263 // Hit statistics
264 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
265 Packet::Command cmd = (Packet::Command)access_idx;
266 const string &cstr = temp_pkt.cmdIdxToString(cmd);
267
268 hits[access_idx]
269 .init(maxThreadsPerCPU)
270 .name(name() + "." + cstr + "_hits")
271 .desc("number of " + cstr + " hits")
272 .flags(total | nozero | nonan)
273 ;
274 }
275
276 demandHits
277 .name(name() + ".demand_hits")
278 .desc("number of demand (read+write) hits")
279 .flags(total)
280 ;
281 demandHits = hits[Packet::ReadReq] + hits[Packet::WriteReq];
282
283 overallHits
284 .name(name() + ".overall_hits")
285 .desc("number of overall hits")
286 .flags(total)
287 ;
288 overallHits = demandHits + hits[Packet::SoftPFReq] + hits[Packet::HardPFReq]
289 + hits[Packet::Writeback];
290
291 // Miss statistics
292 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
293 Packet::Command cmd = (Packet::Command)access_idx;
294 const string &cstr = temp_pkt.cmdIdxToString(cmd);
295
296 misses[access_idx]
297 .init(maxThreadsPerCPU)
298 .name(name() + "." + cstr + "_misses")
299 .desc("number of " + cstr + " misses")
300 .flags(total | nozero | nonan)
301 ;
302 }
303
304 demandMisses
305 .name(name() + ".demand_misses")
306 .desc("number of demand (read+write) misses")
307 .flags(total)
308 ;
309 demandMisses = misses[Packet::ReadReq] + misses[Packet::WriteReq];
310
311 overallMisses
312 .name(name() + ".overall_misses")
313 .desc("number of overall misses")
314 .flags(total)
315 ;
316 overallMisses = demandMisses + misses[Packet::SoftPFReq] +
317 misses[Packet::HardPFReq] + misses[Packet::Writeback];
318
319 // Miss latency statistics
320 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
321 Packet::Command cmd = (Packet::Command)access_idx;
322 const string &cstr = temp_pkt.cmdIdxToString(cmd);
323
324 missLatency[access_idx]
325 .init(maxThreadsPerCPU)
326 .name(name() + "." + cstr + "_miss_latency")
327 .desc("number of " + cstr + " miss cycles")
328 .flags(total | nozero | nonan)
329 ;
330 }
331
332 demandMissLatency
333 .name(name() + ".demand_miss_latency")
334 .desc("number of demand (read+write) miss cycles")
335 .flags(total)
336 ;
337 demandMissLatency = missLatency[Packet::ReadReq] + missLatency[Packet::WriteReq];
338
339 overallMissLatency
340 .name(name() + ".overall_miss_latency")
341 .desc("number of overall miss cycles")
342 .flags(total)
343 ;
344 overallMissLatency = demandMissLatency + missLatency[Packet::SoftPFReq] +
345 missLatency[Packet::HardPFReq];
346
347 // access formulas
348 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
349 Packet::Command cmd = (Packet::Command)access_idx;
350 const string &cstr = temp_pkt.cmdIdxToString(cmd);
351
352 accesses[access_idx]
353 .name(name() + "." + cstr + "_accesses")
354 .desc("number of " + cstr + " accesses(hits+misses)")
355 .flags(total | nozero | nonan)
356 ;
357
358 accesses[access_idx] = hits[access_idx] + misses[access_idx];
359 }
360
361 demandAccesses
362 .name(name() + ".demand_accesses")
363 .desc("number of demand (read+write) accesses")
364 .flags(total)
365 ;
366 demandAccesses = demandHits + demandMisses;
367
368 overallAccesses
369 .name(name() + ".overall_accesses")
370 .desc("number of overall (read+write) accesses")
371 .flags(total)
372 ;
373 overallAccesses = overallHits + overallMisses;
374
375 // miss rate formulas
376 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
377 Packet::Command cmd = (Packet::Command)access_idx;
378 const string &cstr = temp_pkt.cmdIdxToString(cmd);
379
380 missRate[access_idx]
381 .name(name() + "." + cstr + "_miss_rate")
382 .desc("miss rate for " + cstr + " accesses")
383 .flags(total | nozero | nonan)
384 ;
385
386 missRate[access_idx] = misses[access_idx] / accesses[access_idx];
387 }
388
389 demandMissRate
390 .name(name() + ".demand_miss_rate")
391 .desc("miss rate for demand accesses")
392 .flags(total)
393 ;
394 demandMissRate = demandMisses / demandAccesses;
395
396 overallMissRate
397 .name(name() + ".overall_miss_rate")
398 .desc("miss rate for overall accesses")
399 .flags(total)
400 ;
401 overallMissRate = overallMisses / overallAccesses;
402
403 // miss latency formulas
404 for (int access_idx = 0; access_idx < NUM_MEM_CMDS; ++access_idx) {
405 Packet::Command cmd = (Packet::Command)access_idx;
406 const string &cstr = temp_pkt.cmdIdxToString(cmd);
407
408 avgMissLatency[access_idx]
409 .name(name() + "." + cstr + "_avg_miss_latency")
410 .desc("average " + cstr + " miss latency")
411 .flags(total | nozero | nonan)
412 ;
413
414 avgMissLatency[access_idx] =
415 missLatency[access_idx] / misses[access_idx];
416 }
417
418 demandAvgMissLatency
419 .name(name() + ".demand_avg_miss_latency")
420 .desc("average overall miss latency")
421 .flags(total)
422 ;
423 demandAvgMissLatency = demandMissLatency / demandMisses;
424
425 overallAvgMissLatency
426 .name(name() + ".overall_avg_miss_latency")
427 .desc("average overall miss latency")
428 .flags(total)
429 ;
430 overallAvgMissLatency = overallMissLatency / overallMisses;
431
432 blocked_cycles.init(NUM_BLOCKED_CAUSES);
433 blocked_cycles
434 .name(name() + ".blocked_cycles")
435 .desc("number of cycles access was blocked")
436 .subname(Blocked_NoMSHRs, "no_mshrs")
437 .subname(Blocked_NoTargets, "no_targets")
438 ;
439
440
441 blocked_causes.init(NUM_BLOCKED_CAUSES);
442 blocked_causes
443 .name(name() + ".blocked")
444 .desc("number of cycles access was blocked")
445 .subname(Blocked_NoMSHRs, "no_mshrs")
446 .subname(Blocked_NoTargets, "no_targets")
447 ;
448
449 avg_blocked
450 .name(name() + ".avg_blocked_cycles")
451 .desc("average number of cycles each access was blocked")
452 .subname(Blocked_NoMSHRs, "no_mshrs")
453 .subname(Blocked_NoTargets, "no_targets")
454 ;
455
456 avg_blocked = blocked_cycles / blocked_causes;
457
458 fastWrites
459 .name(name() + ".fast_writes")
460 .desc("number of fast writes performed")
461 ;
462
463 cacheCopies
464 .name(name() + ".cache_copies")
465 .desc("number of cache copies performed")
466 ;
467
468 }