CacheMemory.cc revision 9171
113372Sgabeblack@google.com/* 213372Sgabeblack@google.com * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood 313372Sgabeblack@google.com * All rights reserved. 413372Sgabeblack@google.com * 513372Sgabeblack@google.com * Redistribution and use in source and binary forms, with or without 613372Sgabeblack@google.com * modification, are permitted provided that the following conditions are 713372Sgabeblack@google.com * met: redistributions of source code must retain the above copyright 813372Sgabeblack@google.com * notice, this list of conditions and the following disclaimer; 913372Sgabeblack@google.com * redistributions in binary form must reproduce the above copyright 1013372Sgabeblack@google.com * notice, this list of conditions and the following disclaimer in the 1113372Sgabeblack@google.com * documentation and/or other materials provided with the distribution; 1213372Sgabeblack@google.com * neither the name of the copyright holders nor the names of its 1313372Sgabeblack@google.com * contributors may be used to endorse or promote products derived from 1413372Sgabeblack@google.com * this software without specific prior written permission. 1513372Sgabeblack@google.com * 1613372Sgabeblack@google.com * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 1713372Sgabeblack@google.com * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 1813372Sgabeblack@google.com * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 1913372Sgabeblack@google.com * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 2013372Sgabeblack@google.com * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 2113372Sgabeblack@google.com * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 2213372Sgabeblack@google.com * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 2313372Sgabeblack@google.com * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 2413372Sgabeblack@google.com * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 2513372Sgabeblack@google.com * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 2613372Sgabeblack@google.com * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 2713372Sgabeblack@google.com */ 2813372Sgabeblack@google.com 2913372Sgabeblack@google.com#include "base/intmath.hh" 3013372Sgabeblack@google.com#include "debug/RubyCache.hh" 3113372Sgabeblack@google.com#include "debug/RubyCacheTrace.hh" 3213372Sgabeblack@google.com#include "debug/RubyResourceStalls.hh" 3313372Sgabeblack@google.com#include "debug/RubyStats.hh" 3413372Sgabeblack@google.com#include "mem/protocol/AccessPermission.hh" 3513372Sgabeblack@google.com#include "mem/ruby/system/CacheMemory.hh" 3613372Sgabeblack@google.com#include "mem/ruby/system/System.hh" 3713372Sgabeblack@google.com 3813372Sgabeblack@google.comusing namespace std; 3913372Sgabeblack@google.com 4013372Sgabeblack@google.comostream& 4113372Sgabeblack@google.comoperator<<(ostream& out, const CacheMemory& obj) 4213372Sgabeblack@google.com{ 4313372Sgabeblack@google.com obj.print(out); 4413372Sgabeblack@google.com out << flush; 4513372Sgabeblack@google.com return out; 4613372Sgabeblack@google.com} 4713372Sgabeblack@google.com 4813372Sgabeblack@google.comCacheMemory * 4913372Sgabeblack@google.comRubyCacheParams::create() 5013372Sgabeblack@google.com{ 5113372Sgabeblack@google.com return new CacheMemory(this); 5213372Sgabeblack@google.com} 5313372Sgabeblack@google.com 5413372Sgabeblack@google.comCacheMemory::CacheMemory(const Params *p) 5513372Sgabeblack@google.com : SimObject(p), 5613372Sgabeblack@google.com dataArray(p->dataArrayBanks, p->dataAccessLatency, p->start_index_bit), 5713372Sgabeblack@google.com tagArray(p->tagArrayBanks, p->tagAccessLatency, p->start_index_bit) 5813372Sgabeblack@google.com{ 5913372Sgabeblack@google.com m_cache_size = p->size; 6013372Sgabeblack@google.com m_latency = p->latency; 6113372Sgabeblack@google.com m_cache_assoc = p->assoc; 6213372Sgabeblack@google.com m_policy = p->replacement_policy; 6313372Sgabeblack@google.com m_profiler_ptr = new CacheProfiler(name()); 6413372Sgabeblack@google.com m_start_index_bit = p->start_index_bit; 6513372Sgabeblack@google.com m_is_instruction_only_cache = p->is_icache; 6613372Sgabeblack@google.com m_resource_stalls = p->resourceStalls; 6713372Sgabeblack@google.com} 6813372Sgabeblack@google.com 6913372Sgabeblack@google.comvoid 7013372Sgabeblack@google.comCacheMemory::init() 7113372Sgabeblack@google.com{ 7213372Sgabeblack@google.com m_cache_num_sets = (m_cache_size / m_cache_assoc) / 7313372Sgabeblack@google.com RubySystem::getBlockSizeBytes(); 7413372Sgabeblack@google.com assert(m_cache_num_sets > 1); 7513372Sgabeblack@google.com m_cache_num_set_bits = floorLog2(m_cache_num_sets); 7613372Sgabeblack@google.com assert(m_cache_num_set_bits > 0); 7713372Sgabeblack@google.com 7813372Sgabeblack@google.com if (m_policy == "PSEUDO_LRU") 7913372Sgabeblack@google.com m_replacementPolicy_ptr = 8013372Sgabeblack@google.com new PseudoLRUPolicy(m_cache_num_sets, m_cache_assoc); 8113372Sgabeblack@google.com else if (m_policy == "LRU") 8213372Sgabeblack@google.com m_replacementPolicy_ptr = 8313372Sgabeblack@google.com new LRUPolicy(m_cache_num_sets, m_cache_assoc); 8413372Sgabeblack@google.com else 8513372Sgabeblack@google.com assert(false); 8613372Sgabeblack@google.com 8713372Sgabeblack@google.com m_cache.resize(m_cache_num_sets); 8813372Sgabeblack@google.com for (int i = 0; i < m_cache_num_sets; i++) { 8913372Sgabeblack@google.com m_cache[i].resize(m_cache_assoc); 9013372Sgabeblack@google.com for (int j = 0; j < m_cache_assoc; j++) { 9113372Sgabeblack@google.com m_cache[i][j] = NULL; 9213372Sgabeblack@google.com } 9313372Sgabeblack@google.com } 9413372Sgabeblack@google.com} 9513372Sgabeblack@google.com 9613372Sgabeblack@google.comCacheMemory::~CacheMemory() 9713372Sgabeblack@google.com{ 9813372Sgabeblack@google.com if (m_replacementPolicy_ptr != NULL) 9913372Sgabeblack@google.com delete m_replacementPolicy_ptr; 10013372Sgabeblack@google.com delete m_profiler_ptr; 10113372Sgabeblack@google.com for (int i = 0; i < m_cache_num_sets; i++) { 10213372Sgabeblack@google.com for (int j = 0; j < m_cache_assoc; j++) { 10313372Sgabeblack@google.com delete m_cache[i][j]; 10413372Sgabeblack@google.com } 10513372Sgabeblack@google.com } 10613372Sgabeblack@google.com} 10713372Sgabeblack@google.com 10813372Sgabeblack@google.com// convert a Address to its location in the cache 10913372Sgabeblack@google.comIndex 11013372Sgabeblack@google.comCacheMemory::addressToCacheSet(const Address& address) const 11113372Sgabeblack@google.com{ 11213372Sgabeblack@google.com assert(address == line_address(address)); 11313372Sgabeblack@google.com return address.bitSelect(m_start_index_bit, 11413372Sgabeblack@google.com m_start_index_bit + m_cache_num_set_bits - 1); 11513372Sgabeblack@google.com} 11613372Sgabeblack@google.com 11713372Sgabeblack@google.com// Given a cache index: returns the index of the tag in a set. 11813372Sgabeblack@google.com// returns -1 if the tag is not found. 11913372Sgabeblack@google.comint 12013372Sgabeblack@google.comCacheMemory::findTagInSet(Index cacheSet, const Address& tag) const 12113372Sgabeblack@google.com{ 12213372Sgabeblack@google.com assert(tag == line_address(tag)); 12313372Sgabeblack@google.com // search the set for the tags 12413372Sgabeblack@google.com m5::hash_map<Address, int>::const_iterator it = m_tag_index.find(tag); 12513372Sgabeblack@google.com if (it != m_tag_index.end()) 12613372Sgabeblack@google.com if (m_cache[cacheSet][it->second]->m_Permission != 12713372Sgabeblack@google.com AccessPermission_NotPresent) 12813372Sgabeblack@google.com return it->second; 12913372Sgabeblack@google.com return -1; // Not found 13013372Sgabeblack@google.com} 13113372Sgabeblack@google.com 13213372Sgabeblack@google.com// Given a cache index: returns the index of the tag in a set. 13313372Sgabeblack@google.com// returns -1 if the tag is not found. 13413372Sgabeblack@google.comint 13513372Sgabeblack@google.comCacheMemory::findTagInSetIgnorePermissions(Index cacheSet, 13613372Sgabeblack@google.com const Address& tag) const 13713372Sgabeblack@google.com{ 13813372Sgabeblack@google.com assert(tag == line_address(tag)); 13913372Sgabeblack@google.com // search the set for the tags 14013372Sgabeblack@google.com m5::hash_map<Address, int>::const_iterator it = m_tag_index.find(tag); 14113372Sgabeblack@google.com if (it != m_tag_index.end()) 14213372Sgabeblack@google.com return it->second; 14313372Sgabeblack@google.com return -1; // Not found 14413372Sgabeblack@google.com} 14513372Sgabeblack@google.com 14613372Sgabeblack@google.combool 14713372Sgabeblack@google.comCacheMemory::tryCacheAccess(const Address& address, RubyRequestType type, 14813372Sgabeblack@google.com DataBlock*& data_ptr) 14913372Sgabeblack@google.com{ 15013372Sgabeblack@google.com assert(address == line_address(address)); 15113372Sgabeblack@google.com DPRINTF(RubyCache, "address: %s\n", address); 15213372Sgabeblack@google.com Index cacheSet = addressToCacheSet(address); 15313372Sgabeblack@google.com int loc = findTagInSet(cacheSet, address); 15413372Sgabeblack@google.com if (loc != -1) { 15513372Sgabeblack@google.com // Do we even have a tag match? 15613372Sgabeblack@google.com AbstractCacheEntry* entry = m_cache[cacheSet][loc]; 15713372Sgabeblack@google.com m_replacementPolicy_ptr->touch(cacheSet, loc, curTick()); 15813372Sgabeblack@google.com data_ptr = &(entry->getDataBlk()); 15913372Sgabeblack@google.com 160 if (entry->m_Permission == AccessPermission_Read_Write) { 161 return true; 162 } 163 if ((entry->m_Permission == AccessPermission_Read_Only) && 164 (type == RubyRequestType_LD || type == RubyRequestType_IFETCH)) { 165 return true; 166 } 167 // The line must not be accessible 168 } 169 data_ptr = NULL; 170 return false; 171} 172 173bool 174CacheMemory::testCacheAccess(const Address& address, RubyRequestType type, 175 DataBlock*& data_ptr) 176{ 177 assert(address == line_address(address)); 178 DPRINTF(RubyCache, "address: %s\n", address); 179 Index cacheSet = addressToCacheSet(address); 180 int loc = findTagInSet(cacheSet, address); 181 182 if (loc != -1) { 183 // Do we even have a tag match? 184 AbstractCacheEntry* entry = m_cache[cacheSet][loc]; 185 m_replacementPolicy_ptr->touch(cacheSet, loc, curTick()); 186 data_ptr = &(entry->getDataBlk()); 187 188 return m_cache[cacheSet][loc]->m_Permission != 189 AccessPermission_NotPresent; 190 } 191 192 data_ptr = NULL; 193 return false; 194} 195 196// tests to see if an address is present in the cache 197bool 198CacheMemory::isTagPresent(const Address& address) const 199{ 200 assert(address == line_address(address)); 201 Index cacheSet = addressToCacheSet(address); 202 int loc = findTagInSet(cacheSet, address); 203 204 if (loc == -1) { 205 // We didn't find the tag 206 DPRINTF(RubyCache, "No tag match for address: %s\n", address); 207 return false; 208 } 209 DPRINTF(RubyCache, "address: %s found\n", address); 210 return true; 211} 212 213// Returns true if there is: 214// a) a tag match on this address or there is 215// b) an unused line in the same cache "way" 216bool 217CacheMemory::cacheAvail(const Address& address) const 218{ 219 assert(address == line_address(address)); 220 221 Index cacheSet = addressToCacheSet(address); 222 223 for (int i = 0; i < m_cache_assoc; i++) { 224 AbstractCacheEntry* entry = m_cache[cacheSet][i]; 225 if (entry != NULL) { 226 if (entry->m_Address == address || 227 entry->m_Permission == AccessPermission_NotPresent) { 228 // Already in the cache or we found an empty entry 229 return true; 230 } 231 } else { 232 return true; 233 } 234 } 235 return false; 236} 237 238AbstractCacheEntry* 239CacheMemory::allocate(const Address& address, AbstractCacheEntry* entry) 240{ 241 assert(address == line_address(address)); 242 assert(!isTagPresent(address)); 243 assert(cacheAvail(address)); 244 DPRINTF(RubyCache, "address: %s\n", address); 245 246 // Find the first open slot 247 Index cacheSet = addressToCacheSet(address); 248 std::vector<AbstractCacheEntry*> &set = m_cache[cacheSet]; 249 for (int i = 0; i < m_cache_assoc; i++) { 250 if (!set[i] || set[i]->m_Permission == AccessPermission_NotPresent) { 251 set[i] = entry; // Init entry 252 set[i]->m_Address = address; 253 set[i]->m_Permission = AccessPermission_Invalid; 254 DPRINTF(RubyCache, "Allocate clearing lock for addr: %x\n", 255 address); 256 set[i]->m_locked = -1; 257 m_tag_index[address] = i; 258 259 m_replacementPolicy_ptr->touch(cacheSet, i, curTick()); 260 261 return entry; 262 } 263 } 264 panic("Allocate didn't find an available entry"); 265} 266 267void 268CacheMemory::deallocate(const Address& address) 269{ 270 assert(address == line_address(address)); 271 assert(isTagPresent(address)); 272 DPRINTF(RubyCache, "address: %s\n", address); 273 Index cacheSet = addressToCacheSet(address); 274 int loc = findTagInSet(cacheSet, address); 275 if (loc != -1) { 276 delete m_cache[cacheSet][loc]; 277 m_cache[cacheSet][loc] = NULL; 278 m_tag_index.erase(address); 279 } 280} 281 282// Returns with the physical address of the conflicting cache line 283Address 284CacheMemory::cacheProbe(const Address& address) const 285{ 286 assert(address == line_address(address)); 287 assert(!cacheAvail(address)); 288 289 Index cacheSet = addressToCacheSet(address); 290 return m_cache[cacheSet][m_replacementPolicy_ptr->getVictim(cacheSet)]-> 291 m_Address; 292} 293 294// looks an address up in the cache 295AbstractCacheEntry* 296CacheMemory::lookup(const Address& address) 297{ 298 assert(address == line_address(address)); 299 Index cacheSet = addressToCacheSet(address); 300 int loc = findTagInSet(cacheSet, address); 301 if(loc == -1) return NULL; 302 return m_cache[cacheSet][loc]; 303} 304 305// looks an address up in the cache 306const AbstractCacheEntry* 307CacheMemory::lookup(const Address& address) const 308{ 309 assert(address == line_address(address)); 310 Index cacheSet = addressToCacheSet(address); 311 int loc = findTagInSet(cacheSet, address); 312 if(loc == -1) return NULL; 313 return m_cache[cacheSet][loc]; 314} 315 316// Sets the most recently used bit for a cache block 317void 318CacheMemory::setMRU(const Address& address) 319{ 320 Index cacheSet = addressToCacheSet(address); 321 int loc = findTagInSet(cacheSet, address); 322 323 if(loc != -1) 324 m_replacementPolicy_ptr->touch(cacheSet, loc, curTick()); 325} 326 327void 328CacheMemory::profileMiss(const RubyRequest& msg) 329{ 330 m_profiler_ptr->addCacheStatSample(msg.getType(), 331 msg.getAccessMode(), 332 msg.getPrefetch()); 333} 334 335void 336CacheMemory::profileGenericRequest(GenericRequestType requestType, 337 RubyAccessMode accessType, 338 PrefetchBit pfBit) 339{ 340 m_profiler_ptr->addGenericStatSample(requestType, 341 accessType, 342 pfBit); 343} 344 345void 346CacheMemory::recordCacheContents(int cntrl, CacheRecorder* tr) const 347{ 348 uint64 warmedUpBlocks = 0; 349 uint64 totalBlocks M5_VAR_USED = (uint64)m_cache_num_sets 350 * (uint64)m_cache_assoc; 351 352 for (int i = 0; i < m_cache_num_sets; i++) { 353 for (int j = 0; j < m_cache_assoc; j++) { 354 if (m_cache[i][j] != NULL) { 355 AccessPermission perm = m_cache[i][j]->m_Permission; 356 RubyRequestType request_type = RubyRequestType_NULL; 357 if (perm == AccessPermission_Read_Only) { 358 if (m_is_instruction_only_cache) { 359 request_type = RubyRequestType_IFETCH; 360 } else { 361 request_type = RubyRequestType_LD; 362 } 363 } else if (perm == AccessPermission_Read_Write) { 364 request_type = RubyRequestType_ST; 365 } 366 367 if (request_type != RubyRequestType_NULL) { 368 tr->addRecord(cntrl, m_cache[i][j]->m_Address.getAddress(), 369 0, request_type, 370 m_replacementPolicy_ptr->getLastAccess(i, j), 371 m_cache[i][j]->getDataBlk()); 372 warmedUpBlocks++; 373 } 374 } 375 } 376 } 377 378 DPRINTF(RubyCacheTrace, "%s: %lli blocks of %lli total blocks" 379 "recorded %.2f%% \n", name().c_str(), warmedUpBlocks, 380 (uint64)m_cache_num_sets * (uint64)m_cache_assoc, 381 (float(warmedUpBlocks)/float(totalBlocks))*100.0); 382} 383 384void 385CacheMemory::print(ostream& out) const 386{ 387 out << "Cache dump: " << m_cache_name << endl; 388 for (int i = 0; i < m_cache_num_sets; i++) { 389 for (int j = 0; j < m_cache_assoc; j++) { 390 if (m_cache[i][j] != NULL) { 391 out << " Index: " << i 392 << " way: " << j 393 << " entry: " << *m_cache[i][j] << endl; 394 } else { 395 out << " Index: " << i 396 << " way: " << j 397 << " entry: NULL" << endl; 398 } 399 } 400 } 401} 402 403void 404CacheMemory::printData(ostream& out) const 405{ 406 out << "printData() not supported" << endl; 407} 408 409void 410CacheMemory::clearStats() const 411{ 412 m_profiler_ptr->clearStats(); 413} 414 415void 416CacheMemory::printStats(ostream& out) const 417{ 418 m_profiler_ptr->printStats(out); 419} 420 421void 422CacheMemory::setLocked(const Address& address, int context) 423{ 424 DPRINTF(RubyCache, "Setting Lock for addr: %x to %d\n", address, context); 425 assert(address == line_address(address)); 426 Index cacheSet = addressToCacheSet(address); 427 int loc = findTagInSet(cacheSet, address); 428 assert(loc != -1); 429 m_cache[cacheSet][loc]->m_locked = context; 430} 431 432void 433CacheMemory::clearLocked(const Address& address) 434{ 435 DPRINTF(RubyCache, "Clear Lock for addr: %x\n", address); 436 assert(address == line_address(address)); 437 Index cacheSet = addressToCacheSet(address); 438 int loc = findTagInSet(cacheSet, address); 439 assert(loc != -1); 440 m_cache[cacheSet][loc]->m_locked = -1; 441} 442 443bool 444CacheMemory::isLocked(const Address& address, int context) 445{ 446 assert(address == line_address(address)); 447 Index cacheSet = addressToCacheSet(address); 448 int loc = findTagInSet(cacheSet, address); 449 assert(loc != -1); 450 DPRINTF(RubyCache, "Testing Lock for addr: %llx cur %d con %d\n", 451 address, m_cache[cacheSet][loc]->m_locked, context); 452 return m_cache[cacheSet][loc]->m_locked == context; 453} 454 455void 456CacheMemory::recordRequestType(CacheRequestType requestType) { 457 DPRINTF(RubyStats, "Recorded statistic: %s\n", 458 CacheRequestType_to_string(requestType)); 459 switch(requestType) { 460 case CacheRequestType_DataArrayRead: 461 numDataArrayReads++; 462 return; 463 case CacheRequestType_DataArrayWrite: 464 numDataArrayWrites++; 465 return; 466 case CacheRequestType_TagArrayRead: 467 numTagArrayReads++; 468 return; 469 case CacheRequestType_TagArrayWrite: 470 numTagArrayWrites++; 471 return; 472 default: 473 warn("CacheMemory access_type not found: %s", 474 CacheRequestType_to_string(requestType)); 475 } 476} 477 478void 479CacheMemory::regStats() { 480 using namespace Stats; 481 482 numDataArrayReads 483 .name(name() + ".num_data_array_reads") 484 .desc("number of data array reads") 485 ; 486 487 numDataArrayWrites 488 .name(name() + ".num_data_array_writes") 489 .desc("number of data array writes") 490 ; 491 492 numTagArrayReads 493 .name(name() + ".num_tag_array_reads") 494 .desc("number of tag array reads") 495 ; 496 497 numTagArrayWrites 498 .name(name() + ".num_tag_array_writes") 499 .desc("number of tag array writes") 500 ; 501 502 numTagArrayStalls 503 .name(name() + ".num_tag_array_stalls") 504 .desc("number of stalls caused by tag array") 505 ; 506 507 numDataArrayStalls 508 .name(name() + ".num_data_array_stalls") 509 .desc("number of stalls caused by data array") 510 ; 511} 512 513bool 514CacheMemory::checkResourceAvailable(CacheResourceType res, Address addr) 515{ 516 if (!m_resource_stalls) { 517 return true; 518 } 519 520 if (res == CacheResourceType_TagArray) { 521 if (tagArray.tryAccess(addressToCacheSet(addr))) return true; 522 else { 523 DPRINTF(RubyResourceStalls, "Tag array stall on addr %s in set %d\n", addr, addressToCacheSet(addr)); 524 numTagArrayStalls++; 525 return false; 526 } 527 } else if (res == CacheResourceType_DataArray) { 528 if (dataArray.tryAccess(addressToCacheSet(addr))) return true; 529 else { 530 DPRINTF(RubyResourceStalls, "Data array stall on addr %s in set %d\n", addr, addressToCacheSet(addr)); 531 numDataArrayStalls++; 532 return false; 533 } 534 } else { 535 assert(false); 536 return true; 537 } 538} 539