Skip to content

Commit

Permalink
fix: Add some more stats to basic status page of TUI (disk usage, cha…
Browse files Browse the repository at this point in the history
…in timestamp, tx pool size) (#3046)

* fix: Add some more stats to basic status page of TUI (disk usage, chain timestamp, tx pool size)

* chore: add latest header timestamp to TUI

* fix: calculate total disk usage of database to show in TUI
  • Loading branch information
Joseph Goulden authored and antiochp committed Sep 19, 2019
1 parent f3baceb commit 02cee80
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 20 deletions.
7 changes: 4 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion chain/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1204,7 +1204,7 @@ impl Chain {
.map_err(|e| ErrorKind::StoreErr(e, "chain head header".to_owned()).into())
}

/// Gets a block header by hash
/// Gets a block by hash
pub fn get_block(&self, h: &Hash) -> Result<Block, Error> {
self.store
.get_block(h)
Expand Down
1 change: 1 addition & 0 deletions servers/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ serde_derive = "1"
serde_json = "1"
chrono = "0.4.4"
tokio = "0.1.11"
walkdir = "2.2.9"

grin_api = { path = "../api", version = "2.1.0-dev.1" }
grin_chain = { path = "../chain", version = "2.1.0-dev.1" }
Expand Down
30 changes: 27 additions & 3 deletions servers/src/common/stats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ use crate::core::ser::ProtocolVersion;

use chrono::prelude::*;

use crate::chain;
use crate::chain::SyncStatus;
use crate::p2p;
use grin_core::pow::Difficulty;

/// Server state info collection struct, to be passed around into internals
/// and populated when required
Expand All @@ -51,9 +51,9 @@ pub struct ServerStats {
/// Number of peers
pub peer_count: u32,
/// Chain head
pub head: chain::Tip,
pub chain_stats: ChainStats,
/// sync header head
pub header_head: chain::Tip,
pub header_stats: ChainStats,
/// Whether we're currently syncing
pub sync_status: SyncStatus,
/// Handle to current stratum server stats
Expand All @@ -62,8 +62,32 @@ pub struct ServerStats {
pub peer_stats: Vec<PeerStats>,
/// Difficulty calculation statistics
pub diff_stats: DiffStats,
/// Transaction pool statistics
pub tx_stats: TxStats,
/// Disk usage in GB
pub disk_usage_gb: String,
}

/// Chain Statistics
#[derive(Clone, Serialize, Debug)]
pub struct ChainStats {
/// Height of the tip (max height of the fork)
pub height: u64,
/// Last block pushed to the fork
pub last_block_h: Hash,
/// Total difficulty accumulated on that fork
pub total_difficulty: Difficulty,
/// Timestamp of highest block or header
pub latest_timestamp: DateTime<Utc>,
}
/// Transaction Statistics
#[derive(Clone, Serialize, Debug)]
pub struct TxStats {
/// Number of transactions in the transaction pool
pub tx_pool_size: usize,
/// Number of transactions in the stem pool
pub stem_pool_size: usize,
}
/// Struct to return relevant information about stratum workers
#[derive(Clone, Serialize, Debug)]
pub struct WorkerStats {
Expand Down
45 changes: 42 additions & 3 deletions servers/src/grin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use std::{
};

use fs2::FileExt;
use walkdir::WalkDir;

use crate::api;
use crate::api::TLSConfig;
Expand All @@ -35,7 +36,9 @@ use crate::common::adapters::{
ChainToPoolAndNetAdapter, NetToChainAdapter, PoolToChainAdapter, PoolToNetAdapter,
};
use crate::common::hooks::{init_chain_hooks, init_net_hooks};
use crate::common::stats::{DiffBlock, DiffStats, PeerStats, ServerStateInfo, ServerStats};
use crate::common::stats::{
ChainStats, DiffBlock, DiffStats, PeerStats, ServerStateInfo, ServerStats, TxStats,
};
use crate::common::types::{Error, ServerConfig, StratumServerConfig};
use crate::core::core::hash::{Hashed, ZERO_HASH};
use crate::core::core::verifier_cache::{LruVerifierCache, VerifierCache};
Expand Down Expand Up @@ -489,14 +492,50 @@ impl Server {
.into_iter()
.map(|p| PeerStats::from_peer(&p))
.collect();

let tx_stats = TxStats {
tx_pool_size: self.tx_pool.read().txpool.entries.len(),
stem_pool_size: self.tx_pool.read().stempool.entries.len(),
};

let head = self.chain.head_header().unwrap();
let head_stats = ChainStats {
latest_timestamp: head.timestamp,
height: head.height,
last_block_h: head.prev_hash,
total_difficulty: head.total_difficulty(),
};

let header_tip = self.chain.header_head().unwrap();
let header = self.chain.get_block_header(&header_tip.hash()).unwrap();
let header_stats = ChainStats {
latest_timestamp: header.timestamp,
height: header.height,
last_block_h: header.prev_hash,
total_difficulty: header.total_difficulty(),
};

let disk_usage_bytes = WalkDir::new(&self.config.db_root)
.min_depth(1)
.max_depth(3)
.into_iter()
.filter_map(|entry| entry.ok())
.filter_map(|entry| entry.metadata().ok())
.filter(|metadata| metadata.is_file())
.fold(0, |acc, m| acc + m.len());

let disk_usage_gb = format!("{:.*}", 3, (disk_usage_bytes as f64 / 1_000_000_000 as f64));

Ok(ServerStats {
peer_count: self.peer_count(),
head: self.head()?,
header_head: self.header_head()?,
chain_stats: head_stats,
header_stats: header_stats,
sync_status: self.sync_state.status(),
disk_usage_gb: disk_usage_gb,
stratum_stats: stratum_stats,
peer_stats: peer_stats,
diff_stats: diff_stats,
tx_stats: tx_stats,
})
}

Expand Down
5 changes: 4 additions & 1 deletion src/bin/tui/peers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,10 @@ impl TUIStatusListener for TUIPeerView {
let lp_str = match lp {
Some(l) => format!(
"{} D @ {} H vs Us: {} D @ {} H",
l.total_difficulty, l.height, stats.head.total_difficulty, stats.head.height
l.total_difficulty,
l.height,
stats.chain_stats.total_difficulty,
stats.chain_stats.height
)
.to_string(),
None => "".to_string(),
Expand Down
63 changes: 54 additions & 9 deletions src/bin/tui/status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,14 @@ impl TUIStatusListener for TUIStatusView {
.child(TextView::new("Connected Peers: "))
.child(TextView::new("0").with_id("connected_peers")),
)
.child(
LinearLayout::new(Orientation::Horizontal)
.child(TextView::new("Disk Usage (GB): "))
.child(TextView::new("0").with_id("disk_usage")),
)
.child(
LinearLayout::new(Orientation::Horizontal).child(TextView::new(
"------------------------------------------------",
"--------------------------------------------------------",
)),
)
.child(
Expand All @@ -66,9 +71,14 @@ impl TUIStatusListener for TUIStatusView {
.child(TextView::new("Header Cumulative Difficulty: "))
.child(TextView::new(" ").with_id("basic_header_total_difficulty")),
)
.child(
LinearLayout::new(Orientation::Horizontal)
.child(TextView::new("Header Tip Timestamp: "))
.child(TextView::new(" ").with_id("basic_header_timestamp")),
)
.child(
LinearLayout::new(Orientation::Horizontal).child(TextView::new(
"------------------------------------------------",
"--------------------------------------------------------",
)),
)
.child(
Expand All @@ -86,9 +96,29 @@ impl TUIStatusListener for TUIStatusView {
.child(TextView::new("Chain Cumulative Difficulty: "))
.child(TextView::new(" ").with_id("basic_total_difficulty")),
)
.child(
LinearLayout::new(Orientation::Horizontal)
.child(TextView::new("Chain Tip Timestamp: "))
.child(TextView::new(" ").with_id("chain_timestamp")),
)
.child(
LinearLayout::new(Orientation::Horizontal).child(TextView::new(
"--------------------------------------------------------",
)),
)
.child(
LinearLayout::new(Orientation::Horizontal)
.child(TextView::new("Transaction Pool Size: "))
.child(TextView::new(" ").with_id("tx_pool_size")),
)
.child(
LinearLayout::new(Orientation::Horizontal)
.child(TextView::new("Stem Pool Size: "))
.child(TextView::new(" ").with_id("stem_pool_size")),
)
.child(
LinearLayout::new(Orientation::Horizontal).child(TextView::new(
"------------------------------------------------",
"--------------------------------------------------------",
)),
)
.child(
Expand Down Expand Up @@ -244,23 +274,38 @@ impl TUIStatusListener for TUIStatusView {
c.call_on_id("connected_peers", |t: &mut TextView| {
t.set_content(stats.peer_count.to_string());
});
c.call_on_id("disk_usage", |t: &mut TextView| {
t.set_content(stats.disk_usage_gb.clone());
});
c.call_on_id("tip_hash", |t: &mut TextView| {
t.set_content(stats.head.last_block_h.to_string() + "...");
t.set_content(stats.chain_stats.last_block_h.to_string() + "...");
});
c.call_on_id("chain_height", |t: &mut TextView| {
t.set_content(stats.head.height.to_string());
t.set_content(stats.chain_stats.height.to_string());
});
c.call_on_id("basic_total_difficulty", |t: &mut TextView| {
t.set_content(stats.head.total_difficulty.to_string());
t.set_content(stats.chain_stats.total_difficulty.to_string());
});
c.call_on_id("chain_timestamp", |t: &mut TextView| {
t.set_content(stats.chain_stats.latest_timestamp.to_string());
});
c.call_on_id("basic_header_tip_hash", |t: &mut TextView| {
t.set_content(stats.header_head.last_block_h.to_string() + "...");
t.set_content(stats.header_stats.last_block_h.to_string() + "...");
});
c.call_on_id("basic_header_chain_height", |t: &mut TextView| {
t.set_content(stats.header_head.height.to_string());
t.set_content(stats.header_stats.height.to_string());
});
c.call_on_id("basic_header_total_difficulty", |t: &mut TextView| {
t.set_content(stats.header_head.total_difficulty.to_string());
t.set_content(stats.header_stats.total_difficulty.to_string());
});
c.call_on_id("basic_header_timestamp", |t: &mut TextView| {
t.set_content(stats.header_stats.latest_timestamp.to_string());
});
c.call_on_id("tx_pool_size", |t: &mut TextView| {
t.set_content(stats.tx_stats.tx_pool_size.to_string());
});
c.call_on_id("stem_pool_size", |t: &mut TextView| {
t.set_content(stats.tx_stats.stem_pool_size.to_string());
});
/*c.call_on_id("basic_mining_config_status", |t: &mut TextView| {
t.set_content(basic_mining_config_status);
Expand Down

0 comments on commit 02cee80

Please sign in to comment.