Skip to content

Commit 33f37bd

Browse files
authored
Merge branch 'main' into elmattic/api-run-subcommand
2 parents a127f9d + 7589e92 commit 33f37bd

7 files changed

Lines changed: 112 additions & 34 deletions

File tree

Cargo.lock

Lines changed: 30 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@ name = "forest"
1414
[workspace.dependencies]
1515
anyhow = "1"
1616
cid = { version = "0.11", default-features = false, features = ["std"] }
17+
dhat = "0.3"
1718
flume = "0.11"
1819
futures = "0.3"
20+
get-size2 = { version = "0.6", features = ["derive"] }
1921
libp2p = { version = "0.56", default-features = false }
2022
libp2p-swarm-test = { version = "0.6", default-features = false, features = ["tokio"] }
2123
multihash-codetable = { version = "0.1", features = ["blake2b", "blake2s", "blake3", "sha2", "sha3", "strobe"] }
@@ -92,7 +94,7 @@ fvm_ipld_encoding = "0.5.3"
9294
fvm_shared2 = { package = "fvm_shared", version = "~2.11" }
9395
fvm_shared3 = { package = "fvm_shared", version = "~3.13", features = ["proofs"] }
9496
fvm_shared4 = { package = "fvm_shared", version = "~4.7", features = ["proofs"] }
95-
get-size2 = { version = "0.6", features = ["derive"] }
97+
get-size2 = { workspace = true }
9698
gethostname = "1"
9799
git-version = "0.3"
98100
group = "0.13"

