diff --git a/Makefile.in b/Makefile.in index 0392d8e..11dbf75 100644 --- a/Makefile.in +++ b/Makefile.in @@ -126,10 +126,15 @@ TOOLS_SOURCE=\ thin-provisioning/thin_trim.cc DEVTOOLS_SOURCE=\ - base/command_interpreter.cc \ - base/output_formatter.cc \ caching/cache_debug.cc \ caching/devel_commands.cc \ + dbg-lib/bitset_block_dumper.cc \ + dbg-lib/command_interpreter.cc \ + dbg-lib/commands.cc \ + dbg-lib/index_block_dumper.cc \ + dbg-lib/output_formatter.cc \ + dbg-lib/simple_show_traits.cc \ + dbg-lib/sm_show_traits.cc \ era/devel_commands.cc \ era/era_debug.cc \ thin-provisioning/damage_generator.cc \ diff --git a/caching/cache_debug.cc b/caching/cache_debug.cc index 4f0c68d..4f9e033 100644 --- a/caching/cache_debug.cc +++ b/caching/cache_debug.cc @@ -21,11 +21,13 @@ #include #include -#include "base/command_interpreter.h" -#include "base/output_formatter.h" +#include "dbg-lib/array_block_dumper.h" +#include "dbg-lib/btree_node_dumper.h" +#include "dbg-lib/command_interpreter.h" +#include "dbg-lib/commands.h" +#include "dbg-lib/output_formatter.h" +#include "dbg-lib/sm_show_traits.h" #include "persistent-data/file_utils.h" -#include "persistent-data/data-structures/btree.h" -#include "persistent-data/data-structures/simple_traits.h" #include "persistent-data/space-maps/disk_structures.h" #include "caching/commands.h" #include "caching/metadata.h" @@ -39,12 +41,6 @@ using namespace caching; //---------------------------------------------------------------- namespace { - class hello : public dbg::command { - virtual void exec(strings const &args, ostream &out) { - out << "Hello, world!" << endl; - } - }; - class help : public dbg::command { virtual void exec(strings const &args, ostream &out) { out << "Commands:" << endl @@ -55,31 +51,6 @@ namespace { } }; - class exit_handler : public dbg::command { - public: - exit_handler(command_interpreter::ptr interpreter) - : interpreter_(interpreter) { - } - - virtual void exec(strings const &args, ostream &out) { - out << "Goodbye!" << endl; - interpreter_->exit_main_loop(); - } - - command_interpreter::ptr interpreter_; - }; - - class sm_root_show_traits : public persistent_data::sm_disk_detail::sm_root_traits { - public: - static void show(formatter::ptr f, string const &key, - persistent_data::sm_disk_detail::sm_root const &value) { - field(*f, "nr_blocks", value.nr_blocks_); - field(*f, "nr_allocated", value.nr_allocated_); - field(*f, "bitmap_root", value.bitmap_root_); - field(*f, "ref_count_root", value.ref_count_root_); - } - }; - class show_superblock : public dbg::command { public: explicit show_superblock(block_manager::ptr bm) @@ -145,16 +116,6 @@ namespace { block_manager::ptr bm_; }; - // FIXME: duplication - class uint64_show_traits : public uint64_traits { - public: - typedef uint64_traits value_trait; - - static void show(formatter::ptr f, string const &key, uint64_t const &value) { - field(*f, key, boost::lexical_cast(value)); - } - }; - class mapping_show_traits : public caching::mapping_traits { public: typedef mapping_traits value_trait; @@ -165,114 +126,34 @@ namespace { } }; - // FIXME: duplication - template - class show_btree_node : public dbg::command { - public: - explicit show_btree_node(block_manager::ptr bm) - : bm_(bm) { - } - - virtual void exec(strings const &args, ostream &out) { - using namespace persistent_data::btree_detail; - - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = bm_->read_lock(block); - - node_ref n = btree_detail::to_node(rr); - if (n.get_type() == INTERNAL) - show_node(n, out); - else { - node_ref n = btree_detail::to_node(rr); - show_node(n, out); - } - } - - private: - template - void show_node(node_ref n, ostream &out) { - formatter::ptr f = create_xml_formatter(); - - field(*f, "csum", n.get_checksum()); - field(*f, "blocknr", n.get_block_nr()); - field(*f, "type", n.get_type() == INTERNAL ? "internal" : "leaf"); - field(*f, "nr_entries", n.get_nr_entries()); - field(*f, "max_entries", n.get_max_entries()); - field(*f, "value_size", n.get_value_size()); - - for (unsigned i = 0; i < n.get_nr_entries(); i++) { - formatter::ptr f2 = create_xml_formatter(); - field(*f2, "key", n.key_at(i)); - ST::show(f2, "value", n.value_at(i)); - f->child(boost::lexical_cast(i), f2); - } - - f->output(out, 0); - } - - block_manager::ptr bm_; - }; - - template - class show_array_block : public dbg::command { - typedef array_block rblock; - public: - explicit show_array_block(block_manager::ptr bm, - typename ShowTraits::ref_counter rc) - : bm_(bm), rc_(rc) { - } - - virtual void exec(strings const& args, ostream &out) { - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = bm_->read_lock(block); - - rblock b(rr, rc_); - show_array_entries(b, out); - } - - private: - void show_array_entries(rblock const& b, ostream &out) { - formatter::ptr f = create_xml_formatter(); - uint32_t nr_entries = b.nr_entries(); - - field(*f, "max_entries", b.max_entries()); - field(*f, "nr_entries", nr_entries); - field(*f, "value_size", b.value_size()); - - for (unsigned i = 0; i < nr_entries; i++) { - formatter::ptr f2 = create_xml_formatter(); - ShowTraits::show(f2, "value", b.get(i)); - f->child(boost::lexical_cast(i), f2); - } - - f->output(out, 0); - } - - block_manager::ptr bm_; - typename ShowTraits::ref_counter rc_; - }; - //-------------------------------- + template + dbg::command::ptr + create_btree_node_handler(block_manager::ptr bm) { + return create_block_handler(bm, create_btree_node_dumper()); + } + + template + dbg::command::ptr + create_array_block_handler(block_manager::ptr bm, + typename ShowTraits::value_trait::ref_counter rc) { + return create_block_handler(bm, create_array_block_dumper(rc)); + } + int debug(string const &path) { using dbg::command; try { block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY); command_interpreter::ptr interp = create_command_interpreter(cin, cout); - interp->register_command("hello", command::ptr(new hello)); + interp->register_command("hello", create_hello_handler()); interp->register_command("superblock", command::ptr(new show_superblock(bm))); - interp->register_command("block_node", command::ptr(new show_btree_node(bm))); - interp->register_command("mapping_block", command::ptr(new show_array_block(bm, - mapping_show_traits::ref_counter()))); + interp->register_command("block_node", create_btree_node_handler(bm)); + interp->register_command("mapping_block", create_array_block_handler(bm, + mapping_traits::ref_counter())); interp->register_command("help", command::ptr(new help)); - interp->register_command("exit", command::ptr(new exit_handler(interp))); + interp->register_command("exit", create_exit_handler(interp)); interp->enter_main_loop(); } catch (std::exception &e) { diff --git a/dbg-lib/array_block_dumper.h b/dbg-lib/array_block_dumper.h new file mode 100644 index 0000000..b63fa3a --- /dev/null +++ b/dbg-lib/array_block_dumper.h @@ -0,0 +1,53 @@ +#include "dbg-lib/block_dumper.h" +#include "dbg-lib/output_formatter.h" +#include "persistent-data/data-structures/array_block.h" + +//---------------------------------------------------------------- + +namespace dbg { + using persistent_data::block_manager; + using persistent_data::array_block; + + template + class array_block_dumper : public block_dumper { + public: + array_block_dumper(typename ShowTraits::value_trait::ref_counter rc) + : rc_(rc) { + } + + virtual void show(block_manager::read_ref &rr, std::ostream &out) { + rblock b(rr, rc_); + show_array_entries(b, out); + } + + private: + typedef array_block rblock; + + void show_array_entries(rblock const& b, std::ostream &out) { + formatter::ptr f = create_xml_formatter(); + uint32_t nr_entries = b.nr_entries(); + + field(*f, "max_entries", b.max_entries()); + field(*f, "nr_entries", nr_entries); + field(*f, "value_size", b.value_size()); + + for (unsigned i = 0; i < nr_entries; i++) { + formatter::ptr f2 = create_xml_formatter(); + ShowTraits::show(f2, "value", b.get(i)); + f->child(boost::lexical_cast(i), f2); + } + + f->output(out, 0); + } + + typename ShowTraits::value_trait::ref_counter rc_; + }; + + template + block_dumper::ptr + create_array_block_dumper(typename ShowTraits::value_trait::ref_counter rc) { + return block_dumper::ptr(new array_block_dumper(rc)); + } +} + +//---------------------------------------------------------------- diff --git a/dbg-lib/bitset_block_dumper.cc b/dbg-lib/bitset_block_dumper.cc new file mode 100644 index 0000000..8213ee3 --- /dev/null +++ b/dbg-lib/bitset_block_dumper.cc @@ -0,0 +1,137 @@ +#include "dbg-lib/bitset_block_dumper.h" +#include "dbg-lib/output_formatter.h" +#include "persistent-data/data-structures/array_block.h" +#include "persistent-data/data-structures/simple_traits.h" + +using namespace dbg; +using namespace persistent_data; + +//---------------------------------------------------------------- + +namespace { + class bitset_block_dumper : public dbg::block_dumper { + typedef array_block rblock; + public: + explicit bitset_block_dumper() + : BITS_PER_ARRAY_ENTRY(64) { + } + + virtual void show(block_manager::read_ref &rr, ostream &out) { + rblock b(rr, rc_); + show_bitset_entries(b, out); + } + + private: + void show_bitset_entries(rblock const& b, ostream &out) { + formatter::ptr f = create_xml_formatter(); + uint32_t nr_entries = b.nr_entries(); + + field(*f, "max_entries", b.max_entries()); + field(*f, "nr_entries", nr_entries); + field(*f, "value_size", b.value_size()); + + uint32_t end_pos = b.nr_entries() * BITS_PER_ARRAY_ENTRY; + std::pair range = next_set_bits(b, 0); + for (; range.first < end_pos; range = next_set_bits(b, range.second)) { + formatter::ptr f2 = create_xml_formatter(); + field(*f2, "begin", range.first); + field(*f2, "end", range.second); + f->child("set_bits", f2); + } + + f->output(out, 0); + } + + // Returns the range of set bits, starts from the offset. + pair next_set_bits(rblock const &b, uint32_t offset) { + uint32_t end_pos = b.nr_entries() * BITS_PER_ARRAY_ENTRY; + uint32_t begin = find_first_set(b, offset); + + if (begin == end_pos) // not found + return make_pair(end_pos, end_pos); + + uint32_t end = find_first_unset(b, begin + 1); + return make_pair(begin, end); + } + + // Returns the position (zero-based) of the first bit set + // in the array block, starts from the offset. + // Returns the pass-the-end position if not found. + uint32_t find_first_set(rblock const &b, uint32_t offset) { + uint32_t entry = offset / BITS_PER_ARRAY_ENTRY; + uint32_t nr_entries = b.nr_entries(); + + if (entry >= nr_entries) + return entry * BITS_PER_ARRAY_ENTRY; + + uint32_t idx = offset % BITS_PER_ARRAY_ENTRY; + uint64_t v = b.get(entry++) >> idx; + while (!v && entry < nr_entries) { + v = b.get(entry++); + idx = 0; + } + + if (!v) // not found + return entry * BITS_PER_ARRAY_ENTRY; + + return (entry - 1) * BITS_PER_ARRAY_ENTRY + idx + ffsll(static_cast(v)) - 1; + } + + // Returns the position (zero-based) of the first zero bit + // in the array block, starts from the offset. + // Returns the pass-the-end position if not found. + // FIXME: improve efficiency + uint32_t find_first_unset(rblock const& b, uint32_t offset) { + uint32_t entry = offset / BITS_PER_ARRAY_ENTRY; + uint32_t nr_entries = b.nr_entries(); + + if (entry >= nr_entries) + return entry * BITS_PER_ARRAY_ENTRY; + + uint32_t idx = offset % BITS_PER_ARRAY_ENTRY; + uint64_t v = b.get(entry++); + while (all_bits_set(v, idx) && entry < nr_entries) { + v = b.get(entry++); + idx = 0; + } + + if (all_bits_set(v, idx)) // not found + return entry * BITS_PER_ARRAY_ENTRY; + + return (entry - 1) * BITS_PER_ARRAY_ENTRY + idx + count_leading_bits(v, idx); + } + + // Returns true if all the bits beyond the position are set. + bool all_bits_set(uint64_t v, uint32_t offset) { + return (v >> offset) == (numeric_limits::max() >> offset); + } + + // Counts the number of leading 1's in the given value, starts from the offset + // FIXME: improve efficiency + uint32_t count_leading_bits(uint64_t v, uint32_t offset) { + uint32_t count = 0; + + v >>= offset; + while (v & 0x1) { + v >>= 1; + count++; + } + + return count; + } + + block_manager::ptr bm_; + uint64_traits::ref_counter rc_; + + const uint32_t BITS_PER_ARRAY_ENTRY; + }; +} + +//---------------------------------------------------------------- + +block_dumper::ptr +dbg::create_bitset_block_dumper() { + return block_dumper::ptr(new bitset_block_dumper()); +} + +//---------------------------------------------------------------- diff --git a/dbg-lib/bitset_block_dumper.h b/dbg-lib/bitset_block_dumper.h new file mode 100644 index 0000000..5c1a816 --- /dev/null +++ b/dbg-lib/bitset_block_dumper.h @@ -0,0 +1,14 @@ +#ifndef DBG_BITSET_BLOCK_DUMPER +#define DBG_BITSET_BLOCK_DUMPER + +#include "dbg-lib/block_dumper.h" + +//---------------------------------------------------------------- + +namespace dbg { + block_dumper::ptr create_bitset_block_dumper(); +} + +//---------------------------------------------------------------- + +#endif diff --git a/dbg-lib/block_dumper.h b/dbg-lib/block_dumper.h new file mode 100644 index 0000000..6d4461f --- /dev/null +++ b/dbg-lib/block_dumper.h @@ -0,0 +1,21 @@ +#ifndef DBG_BLOCK_DUMPER_H +#define DBG_BLOCK_DUMPER_H + +#include "persistent-data/block.h" + +//---------------------------------------------------------------- + +namespace dbg { + class block_dumper { + public: + typedef std::shared_ptr ptr; + + // pass the read_ref by reference since the caller already held the ref-count + virtual void show(persistent_data::block_manager::read_ref &rr, + std::ostream &out) = 0; + }; +} + +//---------------------------------------------------------------- + +#endif diff --git a/dbg-lib/btree_node_dumper.h b/dbg-lib/btree_node_dumper.h new file mode 100644 index 0000000..f92bce2 --- /dev/null +++ b/dbg-lib/btree_node_dumper.h @@ -0,0 +1,59 @@ +#ifndef DBG_BTREE_NODE_DUMPER +#define DBG_BTREE_NODE_DUMPER + +#include "dbg-lib/block_dumper.h" +#include "dbg-lib/simple_show_traits.h" +#include "persistent-data/data-structures/btree.h" + +//---------------------------------------------------------------- + +namespace dbg { + using persistent_data::block_manager; + using persistent_data::btree_detail::node_ref; + + template + class btree_node_dumper : public block_dumper { + public: + virtual void show(block_manager::read_ref &rr, std::ostream &out) { + node_ref n = btree_detail::to_node(rr); + if (n.get_type() == INTERNAL) + show_node(n, out); + else { + node_ref n = btree_detail::to_node(rr); + show_node(n, out); + } + } + + private: + template + void show_node(node_ref n, std::ostream &out) { + formatter::ptr f = create_xml_formatter(); + + field(*f, "csum", n.get_checksum()); + field(*f, "blocknr", n.get_block_nr()); + field(*f, "type", n.get_type() == INTERNAL ? "internal" : "leaf"); + field(*f, "nr_entries", n.get_nr_entries()); + field(*f, "max_entries", n.get_max_entries()); + field(*f, "value_size", n.get_value_size()); + + for (unsigned i = 0; i < n.get_nr_entries(); i++) { + formatter::ptr f2 = create_xml_formatter(); + field(*f2, "key", n.key_at(i)); + ST::show(f2, "value", n.value_at(i)); + f->child(boost::lexical_cast(i), f2); + } + + f->output(out, 0); + } + }; + + template + block_dumper::ptr + create_btree_node_dumper() { + return block_dumper::ptr(new btree_node_dumper()); + } +} + +//---------------------------------------------------------------- + +#endif diff --git a/base/command_interpreter.cc b/dbg-lib/command_interpreter.cc similarity index 97% rename from base/command_interpreter.cc rename to dbg-lib/command_interpreter.cc index ce7bd97..158831f 100644 --- a/base/command_interpreter.cc +++ b/dbg-lib/command_interpreter.cc @@ -1,4 +1,4 @@ -#include "base/command_interpreter.h" +#include "dbg-lib/command_interpreter.h" using namespace dbg; using namespace std; diff --git a/base/command_interpreter.h b/dbg-lib/command_interpreter.h similarity index 100% rename from base/command_interpreter.h rename to dbg-lib/command_interpreter.h diff --git a/dbg-lib/commands.cc b/dbg-lib/commands.cc new file mode 100644 index 0000000..409cf6f --- /dev/null +++ b/dbg-lib/commands.cc @@ -0,0 +1,79 @@ +#include "dbg-lib/commands.h" +#include "persistent-data/block.h" + +#include +#include + +using namespace dbg; +using namespace persistent_data; +using namespace std; + +//---------------------------------------------------------------- + +namespace { + class hello : public dbg::command { + virtual void exec(dbg::strings const &args, ostream &out) { + out << "Hello, world!" << endl; + } + }; + + class exit_handler : public dbg::command { + public: + exit_handler(command_interpreter::ptr interpreter) + : interpreter_(interpreter) { + } + + virtual void exec(dbg::strings const &args, ostream &out) { + out << "Goodbye!" << endl; + interpreter_->exit_main_loop(); + } + + private: + command_interpreter::ptr interpreter_; + }; + + class block_handler : public dbg::command { + public: + block_handler(block_manager::ptr bm, block_dumper::ptr dumper) + : bm_(bm), dumper_(dumper) { + } + + virtual void exec(dbg::strings const &args, ostream &out) { + if (args.size() != 2) + throw runtime_error("incorrect number of arguments"); + + block_address block = boost::lexical_cast(args[1]); + + // no checksum validation for debugging purpose + block_manager::read_ref rr = bm_->read_lock(block); + + dumper_->show(rr, out); + } + + private: + block_manager::ptr bm_; + block_dumper::ptr dumper_; + }; +} + +//---------------------------------------------------------------- + +command::ptr +dbg::create_hello_handler() +{ + return command::ptr(new hello()); +} + +command::ptr +dbg::create_exit_handler(command_interpreter::ptr interp) +{ + return command::ptr(new exit_handler(interp)); +} + +command::ptr +dbg::create_block_handler(block_manager::ptr bm, block_dumper::ptr dumper) +{ + return command::ptr(new block_handler(bm, dumper)); +} + +//---------------------------------------------------------------- diff --git a/dbg-lib/commands.h b/dbg-lib/commands.h new file mode 100644 index 0000000..76094ad --- /dev/null +++ b/dbg-lib/commands.h @@ -0,0 +1,24 @@ +#ifndef DBG_COMMANDS_H +#define DBG_COMMANDS_H + +#include "dbg-lib/command_interpreter.h" +#include "dbg-lib/block_dumper.h" +#include "persistent-data/block.h" + +//---------------------------------------------------------------- + +namespace dbg { + dbg::command::ptr + create_hello_handler(); + + dbg::command::ptr + create_exit_handler(dbg::command_interpreter::ptr interp); + + dbg::command::ptr + create_block_handler(persistent_data::block_manager::ptr bm, + dbg::block_dumper::ptr dumper); +} + +//---------------------------------------------------------------- + +#endif diff --git a/dbg-lib/index_block_dumper.cc b/dbg-lib/index_block_dumper.cc new file mode 100644 index 0000000..7457d65 --- /dev/null +++ b/dbg-lib/index_block_dumper.cc @@ -0,0 +1,51 @@ +#include "dbg-lib/index_block_dumper.h" +#include "dbg-lib/output_formatter.h" +#include "dbg-lib/sm_show_traits.h" +#include "persistent-data/space-maps/disk_structures.h" + +using namespace dbg; + +//---------------------------------------------------------------- + +namespace { + using persistent_data::block_manager; + + class index_block_dumper : public dbg::block_dumper { + public: + virtual void show(block_manager::read_ref &rr, std::ostream &out) { + sm_disk_detail::metadata_index const *mdi = + reinterpret_cast(rr.data()); + show_metadata_index(mdi, sm_disk_detail::MAX_METADATA_BITMAPS, out); + } + + private: + void show_metadata_index(sm_disk_detail::metadata_index const *mdi, unsigned nr_indexes, std::ostream &out) { + formatter::ptr f = create_xml_formatter(); + field(*f, "csum", to_cpu(mdi->csum_)); + field(*f, "padding", to_cpu(mdi->padding_)); + field(*f, "blocknr", to_cpu(mdi->blocknr_)); + + sm_disk_detail::index_entry ie; + for (unsigned i = 0; i < nr_indexes; i++) { + sm_disk_detail::index_entry_traits::unpack(*(mdi->index + i), ie); + + if (!ie.blocknr_ && !ie.nr_free_ && !ie.none_free_before_) + continue; + + formatter::ptr f2 = create_xml_formatter(); + index_entry_show_traits::show(f2, "value", ie); + f->child(boost::lexical_cast(i), f2); + } + f->output(out, 0); + } + }; +} + +//---------------------------------------------------------------- + +block_dumper::ptr +dbg::create_index_block_dumper() { + return block_dumper::ptr(new index_block_dumper()); +} + +//---------------------------------------------------------------- diff --git a/dbg-lib/index_block_dumper.h b/dbg-lib/index_block_dumper.h new file mode 100644 index 0000000..b1ce10a --- /dev/null +++ b/dbg-lib/index_block_dumper.h @@ -0,0 +1,10 @@ +#include "dbg-lib/block_dumper.h" + +//---------------------------------------------------------------- + +namespace dbg { + block_dumper::ptr + create_index_block_dumper(); +} + +//---------------------------------------------------------------- diff --git a/base/output_formatter.cc b/dbg-lib/output_formatter.cc similarity index 98% rename from base/output_formatter.cc rename to dbg-lib/output_formatter.cc index c62058f..95d8bb8 100644 --- a/base/output_formatter.cc +++ b/dbg-lib/output_formatter.cc @@ -1,6 +1,6 @@ #include -#include "base/output_formatter.h" +#include "dbg-lib/output_formatter.h" using namespace dbg; using namespace std; diff --git a/base/output_formatter.h b/dbg-lib/output_formatter.h similarity index 100% rename from base/output_formatter.h rename to dbg-lib/output_formatter.h diff --git a/dbg-lib/simple_show_traits.cc b/dbg-lib/simple_show_traits.cc new file mode 100644 index 0000000..14b12c5 --- /dev/null +++ b/dbg-lib/simple_show_traits.cc @@ -0,0 +1,19 @@ +#include "dbg-lib/simple_show_traits.h" + +using namespace dbg; + +//---------------------------------------------------------------- + +void +uint32_show_traits::show(formatter::ptr f, string const &key, uint32_t const &value) +{ + field(*f, key, boost::lexical_cast(value)); +} + +void +uint64_show_traits::show(formatter::ptr f, string const &key, uint64_t const &value) +{ + field(*f, key, boost::lexical_cast(value)); +} + +//---------------------------------------------------------------- diff --git a/dbg-lib/simple_show_traits.h b/dbg-lib/simple_show_traits.h new file mode 100644 index 0000000..a21c8ea --- /dev/null +++ b/dbg-lib/simple_show_traits.h @@ -0,0 +1,27 @@ +#ifndef DBG_SIMPLE_SHOW_TRAITS_H +#define DBG_SIMPLE_SHOW_TRAITS_H + +#include "dbg-lib/output_formatter.h" +#include "persistent-data/data-structures/simple_traits.h" + +//---------------------------------------------------------------- + +namespace dbg { + class uint32_show_traits { + public: + typedef persistent_data::uint32_traits value_trait; + + static void show(dbg::formatter::ptr f, std::string const &key, uint32_t const &value); + }; + + class uint64_show_traits { + public: + typedef persistent_data::uint64_traits value_trait; + + static void show(dbg::formatter::ptr f, std::string const &key, uint64_t const &value); + }; +} + +//---------------------------------------------------------------- + +#endif diff --git a/dbg-lib/sm_show_traits.cc b/dbg-lib/sm_show_traits.cc new file mode 100644 index 0000000..4f14bc9 --- /dev/null +++ b/dbg-lib/sm_show_traits.cc @@ -0,0 +1,27 @@ +#include "dbg-lib/sm_show_traits.h" + +using namespace dbg; +using namespace std; + +//---------------------------------------------------------------- + +void +index_entry_show_traits::show(formatter::ptr f, string const &key, + persistent_data::sm_disk_detail::index_entry const &value) +{ + field(*f, "blocknr", value.blocknr_); + field(*f, "nr_free", value.nr_free_); + field(*f, "none_free_before", value.none_free_before_); +} + +void +sm_root_show_traits::show(formatter::ptr f, string const &key, + persistent_data::sm_disk_detail::sm_root const &value) +{ + field(*f, "nr_blocks", value.nr_blocks_); + field(*f, "nr_allocated", value.nr_allocated_); + field(*f, "bitmap_root", value.bitmap_root_); + field(*f, "ref_count_root", value.ref_count_root_); +} + +//---------------------------------------------------------------- diff --git a/dbg-lib/sm_show_traits.h b/dbg-lib/sm_show_traits.h new file mode 100644 index 0000000..bf85fcf --- /dev/null +++ b/dbg-lib/sm_show_traits.h @@ -0,0 +1,29 @@ +#ifndef DBG_SM_SHOW_TRAITS_H +#define DBG_SM_SHOW_TRAITS_H + +#include "dbg-lib/output_formatter.h" +#include "persistent-data/space-maps/disk_structures.h" + +//---------------------------------------------------------------- + +namespace dbg { + class index_entry_show_traits { + public: + typedef persistent_data::sm_disk_detail::index_entry_traits value_trait; + + static void show(dbg::formatter::ptr f, std::string const &key, + persistent_data::sm_disk_detail::index_entry const &value); + }; + + class sm_root_show_traits { + public: + typedef persistent_data::sm_disk_detail::sm_root_traits value_trait; + + static void show(dbg::formatter::ptr f, std::string const &key, + persistent_data::sm_disk_detail::sm_root const &value); + }; +} + +//---------------------------------------------------------------- + +#endif diff --git a/era/era_debug.cc b/era/era_debug.cc index af6764c..5470b4c 100644 --- a/era/era_debug.cc +++ b/era/era_debug.cc @@ -21,11 +21,14 @@ #include #include -#include "base/command_interpreter.h" -#include "base/output_formatter.h" +#include "dbg-lib/array_block_dumper.h" +#include "dbg-lib/btree_node_dumper.h" +#include "dbg-lib/bitset_block_dumper.h" +#include "dbg-lib/command_interpreter.h" +#include "dbg-lib/commands.h" +#include "dbg-lib/output_formatter.h" +#include "dbg-lib/sm_show_traits.h" #include "persistent-data/file_utils.h" -#include "persistent-data/data-structures/btree.h" -#include "persistent-data/data-structures/simple_traits.h" #include "persistent-data/space-maps/disk_structures.h" #include "era/commands.h" #include "era/metadata.h" @@ -39,12 +42,6 @@ using namespace era; //---------------------------------------------------------------- namespace { - class hello : public dbg::command { - virtual void exec(strings const &args, ostream &out) { - out << "Hello, world!" << endl; - } - }; - class help : public dbg::command { virtual void exec(strings const &args, ostream &out) { out << "Commands:" << endl @@ -57,52 +54,6 @@ namespace { } }; - class exit_handler : public dbg::command { - public: - exit_handler(command_interpreter::ptr interpreter) - : interpreter_(interpreter) { - } - - virtual void exec(strings const &args, ostream &out) { - out << "Goodbye!" << endl; - interpreter_->exit_main_loop(); - } - - command_interpreter::ptr interpreter_; - }; - - // FIXME: duplication - class uint32_show_traits : public uint32_traits { - public: - typedef uint32_traits value_trait; - - static void show(formatter::ptr f, string const &key, uint32_t const &value) { - field(*f, key, boost::lexical_cast(value)); - } - }; - - // FIXME: duplication - class uint64_show_traits : public uint64_traits { - public: - typedef uint64_traits value_trait; - - static void show(formatter::ptr f, string const &key, uint64_t const &value) { - field(*f, key, boost::lexical_cast(value)); - } - }; - - // FIXME: duplication - class sm_root_show_traits : public persistent_data::sm_disk_detail::sm_root_traits { - public: - static void show(formatter::ptr f, string const &key, - persistent_data::sm_disk_detail::sm_root const &value) { - field(*f, "nr_blocks", value.nr_blocks_); - field(*f, "nr_allocated", value.nr_allocated_); - field(*f, "bitmap_root", value.bitmap_root_); - field(*f, "ref_count_root", value.ref_count_root_); - } - }; - // for displaying the writeset tree class writeset_show_traits : public era::era_detail_traits { public: @@ -172,227 +123,26 @@ namespace { block_manager::ptr bm_; }; - // FIXME: duplication - template - class show_btree_node : public dbg::command { - public: - explicit show_btree_node(block_manager::ptr bm) - : bm_(bm) { - } - - virtual void exec(strings const &args, ostream &out) { - using namespace persistent_data::btree_detail; - - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = bm_->read_lock(block); - - node_ref n = btree_detail::to_node(rr); - if (n.get_type() == INTERNAL) - show_node(n, out); - else { - node_ref n = btree_detail::to_node(rr); - show_node(n, out); - } - } - - private: - template - void show_node(node_ref n, ostream &out) { - formatter::ptr f = create_xml_formatter(); - - field(*f, "csum", n.get_checksum()); - field(*f, "blocknr", n.get_block_nr()); - field(*f, "type", n.get_type() == INTERNAL ? "internal" : "leaf"); - field(*f, "nr_entries", n.get_nr_entries()); - field(*f, "max_entries", n.get_max_entries()); - field(*f, "value_size", n.get_value_size()); - - for (unsigned i = 0; i < n.get_nr_entries(); i++) { - formatter::ptr f2 = create_xml_formatter(); - field(*f2, "key", n.key_at(i)); - ST::show(f2, "value", n.value_at(i)); - f->child(boost::lexical_cast(i), f2); - } - - f->output(out, 0); - } - - block_manager::ptr bm_; - }; - - // FIXME: duplication - template - class show_array_block : public dbg::command { - typedef array_block rblock; - public: - explicit show_array_block(block_manager::ptr bm, - typename ShowTraits::ref_counter rc) - : bm_(bm), rc_(rc) { - } - - virtual void exec(strings const& args, ostream &out) { - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = bm_->read_lock(block); - - rblock b(rr, rc_); - show_array_entries(b, out); - } - - private: - void show_array_entries(rblock const& b, ostream &out) { - formatter::ptr f = create_xml_formatter(); - uint32_t nr_entries = b.nr_entries(); - - field(*f, "max_entries", b.max_entries()); - field(*f, "nr_entries", nr_entries); - field(*f, "value_size", b.value_size()); - - for (unsigned i = 0; i < nr_entries; i++) { - formatter::ptr f2 = create_xml_formatter(); - ShowTraits::show(f2, "value", b.get(i)); - f->child(boost::lexical_cast(i), f2); - } - - f->output(out, 0); - } - - block_manager::ptr bm_; - typename ShowTraits::ref_counter rc_; - }; - - // FIXME: duplication - class show_bitset_block : public dbg::command { - typedef array_block rblock; - public: - explicit show_bitset_block(block_manager::ptr bm) - : bm_(bm), - BITS_PER_ARRAY_ENTRY(64) { - } - - virtual void exec(strings const& args, ostream &out) { - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = bm_->read_lock(block); - - rblock b(rr, rc_); - show_bitset_entries(b, out); - } - - private: - void show_bitset_entries(rblock const& b, ostream &out) { - formatter::ptr f = create_xml_formatter(); - uint32_t nr_entries = b.nr_entries(); - - field(*f, "max_entries", b.max_entries()); - field(*f, "nr_entries", nr_entries); - field(*f, "value_size", b.value_size()); - - uint32_t end_pos = b.nr_entries() * BITS_PER_ARRAY_ENTRY; - std::pair range = next_set_bits(b, 0); - for (; range.first < end_pos; range = next_set_bits(b, range.second)) { - formatter::ptr f2 = create_xml_formatter(); - field(*f2, "begin", range.first); - field(*f2, "end", range.second); - f->child("set_bits", f2); - } - - f->output(out, 0); - } - - // Returns the range of set bits, starts from the offset. - pair next_set_bits(rblock const &b, uint32_t offset) { - uint32_t end_pos = b.nr_entries() * BITS_PER_ARRAY_ENTRY; - uint32_t begin = find_first_set(b, offset); - - if (begin == end_pos) // not found - return make_pair(end_pos, end_pos); - - uint32_t end = find_first_unset(b, begin + 1); - return make_pair(begin, end); - } - - // Returns the position (zero-based) of the first bit set - // in the array block, starts from the offset. - // Returns the pass-the-end position if not found. - uint32_t find_first_set(rblock const &b, uint32_t offset) { - uint32_t entry = offset / BITS_PER_ARRAY_ENTRY; - uint32_t nr_entries = b.nr_entries(); - - if (entry >= nr_entries) - return entry * BITS_PER_ARRAY_ENTRY; - - uint32_t idx = offset % BITS_PER_ARRAY_ENTRY; - uint64_t v = b.get(entry++) >> idx; - while (!v && entry < nr_entries) { - v = b.get(entry++); - idx = 0; - } - - if (!v) // not found - return entry * BITS_PER_ARRAY_ENTRY; - - return (entry - 1) * BITS_PER_ARRAY_ENTRY + idx + ffsll(static_cast(v)) - 1; - } - - // Returns the position (zero-based) of the first zero bit - // in the array block, starts from the offset. - // Returns the pass-the-end position if not found. - // FIXME: improve efficiency - uint32_t find_first_unset(rblock const& b, uint32_t offset) { - uint32_t entry = offset / BITS_PER_ARRAY_ENTRY; - uint32_t nr_entries = b.nr_entries(); - - if (entry >= nr_entries) - return entry * BITS_PER_ARRAY_ENTRY; - - uint32_t idx = offset % BITS_PER_ARRAY_ENTRY; - uint64_t v = b.get(entry++); - while (all_bits_set(v, idx) && entry < nr_entries) { - v = b.get(entry++); - idx = 0; - } - - if (all_bits_set(v, idx)) // not found - return entry * BITS_PER_ARRAY_ENTRY; - - return (entry - 1) * BITS_PER_ARRAY_ENTRY + idx + count_leading_bits(v, idx); - } - - // Returns true if all the bits beyond the position are set. - bool all_bits_set(uint64_t v, uint32_t offset) { - return (v >> offset) == (numeric_limits::max() >> offset); - } - - // Counts the number of leading 1's in the given value, starts from the offset - // FIXME: improve efficiency - uint32_t count_leading_bits(uint64_t v, uint32_t offset) { - uint32_t count = 0; - - v >>= offset; - while (v & 0x1) { - v >>= 1; - count++; - } - - return count; - } - - block_manager::ptr bm_; - uint64_traits::ref_counter rc_; - - const uint32_t BITS_PER_ARRAY_ENTRY; - }; - //-------------------------------- + template + dbg::command::ptr + create_btree_node_handler(block_manager::ptr bm) { + return create_block_handler(bm, create_btree_node_dumper()); + } + + template + dbg::command::ptr + create_array_block_handler(block_manager::ptr bm, + typename ShowTraits::value_trait::ref_counter rc) { + return create_block_handler(bm, create_array_block_dumper(rc)); + } + + dbg::command::ptr + create_bitset_block_handler(block_manager::ptr bm) { + return create_block_handler(bm, create_bitset_block_dumper()); + } + int debug(string const &path) { using dbg::command; @@ -400,15 +150,15 @@ namespace { block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY); transaction_manager::ptr null_tm = open_tm(bm, era::SUPERBLOCK_LOCATION); command_interpreter::ptr interp = create_command_interpreter(cin, cout); - interp->register_command("hello", command::ptr(new hello)); + interp->register_command("hello", create_hello_handler()); interp->register_command("superblock", command::ptr(new show_superblock(bm))); - interp->register_command("block_node", command::ptr(new show_btree_node(bm))); - interp->register_command("bitset_block", command::ptr(new show_bitset_block(bm))); - interp->register_command("era_block", command::ptr(new show_array_block(bm, - uint32_show_traits::ref_counter()))); - interp->register_command("writeset_node", command::ptr(new show_btree_node(bm))); + interp->register_command("block_node", create_btree_node_handler(bm)); + interp->register_command("bitset_block", create_bitset_block_handler(bm)); + interp->register_command("era_block", create_array_block_handler(bm, + uint32_traits::ref_counter())); + interp->register_command("writeset_node", create_btree_node_handler(bm)); interp->register_command("help", command::ptr(new help)); - interp->register_command("exit", command::ptr(new exit_handler(interp))); + interp->register_command("exit", create_exit_handler(interp)); interp->enter_main_loop(); } catch (std::exception &e) { diff --git a/thin-provisioning/thin_debug.cc b/thin-provisioning/thin_debug.cc index 37a5f18..9bc1ed4 100644 --- a/thin-provisioning/thin_debug.cc +++ b/thin-provisioning/thin_debug.cc @@ -21,11 +21,14 @@ #include #include -#include "base/command_interpreter.h" #include "base/math_utils.h" -#include "base/output_formatter.h" -#include "persistent-data/data-structures/btree.h" -#include "persistent-data/data-structures/simple_traits.h" +#include "dbg-lib/array_block_dumper.h" +#include "dbg-lib/btree_node_dumper.h" +#include "dbg-lib/index_block_dumper.h" +#include "dbg-lib/command_interpreter.h" +#include "dbg-lib/commands.h" +#include "dbg-lib/output_formatter.h" +#include "dbg-lib/sm_show_traits.h" #include "persistent-data/file_utils.h" #include "persistent-data/space-maps/disk_structures.h" #include "thin-provisioning/commands.h" @@ -39,12 +42,6 @@ using namespace std; using namespace thin_provisioning; namespace { - class hello : public dbg::command { - virtual void exec(strings const &args, ostream &out) { - out << "Hello, world!" << endl; - } - }; - class help : public dbg::command { virtual void exec(strings const &args, ostream &out) { out << "Commands:" << endl @@ -58,31 +55,6 @@ namespace { } }; - class exit_handler : public dbg::command { - public: - exit_handler(command_interpreter::ptr interpreter) - : interpreter_(interpreter) { - } - - virtual void exec(strings const &args, ostream &out) { - out << "Goodbye!" << endl; - interpreter_->exit_main_loop(); - } - - command_interpreter::ptr interpreter_; - }; - - class sm_root_show_traits : public persistent_data::sm_disk_detail::sm_root_traits { - public: - static void show(formatter::ptr f, string const &key, - persistent_data::sm_disk_detail::sm_root const &value) { - field(*f, "nr_blocks", value.nr_blocks_); - field(*f, "nr_allocated", value.nr_allocated_); - field(*f, "bitmap_root", value.bitmap_root_); - field(*f, "ref_count_root", value.ref_count_root_); - } - }; - class show_superblock : public dbg::command { public: explicit show_superblock(block_manager::ptr bm) @@ -142,7 +114,7 @@ namespace { block_manager::ptr bm_; }; - class device_details_show_traits : public thin_provisioning::device_tree_detail::device_details_traits { + class device_details_show_traits { public: typedef thin_provisioning::device_tree_detail::device_details_traits value_trait; @@ -155,16 +127,7 @@ namespace { } }; - class uint64_show_traits : public uint64_traits { - public: - typedef uint64_traits value_trait; - - static void show(formatter::ptr f, string const &key, uint64_t const &value) { - field(*f, key, boost::lexical_cast(value)); - } - }; - - class block_show_traits : public thin_provisioning::mapping_tree_detail::block_traits { + class block_show_traits { public: typedef thin_provisioning::mapping_tree_detail::block_traits value_trait; @@ -175,114 +138,18 @@ namespace { } }; - class index_entry_show_traits : public persistent_data::sm_disk_detail::index_entry_traits { - public: - typedef persistent_data::sm_disk_detail::index_entry_traits value_trait; - - static void show(formatter::ptr f, string const &key, - persistent_data::sm_disk_detail::index_entry const &value) { - field(*f, "blocknr", value.blocknr_); - field(*f, "nr_free", value.nr_free_); - field(*f, "none_free_before", value.none_free_before_); - } - }; + //-------------------------------- template - class show_btree_node : public dbg::command { - public: - explicit show_btree_node(block_manager::ptr bm) - : bm_(bm) { - } + dbg::command::ptr + create_btree_node_handler(block_manager::ptr bm) { + return create_block_handler(bm, create_btree_node_dumper()); + } - virtual void exec(strings const &args, ostream &out) { - using namespace persistent_data::btree_detail; - - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = bm_->read_lock(block); - - node_ref n = btree_detail::to_node(rr); - if (n.get_type() == INTERNAL) - show_node(n, out); - else { - node_ref n = btree_detail::to_node(rr); - show_node(n, out); - } - } - - private: - template - void show_node(node_ref n, ostream &out) { - formatter::ptr f = create_xml_formatter(); - - field(*f, "csum", n.get_checksum()); - field(*f, "blocknr", n.get_block_nr()); - field(*f, "type", n.get_type() == INTERNAL ? "internal" : "leaf"); - field(*f, "nr_entries", n.get_nr_entries()); - field(*f, "max_entries", n.get_max_entries()); - field(*f, "value_size", n.get_value_size()); - - for (unsigned i = 0; i < n.get_nr_entries(); i++) { - formatter::ptr f2 = create_xml_formatter(); - field(*f2, "key", n.key_at(i)); - ST::show(f2, "value", n.value_at(i)); - f->child(boost::lexical_cast(i), f2); - } - - f->output(out, 0); - } - - block_manager::ptr bm_; - }; - - class show_index_block : public dbg::command { - public: - explicit show_index_block(block_manager::ptr bm) - : bm_(bm) { - } - - virtual void exec(strings const &args, ostream &out) { - if (args.size() != 2) - throw runtime_error("incorrect number of arguments"); - - block_address block = boost::lexical_cast(args[1]); - - // no checksum validation for debugging purpose - block_manager::read_ref rr = bm_->read_lock(block); - - sm_disk_detail::metadata_index const *mdi = - reinterpret_cast(rr.data()); - show_metadata_index(mdi, sm_disk_detail::MAX_METADATA_BITMAPS, out); - } - - private: - void show_metadata_index(sm_disk_detail::metadata_index const *mdi, block_address nr_indexes, ostream &out) { - formatter::ptr f = create_xml_formatter(); - field(*f, "csum", to_cpu(mdi->csum_)); - field(*f, "padding", to_cpu(mdi->padding_)); - field(*f, "blocknr", to_cpu(mdi->blocknr_)); - - sm_disk_detail::index_entry ie; - for (block_address i = 0; i < nr_indexes; i++) { - sm_disk_detail::index_entry_traits::unpack(*(mdi->index + i), ie); - - if (!ie.blocknr_ && !ie.nr_free_ && !ie.none_free_before_) - continue; - - formatter::ptr f2 = create_xml_formatter(); - index_entry_show_traits::show(f2, "value", ie); - f->child(boost::lexical_cast(i), f2); - } - f->output(out, 0); - } - - unsigned const ENTRIES_PER_BLOCK = (MD_BLOCK_SIZE - sizeof(persistent_data::sm_disk_detail::bitmap_header)) * 4; - block_manager::ptr bm_; - }; - - //-------------------------------- + dbg::command::ptr + create_index_block_handler(block_manager::ptr bm) { + return create_block_handler(bm, create_index_block_dumper()); + } int debug_(string const &path) { using dbg::command; @@ -290,15 +157,15 @@ namespace { try { block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY, 1); command_interpreter::ptr interp = create_command_interpreter(cin, cout); - interp->register_command("hello", command::ptr(new hello)); + interp->register_command("hello", create_hello_handler()); interp->register_command("superblock", command::ptr(new show_superblock(bm))); - interp->register_command("m1_node", command::ptr(new show_btree_node(bm))); - interp->register_command("m2_node", command::ptr(new show_btree_node(bm))); - interp->register_command("detail_node", command::ptr(new show_btree_node(bm))); - interp->register_command("index_block", command::ptr(new show_index_block(bm))); - interp->register_command("index_node", command::ptr(new show_btree_node(bm))); + interp->register_command("m1_node", create_btree_node_handler(bm)); + interp->register_command("m2_node", create_btree_node_handler(bm)); + interp->register_command("detail_node", create_btree_node_handler(bm)); + interp->register_command("index_block", create_index_block_handler(bm)); + interp->register_command("index_node", create_btree_node_handler(bm)); interp->register_command("help", command::ptr(new help)); - interp->register_command("exit", command::ptr(new exit_handler(interp))); + interp->register_command("exit", create_exit_handler(interp)); interp->enter_main_loop(); } catch (std::exception &e) {