170 lines
6.6 KiB
Python
Executable File
170 lines
6.6 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
"""How much of the runtime's public call surface the per-call cost table times, and how much is
|
|
still untimed.
|
|
|
|
scripts/api-cost-coverage # the counts, per type
|
|
scripts/api-cost-coverage --untimed # and every untimed method
|
|
scripts/api-cost-coverage --untimed Texture # and that type's untimed methods
|
|
|
|
The public surface is the methods of the types `crates/metal2vulkan-runtime/src/lib.rs`
|
|
re-exports, in their inherent `impl` blocks (the crate's modules are all private, so these
|
|
re-exports are the whole surface). The timed set is the table that
|
|
`crates/metal2vulkan-runtime/examples/host_ab/percall.rs` declares, one entry per group, which
|
|
the harness checks at run time: a group whose name is not declared there fails.
|
|
|
|
It reads sources only; it builds and runs nothing.
|
|
"""
|
|
|
|
import re
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
ROOT = Path(__file__).resolve().parent.parent
|
|
SRC = ROOT / "crates/metal2vulkan-runtime/src"
|
|
PERCALL = ROOT / "crates/metal2vulkan-runtime/examples/host_ab/percall.rs"
|
|
|
|
# The objects a frame makes its calls on: the device and its queue, the command buffer, its
|
|
# encoders, the resources they bind and the objects they wait on. Their methods are the surface a
|
|
# frame's CPU cost lives in; the rest of the crate's surface is descriptors, queries, formats,
|
|
# errors and the small value types, whose cost a frame does not pay call by call.
|
|
FRAME = [
|
|
"Device",
|
|
"CommandQueue",
|
|
"CommandBuffer",
|
|
"RenderCommandEncoder",
|
|
"ComputeCommandEncoder",
|
|
"BlitCommandEncoder",
|
|
"ResourceStateCommandEncoder",
|
|
"Buffer",
|
|
"Texture",
|
|
"Heap",
|
|
"SharedEvent",
|
|
"Event",
|
|
"Fence",
|
|
"IndirectCommandBuffer",
|
|
"IndirectRenderCommand",
|
|
"IndirectComputeCommand",
|
|
"ArgumentEncoder",
|
|
]
|
|
|
|
# `pub use` of the crate root: `pub use path::{A, B as C};` or `pub use path::X;`, over one line
|
|
# or many.
|
|
USE = re.compile(r"^pub use\s+([^;]+);", re.MULTILINE)
|
|
# An inherent impl block's head at the top level: `impl Type {`, `impl<'a> Type<'a> {`. A trait
|
|
# impl (`impl Trait for Type`) is not part of the named surface.
|
|
IMPL = re.compile(r"^impl(?:<[^>]*>)?\s+([A-Za-z_][A-Za-z0-9_]*)(?:<[^>]*>)?\s*\{")
|
|
METHOD = re.compile(r"^\s*pub(?:\s+unsafe)?(?:\s+const)?\s+fn\s+([a-z_][A-Za-z0-9_]*)")
|
|
# `("group", "Type::method, Type::other")` of percall.rs's declared table, over one line or
|
|
# several, its string joined by a line continuation or not, with a trailing comma or not.
|
|
TIMED = re.compile(r'\(\s*"([a-z0-9_]+)"\s*,\s*"((?:[^"\\]|\\.)*)"\s*,?\s*\)', re.DOTALL)
|
|
CONTINUED = re.compile(r"\\\s*")
|
|
|
|
|
|
def exported():
|
|
"""The names the crate root re-exports."""
|
|
text = (SRC / "lib.rs").read_text()
|
|
names = set()
|
|
for group in USE.findall(text):
|
|
tail = group.split("::")[-1] if "{" not in group else group[group.index("{") + 1 :]
|
|
for part in tail.replace("{", "").replace("}", "").split(","):
|
|
part = part.strip()
|
|
if not part:
|
|
continue
|
|
names.add(part.split()[-1] if " as " in part else part)
|
|
return names
|
|
|
|
|
|
def surface(names):
|
|
"""The public methods of the exported types, as {type: {method}}, and the exported free
|
|
functions (a lower-case name among the re-exports)."""
|
|
found = {}
|
|
for path in sorted(SRC.rglob("*.rs")):
|
|
if "/tests" in str(path):
|
|
continue
|
|
lines = path.read_text().splitlines()
|
|
i = 0
|
|
while i < len(lines):
|
|
head = IMPL.match(lines[i])
|
|
if not head:
|
|
i += 1
|
|
continue
|
|
depth = 0
|
|
while i < len(lines):
|
|
depth += lines[i].count("{") - lines[i].count("}")
|
|
method = METHOD.match(lines[i])
|
|
if method and head.group(1) in names:
|
|
found.setdefault(head.group(1), set()).add(method.group(1))
|
|
i += 1
|
|
if depth <= 0:
|
|
break
|
|
free = {name for name in names if name[:1].islower()}
|
|
return found, free
|
|
|
|
|
|
def timed():
|
|
"""The methods percall.rs declares its groups call, as {(type, method)}, and the groups."""
|
|
text = PERCALL.read_text()
|
|
table = text[text.index("pub const TIMED") :]
|
|
table = table[: table.index("\n];")]
|
|
pairs, groups = set(), {}
|
|
for group, methods in TIMED.findall(table):
|
|
named = [method.strip() for method in CONTINUED.sub(" ", methods).split(",")]
|
|
groups[group] = named
|
|
for method in named:
|
|
kind, _, name = method.partition("::")
|
|
pairs.add((kind, name))
|
|
return pairs, groups
|
|
|
|
|
|
def main():
|
|
argv = sys.argv[1:]
|
|
listed = "--untimed" in argv
|
|
only = argv[argv.index("--untimed") + 1] if listed and len(argv) > argv.index("--untimed") + 1 else None
|
|
names = exported()
|
|
found, free = surface(names)
|
|
pairs, groups = timed()
|
|
unknown = sorted(
|
|
f"{kind}::{name}" for kind, name in pairs if name not in found.get(kind, set())
|
|
)
|
|
if unknown:
|
|
print("percall.rs names methods the runtime's public surface has not:")
|
|
for name in unknown:
|
|
print(f" {name}")
|
|
return 1
|
|
methods = sum(len(ms) for ms in found.values())
|
|
covered = len(pairs)
|
|
print(
|
|
f"metal2vulkan-runtime's public call surface: {methods} methods on "
|
|
f"{len(found)} of its {len(names)} re-exported items, and {len(free)} free functions."
|
|
)
|
|
print(
|
|
f"The per-call cost table times {covered} of them ({covered / methods:.1%}) "
|
|
f"in {len(groups)} groups; {methods - covered} are untimed "
|
|
f"({(methods - covered) / methods:.1%})."
|
|
)
|
|
frame = sum(len(found.get(kind, ())) for kind in FRAME)
|
|
frame_timed = len({(kind, name) for kind, name in pairs if kind in FRAME})
|
|
print(
|
|
f"Of the {frame} methods on the objects a frame makes its calls on "
|
|
f"({len(FRAME)} types), it times {frame_timed} ({frame_timed / frame:.1%}); "
|
|
f"{frame - frame_timed} are untimed."
|
|
)
|
|
print("\n| type | public methods | timed | untimed |")
|
|
print("|---|---:|---:|---:|")
|
|
for kind in sorted(found, key=lambda k: (-len(found[k]), k)):
|
|
mine = {name for owner, name in pairs if owner == kind}
|
|
print(f"| {kind} | {len(found[kind])} | {len(mine)} | {len(found[kind]) - len(mine)} |")
|
|
if listed:
|
|
print("\nuntimed:")
|
|
for kind in sorted(found):
|
|
if only and kind != only:
|
|
continue
|
|
mine = {name for owner, name in pairs if owner == kind}
|
|
for name in sorted(found[kind] - mine):
|
|
print(f" {kind}::{name}")
|
|
return 0
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(main())
|