Measure retained pages and memory across wide scanner rounds

This commit is contained in:
overtrue
2026-12-01 11:04:02 +08:00
committed by overtrue
parent d2c11493f0
commit d0ed41972b
3 changed files with 61 additions and 0 deletions
@@ -372,6 +372,29 @@ async fn committed_keys<S: ScannerObjectIO>(
Ok(known)
}
#[cfg(all(test, feature = "e2e-test-hooks"))]
impl DataUsageCache {
pub(crate) async fn committed_page_bytes_for_test<S: ScannerObjectIO>(
store: Arc<S>,
name: &str,
revisions: &DataUsageCacheRevisions,
) -> Option<(u64, u64)> {
let header = previous_header(store.clone(), name, &revisions.main).await.unwrap()?;
let head = header.manifest()?;
let keys = committed_keys(store.clone(), name, head, &Archive::default()).await.unwrap();
let mut live = 0;
for key in keys {
let block = read_block(store.clone(), name, head.bundle, key)
.await
.expect("read an authenticated checkpoint page");
live += u64::try_from(block.len()).unwrap();
}
// The production writer rotates a bundle once its retained pages would exceed twice the live pages.
assert!(live > 0 && (live..=2 * live).contains(&head.bundle_bytes));
Some((live, head.bundle_bytes))
}
}
pub(super) async fn decode<S: ScannerObjectIO>(store: Arc<S>, load_name: &str, bytes: &[u8]) -> StorageResult<DataUsageCache> {
if !is_manifest(bytes) {
return DataUsageCache::unmarshal(bytes).map_err(|_| invalid("invalid scanner cache"));
@@ -99,6 +99,19 @@ async fn compacted_checkpoint_advances_one_child_without_rewriting_300k_names()
.await
.expect("save compacted receipt");
let after = layout(&store).await;
#[cfg(feature = "e2e-test-hooks")]
{
let revisions = DataUsageCache::read_revisions(store.clone(), NAME).await.unwrap();
let (live, retained) = DataUsageCache::committed_page_bytes_for_test(store.clone(), NAME, &revisions)
.await
.expect("the compacted checkpoint is paged");
let stored: usize = after
.iter()
.filter(|(key, _)| key.contains(".bin.pages/"))
.map(|(_, (_, bytes))| bytes)
.sum();
assert!(live <= retained && retained <= u64::try_from(stored).unwrap());
}
if let Some(before) = before.as_ref() {
let (old_bytes, new_bytes) = page_bytes_after_save(before, &after);
eprintln!("300k-name checkpoint: {new_bytes} bytes in new pages, {old_bytes} bytes in existing pages");
@@ -45,6 +45,9 @@ async fn hundred_thousand_real_objects_and_a_nested_wide_directory_resume_from_d
.unwrap();
let mut wide_progress = Vec::new();
let mut serialized = Vec::new();
let mut page_bytes = Vec::new();
#[cfg(target_os = "linux")]
let mut peak_resident_bytes = Vec::new();
let mut finished = false;
let budget = ScannerCycleBudgetConfig {
max_entries: Some(ENTRIES_PER_ROUND),
@@ -62,6 +65,17 @@ async fn hundred_thousand_real_objects_and_a_nested_wide_directory_resume_from_d
cache = DataUsageCache::default();
revision = cache.load_with_revisions(set.clone(), &name).await.unwrap();
serialized.push(cache.marshal_msg().unwrap().len());
page_bytes.push(DataUsageCache::committed_page_bytes_for_test(set.clone(), &name, &revision).await);
#[cfg(target_os = "linux")]
{
let status = std::fs::read_to_string("/proc/self/status").expect("Linux process memory counters");
let peak = status
.lines()
.find_map(|line| line.strip_prefix("VmHWM:"))
.expect("peak resident memory");
let kib = peak.trim().strip_suffix(" kB").unwrap().parse::<u64>().unwrap();
peak_resident_bytes.push(kib * 1024);
}
if let Some(index) = cache.validated_raw_enumeration_page_index_for_parent("bucket/wide")
&& let Some(position) = index.durable_progress()
{
@@ -85,6 +99,7 @@ async fn hundred_thousand_real_objects_and_a_nested_wide_directory_resume_from_d
.checked_flatten_complete_scope("bucket")
.expect("complete persisted bucket");
assert_eq!((totals.objects, totals.versions, totals.size), (expected, expected, expected));
assert!(page_bytes.iter().flatten().count() >= 2, "at least two paged roots must be measured");
drop(set);
drop(backend);
let roots = [files.path().to_owned(), checkpoint_dir.path().to_owned()];
@@ -95,6 +110,16 @@ async fn hundred_thousand_real_objects_and_a_nested_wide_directory_resume_from_d
"usage_assertion: {}",
serde_json::json!({"case_id":"USAGE-06","assertion_id":"USAGE-06-A01","status":"PASS","evidence":{"wide_objects":WIDE,"nested_objects":NESTED,"durable_frontier_entries":wide_progress,"checkpoint_wire_bytes_by_round":serialized,"certified_objects":totals.objects,"certified_versions":totals.versions,"certified_bytes":totals.size}})
);
#[cfg(target_os = "linux")]
{
// The fixed-size acceptance workload runs in a 4 GiB, swap-free scope on the SSD fleet.
const MEMORY_LIMIT: u64 = 4 * 1024 * 1024 * 1024;
assert!(peak_resident_bytes.iter().all(|&peak| peak > 0 && peak < MEMORY_LIMIT));
println!(
"usage_assertion: {}",
serde_json::json!({"case_id":"USAGE-06","assertion_id":"USAGE-06-A02","status":"PASS","evidence":{"peak_resident_bytes_by_round":peak_resident_bytes,"memory_limit_bytes":MEMORY_LIMIT,"live_and_retained_page_bytes_by_round":page_bytes,"max_retained_multiple":2,"certified_objects":totals.objects,"certified_versions":totals.versions,"certified_bytes":totals.size}})
);
}
println!(
"usage_cleanup: {}",
serde_json::json!({"case_id":"USAGE-06","status":"PASS","fixtures_removed":true})