|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +from pathlib import Path |
| 6 | + |
5 | 7 | import pytest |
6 | 8 | from conftest import RunHost |
7 | 9 |
|
@@ -267,6 +269,87 @@ def run(inputs, ctx): |
267 | 269 | assert run.error["kind"] == "MemoryLimitExceeded" |
268 | 270 |
|
269 | 271 |
|
| 272 | +def test_spec001_ac30_joins_the_cgroup_of_the_attempt_instead_of_limiting_itself( |
| 273 | + run_host: RunHost, tmp_path: Path |
| 274 | +) -> None: |
| 275 | + # A stand-in for the cgroup the runner created: the real memory.max lives in the kernel. |
| 276 | + cgroup = tmp_path / "psr-runs" / "tentativa" |
| 277 | + cgroup.mkdir(parents=True) |
| 278 | + (cgroup / "cgroup.procs").write_text("", encoding="ascii") |
| 279 | + script = """ |
| 280 | + import os |
| 281 | + from psrunner import stage |
| 282 | +
|
| 283 | + @stage |
| 284 | + def run(inputs, ctx): |
| 285 | + data = bytearray(256 * 1024 * 1024) |
| 286 | + return {"pid": os.getpid(), "threads": os.environ.get("OPENBLAS_NUM_THREADS", "")} |
| 287 | + """ |
| 288 | + |
| 289 | + run = run_host( |
| 290 | + script, |
| 291 | + output_fields=[{"name": "pid", "type": "integer"}, {"name": "threads", "type": "string"}], |
| 292 | + memory_limit_mb=128, |
| 293 | + extra_env={"PSR_CGROUP": str(cgroup), "PSR_FALLBACK_THREADS": "1"}, |
| 294 | + ) |
| 295 | + |
| 296 | + assert (cgroup / "cgroup.procs").read_text(encoding="ascii") == str(run.outputs["pid"]) |
| 297 | + assert run.outputs["threads"] == "", "with a cgroup the numeric libraries keep their threads" |
| 298 | + |
| 299 | + |
| 300 | +def test_spec001_ac29_without_the_cgroup_limits_the_threads_and_keeps_the_memory_limit( |
| 301 | + run_host: RunHost, tmp_path: Path |
| 302 | +) -> None: |
| 303 | + script = """ |
| 304 | + import os |
| 305 | + from psrunner import stage |
| 306 | +
|
| 307 | + @stage |
| 308 | + def run(inputs, ctx): |
| 309 | + names = [ |
| 310 | + "OMP_NUM_THREADS", |
| 311 | + "OPENBLAS_NUM_THREADS", |
| 312 | + "MKL_NUM_THREADS", |
| 313 | + "NUMEXPR_NUM_THREADS", |
| 314 | + ] |
| 315 | + threads = ",".join(os.environ.get(name, "") for name in names) |
| 316 | + data = bytearray(1024 * 1024 * 1024) |
| 317 | + return {"threads": threads, "tamanho": len(data)} |
| 318 | + """ |
| 319 | + |
| 320 | + run = run_host( |
| 321 | + script, |
| 322 | + output_fields=[ |
| 323 | + {"name": "threads", "type": "string"}, |
| 324 | + {"name": "tamanho", "type": "integer"}, |
| 325 | + ], |
| 326 | + memory_limit_mb=128, |
| 327 | + extra_env={"PSR_CGROUP": str(tmp_path / "nao-existe"), "PSR_FALLBACK_THREADS": "3"}, |
| 328 | + ) |
| 329 | + |
| 330 | + assert run.error["kind"] == "MemoryLimitExceeded" |
| 331 | + assert "could not join the cgroup" in run.stderr |
| 332 | + |
| 333 | + |
| 334 | +def test_spec001_ac29_fallback_threads_reach_the_script(run_host: RunHost, tmp_path: Path) -> None: |
| 335 | + script = """ |
| 336 | + import os |
| 337 | + from psrunner import stage |
| 338 | +
|
| 339 | + @stage |
| 340 | + def run(inputs, ctx): |
| 341 | + return {"threads": os.environ.get("OPENBLAS_NUM_THREADS", "")} |
| 342 | + """ |
| 343 | + |
| 344 | + run = run_host( |
| 345 | + script, |
| 346 | + output_fields=[{"name": "threads", "type": "string"}], |
| 347 | + extra_env={"PSR_CGROUP": str(tmp_path / "nao-existe"), "PSR_FALLBACK_THREADS": "3"}, |
| 348 | + ) |
| 349 | + |
| 350 | + assert run.outputs["threads"] == "3" |
| 351 | + |
| 352 | + |
270 | 353 | def test_spec002_ac11_protocol_mismatch(run_host: RunHost) -> None: |
271 | 354 | run = run_host("from psrunner import stage\n", protocol="2") |
272 | 355 |
|
|
0 commit comments