Merge branch 'era' of github.com:jthornber/thin-provisioning-tools into era
This commit is contained in:
@@ -19,7 +19,7 @@
|
||||
#ifndef ARRAY_BLOCK_H
|
||||
#define ARRAY_BLOCK_H
|
||||
|
||||
#include "persistent-data/endian_utils.h"
|
||||
#include "base/endian_utils.h"
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
|
||||
@@ -46,6 +46,10 @@ namespace persistent_data {
|
||||
return array_.get_root();
|
||||
}
|
||||
|
||||
unsigned get_nr_bits() const {
|
||||
return nr_bits_;
|
||||
}
|
||||
|
||||
void grow(unsigned new_nr_bits, bool default_value) {
|
||||
pad_last_block(default_value);
|
||||
resize_array(new_nr_bits, default_value);
|
||||
@@ -184,7 +188,7 @@ namespace persistent_data {
|
||||
if (n >= nr_bits_) {
|
||||
std::ostringstream str;
|
||||
str << "bitset index out of bounds ("
|
||||
<< n << " >= " << nr_bits_ << endl;
|
||||
<< n << " >= " << nr_bits_ << ")";
|
||||
throw runtime_error(str.str());
|
||||
}
|
||||
}
|
||||
@@ -214,6 +218,12 @@ persistent_data::bitset::get_root() const
|
||||
return impl_->get_root();
|
||||
}
|
||||
|
||||
unsigned
|
||||
bitset::get_nr_bits() const
|
||||
{
|
||||
return impl_->get_nr_bits();
|
||||
}
|
||||
|
||||
void
|
||||
persistent_data::bitset::grow(unsigned new_nr_bits, bool default_value)
|
||||
{
|
||||
|
||||
@@ -16,8 +16,8 @@
|
||||
// with thin-provisioning-tools. If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef BITSET_H
|
||||
#define BITSET_H
|
||||
#ifndef PERSISTENT_DATA_DATA_STRUCTURES_BITSET_H
|
||||
#define PERSISTENT_DATA_DATA_STRUCTURES_BITSET_H
|
||||
|
||||
#include "persistent-data/run.h"
|
||||
|
||||
@@ -54,6 +54,7 @@ namespace persistent_data {
|
||||
bitset(tm_ptr tm);
|
||||
bitset(tm_ptr tm, block_address root, unsigned nr_bits);
|
||||
block_address get_root() const;
|
||||
unsigned get_nr_bits() const;
|
||||
void grow(unsigned new_nr_bits, bool default_value);
|
||||
void destroy();
|
||||
|
||||
|
||||
146
persistent-data/data-structures/bloom_filter.cc
Normal file
146
persistent-data/data-structures/bloom_filter.cc
Normal file
@@ -0,0 +1,146 @@
|
||||
#include "persistent-data/data-structures/bloom_filter.h"
|
||||
|
||||
#include <stdexcept>
|
||||
|
||||
using namespace persistent_data;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace {
|
||||
static const uint64_t m1 = 0x9e37fffffffc0001UL;
|
||||
static const unsigned bits = 18;
|
||||
|
||||
static uint32_t hash1(block_address const &b) {
|
||||
return (b * m1) >> bits;
|
||||
}
|
||||
|
||||
static uint32_t hash2(block_address const &b) {
|
||||
uint32_t n = b;
|
||||
|
||||
n = n ^ (n >> 16);
|
||||
n = n * 0x85ebca6bu;
|
||||
n = n ^ (n >> 13);
|
||||
n = n * 0xc2b2ae35u;
|
||||
n = n ^ (n >> 16);
|
||||
|
||||
return n;
|
||||
}
|
||||
|
||||
void check_power_of_two(unsigned nr_bits) {
|
||||
if (nr_bits & (nr_bits - 1))
|
||||
throw std::runtime_error("bloom filter needs a power of two nr_bits");
|
||||
}
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
bloom_filter::bloom_filter(tm_ptr tm,
|
||||
unsigned nr_bits, unsigned nr_probes)
|
||||
: tm_(tm),
|
||||
bits_(tm),
|
||||
nr_probes_(nr_probes),
|
||||
mask_(nr_bits - 1)
|
||||
{
|
||||
check_power_of_two(nr_bits);
|
||||
bits_.grow(nr_bits, false);
|
||||
}
|
||||
|
||||
bloom_filter::bloom_filter(tm_ptr tm, block_address root,
|
||||
unsigned nr_bits, unsigned nr_probes)
|
||||
: tm_(tm),
|
||||
bits_(tm, root, nr_bits),
|
||||
nr_probes_(nr_probes),
|
||||
mask_(nr_bits - 1)
|
||||
{
|
||||
check_power_of_two(nr_bits);
|
||||
}
|
||||
|
||||
block_address
|
||||
bloom_filter::get_root() const
|
||||
{
|
||||
return bits_.get_root();
|
||||
}
|
||||
|
||||
bool
|
||||
bloom_filter::test(uint64_t b)
|
||||
{
|
||||
vector<unsigned> probes(nr_probes_);
|
||||
fill_probes(b, probes);
|
||||
|
||||
for (unsigned p = 0; p < nr_probes_; p++)
|
||||
if (!bits_.get(probes[p]))
|
||||
return false;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void
|
||||
bloom_filter::set(uint64_t b)
|
||||
{
|
||||
vector<unsigned> probes(nr_probes_);
|
||||
fill_probes(b, probes);
|
||||
|
||||
for (unsigned p = 0; p < nr_probes_; p++)
|
||||
bits_.set(probes[p], true);
|
||||
}
|
||||
|
||||
void
|
||||
bloom_filter::flush()
|
||||
{
|
||||
bits_.flush();
|
||||
}
|
||||
|
||||
void
|
||||
bloom_filter::fill_probes(block_address b, vector<unsigned> &probes) const
|
||||
{
|
||||
uint32_t h1 = hash1(b) & mask_;
|
||||
uint32_t h2 = hash2(b) & mask_;
|
||||
|
||||
probes[0] = h1;
|
||||
for (unsigned p = 1; p < nr_probes_; p++) {
|
||||
h1 = (h1 + h2) & mask_;
|
||||
h2 = (h2 + p) & mask_;
|
||||
probes[p] = h1;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
bloom_filter::print_debug(ostream &out)
|
||||
{
|
||||
print_residency(out);
|
||||
|
||||
map<unsigned, unsigned> runs;
|
||||
|
||||
for (unsigned i = 0; i < bits_.get_nr_bits();) {
|
||||
bool v = bits_.get(i);
|
||||
unsigned run_length = 1;
|
||||
|
||||
while (++i < bits_.get_nr_bits() && bits_.get(i) == v)
|
||||
run_length++;
|
||||
|
||||
map<unsigned, unsigned>::iterator it = runs.find(run_length);
|
||||
if (it != runs.end())
|
||||
it->second++;
|
||||
else
|
||||
runs.insert(make_pair(run_length, 1));
|
||||
}
|
||||
|
||||
{
|
||||
map<unsigned, unsigned>::const_iterator it;
|
||||
for (it = runs.begin(); it != runs.end(); ++it)
|
||||
out << it->first << ": " << it->second << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
bloom_filter::print_residency(ostream &out)
|
||||
{
|
||||
unsigned count = 0;
|
||||
for (unsigned i = 0; i < bits_.get_nr_bits(); i++)
|
||||
if (bits_.get(i))
|
||||
count++;
|
||||
|
||||
out << "residency: " << count << "/" << bits_.get_nr_bits() << endl;
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
47
persistent-data/data-structures/bloom_filter.h
Normal file
47
persistent-data/data-structures/bloom_filter.h
Normal file
@@ -0,0 +1,47 @@
|
||||
#ifndef PERSISTENT_DATA_DATA_STRUCTURES_BLOOM_FILTER_H
|
||||
#define PERSISTENT_DATA_DATA_STRUCTURES_BLOOM_FILTER_H
|
||||
|
||||
#include "persistent-data/transaction_manager.h"
|
||||
#include "persistent-data/data-structures/bitset.h"
|
||||
|
||||
#include <boost/shared_ptr.hpp>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
namespace persistent_data {
|
||||
class bloom_filter {
|
||||
public:
|
||||
typedef boost::shared_ptr<bloom_filter> ptr;
|
||||
typedef typename persistent_data::transaction_manager::ptr tm_ptr;
|
||||
|
||||
// nr_bits must be a power of two
|
||||
bloom_filter(tm_ptr tm,
|
||||
unsigned nr_bits, unsigned nr_probes);
|
||||
|
||||
bloom_filter(tm_ptr tm, block_address root,
|
||||
unsigned nr_bits_power, unsigned nr_probes);
|
||||
|
||||
block_address get_root() const;
|
||||
|
||||
bool test(uint64_t b); // not const due to caching effects in bitset
|
||||
void set(uint64_t b);
|
||||
void flush();
|
||||
|
||||
void print_debug(ostream &out);
|
||||
|
||||
private:
|
||||
void print_residency(ostream &out);
|
||||
|
||||
void fill_probes(block_address b, vector<unsigned> &probes) const;
|
||||
|
||||
tm_ptr tm_;
|
||||
unsigned nr_bits_;
|
||||
persistent_data::bitset bits_;
|
||||
unsigned nr_probes_;
|
||||
uint64_t mask_;
|
||||
};
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
@@ -19,7 +19,7 @@
|
||||
#ifndef BTREE_H
|
||||
#define BTREE_H
|
||||
|
||||
#include "persistent-data/endian_utils.h"
|
||||
#include "base/endian_utils.h"
|
||||
#include "persistent-data/transaction_manager.h"
|
||||
#include "persistent-data/data-structures/ref_counter.h"
|
||||
|
||||
|
||||
@@ -1,64 +0,0 @@
|
||||
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
||||
//
|
||||
// This file is part of the thin-provisioning-tools source.
|
||||
//
|
||||
// thin-provisioning-tools is free software: you can redistribute it
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// thin-provisioning-tools is distributed in the hope that it will be
|
||||
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with thin-provisioning-tools. If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "endian_utils.h"
|
||||
|
||||
using namespace base;
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
bool
|
||||
base::test_bit_le(void const *bits, unsigned b)
|
||||
{
|
||||
le64 const *w = reinterpret_cast<le64 const *>(bits);
|
||||
w += b / 64;
|
||||
|
||||
uint64_t v = to_cpu<uint64_t>(*w);
|
||||
uint64_t mask = 1;
|
||||
mask = mask << (b % 64);
|
||||
return (v & mask) ? true : false;
|
||||
}
|
||||
|
||||
void
|
||||
base::set_bit_le(void *bits, unsigned b)
|
||||
{
|
||||
le64 *w = reinterpret_cast<le64 *>(bits);
|
||||
w += b / 64;
|
||||
|
||||
uint64_t v = to_cpu<uint64_t>(*w);
|
||||
uint64_t mask = 1;
|
||||
mask = mask << (b % 64);
|
||||
v |= mask;
|
||||
*w = to_disk<le64>(v);
|
||||
}
|
||||
|
||||
void
|
||||
base::clear_bit_le(void *bits, unsigned b)
|
||||
{
|
||||
le64 *w = reinterpret_cast<le64 *>(bits);
|
||||
w += b / 64;
|
||||
|
||||
uint64_t v = to_cpu<uint64_t>(*w);
|
||||
uint64_t mask = 1;
|
||||
mask = mask << (b % 64);
|
||||
mask = ~mask;
|
||||
v &= mask;
|
||||
*w = to_disk<le64>(v);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
@@ -1,110 +0,0 @@
|
||||
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
||||
//
|
||||
// This file is part of the thin-provisioning-tools source.
|
||||
//
|
||||
// thin-provisioning-tools is free software: you can redistribute it
|
||||
// and/or modify it under the terms of the GNU General Public License
|
||||
// as published by the Free Software Foundation, either version 3 of
|
||||
// the License, or (at your option) any later version.
|
||||
//
|
||||
// thin-provisioning-tools is distributed in the hope that it will be
|
||||
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
||||
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
// GNU General Public License for more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along
|
||||
// with thin-provisioning-tools. If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
#ifndef ENDIAN_H
|
||||
#define ENDIAN_H
|
||||
|
||||
#include <endian.h>
|
||||
#include <stdint.h>
|
||||
#include <boost/static_assert.hpp>
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
// FIXME: rename to endian
|
||||
namespace base {
|
||||
|
||||
// These are just little wrapper types to make the compiler
|
||||
// understand that the le types are not assignable to the
|
||||
// corresponding cpu type.
|
||||
struct le16 {
|
||||
explicit le16(uint16_t v = 0)
|
||||
: v_(v) {
|
||||
}
|
||||
|
||||
uint16_t v_;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct le32 {
|
||||
explicit le32(uint32_t v = 0)
|
||||
: v_(v) {
|
||||
}
|
||||
|
||||
uint32_t v_;
|
||||
} __attribute__((packed));
|
||||
|
||||
struct le64 {
|
||||
explicit le64(uint64_t v = 0)
|
||||
: v_(v) {
|
||||
}
|
||||
|
||||
uint64_t v_;
|
||||
} __attribute__((packed));
|
||||
|
||||
//--------------------------------
|
||||
|
||||
// FIXME: actually do the conversions !
|
||||
template <typename CPUType, typename DiskType>
|
||||
CPUType to_cpu(DiskType const &d) {
|
||||
BOOST_STATIC_ASSERT(sizeof(d) == 0);
|
||||
}
|
||||
|
||||
template <typename DiskType, typename CPUType>
|
||||
DiskType to_disk(CPUType const &v) {
|
||||
BOOST_STATIC_ASSERT(sizeof(v) == 0);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline uint16_t to_cpu<uint16_t, le16>(le16 const &d) {
|
||||
return le16toh(d.v_);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline le16 to_disk<le16, uint16_t>(uint16_t const &v) {
|
||||
return le16(htole16(v));
|
||||
}
|
||||
|
||||
template <>
|
||||
inline uint32_t to_cpu<uint32_t, le32>(le32 const &d) {
|
||||
return le32toh(d.v_);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline le32 to_disk<le32, uint32_t>(uint32_t const &v) {
|
||||
return le32(htole32(v));
|
||||
}
|
||||
|
||||
template <>
|
||||
inline uint64_t to_cpu<uint64_t, le64>(le64 const &d) {
|
||||
return le64toh(d.v_);
|
||||
}
|
||||
|
||||
template <>
|
||||
inline le64 to_disk<le64, uint64_t>(uint64_t const &v) {
|
||||
return le64(htole64(v));
|
||||
}
|
||||
|
||||
//--------------------------------
|
||||
|
||||
bool test_bit_le(void const *bits, unsigned b);
|
||||
void set_bit_le(void *bits, unsigned b);
|
||||
void clear_bit_le(void *bits, unsigned b);
|
||||
}
|
||||
|
||||
//----------------------------------------------------------------
|
||||
|
||||
#endif
|
||||
@@ -16,6 +16,8 @@
|
||||
// with thin-provisioning-tools. If not, see
|
||||
// <http://www.gnu.org/licenses/>.
|
||||
|
||||
#include "base/endian_utils.h"
|
||||
|
||||
#include "persistent-data/space-maps/disk.h"
|
||||
#include "persistent-data/space-maps/disk_structures.h"
|
||||
#include "persistent-data/space-maps/recursive.h"
|
||||
@@ -23,7 +25,6 @@
|
||||
|
||||
#include "persistent-data/data-structures/btree_damage_visitor.h"
|
||||
#include "persistent-data/checksum.h"
|
||||
#include "persistent-data/endian_utils.h"
|
||||
#include "persistent-data/math_utils.h"
|
||||
#include "persistent-data/transaction_manager.h"
|
||||
|
||||
|
||||
@@ -19,7 +19,7 @@
|
||||
#ifndef SPACE_MAP_DISK_STRUCTURES_H
|
||||
#define SPACE_MAP_DISK_STRUCTURES_H
|
||||
|
||||
#include "persistent-data/endian_utils.h"
|
||||
#include "base/endian_utils.h"
|
||||
|
||||
// FIXME: what's this included for?
|
||||
#include "persistent-data/data-structures/btree.h"
|
||||
|
||||
Reference in New Issue
Block a user