Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,14 @@ poetry run ./bin/mark_fresh.py --re '^suitesparse_matrix$'
Only entries that already have an uploaded digest for the same source file are
marked. Entries that were never cached are listed and left for `--cache-datasets`.

The same selection also restamps trace records in `statistics.json` (or
`--statistics`), including those for uncached generators, so their statistics
tags survive the source change. Fold them back into metadata afterwards:

```bash
poetry run ./bin/generate_metadata.py --statistics statistics.json
```

Generators marked `cacheable = False` still assemble benchmark inputs during
setup from their shared source datasets. Those transformations must preserve
sparsity and use prepared data. The G-CARE downloader reads graph matrices,
Expand Down
97 changes: 80 additions & 17 deletions bin/mark_fresh.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
#!/usr/bin/env python3
"""Stamp cached manifest entries with the current freshness from metadata.
"""Stamp cached manifest entries and trace statistics with the current freshness
from metadata.

Use this when a source change does not affect generated data (or the data can
no longer be regenerated), so the existing uploads should count as current.
Use this when a source change does not affect generated data or traced behavior
(or the data can no longer be regenerated), so the existing uploads and trace
statistics should count as current.
"""

from __future__ import annotations
Expand All @@ -25,10 +27,12 @@ def selected_datasets(
no_tags: list[str] = (),
patterns: list[str] = (),
no_patterns: list[str] = (),
cacheable_only: bool = True,
) -> dict[str, dict[str, Any]]:
"""Cacheable datasets matching the filters, keyed by manifest key.
"""Datasets matching the filters, keyed by manifest key.

The filters behave like run_benchmark.py's --tag, --no-tag, --re, and --no-re.
Uncached generators are skipped unless *cacheable_only* is false.
"""
include_tags = {tag.strip() for tag in tags if tag.strip()}
exclude_tags = {tag.strip() for tag in no_tags if tag.strip()}
Expand All @@ -42,7 +46,7 @@ def name_matches(compiled, names) -> bool:
selected = {}
for benchmark in metadata["benchmarks"]:
for generator in benchmark["generators"]:
if not generator["cacheable"]:
if cacheable_only and not generator["cacheable"]:
continue
for dataset in generator["datasets"]:
dataset_tags = set(dataset.get("tags", []))
Expand Down Expand Up @@ -84,23 +88,52 @@ def mark_fresh(
record["freshness"] = dataset["freshness"]
marked.append(key)
if marked and not dry_run:
with tempfile.TemporaryDirectory(
prefix=".saps-", dir=manifest_path.parent
) as staging:
staging_path = Path(staging) / "manifest.json"
staging_path.write_text(
json.dumps(manifest, indent=2, sort_keys=True) + "\n",
encoding="utf-8",
)
staging_path.replace(manifest_path)
_replace_json(manifest_path, manifest)
return marked, uncached, already_fresh


def mark_statistics_fresh(
statistics_path: Path, datasets: dict[str, dict[str, Any]], *, dry_run: bool = False
) -> list[str]:
"""Return the trace statistics records restamped for the selected datasets.

Only records traced from the same source file are marked.
"""
statistics = json.loads(statistics_path.read_text(encoding="utf-8"))
marked = []
for benchmark in statistics.get("benchmarks", []):
for generator in benchmark.get("generators", []):
for record in generator.get("datasets", []):
key = f"{generator['name']}.{record['name']}"
dataset = datasets.get(key)
if (
dataset is None
or record.get("file") != dataset["file"]
or record.get("freshness") in (None, dataset["freshness"])
):
continue
record["freshness"] = dataset["freshness"]
marked.append(f"{benchmark['name']}/{key}")
if marked and not dry_run:
_replace_json(statistics_path, statistics)
return sorted(marked)


def _replace_json(path: Path, document: Any) -> None:
with tempfile.TemporaryDirectory(prefix=".saps-", dir=path.parent) as staging:
staging_path = Path(staging) / path.name
staging_path.write_text(
json.dumps(document, indent=2, sort_keys=True) + "\n", encoding="utf-8"
)
staging_path.replace(path)


def main(argv: list[str] | None = None) -> int:
parser = argparse.ArgumentParser(
description=(
"Mark cached manifest entries as fresh for the current source, "
"without regenerating them. Regenerate metadata.json first."
"Mark cached manifest entries and trace statistics as fresh for the "
"current source, without regenerating them. Regenerate metadata.json "
"first."
)
)
parser.add_argument(
Expand All @@ -115,6 +148,12 @@ def main(argv: list[str] | None = None) -> int:
default=Path(os.environ.get("SAPS_MANIFEST_PATH") or "manifest.json"),
help="Manifest to update (default: SAPS_MANIFEST_PATH or manifest.json).",
)
parser.add_argument(
"--statistics",
type=Path,
default=Path("statistics.json"),
help="Trace statistics to update, if present (default: statistics.json).",
)
parser.add_argument(
"--tag",
action="append",
Expand Down Expand Up @@ -145,7 +184,10 @@ def main(argv: list[str] | None = None) -> int:
parser.add_argument(
"--dry-run",
action="store_true",
help="List entries that would be marked without changing the manifest.",
help=(
"List entries that would be marked without changing the manifest or "
"statistics."
),
)
args = parser.parse_args(argv)
if not (args.tag or args.re):
Expand All @@ -171,6 +213,27 @@ def main(argv: list[str] | None = None) -> int:
)
for key in uncached:
print(f" not cached: {key}")

statistics_path = args.statistics.expanduser()
if statistics_path.exists():
# Trace statistics cover uncached generators too.
traced = selected_datasets(
args.metadata.expanduser(),
tags=args.tag,
no_tags=args.no_tag,
patterns=args.re,
no_patterns=args.no_re,
cacheable_only=False,
)
restamped = mark_statistics_fresh(statistics_path, traced, dry_run=args.dry_run)
for key in restamped:
print(key)
print(f"{action} {len(restamped)} trace statistics records fresh")
if restamped and not args.dry_run:
print(
"Rerun generate_metadata.py --statistics "
f"{statistics_path} to fold in their tags."
)
return 0


Expand Down
Loading
Loading