From 087a4711a59c62bec090f8feae5bc90e1f5d64d9 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Wed, 9 Dec 2020 18:40:06 +0800 Subject: [PATCH 01/12] [array (rust)] Implement array basis --- src/pdata/array.rs | 39 ++++++++++++++ src/pdata/array_block.rs | 70 ++++++++++++++++++++++++ src/pdata/array_walker.rs | 108 ++++++++++++++++++++++++++++++++++++++ src/pdata/mod.rs | 3 ++ 4 files changed, 220 insertions(+) create mode 100644 src/pdata/array.rs create mode 100644 src/pdata/array_block.rs create mode 100644 src/pdata/array_walker.rs diff --git a/src/pdata/array.rs b/src/pdata/array.rs new file mode 100644 index 0000000..d8299c2 --- /dev/null +++ b/src/pdata/array.rs @@ -0,0 +1,39 @@ +use nom::{number::complete::*, IResult}; +use std::fmt; + +use crate::pdata::unpack::*; + +//------------------------------------------ + +// TODO: build a data structure representating an array? + +// FIXME: rename this struct +pub struct ArrayBlockEntry { + pub block: u64, +} + +impl Unpack for ArrayBlockEntry { + fn disk_size() -> u32 { + 8 + } + + fn unpack(i: &[u8]) -> IResult<&[u8], ArrayBlockEntry> { + let (i, n) = le_u64(i)?; + let block = n; + + Ok(( + i, + ArrayBlockEntry { + block, + } + )) + } +} + +impl fmt::Display for ArrayBlockEntry { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + write!(f, "{}", self.block) + } +} + +//------------------------------------------ diff --git a/src/pdata/array_block.rs b/src/pdata/array_block.rs new file mode 100644 index 0000000..0cf7069 --- /dev/null +++ b/src/pdata/array_block.rs @@ -0,0 +1,70 @@ +use anyhow::anyhow; +use anyhow::Result; +use nom::{multi::count, number::complete::*, IResult}; + +use crate::pdata::unpack::Unpack; + +//------------------------------------------ + +pub struct ArrayBlockHeader { + pub csum: u32, + pub max_entries: u32, + pub nr_entries: u32, + pub value_size: u32, + pub blocknr: u64, +} + +impl Unpack for ArrayBlockHeader { + fn disk_size() -> u32 { + 24 + } + + fn unpack(data: &[u8]) -> IResult<&[u8], ArrayBlockHeader> { + let (i, csum) = le_u32(data)?; + let (i, max_entries) = le_u32(i)?; + let (i, nr_entries) = le_u32(i)?; + let (i, value_size) = le_u32(i)?; + let (i, blocknr) = le_u64(i)?; + + Ok(( + i, + ArrayBlockHeader { + csum, + max_entries, + nr_entries, + value_size, + blocknr + }, + )) + } +} + +pub struct ArrayBlock { + pub header: ArrayBlockHeader, + pub values: Vec, +} + +impl ArrayBlock { + pub fn get_header(&self) -> &ArrayBlockHeader { + &self.header + } +} + +fn convert_result<'a, V>(r: IResult<&'a [u8], V>) -> Result<(&'a [u8], V)> { + r.map_err(|_| anyhow!("parse error")) +} + +pub fn unpack_array_block( + data: &[u8], +) -> Result> { + // TODO: collect errors + let (i, header) = ArrayBlockHeader::unpack(data).map_err(|_e| anyhow!("Couldn't parse header"))?; + let (_i, values) = convert_result(count(V::unpack, header.nr_entries as usize)(i))?; + + Ok(ArrayBlock { + header, + values, + }) +} + +//------------------------------------------ diff --git a/src/pdata/array_walker.rs b/src/pdata/array_walker.rs new file mode 100644 index 0000000..6322c50 --- /dev/null +++ b/src/pdata/array_walker.rs @@ -0,0 +1,108 @@ +use std::sync::Arc; + +use crate::io_engine::*; +use crate::pdata::array::*; +use crate::pdata::array_block::*; +use crate::pdata::btree::*; +use crate::pdata::btree_walker::*; +use crate::pdata::unpack::*; + +//------------------------------------------ + +pub struct ArrayWalker { + engine: Arc, + ignore_non_fatal: bool +} + +// FIXME: define another Result type for array visiting? +pub trait ArrayBlockVisitor { + fn visit( + &self, + index: u64, + v: V, + ) -> anyhow::Result<()>; +} + +struct BlockValueVisitor { + engine: Arc, + array_block_visitor: Box>, +} + +impl BlockValueVisitor { + pub fn new>( + e: Arc, + v: Box, + ) -> BlockValueVisitor { + BlockValueVisitor { + engine: e, + array_block_visitor: v, + } + } + + pub fn visit_array_block( + &self, + index: u64, + array_block: ArrayBlock, + ) { + let begin = index * u64::from(array_block.header.nr_entries); + for i in 0..array_block.header.nr_entries { + self.array_block_visitor.visit(begin + u64::from(i), array_block.values[i as usize]).unwrap(); + } + } +} + +impl NodeVisitor for BlockValueVisitor { + // FIXME: return errors + fn visit( + &self, + path: &Vec, + _kr: &KeyRange, + _h: &NodeHeader, + keys: &[u64], + values: &[ArrayBlockEntry], + ) -> Result<()> { + for n in 0..keys.len() { + let index = keys[n]; + let b = self.engine.read(values[n].block).map_err(|_| io_err(path))?; + let array_block = unpack_array_block::(b.get_data()).map_err(|_| io_err(path))?; + self.visit_array_block(index, array_block); + } + Ok(()) + } + + // FIXME: stub + fn visit_again(&self, _path: &Vec, _b: u64) -> Result<()> { + Ok(()) + } + + // FIXME: stub + fn end_walk(&self) -> Result<()> { + Ok(()) + } +} + +impl ArrayWalker { + pub fn new(engine: Arc, ignore_non_fatal: bool) -> ArrayWalker { + let r: ArrayWalker = ArrayWalker { + engine, + ignore_non_fatal, + }; + r + } + + // FIXME: pass the visitor by reference? + // FIXME: redefine the Result type for array visiting? + pub fn walk(&self, visitor: Box, root: u64) -> Result<()> + where + BV: 'static + ArrayBlockVisitor, + V: Unpack, + { + let w = BTreeWalker::new(self.engine.clone(), self.ignore_non_fatal); + let mut path = Vec::new(); // FIXME: eliminate this line? + path.push(0); + let v = BlockValueVisitor::::new(self.engine.clone(), visitor); + w.walk(&mut path, &v, root) + } +} + +//------------------------------------------ diff --git a/src/pdata/mod.rs b/src/pdata/mod.rs index 26a7318..370d244 100644 --- a/src/pdata/mod.rs +++ b/src/pdata/mod.rs @@ -1,3 +1,6 @@ +pub mod array; +pub mod array_block; +pub mod array_walker; pub mod btree; pub mod btree_builder; pub mod btree_merge; From faa371c20863d909dc85844063e346ba8c3b67cf Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Tue, 15 Dec 2020 20:03:25 +0800 Subject: [PATCH 02/12] [cache (rust)] Implement cache primitives --- Cargo.lock | 7 ++ Cargo.toml | 1 + src/cache/hint.rs | 33 ++++++++++ src/cache/mapping.rs | 50 +++++++++++++++ src/cache/mod.rs | 3 + src/cache/superblock.rs | 139 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 233 insertions(+) create mode 100644 src/cache/hint.rs create mode 100644 src/cache/mapping.rs create mode 100644 src/cache/superblock.rs diff --git a/Cargo.lock b/Cargo.lock index 761163b..219c98a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -803,6 +803,7 @@ dependencies = [ "thiserror", "threadpool", "tui", + "typenum", ] [[package]] @@ -856,6 +857,12 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "typenum" +version = "1.12.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "373c8a200f9e67a0c95e62a4f52fbf80c23b4381c05a17845531982fa99e6b33" + [[package]] name = "unicode-segmentation" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index a98b956..9221f2e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -32,6 +32,7 @@ threadpool = "1.8" thiserror = "1.0" tui = "0.10" termion = "1.5" +typenum = "1.12.0" [dev-dependencies] json = "0.12" diff --git a/src/cache/hint.rs b/src/cache/hint.rs new file mode 100644 index 0000000..ef0d83f --- /dev/null +++ b/src/cache/hint.rs @@ -0,0 +1,33 @@ +use nom::IResult; +use std::marker::PhantomData; +use std::convert::TryInto; + +use crate::pdata::unpack::*; + +//------------------------------------------ + +#[derive(Clone, Copy)] +pub struct Hint { + pub hint: [u8; 4], // FIXME: support various hint sizes + _not_used: PhantomData, +} + +impl Unpack for Hint { + fn disk_size() -> u32 { + Width::to_u32() + } + + // FIXME: support different width + fn unpack(i: &[u8]) -> IResult<&[u8], Hint> { + let size = Width::to_usize(); + Ok(( + &i[size..], + Hint { + hint: i[0..size].try_into().unwrap(), + _not_used: PhantomData, + }, + )) + } +} + +//------------------------------------------ diff --git a/src/cache/mapping.rs b/src/cache/mapping.rs new file mode 100644 index 0000000..c18325a --- /dev/null +++ b/src/cache/mapping.rs @@ -0,0 +1,50 @@ +use nom::IResult; +use nom::number::complete::*; + +use crate::pdata::unpack::*; + +//------------------------------------------ + +static FLAGS_MASK: u64 = (1 << 16) - 1; + +//------------------------------------------ + +pub enum MappingFlags { + Valid = 1, + Dirty = 2, +} + +#[derive(Clone, Copy)] +pub struct Mapping { + pub oblock: u64, + pub flags: u32, +} + +impl Mapping { + pub fn is_valid(&self) -> bool { + return (self.flags & MappingFlags::Valid as u32) != 0; + } +} + + +impl Unpack for Mapping { + fn disk_size() -> u32 { + 8 + } + + fn unpack(i: &[u8]) -> IResult<&[u8], Mapping> { + let (i, n) = le_u64(i)?; + let oblock = n >> 16; + let flags = n & FLAGS_MASK; + + Ok(( + i, + Mapping { + oblock, + flags: flags as u32, + }, + )) + } +} + +//------------------------------------------ diff --git a/src/cache/mod.rs b/src/cache/mod.rs index 2910ec6..f1cf219 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -1 +1,4 @@ +pub mod hint; +pub mod mapping; +pub mod superblock; pub mod xml; diff --git a/src/cache/superblock.rs b/src/cache/superblock.rs new file mode 100644 index 0000000..e98a3e9 --- /dev/null +++ b/src/cache/superblock.rs @@ -0,0 +1,139 @@ +use anyhow::{anyhow, Result}; + +use crate::io_engine::*; +use nom::{bytes::complete::*, number::complete::*, IResult}; + +//------------------------------------------ + +pub const SUPERBLOCK_LOCATION: u64 = 0; + +const POLICY_NAME_SIZE: usize = 16; +const SPACE_MAP_ROOT_SIZE: usize = 128; + +//------------------------------------------ + +#[derive(Debug, Clone)] +pub struct SuperblockFlags { + pub needs_check: bool, +} + +#[derive(Debug, Clone)] +pub struct Superblock { + pub flags: SuperblockFlags, + pub block: u64, + pub version: u32, + + pub policy_name: Vec, + pub policy_version: Vec, + pub policy_hint_size: u32, + + pub metadata_sm_root: Vec, + pub mapping_root: u64, + pub dirty_root: Option, // format 2 only + pub hint_root: u64, + + pub discard_root: u64, + pub discard_block_size: u64, + pub discard_nr_blocks: u64, + + pub data_block_size: u32, + pub metadata_block_size: u32, + pub cache_blocks: u32, + + pub compat_flags: u32, + pub compat_ro_flags: u32, + pub incompat_flags: u32, + + pub read_hits: u32, + pub read_misses: u32, + pub write_hits: u32, + pub write_misses: u32, +} + +fn unpack(data: &[u8]) -> IResult<&[u8], Superblock> { + let (i, _csum) = le_u32(data)?; + let (i, flags) = le_u32(i)?; + let (i, block) = le_u64(i)?; + let (i, _uuid) = take(16usize)(i)?; + let (i, _magic) = le_u64(i)?; + let (i, version) = le_u32(i)?; + + let (i, policy_name) = take(POLICY_NAME_SIZE)(i)?; + let (i, policy_hint_size) = le_u32(i)?; + + let (i, metadata_sm_root) = take(SPACE_MAP_ROOT_SIZE)(i)?; + let (i, mapping_root) = le_u64(i)?; + let (i, hint_root) = le_u64(i)?; + + let (i, discard_root) = le_u64(i)?; + let (i, discard_block_size) = le_u64(i)?; + let (i, discard_nr_blocks) = le_u64(i)?; + + let (i, data_block_size) = le_u32(i)?; + let (i, metadata_block_size) = le_u32(i)?; + let (i, cache_blocks) = le_u32(i)?; + + let (i, compat_flags) = le_u32(i)?; + let (i, compat_ro_flags) = le_u32(i)?; + let (i, incompat_flags) = le_u32(i)?; + + let (i, read_hits) = le_u32(i)?; + let (i, read_misses) = le_u32(i)?; + let (i, write_hits) = le_u32(i)?; + let (i, write_misses) = le_u32(i)?; + + let (i, vsn_major) = le_u32(i)?; + let (i, vsn_minor) = le_u32(i)?; + let (i, vsn_patch) = le_u32(i)?; + + let mut i = i; + let mut dirty_root = None; + if version >= 2 { + let (m, root) = le_u64(i)?; + dirty_root = Some(root); + i = &m; + } + + Ok(( + i, + Superblock { + flags: SuperblockFlags { + needs_check: (flags | 0x1) != 0, + }, + block, + version, + policy_name: policy_name.to_vec(), + policy_version: vec![vsn_major, vsn_minor, vsn_patch], + policy_hint_size, + metadata_sm_root: metadata_sm_root.to_vec(), + mapping_root, + dirty_root, + hint_root, + discard_root, + discard_block_size, + discard_nr_blocks, + data_block_size, + metadata_block_size, + cache_blocks, + compat_flags, + compat_ro_flags, + incompat_flags, + read_hits, + read_misses, + write_hits, + write_misses, + }, + )) +} + +pub fn read_superblock(engine: &dyn IoEngine, loc: u64) -> Result { + let b = engine.read(loc)?; + + if let Ok((_, sb)) = unpack(&b.get_data()) { + Ok(sb) + } else { + Err(anyhow!("couldn't unpack superblock")) + } +} + +//------------------------------------------ From 58cd881340357d97dc1ddfbdc16557423a65c792 Mon Sep 17 00:00:00 2001 From: Joe Thornber Date: Wed, 17 Feb 2021 15:15:57 +0000 Subject: [PATCH 03/12] Fix regression where era_restore wouldn't work with devices. check_file_exists() had an extra parameter added with a default, which was the wrong default for era_restore. --- era/era_restore.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/era/era_restore.cc b/era/era_restore.cc index 0603a56..27dcadc 100644 --- a/era/era_restore.cc +++ b/era/era_restore.cc @@ -37,7 +37,7 @@ namespace { bool metadata_touched = false; try { block_manager::ptr bm = open_bm(*fs.output, block_manager::READ_WRITE); - file_utils::check_file_exists(*fs.output); + file_utils::check_file_exists(*fs.output, false); metadata_touched = true; metadata::ptr md(new metadata(bm, metadata::CREATE)); emitter::ptr restorer = create_restore_emitter(*md); From e3a646e4d2ca423feb4493f0b92e72f6e475aa8b Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 5 Feb 2021 16:15:24 +0800 Subject: [PATCH 04/12] [dbg] Improve error resilience of debugging tools Do not open the metadata. The tool interprets user specified blocks one-by-one. Thus, there's no need to preload the metadata structures. Also remove the unused --ignore-metadata-space-map option. It was designed to control the loading of space maps with the old metadata constructors. --- caching/cache_debug.cc | 42 ++++++++++--------- thin-provisioning/thin_debug.cc | 71 ++++++++++++++++----------------- 2 files changed, 57 insertions(+), 56 deletions(-) diff --git a/caching/cache_debug.cc b/caching/cache_debug.cc index 1053560..155fd94 100644 --- a/caching/cache_debug.cc +++ b/caching/cache_debug.cc @@ -48,7 +48,7 @@ namespace { class help : public dbg::command { virtual void exec(strings const &args, ostream &out) { out << "Commands:" << endl - << " superblock" << endl + << " superblock [block#]" << endl << " mapping_node " << endl << " mapping_array " << endl << " exit" << endl; @@ -82,16 +82,21 @@ namespace { class show_superblock : public dbg::command { public: - explicit show_superblock(metadata::ptr md) - : md_(md) { + explicit show_superblock(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 b = caching::SUPERBLOCK_LOCATION; + if (args.size() == 2) + b = boost::lexical_cast(args[1]); + caching::superblock sb = read_superblock(bm_, b); + formatter::ptr f = create_xml_formatter(); ostringstream version; - - superblock const &sb = md_->sb_; - field(*f, "csum", sb.csum); field(*f, "flags", sb.flags.encode()); field(*f, "blocknr", sb.blocknr); @@ -137,7 +142,7 @@ namespace { } private: - metadata::ptr md_; + block_manager::ptr bm_; }; class uint64_show_traits : public uint64_traits { @@ -167,8 +172,8 @@ namespace { template class show_btree_node : public dbg::command { public: - explicit show_btree_node(metadata::ptr md) - : md_(md) { + explicit show_btree_node(block_manager::ptr bm) + : bm_(bm) { } virtual void exec(strings const &args, ostream &out) { @@ -178,7 +183,7 @@ namespace { throw runtime_error("incorrect number of arguments"); block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = md_->tm_->read_lock(block); + block_manager::read_ref rr = bm_->read_lock(block); node_ref n = btree_detail::to_node(rr); if (n.get_type() == INTERNAL) @@ -211,15 +216,15 @@ namespace { f->output(out, 0); } - metadata::ptr md_; + block_manager::ptr bm_; }; template class show_array_block : public dbg::command { typedef array_block rblock; public: - explicit show_array_block(metadata::ptr md) - : md_(md) { + explicit show_array_block(block_manager::ptr bm) + : bm_(bm) { } virtual void exec(strings const& args, ostream &out) { @@ -227,7 +232,7 @@ namespace { throw runtime_error("incorrect number of arguments"); block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = md_->tm_->read_lock(block); + block_manager::read_ref rr = bm_->read_lock(block); rblock b(rr, typename ValueTraits::ref_counter()); show_array_entries(b, out); @@ -252,7 +257,7 @@ namespace { f->output(out, 0); } - metadata::ptr md_; + block_manager::ptr bm_; }; //-------------------------------- @@ -262,12 +267,11 @@ namespace { try { block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY); - metadata::ptr md(new metadata(bm)); command_interpreter::ptr interp = create_command_interpreter(cin, cout); interp->register_command("hello", command::ptr(new hello)); - interp->register_command("superblock", command::ptr(new show_superblock(md))); - interp->register_command("mapping_node", command::ptr(new show_btree_node(md))); - interp->register_command("mapping_array", command::ptr(new show_array_block(md))); + interp->register_command("superblock", command::ptr(new show_superblock(bm))); + interp->register_command("mapping_node", command::ptr(new show_btree_node(bm))); + interp->register_command("mapping_array", command::ptr(new show_array_block(bm))); interp->register_command("help", command::ptr(new help)); interp->register_command("exit", command::ptr(new exit_handler(interp))); interp->enter_main_loop(); diff --git a/thin-provisioning/thin_debug.cc b/thin-provisioning/thin_debug.cc index e458d7d..37a5f18 100644 --- a/thin-provisioning/thin_debug.cc +++ b/thin-provisioning/thin_debug.cc @@ -48,7 +48,7 @@ namespace { class help : public dbg::command { virtual void exec(strings const &args, ostream &out) { out << "Commands:" << endl - << " superblock" << endl + << " superblock [block#]" << endl << " m1_node " << endl << " m2_node " << endl << " detail_node " << endl @@ -85,15 +85,20 @@ namespace { class show_superblock : public dbg::command { public: - explicit show_superblock(metadata::ptr md) - : md_(md) { + explicit show_superblock(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 b = superblock_detail::SUPERBLOCK_LOCATION; + if (args.size() == 2) + b = boost::lexical_cast(args[1]); + superblock_detail::superblock sb = read_superblock(bm_, b); + formatter::ptr f = create_xml_formatter(); - - thin_provisioning::superblock_detail::superblock const &sb = md_->sb_; - field(*f, "csum", sb.csum_); field(*f, "flags", sb.flags_); field(*f, "blocknr", sb.blocknr_); @@ -134,7 +139,7 @@ namespace { } private: - metadata::ptr md_; + block_manager::ptr bm_; }; class device_details_show_traits : public thin_provisioning::device_tree_detail::device_details_traits { @@ -185,8 +190,8 @@ namespace { template class show_btree_node : public dbg::command { public: - explicit show_btree_node(metadata::ptr md) - : md_(md) { + explicit show_btree_node(block_manager::ptr bm) + : bm_(bm) { } virtual void exec(strings const &args, ostream &out) { @@ -196,7 +201,7 @@ namespace { throw runtime_error("incorrect number of arguments"); block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = md_->tm_->read_lock(block); + block_manager::read_ref rr = bm_->read_lock(block); node_ref n = btree_detail::to_node(rr); if (n.get_type() == INTERNAL) @@ -229,32 +234,27 @@ namespace { f->output(out, 0); } - metadata::ptr md_; + block_manager::ptr bm_; }; class show_index_block : public dbg::command { public: - explicit show_index_block(metadata::ptr md) - : md_(md) { + 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"); - // manually load metadata_index, without using index_validator() block_address block = boost::lexical_cast(args[1]); - block_manager::read_ref rr = md_->tm_->read_lock(block); - sm_disk_detail::sm_root_disk const *d = - reinterpret_cast(md_->sb_.metadata_space_map_root_); - sm_disk_detail::sm_root v; - sm_disk_detail::sm_root_traits::unpack(*d, v); - block_address nr_indexes = base::div_up(v.nr_blocks_, ENTRIES_PER_BLOCK); + // 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, nr_indexes, out); + show_metadata_index(mdi, sm_disk_detail::MAX_METADATA_BITMAPS, out); } private: @@ -267,6 +267,10 @@ namespace { 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); @@ -275,25 +279,24 @@ namespace { } unsigned const ENTRIES_PER_BLOCK = (MD_BLOCK_SIZE - sizeof(persistent_data::sm_disk_detail::bitmap_header)) * 4; - metadata::ptr md_; + block_manager::ptr bm_; }; //-------------------------------- - int debug_(string const &path, bool ignore_metadata_sm) { + int debug_(string const &path) { using dbg::command; try { block_manager::ptr bm = open_bm(path, block_manager::READ_ONLY, 1); - metadata::ptr md(new metadata(bm, false)); command_interpreter::ptr interp = create_command_interpreter(cin, cout); interp->register_command("hello", command::ptr(new hello)); - interp->register_command("superblock", command::ptr(new show_superblock(md))); - interp->register_command("m1_node", command::ptr(new show_btree_node(md))); - interp->register_command("m2_node", command::ptr(new show_btree_node(md))); - interp->register_command("detail_node", command::ptr(new show_btree_node(md))); - interp->register_command("index_block", command::ptr(new show_index_block(md))); - interp->register_command("index_node", command::ptr(new show_btree_node(md))); + 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("help", command::ptr(new help)); interp->register_command("exit", command::ptr(new exit_handler(interp))); interp->enter_main_loop(); @@ -329,10 +332,8 @@ thin_debug_cmd::run(int argc, char **argv) const struct option longopts[] = { { "help", no_argument, NULL, 'h'}, { "version", no_argument, NULL, 'V'}, - { "ignore-metadata-sm", no_argument, NULL, 1}, { NULL, no_argument, NULL, 0 } }; - bool ignore_metadata_sm = false; while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { switch(c) { @@ -343,10 +344,6 @@ thin_debug_cmd::run(int argc, char **argv) case 'V': cerr << THIN_PROVISIONING_TOOLS_VERSION << endl; return 0; - - case 1: - ignore_metadata_sm = true; - break; } } @@ -355,5 +352,5 @@ thin_debug_cmd::run(int argc, char **argv) exit(1); } - return debug_(argv[optind], ignore_metadata_sm); + return debug_(argv[optind]); } From 1bc88bfde88f189e719e611bcd326c2ba4d5c5e3 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Sun, 7 Feb 2021 02:44:08 +0800 Subject: [PATCH 05/12] [cache_debug] Simplify the trait types --- caching/cache_debug.cc | 59 +++++++++++++++++++++--------------------- 1 file changed, 29 insertions(+), 30 deletions(-) diff --git a/caching/cache_debug.cc b/caching/cache_debug.cc index 155fd94..4f0c68d 100644 --- a/caching/cache_debug.cc +++ b/caching/cache_debug.cc @@ -49,8 +49,8 @@ namespace { virtual void exec(strings const &args, ostream &out) { out << "Commands:" << endl << " superblock [block#]" << endl - << " mapping_node " << endl - << " mapping_array " << endl + << " block_node " << endl + << " mapping_block " << endl << " exit" << endl; } }; @@ -145,31 +145,28 @@ 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)); } }; - // FIXME: Expose the array ValueTraits parameter or not? - // Since the block_traits isn't related to the ValueTraits. - class block_show_traits : public persistent_data::array::block_traits { - public: - static void show(formatter::ptr f, string const &key, block_address const &value) { - field(*f, "block", value); - } - }; - class mapping_show_traits : public caching::mapping_traits { public: + typedef mapping_traits value_trait; + static void show(formatter::ptr f, string const &key, caching::mapping const &value) { field(*f, "oblock", value.oblock_); field(*f, "flags", value.flags_); } }; - template + // FIXME: duplication + template class show_btree_node : public dbg::command { public: explicit show_btree_node(block_manager::ptr bm) @@ -185,22 +182,22 @@ namespace { 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); + 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); + node_ref n = btree_detail::to_node(rr); + show_node(n, out); } } private: - template - void show_node(node_ref n, ostream &out) { + 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_location()); + 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()); @@ -209,8 +206,8 @@ namespace { for (unsigned i = 0; i < n.get_nr_entries(); i++) { formatter::ptr f2 = create_xml_formatter(); field(*f2, "key", n.key_at(i)); - VT::show(f2, "value", n.value_at(i)); - f->child("child", f2); + ST::show(f2, "value", n.value_at(i)); + f->child(boost::lexical_cast(i), f2); } f->output(out, 0); @@ -219,12 +216,13 @@ namespace { block_manager::ptr bm_; }; - template + template class show_array_block : public dbg::command { - typedef array_block rblock; + typedef array_block rblock; public: - explicit show_array_block(block_manager::ptr bm) - : bm_(bm) { + 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) { @@ -234,7 +232,7 @@ namespace { block_address block = boost::lexical_cast(args[1]); block_manager::read_ref rr = bm_->read_lock(block); - rblock b(rr, typename ValueTraits::ref_counter()); + rblock b(rr, rc_); show_array_entries(b, out); } @@ -249,15 +247,15 @@ namespace { for (unsigned i = 0; i < nr_entries; i++) { formatter::ptr f2 = create_xml_formatter(); - field(*f2, "index", i); - ValueTraits::show(f2, "value", b.get(i)); - f->child("child", f2); + 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_; }; //-------------------------------- @@ -270,8 +268,9 @@ namespace { command_interpreter::ptr interp = create_command_interpreter(cin, cout); interp->register_command("hello", command::ptr(new hello)); interp->register_command("superblock", command::ptr(new show_superblock(bm))); - interp->register_command("mapping_node", command::ptr(new show_btree_node(bm))); - interp->register_command("mapping_array", command::ptr(new show_array_block(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("help", command::ptr(new help)); interp->register_command("exit", command::ptr(new exit_handler(interp))); interp->enter_main_loop(); From afbd913e2233d8baf967f867c2e6be874707253f Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Sun, 7 Feb 2021 16:03:58 +0800 Subject: [PATCH 06/12] [era] Add era_debug --- Makefile.in | 1 + era/commands.h | 12 ++ era/devel_commands.cc | 2 + era/era_debug.cc | 346 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 361 insertions(+) create mode 100644 era/era_debug.cc diff --git a/Makefile.in b/Makefile.in index 949e30a..0392d8e 100644 --- a/Makefile.in +++ b/Makefile.in @@ -131,6 +131,7 @@ DEVTOOLS_SOURCE=\ caching/cache_debug.cc \ caching/devel_commands.cc \ era/devel_commands.cc \ + era/era_debug.cc \ thin-provisioning/damage_generator.cc \ thin-provisioning/devel_commands.cc \ thin-provisioning/thin_debug.cc \ diff --git a/era/commands.h b/era/commands.h index 8e7a92c..1ccaf0b 100644 --- a/era/commands.h +++ b/era/commands.h @@ -34,6 +34,18 @@ namespace era { virtual int run(int argc, char **argv); }; + //------------------------------------------------------ + + class era_debug_cmd : public base::command { + public: + era_debug_cmd(); + + virtual void usage(std::ostream &out) const; + virtual int run(int argc, char **argv); + }; + + //------------------------------------------------------ + void register_era_commands(base::application &app); } diff --git a/era/devel_commands.cc b/era/devel_commands.cc index eac6827..bfee083 100644 --- a/era/devel_commands.cc +++ b/era/devel_commands.cc @@ -1,12 +1,14 @@ #include "era/commands.h" using namespace base; +using namespace era; //---------------------------------------------------------------- void era::register_era_commands(base::application &app) { + app.add_cmd(command::ptr(new era_debug_cmd)); } //---------------------------------------------------------------- diff --git a/era/era_debug.cc b/era/era_debug.cc new file mode 100644 index 0000000..cf1f682 --- /dev/null +++ b/era/era_debug.cc @@ -0,0 +1,346 @@ +// Copyright (C) 2012 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 +// . + +#include +#include +#include +#include + +#include "base/command_interpreter.h" +#include "base/output_formatter.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" +#include "version.h" + +using namespace dbg; +using namespace persistent_data; +using namespace std; +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 + << " superblock [block#]" << endl + << " block_node " << endl + << " bitset_block " << endl + << " era_block " << endl + << " writeset_node " << endl + << " exit" << endl; + } + }; + + 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: + typedef era_detail_traits value_trait; + + static void show(formatter::ptr f, string const &key, era_detail const &value) { + field(*f, "nr_bits", value.nr_bits); + field(*f, "writeset_root", value.writeset_root); + } + }; + + class show_superblock : public dbg::command { + public: + explicit show_superblock(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 b = era::SUPERBLOCK_LOCATION; + if (args.size() == 2) + b = boost::lexical_cast(args[1]); + era::superblock sb = read_superblock(bm_, b); + + formatter::ptr f = create_xml_formatter(); + ostringstream version; + field(*f, "csum", sb.csum); + field(*f, "flags", sb.flags.encode()); + field(*f, "blocknr", sb.blocknr); + field(*f, "uuid", sb.uuid); // FIXME: delimit, and handle non-printable chars + field(*f, "magic", sb.magic); + field(*f, "version", sb.version); + + sm_disk_detail::sm_root_disk const *d; + sm_disk_detail::sm_root v; + { + d = reinterpret_cast(sb.metadata_space_map_root); + sm_disk_detail::sm_root_traits::unpack(*d, v); + formatter::ptr f2 = create_xml_formatter(); + sm_root_show_traits::show(f2, "value", v); + f->child("metadata_space_map_root", f2); + } + + field(*f, "data_block_size", sb.data_block_size); + field(*f, "metadata_block_size", sb.metadata_block_size); + field(*f, "nr_blocks", sb.nr_blocks); + + field(*f, "current_era", sb.current_era); + { + formatter::ptr f2 = create_xml_formatter(); + writeset_show_traits::show(f2, "value", sb.current_detail); + f->child("current_writeset", f2); + } + + field(*f, "writeset_tree_root", sb.writeset_tree_root); + field(*f, "era_array_root", sb.era_array_root); + + if (sb.metadata_snap) + field(*f, "metadata_snap", *sb.metadata_snap); + + f->output(out, 0); + } + + private: + 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_; + }; + + //-------------------------------- + + int debug(string const &path) { + using dbg::command; + + try { + 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("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_array_block(bm, + uint64_show_traits::ref_counter()))); + 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("help", command::ptr(new help)); + interp->register_command("exit", command::ptr(new exit_handler(interp))); + interp->enter_main_loop(); + + } catch (std::exception &e) { + cerr << e.what(); + return 1; + } + + return 0; + } +} + +//---------------------------------------------------------------- + +era_debug_cmd::era_debug_cmd() + : command("era_debug") +{ +} + +void +era_debug_cmd::usage(std::ostream &out) const +{ + out << "Usage: " << get_name() << " {device|file}" << endl + << "Options:" << endl + << " {-h|--help}" << endl + << " {-V|--version}" << endl; +} + +int +era_debug_cmd::run(int argc, char **argv) +{ + int c; + const char shortopts[] = "hV"; + const struct option longopts[] = { + { "help", no_argument, NULL, 'h'}, + { "version", no_argument, NULL, 'V'}, + { NULL, no_argument, NULL, 0 } + }; + + while ((c = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { + switch(c) { + case 'h': + usage(cout); + return 0; + + case 'V': + cerr << THIN_PROVISIONING_TOOLS_VERSION << endl; + return 0; + } + } + + if (argc == optind) { + usage(cerr); + exit(1); + } + + return debug(argv[optind]); +} + +//---------------------------------------------------------------- From c95e31bef6e9e14000b3003cecb6579986e786e2 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Thu, 18 Feb 2021 18:20:01 +0800 Subject: [PATCH 07/12] [era_debug] Display bitset entries in run-length fashion --- era/era_debug.cc | 128 ++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 126 insertions(+), 2 deletions(-) diff --git a/era/era_debug.cc b/era/era_debug.cc index cf1f682..af6764c 100644 --- a/era/era_debug.cc +++ b/era/era_debug.cc @@ -266,6 +266,131 @@ namespace { 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; + }; + //-------------------------------- int debug(string const &path) { @@ -278,8 +403,7 @@ namespace { interp->register_command("hello", command::ptr(new hello)); 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_array_block(bm, - uint64_show_traits::ref_counter()))); + 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))); From a81cef44674dd71e532fc3a11d6a232257a0059c Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 19 Feb 2021 14:06:21 +0800 Subject: [PATCH 08/12] [dbg] Pull out common code into dbg-lib - Modularize common routines - Extract the block_dumper interface for displaying blocks - Remove inheritance from show_traits --- Makefile.in | 9 +- caching/cache_debug.cc | 167 ++---------- dbg-lib/array_block_dumper.h | 53 ++++ dbg-lib/bitset_block_dumper.cc | 137 ++++++++++ dbg-lib/bitset_block_dumper.h | 14 + dbg-lib/block_dumper.h | 21 ++ dbg-lib/btree_node_dumper.h | 59 +++++ {base => dbg-lib}/command_interpreter.cc | 2 +- {base => dbg-lib}/command_interpreter.h | 0 dbg-lib/commands.cc | 79 ++++++ dbg-lib/commands.h | 24 ++ dbg-lib/index_block_dumper.cc | 51 ++++ dbg-lib/index_block_dumper.h | 10 + {base => dbg-lib}/output_formatter.cc | 2 +- {base => dbg-lib}/output_formatter.h | 0 dbg-lib/simple_show_traits.cc | 19 ++ dbg-lib/simple_show_traits.h | 27 ++ dbg-lib/sm_show_traits.cc | 27 ++ dbg-lib/sm_show_traits.h | 29 +++ era/era_debug.cc | 314 +++-------------------- thin-provisioning/thin_debug.cc | 183 ++----------- 21 files changed, 640 insertions(+), 587 deletions(-) create mode 100644 dbg-lib/array_block_dumper.h create mode 100644 dbg-lib/bitset_block_dumper.cc create mode 100644 dbg-lib/bitset_block_dumper.h create mode 100644 dbg-lib/block_dumper.h create mode 100644 dbg-lib/btree_node_dumper.h rename {base => dbg-lib}/command_interpreter.cc (97%) rename {base => dbg-lib}/command_interpreter.h (100%) create mode 100644 dbg-lib/commands.cc create mode 100644 dbg-lib/commands.h create mode 100644 dbg-lib/index_block_dumper.cc create mode 100644 dbg-lib/index_block_dumper.h rename {base => dbg-lib}/output_formatter.cc (98%) rename {base => dbg-lib}/output_formatter.h (100%) create mode 100644 dbg-lib/simple_show_traits.cc create mode 100644 dbg-lib/simple_show_traits.h create mode 100644 dbg-lib/sm_show_traits.cc create mode 100644 dbg-lib/sm_show_traits.h 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) { From 29f9182078624095e9716eb99dd89f1f39c40900 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Sun, 21 Feb 2021 00:49:53 +0800 Subject: [PATCH 09/12] [dbg] Remove nested function template to reduce code size --- dbg-lib/btree_node_dumper.h | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/dbg-lib/btree_node_dumper.h b/dbg-lib/btree_node_dumper.h index f92bce2..44fd2a9 100644 --- a/dbg-lib/btree_node_dumper.h +++ b/dbg-lib/btree_node_dumper.h @@ -17,16 +17,14 @@ namespace dbg { 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); + btree_node_dumper::show_node(n, out); else { node_ref n = btree_detail::to_node(rr); - show_node(n, out); + show_node(n, out); } } - private: - template - void show_node(node_ref n, std::ostream &out) { + static void show_node(node_ref n, std::ostream &out) { formatter::ptr f = create_xml_formatter(); field(*f, "csum", n.get_checksum()); @@ -39,7 +37,7 @@ namespace dbg { 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)); + ShowTraits::show(f2, "value", n.value_at(i)); f->child(boost::lexical_cast(i), f2); } From e8410dec04003ae91d1cfc05cd83bfe160a8cc23 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Sun, 21 Feb 2021 15:49:02 +0800 Subject: [PATCH 10/12] [dbg] Add missing commands to cache/era_debug --- caching/cache_debug.cc | 16 ++++++++++++++++ era/era_debug.cc | 8 ++++++++ 2 files changed, 24 insertions(+) diff --git a/caching/cache_debug.cc b/caching/cache_debug.cc index 4f9e033..838bd24 100644 --- a/caching/cache_debug.cc +++ b/caching/cache_debug.cc @@ -23,8 +23,10 @@ #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/index_block_dumper.h" #include "dbg-lib/output_formatter.h" #include "dbg-lib/sm_show_traits.h" #include "persistent-data/file_utils.h" @@ -46,6 +48,8 @@ namespace { out << "Commands:" << endl << " superblock [block#]" << endl << " block_node " << endl + << " bitset_block " << endl + << " index_block " << endl << " mapping_block " << endl << " exit" << endl; } @@ -141,6 +145,16 @@ namespace { 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()); + } + + 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; @@ -150,6 +164,8 @@ namespace { interp->register_command("hello", create_hello_handler()); interp->register_command("superblock", command::ptr(new show_superblock(bm))); interp->register_command("block_node", create_btree_node_handler(bm)); + interp->register_command("bitset_block", create_bitset_block_handler(bm)); + interp->register_command("index_block", create_index_block_handler(bm)); interp->register_command("mapping_block", create_array_block_handler(bm, mapping_traits::ref_counter())); interp->register_command("help", command::ptr(new help)); diff --git a/era/era_debug.cc b/era/era_debug.cc index 5470b4c..3769d4d 100644 --- a/era/era_debug.cc +++ b/era/era_debug.cc @@ -26,6 +26,7 @@ #include "dbg-lib/bitset_block_dumper.h" #include "dbg-lib/command_interpreter.h" #include "dbg-lib/commands.h" +#include "dbg-lib/index_block_dumper.h" #include "dbg-lib/output_formatter.h" #include "dbg-lib/sm_show_traits.h" #include "persistent-data/file_utils.h" @@ -49,6 +50,7 @@ namespace { << " block_node " << endl << " bitset_block " << endl << " era_block " << endl + << " index_block " << endl << " writeset_node " << endl << " exit" << endl; } @@ -143,6 +145,11 @@ namespace { return create_block_handler(bm, create_bitset_block_dumper()); } + 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; @@ -156,6 +163,7 @@ namespace { 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("index_block", create_index_block_handler(bm)); interp->register_command("writeset_node", create_btree_node_handler(bm)); interp->register_command("help", command::ptr(new help)); interp->register_command("exit", create_exit_handler(interp)); From 2bb3bf65b7e3ce56dbcaa8e498229c0263717235 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Sun, 20 Dec 2020 20:23:30 +0800 Subject: [PATCH 11/12] [cache_check (rust)] Implement basic functions --- src/bin/cache_check.rs | 62 +++++++++++++++ src/cache/check.rs | 166 +++++++++++++++++++++++++++++++++++++++++ src/cache/mod.rs | 1 + 3 files changed, 229 insertions(+) create mode 100644 src/bin/cache_check.rs create mode 100644 src/cache/check.rs diff --git a/src/bin/cache_check.rs b/src/bin/cache_check.rs new file mode 100644 index 0000000..4fed20c --- /dev/null +++ b/src/bin/cache_check.rs @@ -0,0 +1,62 @@ +extern crate clap; +extern crate thinp; + +use clap::{App, Arg}; +use std::path::Path; +use thinp::cache::check::{check, CacheCheckOptions}; + +//------------------------------------------ + +fn main() { + let parser = App::new("cache_check") + .version(thinp::version::TOOLS_VERSION) + .arg( + Arg::with_name("INPUT") + .help("Specify the input device to check") + .required(true) + .index(1), + ) + .arg( + Arg::with_name("SB_ONLY") + .help("Only check the superblock.") + .long("super-block-only") + .value_name("SB_ONLY"), + ) + .arg( + Arg::with_name("SKIP_MAPPINGS") + .help("Don't check the mapping array") + .long("skip-mappings") + .value_name("SKIP_MAPPINGS"), + ) + .arg( + Arg::with_name("SKIP_HINTS") + .help("Don't check the hint array") + .long("skip-hints") + .value_name("SKIP_HINTS"), + ) + .arg( + Arg::with_name("SKIP_DISCARDS") + .help("Don't check the discard bitset") + .long("skip-discards") + .value_name("SKIP_DISCARDS"), + ); + + let matches = parser.get_matches(); + let input_file = Path::new(matches.value_of("INPUT").unwrap()); + + let opts = CacheCheckOptions { + dev: &input_file, + async_io: false, + sb_only: matches.is_present("SB_ONLY"), + skip_mappings: matches.is_present("SKIP_MAPPINGS"), + skip_hints: matches.is_present("SKIP_HINTS"), + skip_discards: matches.is_present("SKIP_DISCARDS"), + }; + + if let Err(reason) = check(opts) { + eprintln!("{}", reason); + std::process::exit(1); + } +} + +//------------------------------------------ diff --git a/src/cache/check.rs b/src/cache/check.rs new file mode 100644 index 0000000..92895e1 --- /dev/null +++ b/src/cache/check.rs @@ -0,0 +1,166 @@ +use anyhow::anyhow; +use std::marker::PhantomData; +use std::path::Path; +use std::sync::{Arc, Mutex}; +use std::collections::*; + +use crate::io_engine::{AsyncIoEngine, IoEngine, SyncIoEngine}; +use crate::cache::hint::*; +use crate::cache::mapping::*; +use crate::cache::superblock::*; +use crate::pdata::array_walker::*; + +//------------------------------------------ + +const MAX_CONCURRENT_IO: u32 = 1024; + +//------------------------------------------ + +struct CheckMappingVisitor { + allowed_flags: u32, + seen_oblocks: Arc>>, +} + +impl CheckMappingVisitor { + fn new(metadata_version: u32) -> CheckMappingVisitor { + let mut flags: u32 = MappingFlags::Valid as u32; + if metadata_version == 1 { + flags |= MappingFlags::Dirty as u32; + } + CheckMappingVisitor { + allowed_flags: flags, + seen_oblocks: Arc::new(Mutex::new(BTreeSet::new())), + } + } + + fn seen_oblock(&self, b: u64) -> bool { + let seen_oblocks = self.seen_oblocks.lock().unwrap(); + return seen_oblocks.contains(&b); + } + + fn record_oblock(&self, b: u64) { + let mut seen_oblocks = self.seen_oblocks.lock().unwrap(); + seen_oblocks.insert(b); + } + + // FIXME: is it possible to validate flags at an early phase? + // e.g., move to ctor of Mapping? + fn has_unknown_flags(&self, m: &Mapping) -> bool { + return (m.flags & self.allowed_flags) != 0; + } +} + +impl ArrayBlockVisitor for CheckMappingVisitor { + fn visit(&self, _index: u64, m: Mapping) -> anyhow::Result<()> { + if !m.is_valid() { + return Ok(()); + } + + if self.seen_oblock(m.oblock) { + return Err(anyhow!("origin block already mapped")); + } + + self.record_oblock(m.oblock); + + if !self.has_unknown_flags(&m) { + return Err(anyhow!("unknown flags in mapping")); + } + + Ok(()) + } +} + +//------------------------------------------ + +struct CheckHintVisitor { + _not_used: PhantomData, +} + +impl CheckHintVisitor { + fn new() -> CheckHintVisitor { + CheckHintVisitor { + _not_used: PhantomData, + } + } +} + +impl ArrayBlockVisitor> for CheckHintVisitor { + fn visit(&self, _index: u64, _hint: Hint) -> anyhow::Result<()> { + // TODO: check hints + Ok(()) + } +} + +//------------------------------------------ + +// TODO: ignore_non_fatal, clear_needs_check, auto_repair +pub struct CacheCheckOptions<'a> { + pub dev: &'a Path, + pub async_io: bool, + pub sb_only: bool, + pub skip_mappings: bool, + pub skip_hints: bool, + pub skip_discards: bool, +} + +// TODO: thread pool, report +struct Context { + engine: Arc, +} + +fn mk_context(opts: &CacheCheckOptions) -> anyhow::Result { + let engine: Arc; + + if opts.async_io { + engine = Arc::new(AsyncIoEngine::new( + opts.dev, + MAX_CONCURRENT_IO, + false, + )?); + } else { + let nr_threads = std::cmp::max(8, num_cpus::get() * 2); + engine = Arc::new(SyncIoEngine::new(opts.dev, nr_threads, false)?); + } + + Ok(Context { + engine, + }) +} + +pub fn check(opts: CacheCheckOptions) -> anyhow::Result<()> { + let ctx = mk_context(&opts)?; + + let engine = &ctx.engine; + + let sb = read_superblock(engine.as_ref(), SUPERBLOCK_LOCATION)?; + + if opts.sb_only { + return Ok(()); + } + + // TODO: factor out into check_mappings() + if !opts.skip_mappings { + let w = ArrayWalker::new(engine.clone(), false); + let c = Box::new(CheckMappingVisitor::new(sb.version)); + w.walk(c, sb.mapping_root)?; + + if sb.version >= 2 { + // TODO: check dirty bitset + } + } + + if !opts.skip_hints && sb.hint_root != 0 { + let w = ArrayWalker::new(engine.clone(), false); + type Width = typenum::U4; // FIXME: check sb.policy_hint_size + let c = Box::new(CheckHintVisitor::::new()); + w.walk(c, sb.hint_root)?; + } + + if !opts.skip_discards { + // TODO: check discard bitset + } + + Ok(()) +} + +//------------------------------------------ diff --git a/src/cache/mod.rs b/src/cache/mod.rs index f1cf219..a120ce6 100644 --- a/src/cache/mod.rs +++ b/src/cache/mod.rs @@ -1,3 +1,4 @@ +pub mod check; pub mod hint; pub mod mapping; pub mod superblock; From a7ecfba2b4de69b72ec537170a8f37ff75521e4f Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Wed, 24 Feb 2021 20:21:10 +0800 Subject: [PATCH 12/12] [cache (rust)] Fix merge conflicts --- src/pdata/array_walker.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/pdata/array_walker.rs b/src/pdata/array_walker.rs index 6322c50..e8287b1 100644 --- a/src/pdata/array_walker.rs +++ b/src/pdata/array_walker.rs @@ -55,7 +55,7 @@ impl NodeVisitor for BlockValueVisitor { // FIXME: return errors fn visit( &self, - path: &Vec, + path: &[u64], _kr: &KeyRange, _h: &NodeHeader, keys: &[u64], @@ -71,7 +71,7 @@ impl NodeVisitor for BlockValueVisitor { } // FIXME: stub - fn visit_again(&self, _path: &Vec, _b: u64) -> Result<()> { + fn visit_again(&self, _path: &[u64], _b: u64) -> Result<()> { Ok(()) }