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/ruby/filters/MultiGrainBloomFilter.hh"
35 MultiGrainBloomFilter::MultiGrainBloomFilter(string str
)
41 split_first(str
, head
, tail
, '_');
44 // head contains size of 1st bloom filter, tail contains size of
47 m_filter_size
= atoi(head
.c_str());
48 m_filter_size_bits
= floorLog2(m_filter_size
);
50 m_page_filter_size
= atoi(tail
.c_str());
51 m_page_filter_size_bits
= floorLog2(m_page_filter_size
);
53 m_filter
.resize(m_filter_size
);
54 m_page_filter
.resize(m_page_filter_size
);
58 MultiGrainBloomFilter::~MultiGrainBloomFilter()
63 MultiGrainBloomFilter::clear()
65 for (int i
= 0; i
< m_filter_size
; i
++) {
68 for(int i
=0; i
< m_page_filter_size
; ++i
){
74 MultiGrainBloomFilter::increment(const Address
& addr
)
81 MultiGrainBloomFilter::decrement(const Address
& addr
)
87 MultiGrainBloomFilter::merge(AbstractBloomFilter
*other_filter
)
93 MultiGrainBloomFilter::set(const Address
& addr
)
95 int i
= get_block_index(addr
);
96 assert(i
< m_filter_size
);
97 assert(get_page_index(addr
) < m_page_filter_size
);
104 MultiGrainBloomFilter::unset(const Address
& addr
)
110 MultiGrainBloomFilter::isSet(const Address
& addr
)
112 int i
= get_block_index(addr
);
113 assert(i
< m_filter_size
);
114 assert(get_page_index(addr
) < m_page_filter_size
);
115 // we have to have both indices set
116 return (m_filter
[i
] && m_page_filter
[i
]);
120 MultiGrainBloomFilter::getCount(const Address
& addr
)
127 MultiGrainBloomFilter::getTotalCount()
131 for (int i
= 0; i
< m_filter_size
; i
++) {
132 count
+= m_filter
[i
];
135 for(int i
=0; i
< m_page_filter_size
; ++i
) {
136 count
+= m_page_filter
[i
] = 0;
143 MultiGrainBloomFilter::getIndex(const Address
& addr
)
150 MultiGrainBloomFilter::readBit(const int index
)
157 MultiGrainBloomFilter::writeBit(const int index
, const int value
)
163 MultiGrainBloomFilter::print(ostream
& out
) const
168 MultiGrainBloomFilter::get_block_index(const Address
& addr
)
170 // grap a chunk of bits after byte offset
171 return addr
.bitSelect(RubySystem::getBlockSizeBits(),
172 RubySystem::getBlockSizeBits() +
173 m_filter_size_bits
- 1);
177 MultiGrainBloomFilter::get_page_index(const Address
& addr
)
179 int bits
= RubySystem::getBlockSizeBits() + m_filter_size_bits
- 1;
181 // grap a chunk of bits after first chunk
182 return addr
.bitSelect(bits
, bits
+ m_page_filter_size_bits
- 1);