compressed_tags.cc revision 13945:a573bed35a8b
1/*
2 * Copyright (c) 2018 Inria
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: Daniel Carvalho
29 */
30
31/**
32 * @file
33 * Definitions of a base set associative compressed superblocks tag store.
34 */
35
36#include "mem/cache/tags/compressed_tags.hh"
37
38#include "mem/cache/replacement_policies/base.hh"
39#include "mem/cache/tags/indexing_policies/base.hh"
40#include "mem/packet.hh"
41#include "params/CompressedTags.hh"
42
43CompressedTags::CompressedTags(const Params *p)
44    : SectorTags(p)
45{
46}
47
48void
49CompressedTags::tagsInit()
50{
51    // Create blocks and superblocks
52    blks = std::vector<CompressionBlk>(numBlocks);
53    superBlks = std::vector<SuperBlk>(numSectors);
54
55    // Initialize all blocks
56    unsigned blk_index = 0;          // index into blks array
57    for (unsigned superblock_index = 0; superblock_index < numSectors;
58         superblock_index++)
59    {
60        // Locate next cache superblock
61        SuperBlk* superblock = &superBlks[superblock_index];
62
63        // Link block to indexing policy
64        indexingPolicy->setEntry(superblock, superblock_index);
65
66        // Associate a replacement data entry to the block
67        superblock->replacementData = replacementPolicy->instantiateEntry();
68
69        // Initialize all blocks in this superblock
70        superblock->blks.resize(numBlocksPerSector, nullptr);
71        for (unsigned k = 0; k < numBlocksPerSector; ++k){
72            // Select block within the set to be linked
73            SectorSubBlk*& blk = superblock->blks[k];
74
75            // Locate next cache block
76            blk = &blks[blk_index];
77
78            // Associate a data chunk to the block
79            blk->data = &dataBlks[blkSize*blk_index];
80
81            // Associate superblock to this block
82            blk->setSectorBlock(superblock);
83
84            // Associate the superblock replacement data to this block
85            blk->replacementData = superblock->replacementData;
86
87            // Set its index and sector offset
88            blk->setSectorOffset(k);
89
90            // Update block index
91            ++blk_index;
92        }
93    }
94}
95
96void
97CompressedTags::insertBlock(const PacketPtr pkt, CacheBlk *blk)
98{
99    // Insert block
100    SectorTags::insertBlock(pkt, blk);
101
102    // @todo We always store compressed blocks when possible
103    CompressionBlk* compression_blk = static_cast<CompressionBlk*>(blk);
104    compression_blk->setUncompressed();
105}
106
107void
108CompressedTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
109{
110    for (CompressionBlk& blk : blks) {
111        visitor(blk);
112    }
113}
114
115bool
116CompressedTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
117{
118    for (CompressionBlk& blk : blks) {
119        if (visitor(blk)) {
120            return true;
121        }
122    }
123    return false;
124}
125
126CompressedTags *
127CompressedTagsParams::create()
128{
129    return new CompressedTags(this);
130}
131