# SPDX-License-Identifier: CC0-1.0 """Observe HDF5 file-driver reads, not physical disk I/O or elapsed-time speed.""" import argparse import hashlib import io import json import platform import sys import zlib from datetime import datetime, timezone from pathlib import Path import h5py import numpy as np CHUNK = 32 SHAPE = (64, 64) CHUNK_BYTES = CHUNK * CHUNK * 4 COORDS = ((0, 0), (0, 32), (32, 0), (32, 32)) CASES = ( ("one-slot", 1, 4 * CHUNK_BYTES, "round-robin"), ("more-bytes-one-slot", 1, 16 * CHUNK_BYTES, "round-robin"), ("four-chunks-fit", 17, 4 * CHUNK_BYTES, "round-robin"), ("compressed-only-fits", 17, CHUNK_BYTES // 2, "round-robin"), ("one-chunk-fits", 17, CHUNK_BYTES, "round-robin"), ("grouped-one-slot", 1, CHUNK_BYTES, "grouped"), ) class ReadTrace(io.BytesIO): def __init__(self, image): super().__init__(image) self.events = [] def readinto(self, buffer): offset = self.tell() count = super().readinto(buffer) self.events.append({"offset": offset, "bytes": count}) return count def read(self, size=-1): offset = self.tell() result = super().read(size) self.events.append({"offset": offset, "bytes": len(result)}) return result def raw_chunk_reads(events, chunks): """Reject ambiguous overlaps instead of silently calling them cache misses.""" hits = [] for event in events: offset, size = event["offset"], event["bytes"] if type(offset) is not int or type(size) is not int or offset < 0 or size < 0: raise ValueError("Invalid read extent") for index, chunk in enumerate(chunks): start, length = chunk["offset"], chunk["storedBytes"] if offset < start + length and start < offset + size: if offset != start or size != length: raise ValueError("Read overlaps a chunk without matching its exact extent") hits.append(index) return hits def fixture(): payload = np.arange(SHAPE[0] * SHAPE[1], dtype=" 1: raise RuntimeError("Unexpected chunk read for scalar selection") steps.append({"coordinate": list(coord), "value": value, "rawChunkReads": hits, "driverReads": events}) reads = sum(len(step["rawChunkReads"]) for step in steps) raw_bytes = sum(chunks[index]["storedBytes"] for step in steps for index in step["rawChunkReads"]) return {"case": label, "order": order, "rdccNslots": slots, "rdccNbytes": capacity, "rdccW0": 0.0, "scalarSelections": len(steps), "selectedBytes": len(steps) * 4, "rawChunkReads": reads, "rawChunkBytes": raw_bytes, "chunkDecodeBytesInferred": reads * CHUNK_BYTES, "steps": steps} def run(repetitions=3): if type(repetitions) is not int or not 1 <= repetitions <= 20: raise ValueError("repetitions must be an integer from 1 to 20") image, payload, chunks = fixture() rows = [] for repetition in range(1, repetitions + 1): for case in CASES: rows.append({"repetition": repetition, **observe(image, payload, chunks, case)}) return { "schemaVersion": 1, "generatedAt": datetime.now(timezone.utc).isoformat(), "evidenceType": "measured-library-reads", "license": "CC0-1.0", "environment": {"python": platform.python_version(), "h5py": h5py.__version__, "hdf5": h5py.version.hdf5_version, "numpy": np.__version__, "zlib": zlib.ZLIB_RUNTIME_VERSION, "system": platform.system(), "machine": platform.machine(), "driver": "fileobj over BytesIO", "formatBounds": ["v110", "v110"]}, "fixture": {"shape": list(SHAPE), "chunkShape": [CHUNK, CHUNK], "dtype": "