climate_ref_core.resources
#
Measurement of the resources a block of work consumes.
A block can be measured via a context manager, :func:measure_resources,
and results are returned as a frozen dataclass, :class:ResourceUsage.
.. code-block:: python
with measure_resources() as recorder:
run_diagnostic()
usage = recorder.usage
print(usage.wall_seconds, usage.peak_memory_bytes, usage.memory_source)
Measurement never raises.
Any probe that fails degrades a single field to None,
or degrades :attr:ResourceUsage.memory_source to "unavailable".
Peak memory comes from a summed sweep of the process tree by default.
proc_tree observes this block's processes and nothing else.
A cgroup reading covers every process in the container,
so it only describes this block when the caller declares, via cgroup_exclusive,
that nothing else shares the cgroup.
The fallbacks, in order, are a cgroup reading and then getrusage.
:attr:ResourceUsage.memory_source records which one won,
because a getrusage figure must never be silently compared against a cgroup figure.
Both sampled peaks are always recorded,
in :attr:ResourceUsage.cgroup_peak_bytes and :attr:ResourceUsage.proc_tree_peak_bytes,
whichever of them was reported.
A large divergence between the two is itself the evidence that the cgroup was shared.
CGROUP_V2_MOUNT = Path('/sys/fs/cgroup')
module-attribute
#
Mount point of the cgroup v2 unified hierarchy.
MemorySource = Literal['cgroup', 'proc_tree', 'rusage', 'unavailable']
module-attribute
#
Provenance of a peak memory measurement.
ResourceRecorder
#
Handle yielded by :func:measure_resources.
:attr:usage is None while the block runs and holds a :class:ResourceUsage once it exits.
The priority for the method of determining memory usage is:
cgroup (exclusive) > proc_tree > cgroup > rusage
In practice this generally means proc_tree for most use cases
(LocalExecutor or Celery under MacOS/Linux).
Source code in packages/climate-ref-core/src/climate_ref_core/resources.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 | |
ResourceUsage
#
What one block of work cost.
Every field except :attr:wall_seconds and :attr:exclusive is nullable,
because each of them comes from a probe that a given host may not answer.
Source code in packages/climate-ref-core/src/climate_ref_core/resources.py
cgroup_peak_bytes
instance-attribute
#
Peak memory of the whole cgroup, whether or not it was the source that won.
None when this is not a cgroup v2 host or the control files could not be read.
context
instance-attribute
#
Host and sampler detail, JSON serialisable.
cpu_limit
instance-attribute
#
cgroup cpu.max quota expressed in cores, or None when the group is unlimited.
cpu_seconds
instance-attribute
#
CPU time used, self plus children, user plus system.
exclusive
instance-attribute
#
Whether the cgroup readings are attributable to this block alone.
True only when the caller declared the cgroup exclusive and no other measured block overlapped this one in this process. Sibling worker processes saturating the same container are invisible from here, so without it a cgroup figure describes the container rather than this block.
memory_limit_bytes
instance-attribute
#
cgroup memory.max at run time, or None when the group is unlimited.
memory_source
instance-attribute
#
Provenance of :attr:peak_memory_bytes.
Two numbers are only comparable when they share a source.
peak_memory_bytes
instance-attribute
#
Peak memory, measured by whichever source :attr:memory_source names.
proc_tree_peak_bytes
instance-attribute
#
Peak summed resident memory of this process and its descendants, whether or not it won.
None when psutil is missing or every sweep failed.
wall_seconds
instance-attribute
#
Elapsed monotonic time.
measure_resources(*, interval=0.5, enabled=True, cgroup_exclusive=False)
#
Measure wall time, CPU time and peak memory of everything done in the block.
The yielded recorder exposes a single attribute,
usage, which is None inside the block and a :class:ResourceUsage after it exits.
A sampling failure degrades individual fields to None rather than failing the execution.
An exception raised inside the block still propagates, with usage populated.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
interval
|
float
|
Seconds between memory samples. |
0.5
|
enabled
|
bool
|
Whether to measure at all. When False the block runs untouched and |
True
|
cgroup_exclusive
|
bool
|
Whether the caller can promise that nothing else shares this process's cgroup while the block runs. Only a caller that owns the concurrency knows this. It defaults to False, under which a cgroup figure is reported only when the process tree cannot be swept, and is marked as non-exclusive so aggregation excludes it. |
False
|
Yields:
| Type | Description |
|---|---|
ResourceRecorder
|
The recorder holding the result. |