Skip to content

Commit bd5af43

Browse files
Neil4561Neil4561
andauthored
Harden perf-smoke seeder mount permissions (#6590)
## Summary - write benchmark output inside the container and copy normalized artifacts back to the runner - isolate writable JIT and Kit caches per run/task/backend while preserving reuse across samples - use disposable source clones and clean all run-scoped directories to avoid stale ownership on long-lived runners ## Test plan - [x] Run the complete perf-smoke unit suite (`52 passed, 1 skipped`) - [x] Run pre-commit hooks on both changed files - [x] Verify cache reuse, artifact extraction, source reuse, and cleanup with UID-1000 Docker containers Co-authored-by: Neil4561 <neilmehta456@gmail.com>
1 parent e7b1620 commit bd5af43

2 files changed

Lines changed: 140 additions & 30 deletions

File tree

‎tools/perf_smoke_test/seed_baselines.py‎

Lines changed: 53 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,11 @@ def _prepare_seed_source(workdir: Path, seed_src_dir: Path, sha: str) -> None:
350350
subprocess.run(["chmod", "-R", "a+rwX", str(seed_src_dir)], check=False)
351351

352352

353+
def _create_seed_source_dir() -> Path:
354+
"""Create a unique disposable source-clone directory."""
355+
return Path(tempfile.mkdtemp(prefix="perf-seed-src-"))
356+
357+
353358
def _prepare_jit_cache(cache_dir: Path) -> Path:
354359
"""Create a writable JIT cache directory for one task/backend bucket."""
355360
for subdir in (cache_dir / "warp", cache_dir / "nv"):
@@ -359,7 +364,15 @@ def _prepare_jit_cache(cache_dir: Path) -> Path:
359364
return cache_dir
360365

361366

362-
def _jit_cache_path(cache_root: Path, target_branch: str, commit: str, task_id: str, backend_key: str) -> Path:
367+
def _prepare_kit_cache(cache_dir: Path) -> Path:
368+
"""Create a writable Kit cache directory for one task/backend bucket."""
369+
cache_dir.mkdir(parents=True, exist_ok=True)
370+
# Kit writes shader and generated-node caches from the in-container user.
371+
_run(["chmod", "-R", "0777", str(cache_dir)], check=False)
372+
return cache_dir
373+
374+
375+
def _cache_bucket_path(cache_root: Path, target_branch: str, commit: str, task_id: str, backend_key: str) -> Path:
363376
"""Return the cache shared by repeated samples of one task/backend bucket."""
364377
return (
365378
cache_root
@@ -370,14 +383,14 @@ def _jit_cache_path(cache_root: Path, target_branch: str, commit: str, task_id:
370383
)
371384

372385

373-
def _cleanup_jit_cache(cache_dir: Path) -> None:
374-
"""Remove the disposable JIT cache for one seeder invocation."""
375-
if not cache_dir.exists():
386+
def _cleanup_run_dir(run_dir: Path) -> None:
387+
"""Remove a disposable directory for one seeder invocation."""
388+
if not run_dir.exists():
376389
return
377-
subprocess.run(["chmod", "-R", "a+rwX", str(cache_dir)], check=False, capture_output=True, text=True)
378-
shutil.rmtree(cache_dir, ignore_errors=True)
379-
if cache_dir.exists():
380-
print(f"::warning::[seed] Could not fully remove JIT cache {cache_dir}")
390+
subprocess.run(["chmod", "-R", "a+rwX", str(run_dir)], check=False, capture_output=True, text=True)
391+
shutil.rmtree(run_dir, ignore_errors=True)
392+
if run_dir.exists():
393+
print(f"::warning::[seed] Could not fully remove run directory {run_dir}")
381394

382395

383396
def _docker_run_benchmark(
@@ -395,9 +408,10 @@ def _docker_run_benchmark(
395408
seed_token = f"--seed {task.seed}" if task.seed is not None else ""
396409
inner = (
397410
"set -e\n"
398-
# Warp creates nested cache directories at runtime. A permissive umask
399-
# keeps them writable when successive containers use different host uids.
411+
# Warp and Kit create nested cache directories at runtime. A permissive
412+
# umask keeps them writable for successive containers.
400413
"umask 000\n"
414+
"mkdir -p /tmp/bench_out\n"
401415
"cd /workspace/isaaclab\n"
402416
"rm -f _isaac_sim\n"
403417
"ln -s /isaac-sim _isaac_sim\n"
@@ -414,7 +428,6 @@ def _docker_run_benchmark(
414428
cmd = [
415429
"docker",
416430
"run",
417-
"--rm",
418431
"--name",
419432
container_name,
420433
"--init",
@@ -449,8 +462,6 @@ def _docker_run_benchmark(
449462
"-e",
450463
"CUDA_CACHE_PATH=/tmp/jit-cache/nv",
451464
"-v",
452-
f"{artifact_dir}:/tmp/bench_out",
453-
"-v",
454465
f"{jit_cache}:/tmp/jit-cache",
455466
"-v",
456467
f"{kit_cache}:/isaac-sim/kit/cache",
@@ -462,7 +473,20 @@ def _docker_run_benchmark(
462473
cmd += [image, "-c", inner]
463474

464475
result = subprocess.run(cmd, text=True)
465-
return result.returncode
476+
permissions_returncode = 0
477+
try:
478+
copy_result = subprocess.run(
479+
["docker", "cp", f"{container_name}:/tmp/bench_out/.", str(artifact_dir)],
480+
text=True,
481+
)
482+
if copy_result.returncode == 0:
483+
permissions_returncode = subprocess.run(
484+
["chmod", "-R", "a+rwX", str(artifact_dir)],
485+
text=True,
486+
).returncode
487+
finally:
488+
subprocess.run(["docker", "rm", "-f", container_name], capture_output=True, text=True)
489+
return result.returncode or copy_result.returncode or permissions_returncode
466490

467491

468492
def _build_bench_result(task: TaskConfig, artifact_dir: Path, exit_code: int, wall_time_s: int) -> None:
@@ -621,16 +645,18 @@ def main() -> int:
621645
artifacts_root = (workdir / args.artifacts_root).resolve()
622646
artifacts_root.mkdir(parents=True, exist_ok=True)
623647

624-
seed_src_dir = (
625-
Path(args.seed_src_dir).resolve() if args.seed_src_dir else Path(tempfile.gettempdir()) / "perf-seed-src"
626-
)
648+
if args.seed_src_dir:
649+
seed_src_dir = Path(args.seed_src_dir).resolve()
650+
else:
651+
seed_src_dir = _create_seed_source_dir()
652+
atexit.register(_cleanup_run_dir, seed_src_dir)
627653
run_id = os.environ.get("GITHUB_RUN_ID", f"local-{os.getpid()}")
628654
run_attempt = os.environ.get("GITHUB_RUN_ATTEMPT", "0")
629-
jit_cache_root = workdir / "jit-cache" / "seed" / f"run-{run_id}-attempt-{run_attempt}"
630-
atexit.register(_cleanup_jit_cache, jit_cache_root)
631-
kit_cache = workdir / "kit-cache"
632-
kit_cache.mkdir(parents=True, exist_ok=True)
633-
_run(["chmod", "-R", "0777", str(kit_cache)], check=False)
655+
cache_run = f"run-{run_id}-attempt-{run_attempt}"
656+
jit_cache_root = workdir / "jit-cache" / "seed" / cache_run
657+
kit_cache_root = workdir / "kit-cache" / "seed" / cache_run
658+
atexit.register(_cleanup_run_dir, jit_cache_root)
659+
atexit.register(_cleanup_run_dir, kit_cache_root)
634660

635661
plan = _build_seed_plan(args)
636662
plan = _filter_plan_by_ancestry(
@@ -675,7 +701,10 @@ def main() -> int:
675701
continue
676702
for task in tasks:
677703
task_jit_cache = _prepare_jit_cache(
678-
_jit_cache_path(jit_cache_root, target_branch, commit, task.task_id, task.backend_key)
704+
_cache_bucket_path(jit_cache_root, target_branch, commit, task.task_id, task.backend_key)
705+
)
706+
task_kit_cache = _prepare_kit_cache(
707+
_cache_bucket_path(kit_cache_root, target_branch, commit, task.task_id, task.backend_key)
679708
)
680709
for sample_idx in range(args.samples_per_commit):
681710
total += 1
@@ -699,7 +728,7 @@ def main() -> int:
699728
task=task,
700729
artifact_dir=artifact_dir,
701730
jit_cache=task_jit_cache,
702-
kit_cache=kit_cache,
731+
kit_cache=task_kit_cache,
703732
seed_src_dir=seed_src_dir if source_mount else None,
704733
container_name=container,
705734
)

‎tools/perf_smoke_test/test/test_seed_ancestry.py‎

Lines changed: 87 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import subprocess
1717
import sys
1818
from pathlib import Path
19+
from types import SimpleNamespace
1920

2021
import pytest
2122

@@ -145,16 +146,29 @@ def test_prepare_jit_cache_opens_task_cache(tmp_path: Path) -> None:
145146
assert cache_dir.stat().st_mode & 0o777 == 0o777
146147

147148

149+
def test_seed_source_directories_are_unique_and_disposable() -> None:
150+
"""Independent seeder invocations do not reuse source-clone residue."""
151+
first = seed_baselines._create_seed_source_dir()
152+
second = seed_baselines._create_seed_source_dir()
153+
try:
154+
assert first != second
155+
assert first.is_dir()
156+
assert second.is_dir()
157+
finally:
158+
seed_baselines._cleanup_run_dir(first)
159+
seed_baselines._cleanup_run_dir(second)
160+
161+
148162
def test_jit_cache_reuses_samples_and_isolates_backends(tmp_path: Path) -> None:
149163
"""Repeated samples share compiled kernels, but different backends do not."""
150164
cache_root = tmp_path / "jit-cache" / "seed" / "run-1-attempt-1"
151-
newton_cache = seed_baselines._jit_cache_path(
165+
newton_cache = seed_baselines._cache_bucket_path(
152166
cache_root, "perf-smoke/develop-staging", "abcdef123456", "Isaac-Cartpole-Direct", "newton"
153167
)
154-
repeated_newton_cache = seed_baselines._jit_cache_path(
168+
repeated_newton_cache = seed_baselines._cache_bucket_path(
155169
cache_root, "perf-smoke/develop-staging", "abcdef123456", "Isaac-Cartpole-Direct", "newton"
156170
)
157-
physx_cache = seed_baselines._jit_cache_path(
171+
physx_cache = seed_baselines._cache_bucket_path(
158172
cache_root, "perf-smoke/develop-staging", "abcdef123456", "Isaac-Cartpole-Direct", "physx"
159173
)
160174

@@ -169,14 +183,81 @@ def test_jit_cache_reuses_samples_and_isolates_backends(tmp_path: Path) -> None:
169183
assert not physx_cache.exists()
170184

171185

172-
def test_cleanup_jit_cache_removes_run_tree(tmp_path: Path) -> None:
173-
"""Seeder exit cleanup removes compiled kernels from its run directory."""
186+
def test_kit_cache_reuses_samples_and_isolates_backends(tmp_path: Path) -> None:
187+
"""Repeated samples share Kit data, but different backends do not."""
188+
cache_root = tmp_path / "kit-cache" / "seed" / "run-1-attempt-1"
189+
newton_cache = seed_baselines._cache_bucket_path(
190+
cache_root, "perf-smoke/develop-staging", "abcdef123456", "Isaac-Camera-Direct", "newton"
191+
)
192+
repeated_newton_cache = seed_baselines._cache_bucket_path(
193+
cache_root, "perf-smoke/develop-staging", "abcdef123456", "Isaac-Camera-Direct", "newton"
194+
)
195+
physx_cache = seed_baselines._cache_bucket_path(
196+
cache_root, "perf-smoke/develop-staging", "abcdef123456", "Isaac-Camera-Direct", "physx"
197+
)
198+
199+
_ = seed_baselines._prepare_kit_cache(newton_cache)
200+
shader_cache = newton_cache / "shader-cache.bin"
201+
shader_cache.write_bytes(b"compiled")
202+
_ = seed_baselines._prepare_kit_cache(repeated_newton_cache)
203+
204+
assert repeated_newton_cache == newton_cache
205+
assert shader_cache.read_bytes() == b"compiled"
206+
assert newton_cache.stat().st_mode & 0o777 == 0o777
207+
assert physx_cache != newton_cache
208+
assert not physx_cache.exists()
209+
210+
211+
def test_cleanup_run_dir_removes_run_tree(tmp_path: Path) -> None:
212+
"""Seeder exit cleanup removes generated files from its run directory."""
174213
cache_dir = tmp_path / "jit-cache" / "seed" / "run-1-attempt-1"
175214
compiled_dir = cache_dir / "warp" / "version" / "module"
176215
compiled_dir.mkdir(parents=True)
177216
(compiled_dir / "kernel.so").write_bytes(b"compiled")
178217
compiled_dir.chmod(0o500)
179218

180-
seed_baselines._cleanup_jit_cache(cache_dir)
219+
seed_baselines._cleanup_run_dir(cache_dir)
181220

182221
assert not cache_dir.exists()
222+
223+
224+
def test_docker_benchmark_copies_internal_output_after_exit(tmp_path: Path, monkeypatch) -> None:
225+
"""Benchmark output is copied from the container instead of bind-mounted."""
226+
calls: list[list[str]] = []
227+
228+
def _run(cmd: list[str], **_kwargs) -> SimpleNamespace:
229+
calls.append(cmd)
230+
return SimpleNamespace(returncode=0)
231+
232+
task = SimpleNamespace(
233+
task_id="Isaac-Cartpole-Direct",
234+
num_envs=16,
235+
num_frames=10,
236+
warmup_frames=2,
237+
seed=42,
238+
)
239+
artifact_dir = tmp_path / "artifacts"
240+
monkeypatch.setattr(seed_baselines, "hydra_args_for_task", lambda _task: [])
241+
monkeypatch.setattr(seed_baselines.subprocess, "run", _run)
242+
243+
exit_code = seed_baselines._docker_run_benchmark(
244+
image="isaac-lab:test",
245+
task=task,
246+
artifact_dir=artifact_dir,
247+
jit_cache=tmp_path / "jit-cache",
248+
kit_cache=tmp_path / "kit-cache",
249+
seed_src_dir=tmp_path / "source",
250+
container_name="perf-seed-test",
251+
)
252+
253+
docker_run = calls[0]
254+
assert exit_code == 0
255+
assert "--rm" not in docker_run
256+
assert f"{artifact_dir}:/tmp/bench_out" not in docker_run
257+
assert f"{tmp_path / 'jit-cache'}:/tmp/jit-cache" in docker_run
258+
assert f"{tmp_path / 'kit-cache'}:/isaac-sim/kit/cache" in docker_run
259+
assert "umask 000" in docker_run[-1]
260+
assert "mkdir -p /tmp/bench_out" in docker_run[-1]
261+
assert calls[1] == ["docker", "cp", "perf-seed-test:/tmp/bench_out/.", str(artifact_dir)]
262+
assert calls[2] == ["chmod", "-R", "a+rwX", str(artifact_dir)]
263+
assert calls[3] == ["docker", "rm", "-f", "perf-seed-test"]

0 commit comments

Comments
 (0)