2020-07-28 15:21:48 +05:30
|
|
|
use anyhow::{anyhow, Result};
|
2020-07-30 14:42:51 +05:30
|
|
|
use nom::{number::complete::*, IResult};
|
2020-07-27 20:23:42 +05:30
|
|
|
use std::path::Path;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2020-07-30 14:42:51 +05:30
|
|
|
use std::time::Instant;
|
2020-07-29 21:08:52 +05:30
|
|
|
use threadpool::ThreadPool;
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-30 14:42:51 +05:30
|
|
|
use crate::block_manager::{AsyncIoEngine, Block, IoEngine};
|
2020-08-03 19:34:59 +05:30
|
|
|
use crate::pdata::btree::{BTreeWalker, Node, NodeVisitor, ValueType, ValueU64};
|
2020-07-28 15:21:48 +05:30
|
|
|
use crate::thin::superblock::*;
|
2020-07-29 15:42:03 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|
|
|
|
|
2020-07-30 14:42:51 +05:30
|
|
|
#[allow(dead_code)]
|
2020-07-28 15:21:48 +05:30
|
|
|
struct BlockTime {
|
|
|
|
block: u64,
|
|
|
|
time: u32,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ValueBlockTime;
|
|
|
|
|
|
|
|
impl ValueType for ValueBlockTime {
|
|
|
|
type Value = BlockTime;
|
2020-08-03 19:34:59 +05:30
|
|
|
|
|
|
|
fn disk_size() -> u32 {
|
|
|
|
8
|
|
|
|
}
|
|
|
|
|
2020-07-28 15:21:48 +05:30
|
|
|
fn unpack(i: &[u8]) -> IResult<&[u8], BlockTime> {
|
|
|
|
let (i, n) = le_u64(i)?;
|
|
|
|
let block = n >> 24;
|
|
|
|
let time = n & ((1 << 24) - 1);
|
|
|
|
|
|
|
|
Ok((
|
|
|
|
i,
|
|
|
|
BlockTime {
|
|
|
|
block,
|
|
|
|
time: time as u32,
|
|
|
|
},
|
|
|
|
))
|
2020-06-09 13:45:00 +05:30
|
|
|
}
|
2020-07-28 15:21:48 +05:30
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
struct TopLevelVisitor {}
|
|
|
|
|
|
|
|
impl NodeVisitor<ValueU64> for TopLevelVisitor {
|
2020-07-29 21:08:52 +05:30
|
|
|
fn visit(&mut self, w: &BTreeWalker, _b: &Block, node: &Node<ValueU64>) -> Result<()> {
|
2020-07-29 15:42:03 +05:30
|
|
|
if let Node::Leaf {
|
|
|
|
header: _h,
|
|
|
|
keys,
|
|
|
|
values,
|
|
|
|
} = node
|
|
|
|
{
|
2020-07-29 21:08:52 +05:30
|
|
|
let mut blocks = Vec::new();
|
|
|
|
let mut thin_ids = Vec::new();
|
|
|
|
let seen = w.seen.lock().unwrap();
|
|
|
|
for n in 0..keys.len() {
|
|
|
|
let b = values[n];
|
|
|
|
if !seen[b as usize] {
|
|
|
|
thin_ids.push(keys[n]);
|
|
|
|
blocks.push(Block::new(b));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
drop(seen);
|
|
|
|
|
2020-07-30 14:29:02 +05:30
|
|
|
w.engine.read_many(&mut blocks)?;
|
2020-07-29 21:08:52 +05:30
|
|
|
|
2020-08-03 17:07:32 +05:30
|
|
|
// FIXME: with a thread pool we need to return errors another way.
|
2020-07-30 14:47:36 +05:30
|
|
|
let nr_workers = 4;
|
2020-07-29 21:08:52 +05:30
|
|
|
let pool = ThreadPool::new(nr_workers);
|
|
|
|
|
|
|
|
let mut n = 0;
|
|
|
|
for b in blocks {
|
|
|
|
let thin_id = thin_ids[n];
|
|
|
|
n += 1;
|
2020-08-03 17:07:32 +05:30
|
|
|
|
2020-07-29 21:08:52 +05:30
|
|
|
let mut w = w.clone();
|
|
|
|
pool.execute(move || {
|
|
|
|
let mut v = BottomLevelVisitor {};
|
2020-08-03 19:34:59 +05:30
|
|
|
let result = w.walk(&mut v, &b).expect("walk failed"); // FIXME: return error
|
|
|
|
eprintln!("checked thin_dev {} -> {:?}", thin_id, result);
|
2020-07-29 21:08:52 +05:30
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
pool.join();
|
2020-07-28 16:15:25 +05:30
|
|
|
}
|
2020-07-29 21:08:52 +05:30
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
Ok(())
|
2020-07-28 16:15:25 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
struct BottomLevelVisitor {}
|
2020-07-28 15:21:48 +05:30
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
impl NodeVisitor<ValueBlockTime> for BottomLevelVisitor {
|
2020-07-29 21:08:52 +05:30
|
|
|
fn visit(&mut self, _w: &BTreeWalker, _b: &Block, _node: &Node<ValueBlockTime>) -> Result<()> {
|
2020-07-29 15:42:03 +05:30
|
|
|
Ok(())
|
2020-07-28 15:21:48 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
//------------------------------------------
|
|
|
|
|
2020-07-28 15:21:48 +05:30
|
|
|
pub fn check(dev: &Path) -> Result<()> {
|
2020-07-28 17:27:30 +05:30
|
|
|
//let mut engine = SyncIoEngine::new(dev)?;
|
|
|
|
let mut engine = AsyncIoEngine::new(dev, 256)?;
|
2020-06-09 13:45:00 +05:30
|
|
|
|
2020-07-27 20:23:42 +05:30
|
|
|
let now = Instant::now();
|
2020-07-28 15:21:48 +05:30
|
|
|
let sb = read_superblock(&mut engine, SUPERBLOCK_LOCATION)?;
|
|
|
|
eprintln!("{:?}", sb);
|
2020-07-28 16:15:25 +05:30
|
|
|
|
|
|
|
let mut root = Block::new(sb.mapping_root);
|
|
|
|
engine.read(&mut root)?;
|
|
|
|
|
2020-07-29 15:42:03 +05:30
|
|
|
let mut visitor = TopLevelVisitor {};
|
2020-08-03 19:34:59 +05:30
|
|
|
let mut w = BTreeWalker::new(engine, false);
|
|
|
|
let _result = w.walk(&mut visitor, &root)?;
|
2020-07-29 15:42:03 +05:30
|
|
|
println!("read mapping tree in {} ms", now.elapsed().as_millis());
|
2020-07-27 20:23:42 +05:30
|
|
|
|
2020-06-09 13:45:00 +05:30
|
|
|
Ok(())
|
|
|
|
}
|
2020-07-29 21:08:52 +05:30
|
|
|
|
|
|
|
//------------------------------------------
|