interop-tests/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,15 @@ publish = false
1313
[dev-dependencies]
1414
anyhow = { workspace = true }
1515
cid = { workspace = true }
16+
dhat = { workspace = true }
1617
flume = { workspace = true }
1718
forest = { package = "forest-filecoin", path = "../", default-features = false, features = [
19+
"rustalloc",
1820
"interop-tests-private",
1921
"no-f3-sidecar",
2022
] }
2123
futures = { workspace = true }
24+
get-size2 = { workspace = true }
2225
libp2p = { workspace = true, features = [
2326
'kad',
2427
'identify',
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2019-2025 ChainSafe Systems
2+
// SPDX-License-Identifier: Apache-2.0, MIT
3+
4+
use forest::interop_tests_private::beacon::BeaconEntry;
5+
use get_size2::GetSize;
6+
use std::mem::size_of;
7+
8+
#[global_allocator]
9+
static ALLOC: dhat::Alloc = dhat::Alloc;
10+
11+
#[test]
12+
fn test_get_size_beacon_entry() {
13+
let _profiler = dhat::Profiler::builder().testing().build();
14+
let b = BeaconEntry::new(1, vec![0; 10]);
15+
16+
let stats = dhat::HeapStats::get();
17+
dhat::assert_eq!(stats.curr_bytes, 10);
18+
dhat::assert_eq!(stats.curr_bytes, b.get_heap_size());
19+
20+
let mut v = vec![
21+
b,
22+
BeaconEntry::new(2, vec![0; 20]),
23+
BeaconEntry::new(3, vec![0; 30]),
24+
];
25+
26+
let inner_bytes = v.iter().map(GetSize::get_heap_size).sum::<usize>();
27+
28+
let stats = dhat::HeapStats::get();
29+
assert!(v.capacity() >= v.len());
30+
dhat::assert_eq!(
31+
stats.curr_bytes,
32+
size_of::<BeaconEntry>() * v.capacity() + inner_bytes
33+
);
34+
dhat::assert_eq!(stats.curr_bytes, v.get_heap_size());
35+
36+
v.reserve_exact(100);
37+
38+
let stats = dhat::HeapStats::get();
39+
assert!(v.capacity() >= 100 + v.len());
40+
dhat::assert_eq!(
41+
stats.curr_bytes,
42+
size_of::<BeaconEntry>() * v.capacity() + inner_bytes
43+
);
44+
dhat::assert_eq!(stats.curr_bytes, v.get_heap_size());
45+
46+
v.shrink_to_fit();
47+
let stats = dhat::HeapStats::get();
48+
// `dhat::Alloc` works fine with `shrink_to_fit`
49+
assert_eq!(v.capacity(), v.len());
50+
dhat::assert_eq!(
51+
stats.curr_bytes,
52+
size_of::<BeaconEntry>() * v.capacity() + 60
53+
);
54+
dhat::assert_eq!(stats.curr_bytes, v.get_heap_size());
55+
}

src/cli/subcommands/mod.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ mod snapshot_cmd;
2020
mod state_cmd;
2121
mod sync_cmd;
2222
mod wait_api_cmd;
23-
use std::io::Write;
2423

2524
pub(crate) use crate::cli_shared::cli::Config;
2625
use crate::cli_shared::cli::HELP_MESSAGE;

src/cli/subcommands/snapshot_cmd.rs

Lines changed: 18 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright 2019-2025 ChainSafe Systems
22
// SPDX-License-Identifier: Apache-2.0, MIT
33

4-
use super::*;
54
use crate::chain::FilecoinSnapshotVersion;
65
use crate::chain_sync::SyncConfig;
76
use crate::cli_shared::snapshot::{self, TrustedVendor};
@@ -11,11 +10,10 @@ use crate::rpc::{self, chain::ForestChainExportParams, prelude::*, types::ApiTip
1110
use anyhow::Context as _;
1211
use chrono::DateTime;
1312
use clap::Subcommand;
14-
use human_repr::HumanCount;
15-
use num::Zero as _;
13+
use indicatif::{ProgressBar, ProgressStyle};
1614
use std::{
1715
path::{Path, PathBuf},
18-
time::{Duration, Instant},
16+
time::Duration,
1917
};
2018
use tokio::io::AsyncWriteExt;
2119

@@ -99,48 +97,36 @@ impl SnapshotCommands {
9997
dry_run,
10098
};
10199

100+
let pb = ProgressBar::new_spinner().with_style(
101+
ProgressStyle::with_template(
102+
"{spinner} {msg} {binary_total_bytes} written in {elapsed} ({binary_bytes_per_sec})",
103+
)
104+
.expect("indicatif template must be valid"),
105+
).with_message(format!("Exporting {} ...", output_path.display()));
106+
pb.enable_steady_tick(std::time::Duration::from_millis(80));
102107
let handle = tokio::spawn({
103-
let start = Instant::now();
104-
let tmp_file = temp_path.to_owned();
105-
let output_path = output_path.clone();
108+
let path: PathBuf = (&temp_path).into();
109+
let pb = pb.clone();
110+
let mut interval = tokio::time::interval(tokio::time::Duration::from_secs(1));
106111
async move {
107-
let mut interval =
108-
tokio::time::interval(tokio::time::Duration::from_secs_f32(0.5));
109-
println!("Getting ready to export...");
110112
loop {
111113
interval.tick().await;
112-
let snapshot_size = std::fs::metadata(&tmp_file)
113-
.map(|meta| meta.len())
114-
.unwrap_or(0);
115-
print!(
116-
"{}{}",
117-
anes::MoveCursorToPreviousLine(1),
118-
anes::ClearLine::All
119-
);
120-
let elapsed_secs = start.elapsed().as_secs_f64();
121-
println!(
122-
"{}: {} ({}/s)",
123-
&output_path.to_string_lossy(),
124-
snapshot_size.human_count_bytes(),
125-
if elapsed_secs.is_zero() {
126-
0.
127-
} else {
128-
(snapshot_size as f64) / elapsed_secs
129-
}
130-
.human_count_bytes(),
131-
);
132-
let _ = std::io::stdout().flush();
114+
if let Ok(meta) = std::fs::metadata(&path) {
115+
pb.set_position(meta.len());
116+
}
133117
}
134118
}
135119
});
120+
136121
// Manually construct RpcRequest because snapshot export could
137122
// take a few hours on mainnet
138123
let hash_result = client
139124
.call(ForestChainExport::request((params,))?.with_timeout(Duration::MAX))
140125
.await?;
141126

142127
handle.abort();
143-
let _ = handle.await;
128+
pb.finish();
129+
_ = handle.await;
144130

145131
if let Some(hash) = hash_result {
146132
save_checksum(&output_path, hash).await?;

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,9 @@ pub mod interop_tests_private {
113113
pub mod libp2p_bitswap {
114114
pub use crate::libp2p_bitswap::*;
115115
}
116+
pub mod beacon {
117+
pub use crate::beacon::BeaconEntry;
118+
}
116119
}
117120

118121
// These should be made private in https://github.com/ChainSafe/forest/issues/3013

0 commit comments

Comments
 (0)