base_set_assoc.cc revision 12684:44ebd2bc020f
11917SN/A/*
21917SN/A * Copyright (c) 2012-2014 ARM Limited
31917SN/A * All rights reserved.
41917SN/A *
51917SN/A * The license below extends only to copyright in the software and shall
61917SN/A * not be construed as granting a license to any other intellectual
71917SN/A * property including but not limited to intellectual property relating
81917SN/A * to a hardware implementation of the functionality of the software
91917SN/A * licensed hereunder.  You may use the software subject to the license
101917SN/A * terms below provided that you ensure that this notice is replicated
111917SN/A * unmodified and in its entirety in all distributions of the software,
121917SN/A * modified or unmodified, in source code or in binary form.
131917SN/A *
141917SN/A * Copyright (c) 2003-2005,2014 The Regents of The University of Michigan
151917SN/A * All rights reserved.
161917SN/A *
171917SN/A * Redistribution and use in source and binary forms, with or without
181917SN/A * modification, are permitted provided that the following conditions are
191917SN/A * met: redistributions of source code must retain the above copyright
201917SN/A * notice, this list of conditions and the following disclaimer;
211917SN/A * redistributions in binary form must reproduce the above copyright
221917SN/A * notice, this list of conditions and the following disclaimer in the
231917SN/A * documentation and/or other materials provided with the distribution;
241917SN/A * neither the name of the copyright holders nor the names of its
251917SN/A * contributors may be used to endorse or promote products derived from
261917SN/A * this software without specific prior written permission.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
291917SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
301917SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
311917SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
321917SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
331917SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
341917SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
351917SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
361917SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
372680Sktlim@umich.edu * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
381917SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
391917SN/A *
401917SN/A * Authors: Erik Hallnor
411917SN/A */
421917SN/A
432680Sktlim@umich.edu/**
441917SN/A * @file
451917SN/A * Definitions of a base set associative tag store.
461917SN/A */
471917SN/A
481917SN/A#include "mem/cache/tags/base_set_assoc.hh"
491917SN/A
501917SN/A#include <string>
511917SN/A
522680Sktlim@umich.edu#include "base/intmath.hh"
531917SN/A
541917SN/ABaseSetAssoc::BaseSetAssoc(const Params *p)
551917SN/A    :BaseTags(p), assoc(p->assoc), allocAssoc(p->assoc),
561917SN/A     blks(p->size / p->block_size),
571917SN/A     numSets(p->size / (p->block_size * p->assoc)),
581917SN/A     sequentialAccess(p->sequential_access),
591917SN/A     sets(p->size / (p->block_size * p->assoc)),
601917SN/A     replacementPolicy(p->replacement_policy)
612107SN/A{
622107SN/A    // Check parameters
631917SN/A    if (blkSize < 4 || !isPowerOf2(blkSize)) {
642680Sktlim@umich.edu        fatal("Block size must be at least 4 and a power of 2");
651917SN/A    }
661917SN/A    if (!isPowerOf2(numSets)) {
671917SN/A        fatal("# of sets must be non-zero and a power of 2");
681917SN/A    }
691917SN/A    if (assoc <= 0) {
701917SN/A        fatal("associativity must be greater than zero");
711917SN/A    }
721917SN/A
732680Sktlim@umich.edu    setShift = floorLog2(blkSize);
741977SN/A    setMask = numSets - 1;
751917SN/A    tagShift = setShift + floorLog2(numSets);
761977SN/A
772680Sktlim@umich.edu    unsigned blkIndex = 0;       // index into blks array
781917SN/A    for (unsigned i = 0; i < numSets; ++i) {
791917SN/A        sets[i].assoc = assoc;
801977SN/A
811977SN/A        sets[i].blks.resize(assoc);
822680Sktlim@umich.edu
831977SN/A        // link in the data blocks
841977SN/A        for (unsigned j = 0; j < assoc; ++j) {
851977SN/A            // Select block within the set to be linked
862680Sktlim@umich.edu            BlkType*& blk = sets[i].blks[j];
872680Sktlim@umich.edu
881977SN/A            // Locate next cache block
891917SN/A            blk = &blks[blkIndex];
901917SN/A
911977SN/A            // Associate a data chunk to the block
921977SN/A            blk->data = &dataBlks[blkSize*blkIndex];
931977SN/A
941977SN/A            // Associate a replacement data entry to the block
951917SN/A            blk->replacementData = replacementPolicy->instantiateEntry();
961917SN/A
971917SN/A            // Setting the tag to j is just to prevent long chains in the
981917SN/A            // hash table; won't matter because the block is invalid
991917SN/A            blk->tag = j;
1001917SN/A
1011917SN/A            // Set its set and way
1021917SN/A            blk->set = i;
1031917SN/A            blk->way = j;
1041917SN/A
1051917SN/A            // Update block index
1061917SN/A            ++blkIndex;
1071917SN/A        }
1081977SN/A    }
1092680Sktlim@umich.edu}
1101917SN/A
1111917SN/Avoid
1121977SN/ABaseSetAssoc::invalidate(CacheBlk *blk)
1131917SN/A{
1141977SN/A    BaseTags::invalidate(blk);
1151977SN/A
1161977SN/A    // Invalidate replacement data
1172680Sktlim@umich.edu    replacementPolicy->invalidate(blk->replacementData);
1181977SN/A}
1191917SN/A
1201917SN/ACacheBlk*
1211917SN/ABaseSetAssoc::findBlock(Addr addr, bool is_secure) const
122{
123    Addr tag = extractTag(addr);
124    unsigned set = extractSet(addr);
125    BlkType *blk = sets[set].findBlk(tag, is_secure);
126    return blk;
127}
128
129CacheBlk*
130BaseSetAssoc::findBlockBySetAndWay(int set, int way) const
131{
132    return sets[set].blks[way];
133}
134
135std::string
136BaseSetAssoc::print() const {
137    std::string cache_state;
138    for (const CacheBlk& blk : blks) {
139        if (blk.isValid())
140            cache_state += csprintf("\tset: %d way: %d %s\n", blk.set,
141                                    blk.way, blk.print());
142    }
143    if (cache_state.empty())
144        cache_state = "no valid tags\n";
145    return cache_state;
146}
147
148void
149BaseSetAssoc::cleanupRefs()
150{
151    for (const CacheBlk& blk : blks) {
152        if (blk.isValid()) {
153            totalRefs += blk.refCount;
154            ++sampledRefs;
155        }
156    }
157}
158
159void
160BaseSetAssoc::computeStats()
161{
162    for (unsigned i = 0; i < ContextSwitchTaskId::NumTaskId; ++i) {
163        occupanciesTaskId[i] = 0;
164        for (unsigned j = 0; j < 5; ++j) {
165            ageTaskId[i][j] = 0;
166        }
167    }
168
169    for (const CacheBlk& blk : blks) {
170        if (blk.isValid()) {
171            assert(blk.task_id < ContextSwitchTaskId::NumTaskId);
172            occupanciesTaskId[blk.task_id]++;
173            assert(blk.tickInserted <= curTick());
174            Tick age = curTick() - blk.tickInserted;
175
176            int age_index;
177            if (age / SimClock::Int::us < 10) { // <10us
178                age_index = 0;
179            } else if (age / SimClock::Int::us < 100) { // <100us
180                age_index = 1;
181            } else if (age / SimClock::Int::ms < 1) { // <1ms
182                age_index = 2;
183            } else if (age / SimClock::Int::ms < 10) { // <10ms
184                age_index = 3;
185            } else
186                age_index = 4; // >10ms
187
188            ageTaskId[blk.task_id][age_index]++;
189        }
190    }
191}
192
193BaseSetAssoc *
194BaseSetAssocParams::create()
195{
196    return new BaseSetAssoc(this);
197}
198