CacheMemory.cc revision 9105
16145SN/A/*
28688SN/A * Copyright (c) 1999-2012 Mark D. Hill and David A. Wood
36145SN/A * All rights reserved.
46145SN/A *
56145SN/A * Redistribution and use in source and binary forms, with or without
66145SN/A * modification, are permitted provided that the following conditions are
76145SN/A * met: redistributions of source code must retain the above copyright
86145SN/A * notice, this list of conditions and the following disclaimer;
96145SN/A * redistributions in binary form must reproduce the above copyright
106145SN/A * notice, this list of conditions and the following disclaimer in the
116145SN/A * documentation and/or other materials provided with the distribution;
126145SN/A * neither the name of the copyright holders nor the names of its
136145SN/A * contributors may be used to endorse or promote products derived from
146145SN/A * this software without specific prior written permission.
156145SN/A *
166145SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
176145SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
186145SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
196145SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
206145SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
216145SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
226145SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
236145SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
246145SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
256145SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
266145SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
276145SN/A */
286145SN/A
296145SN/A#include "base/intmath.hh"
307039SN/A#include "debug/RubyCache.hh"
317039SN/A#include "debug/RubyCacheTrace.hh"
327039SN/A#include "debug/RubyResourceStalls.hh"
336145SN/A#include "debug/RubyStats.hh"
346145SN/A#include "mem/protocol/AccessPermission.hh"
3512492Sodanrc@yahoo.com.br#include "mem/ruby/system/CacheMemory.hh"
3612492Sodanrc@yahoo.com.br#include "mem/ruby/system/System.hh"
376145SN/A
387039SN/Ausing namespace std;
399350SN/A
4011108Sdavid.hashe@amd.comostream&
4110301SN/Aoperator<<(ostream& out, const CacheMemory& obj)
4210301SN/A{
4310301SN/A    obj.print(out);
447039SN/A    out << flush;
459206SN/A    return out;
466145SN/A}
477039SN/A
4810920SN/ACacheMemory *
496285SN/ARubyCacheParams::create()
509206SN/A{
517039SN/A    return new CacheMemory(this);
527039SN/A}
536876SN/A
546876SN/ACacheMemory::CacheMemory(const Params *p)
557039SN/A    : SimObject(p),
566145SN/A    dataArray(p->dataArrayBanks, p->dataAccessLatency, p->start_index_bit),
577039SN/A    tagArray(p->tagArrayBanks, p->tagAccessLatency, p->start_index_bit)
587039SN/A{
599504SN/A    m_cache_size = p->size;
609504SN/A    m_latency = p->latency;
619504SN/A    m_cache_assoc = p->assoc;
6210837SN/A    m_policy = p->replacement_policy;
6310837SN/A    m_profiler_ptr = new CacheProfiler(name());
646285SN/A    m_start_index_bit = p->start_index_bit;
6510525SN/A    m_is_instruction_only_cache = p->is_icache;
6610918SN/A    m_resource_stalls = p->resourceStalls;
6711294Sandreas.hansson@arm.com}
6810525SN/A
697039SN/Avoid
707039SN/ACacheMemory::init()
717039SN/A{
727039SN/A    m_cache_num_sets = (m_cache_size / m_cache_assoc) /
7310012SN/A        RubySystem::getBlockSizeBytes();
7410012SN/A    assert(m_cache_num_sets > 1);
757039SN/A    m_cache_num_set_bits = floorLog2(m_cache_num_sets);
766285SN/A    assert(m_cache_num_set_bits > 0);
7711523Sdavid.guillen@arm.com
7811523Sdavid.guillen@arm.com    if (m_policy == "PSEUDO_LRU")
7911523Sdavid.guillen@arm.com        m_replacementPolicy_ptr =
8011523Sdavid.guillen@arm.com            new PseudoLRUPolicy(m_cache_num_sets, m_cache_assoc);
8110012SN/A    else if (m_policy == "LRU")
8211169Sandreas.hansson@arm.com        m_replacementPolicy_ptr =
836285SN/A            new LRUPolicy(m_cache_num_sets, m_cache_assoc);
8411169Sandreas.hansson@arm.com    else
8511168Sandreas.hansson@arm.com        assert(false);
8611168Sandreas.hansson@arm.com
8711168Sandreas.hansson@arm.com    m_cache.resize(m_cache_num_sets);
888688SN/A    for (int i = 0; i < m_cache_num_sets; i++) {
8911169Sandreas.hansson@arm.com        m_cache[i].resize(m_cache_assoc);
909270SN/A        for (int j = 0; j < m_cache_assoc; j++) {
919270SN/A            m_cache[i][j] = NULL;
927562SN/A        }
938436SN/A    }
948436SN/A}
958436SN/A
968937SN/ACacheMemory::~CacheMemory()
978937SN/A{
988937SN/A    if (m_replacementPolicy_ptr != NULL)
9912133Sspwilson2@wisc.edu        delete m_replacementPolicy_ptr;
10012133Sspwilson2@wisc.edu    delete m_profiler_ptr;
1018937SN/A    for (int i = 0; i < m_cache_num_sets; i++) {
1028937SN/A        for (int j = 0; j < m_cache_assoc; j++) {
1038937SN/A            delete m_cache[i][j];
1047039SN/A        }
1057039SN/A    }
1067039SN/A}
1077039SN/A
1086285SN/Avoid
10910991SN/ACacheMemory::printConfig(ostream& out)
11011061SN/A{
11111061SN/A    int block_size = RubySystem::getBlockSizeBytes();
11210991SN/A
11311060SN/A    out << "Cache config: " << m_cache_name << endl;
11411060SN/A    out << "  cache_associativity: " << m_cache_assoc << endl;
11511061SN/A    out << "  num_cache_sets_bits: " << m_cache_num_set_bits << endl;
11611060SN/A    const int cache_num_sets = 1 << m_cache_num_set_bits;
11711061SN/A    out << "  num_cache_sets: " << cache_num_sets << endl;
1187039SN/A    out << "  cache_set_size_bytes: " << cache_num_sets * block_size << endl;
11912133Sspwilson2@wisc.edu    out << "  cache_set_size_Kbytes: "
1207039SN/A        << double(cache_num_sets * block_size) / (1<<10) << endl;
1217039SN/A    out << "  cache_set_size_Mbytes: "
1227039SN/A        << double(cache_num_sets * block_size) / (1<<20) << endl;
1239504SN/A    out << "  cache_size_bytes: "
1249504SN/A        << cache_num_sets * block_size * m_cache_assoc << endl;
1259504SN/A    out << "  cache_size_Kbytes: "
12610919SN/A        << double(cache_num_sets * block_size * m_cache_assoc) / (1<<10)
12710837SN/A        << endl;
12810837SN/A    out << "  cache_size_Mbytes: "
12910837SN/A        << double(cache_num_sets * block_size * m_cache_assoc) / (1<<20)
13010525SN/A        << endl;
13110706SN/A}
1327039SN/A
13310012SN/A// convert a Address to its location in the cache
1349300SN/AIndex
13510918SN/ACacheMemory::addressToCacheSet(const Address& address) const
1369103SN/A{
1377039SN/A    assert(address == line_address(address));
13810012SN/A    return address.bitSelect(m_start_index_bit,
1398688SN/A                             m_start_index_bit + m_cache_num_set_bits - 1);
14010920SN/A}
1416145SN/A
1426145SN/A// Given a cache index: returns the index of the tag in a set.
14310012SN/A// returns -1 if the tag is not found.
1446895SN/Aint
1456895SN/ACacheMemory::findTagInSet(Index cacheSet, const Address& tag) const
14610919SN/A{
1476895SN/A    assert(tag == line_address(tag));
1486895SN/A    // search the set for the tags
14910012SN/A    m5::hash_map<Address, int>::const_iterator it = m_tag_index.find(tag);
15010919SN/A    if (it != m_tag_index.end())
15110919SN/A        if (m_cache[cacheSet][it->second]->m_Permission !=
1526895SN/A            AccessPermission_NotPresent)
1536895SN/A            return it->second;
15412492Sodanrc@yahoo.com.br    return -1; // Not found
155}
156
157// Given a cache index: returns the index of the tag in a set.
158// returns -1 if the tag is not found.
159int
160CacheMemory::findTagInSetIgnorePermissions(Index cacheSet,
161                                           const Address& tag) const
162{
163    assert(tag == line_address(tag));
164    // search the set for the tags
165    m5::hash_map<Address, int>::const_iterator it = m_tag_index.find(tag);
166    if (it != m_tag_index.end())
167        return it->second;
168    return -1; // Not found
169}
170
171bool
172CacheMemory::tryCacheAccess(const Address& address, RubyRequestType type,
173                            DataBlock*& data_ptr)
174{
175    assert(address == line_address(address));
176    DPRINTF(RubyCache, "address: %s\n", address);
177    Index cacheSet = addressToCacheSet(address);
178    int loc = findTagInSet(cacheSet, address);
179    if (loc != -1) {
180        // Do we even have a tag match?
181        AbstractCacheEntry* entry = m_cache[cacheSet][loc];
182        m_replacementPolicy_ptr->
183            touch(cacheSet, loc, g_eventQueue_ptr->getTime());
184        data_ptr = &(entry->getDataBlk());
185
186        if (entry->m_Permission == AccessPermission_Read_Write) {
187            return true;
188        }
189        if ((entry->m_Permission == AccessPermission_Read_Only) &&
190            (type == RubyRequestType_LD || type == RubyRequestType_IFETCH)) {
191            return true;
192        }
193        // The line must not be accessible
194    }
195    data_ptr = NULL;
196    return false;
197}
198
199bool
200CacheMemory::testCacheAccess(const Address& address, RubyRequestType type,
201                             DataBlock*& data_ptr)
202{
203    assert(address == line_address(address));
204    DPRINTF(RubyCache, "address: %s\n", address);
205    Index cacheSet = addressToCacheSet(address);
206    int loc = findTagInSet(cacheSet, address);
207
208    if (loc != -1) {
209        // Do we even have a tag match?
210        AbstractCacheEntry* entry = m_cache[cacheSet][loc];
211        m_replacementPolicy_ptr->
212            touch(cacheSet, loc, g_eventQueue_ptr->getTime());
213        data_ptr = &(entry->getDataBlk());
214
215        return m_cache[cacheSet][loc]->m_Permission !=
216            AccessPermission_NotPresent;
217    }
218
219    data_ptr = NULL;
220    return false;
221}
222
223// tests to see if an address is present in the cache
224bool
225CacheMemory::isTagPresent(const Address& address) const
226{
227    assert(address == line_address(address));
228    Index cacheSet = addressToCacheSet(address);
229    int loc = findTagInSet(cacheSet, address);
230
231    if (loc == -1) {
232        // We didn't find the tag
233        DPRINTF(RubyCache, "No tag match for address: %s\n", address);
234        return false;
235    }
236    DPRINTF(RubyCache, "address: %s found\n", address);
237    return true;
238}
239
240// Returns true if there is:
241//   a) a tag match on this address or there is
242//   b) an unused line in the same cache "way"
243bool
244CacheMemory::cacheAvail(const Address& address) const
245{
246    assert(address == line_address(address));
247
248    Index cacheSet = addressToCacheSet(address);
249
250    for (int i = 0; i < m_cache_assoc; i++) {
251        AbstractCacheEntry* entry = m_cache[cacheSet][i];
252        if (entry != NULL) {
253            if (entry->m_Address == address ||
254                entry->m_Permission == AccessPermission_NotPresent) {
255                // Already in the cache or we found an empty entry
256                return true;
257            }
258        } else {
259            return true;
260        }
261    }
262    return false;
263}
264
265AbstractCacheEntry*
266CacheMemory::allocate(const Address& address, AbstractCacheEntry* entry)
267{
268    assert(address == line_address(address));
269    assert(!isTagPresent(address));
270    assert(cacheAvail(address));
271    DPRINTF(RubyCache, "address: %s\n", address);
272
273    // Find the first open slot
274    Index cacheSet = addressToCacheSet(address);
275    std::vector<AbstractCacheEntry*> &set = m_cache[cacheSet];
276    for (int i = 0; i < m_cache_assoc; i++) {
277        if (!set[i] || set[i]->m_Permission == AccessPermission_NotPresent) {
278            set[i] = entry;  // Init entry
279            set[i]->m_Address = address;
280            set[i]->m_Permission = AccessPermission_Invalid;
281            DPRINTF(RubyCache, "Allocate clearing lock for addr: %x\n",
282                    address);
283            set[i]->m_locked = -1;
284            m_tag_index[address] = i;
285
286            m_replacementPolicy_ptr->
287                touch(cacheSet, i, g_eventQueue_ptr->getTime());
288
289            return entry;
290        }
291    }
292    panic("Allocate didn't find an available entry");
293}
294
295void
296CacheMemory::deallocate(const Address& address)
297{
298    assert(address == line_address(address));
299    assert(isTagPresent(address));
300    DPRINTF(RubyCache, "address: %s\n", address);
301    Index cacheSet = addressToCacheSet(address);
302    int loc = findTagInSet(cacheSet, address);
303    if (loc != -1) {
304        delete m_cache[cacheSet][loc];
305        m_cache[cacheSet][loc] = NULL;
306        m_tag_index.erase(address);
307    }
308}
309
310// Returns with the physical address of the conflicting cache line
311Address
312CacheMemory::cacheProbe(const Address& address) const
313{
314    assert(address == line_address(address));
315    assert(!cacheAvail(address));
316
317    Index cacheSet = addressToCacheSet(address);
318    return m_cache[cacheSet][m_replacementPolicy_ptr->getVictim(cacheSet)]->
319        m_Address;
320}
321
322// looks an address up in the cache
323AbstractCacheEntry*
324CacheMemory::lookup(const Address& address)
325{
326    assert(address == line_address(address));
327    Index cacheSet = addressToCacheSet(address);
328    int loc = findTagInSet(cacheSet, address);
329    if(loc == -1) return NULL;
330    return m_cache[cacheSet][loc];
331}
332
333// looks an address up in the cache
334const AbstractCacheEntry*
335CacheMemory::lookup(const Address& address) const
336{
337    assert(address == line_address(address));
338    Index cacheSet = addressToCacheSet(address);
339    int loc = findTagInSet(cacheSet, address);
340    if(loc == -1) return NULL;
341    return m_cache[cacheSet][loc];
342}
343
344// Sets the most recently used bit for a cache block
345void
346CacheMemory::setMRU(const Address& address)
347{
348    Index cacheSet = addressToCacheSet(address);
349    int loc = findTagInSet(cacheSet, address);
350
351    if(loc != -1)
352        m_replacementPolicy_ptr->
353             touch(cacheSet, loc, g_eventQueue_ptr->getTime());
354}
355
356void
357CacheMemory::profileMiss(const RubyRequest& msg)
358{
359    m_profiler_ptr->addCacheStatSample(msg.getType(),
360                                       msg.getAccessMode(),
361                                       msg.getPrefetch());
362}
363
364void
365CacheMemory::profileGenericRequest(GenericRequestType requestType,
366                                   RubyAccessMode accessType,
367                                   PrefetchBit pfBit)
368{
369    m_profiler_ptr->addGenericStatSample(requestType,
370                                         accessType,
371                                         pfBit);
372}
373
374void
375CacheMemory::recordCacheContents(int cntrl, CacheRecorder* tr) const
376{
377    uint64 warmedUpBlocks = 0;
378    uint64 totalBlocks M5_VAR_USED = (uint64)m_cache_num_sets
379                                                  * (uint64)m_cache_assoc;
380
381    for (int i = 0; i < m_cache_num_sets; i++) {
382        for (int j = 0; j < m_cache_assoc; j++) {
383            if (m_cache[i][j] != NULL) {
384                AccessPermission perm = m_cache[i][j]->m_Permission;
385                RubyRequestType request_type = RubyRequestType_NULL;
386                if (perm == AccessPermission_Read_Only) {
387                    if (m_is_instruction_only_cache) {
388                        request_type = RubyRequestType_IFETCH;
389                    } else {
390                        request_type = RubyRequestType_LD;
391                    }
392                } else if (perm == AccessPermission_Read_Write) {
393                    request_type = RubyRequestType_ST;
394                }
395
396                if (request_type != RubyRequestType_NULL) {
397                    tr->addRecord(cntrl, m_cache[i][j]->m_Address.getAddress(),
398                                  0, request_type,
399                                  m_replacementPolicy_ptr->getLastAccess(i, j),
400                                  m_cache[i][j]->getDataBlk());
401                    warmedUpBlocks++;
402                }
403            }
404        }
405    }
406
407    DPRINTF(RubyCacheTrace, "%s: %lli blocks of %lli total blocks"
408            "recorded %.2f%% \n", name().c_str(), warmedUpBlocks,
409            (uint64)m_cache_num_sets * (uint64)m_cache_assoc,
410            (float(warmedUpBlocks)/float(totalBlocks))*100.0);
411}
412
413void
414CacheMemory::print(ostream& out) const
415{
416    out << "Cache dump: " << m_cache_name << endl;
417    for (int i = 0; i < m_cache_num_sets; i++) {
418        for (int j = 0; j < m_cache_assoc; j++) {
419            if (m_cache[i][j] != NULL) {
420                out << "  Index: " << i
421                    << " way: " << j
422                    << " entry: " << *m_cache[i][j] << endl;
423            } else {
424                out << "  Index: " << i
425                    << " way: " << j
426                    << " entry: NULL" << endl;
427            }
428        }
429    }
430}
431
432void
433CacheMemory::printData(ostream& out) const
434{
435    out << "printData() not supported" << endl;
436}
437
438void
439CacheMemory::clearStats() const
440{
441    m_profiler_ptr->clearStats();
442}
443
444void
445CacheMemory::printStats(ostream& out) const
446{
447    m_profiler_ptr->printStats(out);
448}
449
450void
451CacheMemory::setLocked(const Address& address, int context)
452{
453    DPRINTF(RubyCache, "Setting Lock for addr: %x to %d\n", address, context);
454    assert(address == line_address(address));
455    Index cacheSet = addressToCacheSet(address);
456    int loc = findTagInSet(cacheSet, address);
457    assert(loc != -1);
458    m_cache[cacheSet][loc]->m_locked = context;
459}
460
461void
462CacheMemory::clearLocked(const Address& address)
463{
464    DPRINTF(RubyCache, "Clear Lock for addr: %x\n", address);
465    assert(address == line_address(address));
466    Index cacheSet = addressToCacheSet(address);
467    int loc = findTagInSet(cacheSet, address);
468    assert(loc != -1);
469    m_cache[cacheSet][loc]->m_locked = -1;
470}
471
472bool
473CacheMemory::isLocked(const Address& address, int context)
474{
475    assert(address == line_address(address));
476    Index cacheSet = addressToCacheSet(address);
477    int loc = findTagInSet(cacheSet, address);
478    assert(loc != -1);
479    DPRINTF(RubyCache, "Testing Lock for addr: %llx cur %d con %d\n",
480            address, m_cache[cacheSet][loc]->m_locked, context);
481    return m_cache[cacheSet][loc]->m_locked == context;
482}
483
484void
485CacheMemory::recordRequestType(CacheRequestType requestType) {
486    DPRINTF(RubyStats, "Recorded statistic: %s\n",
487            CacheRequestType_to_string(requestType));
488    switch(requestType) {
489    case CacheRequestType_DataArrayRead:
490        numDataArrayReads++;
491        return;
492    case CacheRequestType_DataArrayWrite:
493        numDataArrayWrites++;
494        return;
495    case CacheRequestType_TagArrayRead:
496        numTagArrayReads++;
497        return;
498    case CacheRequestType_TagArrayWrite:
499        numTagArrayWrites++;
500        return;
501    default:
502        warn("CacheMemory access_type not found: %s",
503             CacheRequestType_to_string(requestType));
504    }
505}
506
507void
508CacheMemory::regStats() {
509    using namespace Stats;
510
511    numDataArrayReads
512        .name(name() + ".num_data_array_reads")
513        .desc("number of data array reads")
514        ;
515
516    numDataArrayWrites
517        .name(name() + ".num_data_array_writes")
518        .desc("number of data array writes")
519        ;
520
521    numTagArrayReads
522        .name(name() + ".num_tag_array_reads")
523        .desc("number of tag array reads")
524        ;
525
526    numTagArrayWrites
527        .name(name() + ".num_tag_array_writes")
528        .desc("number of tag array writes")
529        ;
530
531    numTagArrayStalls
532        .name(name() + ".num_tag_array_stalls")
533        .desc("number of stalls caused by tag array")
534        ;
535
536    numDataArrayStalls
537        .name(name() + ".num_data_array_stalls")
538        .desc("number of stalls caused by data array")
539        ;
540}
541
542bool
543CacheMemory::checkResourceAvailable(CacheResourceType res, Address addr)
544{
545    if (!m_resource_stalls) {
546        return true;
547    }
548
549    if (res == CacheResourceType_TagArray) {
550        if (tagArray.tryAccess(addressToCacheSet(addr))) return true;
551        else {
552            DPRINTF(RubyResourceStalls, "Tag array stall on addr %s in set %d\n", addr, addressToCacheSet(addr));
553            numTagArrayStalls++;
554            return false;
555        }
556    } else if (res == CacheResourceType_DataArray) {
557        if (dataArray.tryAccess(addressToCacheSet(addr))) return true;
558        else {
559            DPRINTF(RubyResourceStalls, "Data array stall on addr %s in set %d\n", addr, addressToCacheSet(addr));
560            numDataArrayStalls++;
561            return false;
562        }
563    } else {
564        assert(false);
565        return true;
566    }
567}
568
569