2 * Copyright (c) 1999-2008 Mark D. Hill and David A. Wood
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.
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.
29 #include "base/intmath.hh"
30 #include "base/str.hh"
31 #include "mem/gems_common/Map.hh"
32 #include "mem/ruby/common/Address.hh"
33 #include "mem/ruby/filters/MultiGrainBloomFilter.hh"
37 MultiGrainBloomFilter::MultiGrainBloomFilter(string str
)
43 split_first(str
, head
, tail
, '_');
46 // head contains size of 1st bloom filter, tail contains size of
49 m_filter_size
= atoi(head
.c_str());
50 m_filter_size_bits
= floorLog2(m_filter_size
);
52 m_page_filter_size
= atoi(tail
.c_str());
53 m_page_filter_size_bits
= floorLog2(m_page_filter_size
);
55 m_filter
.resize(m_filter_size
);
56 m_page_filter
.resize(m_page_filter_size
);
60 MultiGrainBloomFilter::~MultiGrainBloomFilter()
65 MultiGrainBloomFilter::clear()
67 for (int i
= 0; i
< m_filter_size
; i
++) {
70 for(int i
=0; i
< m_page_filter_size
; ++i
){
76 MultiGrainBloomFilter::increment(const Address
& addr
)
83 MultiGrainBloomFilter::decrement(const Address
& addr
)
89 MultiGrainBloomFilter::merge(AbstractBloomFilter
*other_filter
)
95 MultiGrainBloomFilter::set(const Address
& addr
)
97 int i
= get_block_index(addr
);
98 assert(i
< m_filter_size
);
99 assert(get_page_index(addr
) < m_page_filter_size
);
101 m_page_filter
[i
] = 1;
106 MultiGrainBloomFilter::unset(const Address
& addr
)
112 MultiGrainBloomFilter::isSet(const Address
& addr
)
114 int i
= get_block_index(addr
);
115 assert(i
< m_filter_size
);
116 assert(get_page_index(addr
) < m_page_filter_size
);
117 // we have to have both indices set
118 return (m_filter
[i
] && m_page_filter
[i
]);
122 MultiGrainBloomFilter::getCount(const Address
& addr
)
129 MultiGrainBloomFilter::getTotalCount()
133 for (int i
= 0; i
< m_filter_size
; i
++) {
134 count
+= m_filter
[i
];
137 for(int i
=0; i
< m_page_filter_size
; ++i
) {
138 count
+= m_page_filter
[i
] = 0;
145 MultiGrainBloomFilter::getIndex(const Address
& addr
)
152 MultiGrainBloomFilter::readBit(const int index
)
159 MultiGrainBloomFilter::writeBit(const int index
, const int value
)
165 MultiGrainBloomFilter::print(ostream
& out
) const
170 MultiGrainBloomFilter::get_block_index(const Address
& addr
)
172 // grap a chunk of bits after byte offset
173 return addr
.bitSelect(RubySystem::getBlockSizeBits(),
174 RubySystem::getBlockSizeBits() +
175 m_filter_size_bits
- 1);
179 MultiGrainBloomFilter::get_page_index(const Address
& addr
)
181 int bits
= RubySystem::getBlockSizeBits() + m_filter_size_bits
- 1;
183 // grap a chunk of bits after first chunk
184 return addr
.bitSelect(bits
, bits
+ m_page_filter_size_bits
- 1);