From bdf90df56f1d5d9f2c99bcca001d5a959db448dd Mon Sep 17 00:00:00 2001 From: Robert Habermeier Date: Fri, 23 Dec 2016 14:50:42 +0100 Subject: [PATCH] client report and heap size for header chain --- ethcore/light/src/client/header_chain.rs | 19 ++++++++++++++++++- ethcore/light/src/client/mod.rs | 15 +++++++++++++-- 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/ethcore/light/src/client/header_chain.rs b/ethcore/light/src/client/header_chain.rs index a093dd170..388f54d5a 100644 --- a/ethcore/light/src/client/header_chain.rs +++ b/ethcore/light/src/client/header_chain.rs @@ -34,7 +34,7 @@ use ethcore::block_status::BlockStatus; use ethcore::error::BlockError; use ethcore::ids::BlockId; use ethcore::views::HeaderView; -use util::{Bytes, H256, U256, Mutex, RwLock}; +use util::{Bytes, H256, U256, HeapSizeOf, Mutex, RwLock}; use smallvec::SmallVec; @@ -66,6 +66,15 @@ struct Entry { canonical_hash: H256, } +impl HeapSizeOf for Entry { + fn heap_size_of_children(&self) -> usize { + match self.candidates.spilled() { + false => 0, + true => self.candidates.capacity() * ::std::mem::size_of::(), + } + } +} + /// Header chain. See module docs for more details. pub struct HeaderChain { genesis_header: Bytes, // special-case the genesis. @@ -255,6 +264,14 @@ impl HeaderChain { } } +impl HeapSizeOf for HeaderChain { + fn heap_size_of_children(&self) -> usize { + self.candidates.read().heap_size_of_children() + + self.headers.read().heap_size_of_children() + + self.cht_roots.lock().heap_size_of_children() + } +} + #[cfg(test)] mod tests { use super::HeaderChain; diff --git a/ethcore/light/src/client/mod.rs b/ethcore/light/src/client/mod.rs index 16b4547df..65a83c7e3 100644 --- a/ethcore/light/src/client/mod.rs +++ b/ethcore/light/src/client/mod.rs @@ -18,6 +18,7 @@ use ethcore::block_import_error::BlockImportError; use ethcore::block_status::BlockStatus; +use ethcore::client::ClientReport; use ethcore::ids::BlockId; use ethcore::header::Header; use ethcore::verification::queue::{self, HeaderQueue}; @@ -28,7 +29,7 @@ use ethcore::service::ClientIoMessage; use io::IoChannel; use util::hash::{H256, H256FastMap}; -use util::{Bytes, Mutex}; +use util::{Bytes, Mutex, RwLock}; use provider::Provider; use request; @@ -76,6 +77,7 @@ pub struct Client { queue: HeaderQueue, chain: HeaderChain, tx_pool: Mutex>, + report: RwLock, import_lock: Mutex<()>, } @@ -86,6 +88,7 @@ impl Client { queue: HeaderQueue::new(config.queue, spec.engine.clone(), io_channel, true), chain: HeaderChain::new(&::rlp::encode(&spec.genesis_header())), tx_pool: Mutex::new(Default::default()), + report: RwLock::new(ClientReport::default()), import_lock: Mutex::new(()), } } @@ -164,7 +167,10 @@ impl Client { let (num, hash) = (verified_header.number(), verified_header.hash()); match self.chain.insert(::rlp::encode(&verified_header).to_vec()) { - Ok(()) => good.push(hash), + Ok(()) => { + good.push(hash); + self.report.write().blocks_imported += 1; + } Err(e) => { debug!(target: "client", "Error importing header {:?}: {}", (num, hash), e); bad.push(hash); @@ -175,6 +181,11 @@ impl Client { self.queue.mark_as_bad(&bad); self.queue.mark_as_good(&good); } + + /// Get a report about blocks imported. + pub fn report(&self) -> ClientReport { + ::std::mem::replace(&mut *self.report.write(), ClientReport::default()) + } } impl LightChainClient for Client {