20261112 完成#187真实序列录制回放与七指标评估
This commit is contained in:
135
scripts/experiments/reuse_eval/cache_size.py
Normal file
135
scripts/experiments/reuse_eval/cache_size.py
Normal file
@@ -0,0 +1,135 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Attribute retained validation bytes from exact DB statistics and a run contract."""
|
||||
from __future__ import annotations
|
||||
|
||||
import argparse
|
||||
import json
|
||||
from pathlib import Path
|
||||
import stat
|
||||
|
||||
from common import atomic_json, file_sha256, read_json
|
||||
|
||||
COMPONENTS = {
|
||||
"roa": "roa_cache_projection",
|
||||
"child_certificate": "child_certificate_cache_projection",
|
||||
"publication_point": "publication_point_cache_projection",
|
||||
"common_vcir": "vcir",
|
||||
"common_manifest_replay": "manifest_replay_meta",
|
||||
"common_failed_fetch_identity": "vcir_failed_fetch_reuse_identity",
|
||||
"transport_prefetch": "transport_prefetch",
|
||||
}
|
||||
PROFILES = {
|
||||
"baseline": (False, False, False, False),
|
||||
"signature": (False, False, False, True),
|
||||
"object": (False, True, True, False),
|
||||
"pp": (True, False, False, False),
|
||||
"hybrid": (True, True, True, False),
|
||||
}
|
||||
|
||||
|
||||
def profile_from_contract(contract: dict) -> tuple[str, dict]:
|
||||
cache = contract["cache"]
|
||||
keys = ("publicationPoint", "roa", "childCertificate", "cryptoSignature", "transportPrefetch")
|
||||
if any(type(cache.get(key)) is not bool for key in keys):
|
||||
raise ValueError("contract must explicitly contain boolean cache settings")
|
||||
if cache["transportPrefetch"]:
|
||||
raise ValueError("transport prefetch is outside the five-profile matrix")
|
||||
flags = tuple(cache[key] for key in keys[:4])
|
||||
for name, expected in PROFILES.items():
|
||||
if flags == expected:
|
||||
return name, {"roa": cache["roa"], "child_certificate": cache["childCertificate"],
|
||||
"publication_point": cache["publicationPoint"], "signature": cache["cryptoSignature"]}
|
||||
raise ValueError("cache combination is outside the five-profile matrix")
|
||||
|
||||
|
||||
def file_inventory(path: Path) -> dict:
|
||||
if path.is_symlink():
|
||||
raise ValueError(f"cache sidecar must not be a symlink: {path}")
|
||||
if not path.exists():
|
||||
return {"path": str(path), "exists": False, "files": 0, "file_bytes": 0, "allocated_bytes": 0}
|
||||
files = [path] if path.is_file() else sorted(path.rglob("*"))
|
||||
size, allocated, count, inodes = 0, 0, 0, set()
|
||||
for item in files:
|
||||
meta = item.lstat()
|
||||
if stat.S_ISDIR(meta.st_mode):
|
||||
continue
|
||||
if not stat.S_ISREG(meta.st_mode):
|
||||
raise ValueError(f"non-regular cache sidecar: {item}")
|
||||
size += meta.st_size
|
||||
count += 1
|
||||
identity = (meta.st_dev, meta.st_ino)
|
||||
if identity not in inodes:
|
||||
if not hasattr(meta, "st_blocks"):
|
||||
raise ValueError("allocated-byte measurement requires filesystem block counts")
|
||||
allocated += meta.st_blocks * 512
|
||||
inodes.add(identity)
|
||||
return {"path": str(path), "exists": True, "files": count, "file_bytes": size, "allocated_bytes": allocated}
|
||||
|
||||
|
||||
def account(db: Path, stats_path: Path, contract_path: Path) -> dict:
|
||||
db = db.resolve()
|
||||
stats = {}
|
||||
for line in stats_path.read_text().splitlines():
|
||||
key, sep, value = line.partition("=")
|
||||
if not sep:
|
||||
continue
|
||||
if key in stats:
|
||||
raise ValueError(f"duplicate DB statistics field: {key}")
|
||||
stats[key] = value
|
||||
if stats.get("mode") != "exact" or stats.get("logical_bytes_available") != "true":
|
||||
raise ValueError("cache accounting requires db_stats --exact")
|
||||
if Path(stats["db"]).resolve() != db:
|
||||
raise ValueError("DB statistics belong to a different state path")
|
||||
contract = read_json(contract_path)
|
||||
profile, enabled = profile_from_contract(contract)
|
||||
components = {}
|
||||
for name, cf in COMPONENTS.items():
|
||||
item = {"column_family": cf, "required_for_selected_reuse": enabled.get(name, False)}
|
||||
for field in ("keys", "key_bytes", "value_bytes", "live_sst_size_bytes"):
|
||||
value = int(stats[f"cf.{cf}.{field}"])
|
||||
if value < 0:
|
||||
raise ValueError("negative DB statistics")
|
||||
item[field] = value
|
||||
item["logical_bytes"] = item["key_bytes"] + item["value_bytes"]
|
||||
components[name] = item
|
||||
sidecars = {
|
||||
"signature": file_inventory(db.with_name(db.name + ".crypto-sig-cache")),
|
||||
"publication_point": file_inventory(db.with_name(db.name + ".pp-cache-index")),
|
||||
"child_certificate": file_inventory(db.with_name(db.name + ".child-cert-cache-index")),
|
||||
}
|
||||
required_cf = sum(item["logical_bytes"] for item in components.values() if item["required_for_selected_reuse"])
|
||||
required_files = sum(item["file_bytes"] for name, item in sidecars.items() if enabled[name])
|
||||
required = required_cf + required_files
|
||||
return {
|
||||
"schema": "rpki-validation-cache-accounting-v1", "db": str(db), "profile": profile,
|
||||
"validation_contract_sha256": file_sha256(contract_path), "db_stats_sha256": file_sha256(stats_path),
|
||||
"cache_settings": contract["cache"], "measurement_outside_timed_window": True,
|
||||
"required_serialized_bytes": required, "required_serialized_mib": required / 1024**2,
|
||||
"required_cf_key_value_bytes": required_cf, "required_sidecar_file_bytes": required_files,
|
||||
"all_retained_validation_cf_key_value_bytes": sum(item["logical_bytes"] for item in components.values()),
|
||||
"all_retained_sidecar_file_bytes": sum(item["file_bytes"] for item in sidecars.values()),
|
||||
"components": components, "sidecars": sidecars,
|
||||
"accounting_notes": [
|
||||
"Raw repository objects, RRDP state, logs and reports are excluded from validation cache bytes.",
|
||||
"Disabled reuse may still retain common validation projections; those bytes are listed separately.",
|
||||
"Separate CF records and index files are actual serialized copies and are counted separately.",
|
||||
"CF compressed SST file lengths and sidecar allocated blocks are separate measures; shared DB WAL/metadata are not apportioned.",
|
||||
"This is retained serialized cache size, not in-memory cache usage or a minimal theoretical representation.",
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def main():
|
||||
parser = argparse.ArgumentParser(description=__doc__)
|
||||
parser.add_argument("--db", type=Path, required=True)
|
||||
parser.add_argument("--db-stats", type=Path, required=True)
|
||||
parser.add_argument("--contract", type=Path, required=True)
|
||||
parser.add_argument("--out", type=Path, required=True)
|
||||
args = parser.parse_args()
|
||||
result = account(args.db, args.db_stats, args.contract)
|
||||
atomic_json(args.out, result)
|
||||
print(json.dumps({key: result[key] for key in ("profile", "required_serialized_bytes", "required_serialized_mib")}))
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user