cc799df24fa208a88d01ac50c337146921f1c8bb
[gem5.git] / src / mem / cache / tags / compressed_tags.cc
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 "params/CompressedTags.hh"
41
42 CompressedTags::CompressedTags(const Params *p)
43 : SectorTags(p)
44 {
45 }
46
47 void
48 CompressedTags::tagsInit()
49 {
50 // Create blocks and superblocks
51 blks = std::vector<CompressionBlk>(numBlocks);
52 superBlks = std::vector<SuperBlk>(numSectors);
53
54 // Initialize all blocks
55 unsigned blk_index = 0; // index into blks array
56 for (unsigned superblock_index = 0; superblock_index < numSectors;
57 superblock_index++)
58 {
59 // Locate next cache superblock
60 SuperBlk* superblock = &superBlks[superblock_index];
61
62 // Link block to indexing policy
63 indexingPolicy->setEntry(superblock, superblock_index);
64
65 // Associate a replacement data entry to the block
66 superblock->replacementData = replacementPolicy->instantiateEntry();
67
68 // Initialize all blocks in this superblock
69 superblock->blks.resize(numBlocksPerSector, nullptr);
70 for (unsigned k = 0; k < numBlocksPerSector; ++k){
71 // Select block within the set to be linked
72 SectorSubBlk*& blk = superblock->blks[k];
73
74 // Locate next cache block
75 blk = &blks[blk_index];
76
77 // Associate a data chunk to the block
78 blk->data = &dataBlks[blkSize*blk_index];
79
80 // Associate superblock to this block
81 blk->setSectorBlock(superblock);
82
83 // Associate the superblock replacement data to this block
84 blk->replacementData = superblock->replacementData;
85
86 // Set its index and sector offset
87 blk->setSectorOffset(k);
88
89 // Update block index
90 ++blk_index;
91 }
92 }
93 }
94
95 void
96 CompressedTags::forEachBlk(std::function<void(CacheBlk &)> visitor)
97 {
98 for (CompressionBlk& blk : blks) {
99 visitor(blk);
100 }
101 }
102
103 bool
104 CompressedTags::anyBlk(std::function<bool(CacheBlk &)> visitor)
105 {
106 for (CompressionBlk& blk : blks) {
107 if (visitor(blk)) {
108 return true;
109 }
110 }
111 return false;
112 }
113
114 CompressedTags *
115 CompressedTagsParams::create()
116 {
117 return new CompressedTags(this);
118 }