Skip to content

Commit

Permalink
Add offset command line option to store-histogram (#3296)
Browse files Browse the repository at this point in the history
* Add offset command line option to store-histogram

* break line

* Condition

* Feedback

* Feedback

* Split

* Delete 10k

* limit
  • Loading branch information
dmakarov authored Oct 28, 2024
1 parent 773fe22 commit d8225b5
Showing 1 changed file with 66 additions and 32 deletions.
98 changes: 66 additions & 32 deletions accounts-db/store-histogram/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#![allow(clippy::arithmetic_side_effects)]
use {
clap::{crate_description, crate_name, value_t_or_exit, App, Arg},
std::{fs, path::PathBuf},
clap::{crate_description, crate_name, value_t, value_t_or_exit, App, Arg},
std::{fmt::Display, fs, path::PathBuf, str::FromStr},
};

struct Bin {
Expand Down Expand Up @@ -31,19 +31,32 @@ fn get_stars(x: usize, max: usize, width: usize) -> String {
s
}

fn calc(info: &[(usize, usize)], bin_widths: Vec<usize>) {
fn is_parsable<T>(string: String) -> Result<(), String>
where
T: FromStr,
T::Err: Display,
{
string
.parse::<T>()
.map(|_| ())
.map_err(|err| format!("error parsing '{string}': {err}"))
}

fn calc(info: &[(usize, usize)], bin_widths: Vec<usize>, offset: i64) {
let mut info = info.to_owned();
info.sort();
let min = info.first().unwrap().0;
let max_inclusive = info.last().unwrap().0;
let outside_slot = 432_000 - offset as usize;
eprintln!("storages: {}", info.len());
eprintln!("lowest slot: {min}");
eprintln!("highest slot: {max_inclusive}");
eprintln!("slot range: {}", max_inclusive - min + 1);
eprintln!("ancient boundary: {}", outside_slot);
eprintln!(
"outside of epoch: {}",
"number of slots beyond ancient bondary: {}",
info.iter()
.filter(|x| x.0 < max_inclusive - 432_000)
.filter(|x| x.0 < max_inclusive - outside_slot)
.count()
);

Expand All @@ -54,14 +67,36 @@ fn calc(info: &[(usize, usize)], bin_widths: Vec<usize>) {
} else {
bin_widths[i + 1]
};
let abin = Bin {
slot_min: bin_widths[i],
slot_max: next,
count: 0,
min_size: usize::MAX,
max_size: 0,
sum_size: 0,
avg: 0,
let abin = if bin_widths[i] < outside_slot && outside_slot < next {
let abin = Bin {
slot_min: bin_widths[i],
slot_max: outside_slot,
count: 0,
min_size: usize::MAX,
max_size: 0,
sum_size: 0,
avg: 0,
};
bins.push(abin);
Bin {
slot_min: outside_slot,
slot_max: next,
count: 0,
min_size: usize::MAX,
max_size: 0,
sum_size: 0,
avg: 0,
}
} else {
Bin {
slot_min: bin_widths[i],
slot_max: next,
count: 0,
min_size: usize::MAX,
max_size: 0,
sum_size: 0,
avg: 0,
}
};
bins.push(abin);
}
Expand Down Expand Up @@ -130,8 +165,8 @@ fn calc(info: &[(usize, usize)], bin_widths: Vec<usize>) {
eprintln!("...");
}
let bin = &bins[i];
if bin.slot_min == 432_000 {
eprintln!("------------------------------------------------------------------------------------------------------------------------------------------------------------------------");
if bin.slot_min == outside_slot {
eprintln!("{}", String::from_utf8(vec![b'-'; 168]).unwrap());
}
let offset = format!("{:8}", bin.slot_min);

Expand Down Expand Up @@ -214,15 +249,9 @@ fn normal_bin_widths() -> Vec<usize> {
bin_widths
}

fn normal_ancient() -> Vec<usize> {
fn normal_ancient(offset: i64) -> Vec<usize> {
let mut bin_widths = vec![0];
bin_widths.push(432_000);
bin_widths
}
fn normal_10k() -> Vec<usize> {
let mut bin_widths = vec![0];
bin_widths.push(432_000);
bin_widths.push(442_000);
bin_widths.push((432_000 - offset) as usize);
bin_widths
}

Expand All @@ -237,9 +266,18 @@ fn main() {
.value_name("PATH")
.help("ledger path"),
)
.arg(
Arg::with_name("offset")
.long("offset")
.takes_value(true)
.value_name("SLOT-OFFSET")
.validator(is_parsable::<i64>)
.help("ancient offset"),
)
.get_matches();

let ledger = value_t_or_exit!(matches, "ledger", String);
let offset = value_t!(matches, "offset", i64).unwrap_or(100_000);
let path: PathBuf = [&ledger, "accounts", "run"].iter().collect();

if path.is_dir() {
Expand All @@ -263,15 +301,11 @@ fn main() {
}
}
eprintln!("======== Normal Histogram");
calc(&info, normal_bin_widths());
calc(&info, normal_bin_widths(), offset);
eprintln!("========");

eprintln!("\n======== Normal Ancient Histogram");
calc(&info, normal_ancient());
eprintln!("========");

eprintln!("\n======== Normal Ancient 10K Histogram");
calc(&info, normal_10k());
calc(&info, normal_ancient(offset), offset);
eprintln!("========");
} else {
panic!("couldn't read folder: {path:?}, {:?}", dir);
Expand Down Expand Up @@ -302,8 +336,8 @@ pub mod tests {
.into_iter()
.map(|(slot, size)| (max - slot + base, size))
.collect::<Vec<_>>();
calc(&info, normal_bin_widths());
calc(&info, normal_ancient());
calc(&info, normal_10k());
let offset = 100_000i64;
calc(&info, normal_bin_widths(), offset);
calc(&info, normal_ancient(offset), offset);
}
}

0 comments on commit d8225b5

Please sign in to comment.