Skip to content
Projects

scope_profiler.profile_config

Configuration for profiling managers.

Attributes

NameDescription
tomllibNo description.

Classes

NameDescription
GPUOptionsGPU timing and NVTX settings, as a group.
HDF5OptionsHDF5 storage settings, as a group.
MemrayOptionsMemray capture settings, as a group.
ProfilingConfigConfiguration for one profiling manager.
ProfilingOptionsA bag of :meth:ProfileManager.setup settings, to reuse or pass around.
SetupOptionsThe keyword names :meth:ProfileManager.setup accepts, for typing.

Functions

NameDescription
load_profiling_configLoad the [profiling] table from a TOML settings file.

tomllibattributemodule attribute#

tomllib = _tomllib

GPUOptionsclassdataclass#

class GPUOptions(timing: bool | None = None, backend: Any = None, nvtx: bool | None = None, sync_on_exit: bool | None = None)

Bases: _OptionGroup

GPU timing and NVTX settings, as a group.

In a TOML config file this is the [profiling.gpu] table.

Attributes

timing : bool or None Record CUDA-event elapsed device time per region (use_gpu_timing). backend : str, object, or None CUDA-event backend (gpu_timing_backend): "auto", "torch", "cupy", or an object implementing record_event() and elapsed_time_ns(start_event, end_event). sync_on_exit : bool or None Synchronize the device at region exit (gpu_sync_on_exit). Requires timing and removes CPU/GPU overlap; disabled by default. nvtx : bool or None Emit NVTX ranges for NVIDIA Nsight tools (use_nvtx).

backendattributeclass attributeinstance attribute#

backend: Any = None

nvtxattributeclass attributeinstance attribute#

nvtx: bool | None = None

sync_on_exitattributeclass attributeinstance attribute#

sync_on_exit: bool | None = None

timingattributeclass attributeinstance attribute#

timing: bool | None = None
Inherited from scope_profiler.profile_config._OptionGroup

to_kwargsmethod#

Inherited from scope_profiler.profile_config._OptionGroupView source
def to_kwargs() -> dict

This group’s explicitly-set fields, under their flat names.

HDF5Optionsclassdataclass#

class HDF5Options(compression: str | None = None, compression_level: int | None = None, chunk_size: int | None = None)

Bases: _OptionGroup

HDF5 storage settings, as a group.

In a TOML config file this is the [profiling.hdf5] table.

Attributes

compression : str or None Compression filter for timestamp and GPU-duration datasets (hdf5_compression): "gzip", "lzf", "zstd", or "auto" to compress only runs large enough to repay the write CPU. compression_level : int or None GZIP level 0—9 or Zstandard level 1—22 (hdf5_compression_level). chunk_size : int or None Maximum events per dataset chunk (hdf5_chunk_size). Enables chunked partial reads even without compression.

chunk_sizeattributeclass attributeinstance attribute#

chunk_size: int | None = None

compressionattributeclass attributeinstance attribute#

compression: str | None = None

compression_levelattributeclass attributeinstance attribute#

compression_level: int | None = None
Inherited from scope_profiler.profile_config._OptionGroup

to_kwargsmethod#

Inherited from scope_profiler.profile_config._OptionGroupView source
def to_kwargs() -> dict

This group’s explicitly-set fields, under their flat names.

MemrayOptionsclassdataclass#

class MemrayOptions(enabled: bool | None = None, path: str | None = None, native_traces: bool | None = None, trace_python_allocators: bool | None = None, follow_fork: bool | None = None)

Bases: _OptionGroup

Memray capture settings, as a group.

ProfilingOptions(memray=MemrayOptions(enabled=True, native_traces=True)) configures the same run as ProfilingOptions(use_memray=True, memray_native_traces=True); use whichever reads better at the call site. In a TOML config file the group is the [profiling.memray] table.

Attributes

enabled : bool or None Record process-wide allocations with Memray (use_memray). path : str or None Capture path (memory_profile_path); defaults to <file-stem>.memray.bin. native_traces : bool or None Capture native stack frames (memray_native_traces). trace_python_allocators : bool or None Trace Python’s own allocators (memray_trace_python_allocators). Creates much larger traces and has materially higher overhead. follow_fork : bool or None Keep tracking across fork() (memray_follow_fork).

enabledattributeclass attributeinstance attribute#

enabled: bool | None = None

follow_forkattributeclass attributeinstance attribute#

follow_fork: bool | None = None

native_tracesattributeclass attributeinstance attribute#

native_traces: bool | None = None

pathattributeclass attributeinstance attribute#

path: str | None = None

trace_python_allocatorsattributeclass attributeinstance attribute#

trace_python_allocators: bool | None = None
Inherited from scope_profiler.profile_config._OptionGroup

to_kwargsmethod#

Inherited from scope_profiler.profile_config._OptionGroupView source
def to_kwargs() -> dict

This group’s explicitly-set fields, under their flat names.

ProfilingConfigclass#

class ProfilingConfig(file_path: str = 'profiling_data.h5', label: str | None = None, use_likwid: bool = False, perf_events: list[str] | tuple[str, ...] | str | None = None, use_line_profiler: bool = False, use_memray: bool = False, memory_profile_path: str | None = None, memray_native_traces: bool = False, memray_trace_python_allocators: bool = False, memray_follow_fork: bool = False, deactivate_profiling: bool = False, use_nvtx: bool = False, use_gpu_timing: bool = False, gpu_timing_backend='auto', deactivate_file_output: bool = False, recursive_profile: bool = False, aggregation_mode: bool = False, profile_mpi_calls: bool = False, track_threads: bool = False, track_async: bool = False, capture_region_source: bool = False, metadata_detail: str = 'minimal', buffer_limit: int = 1024, output_mode: str = 'auto', hdf5_compression: str | None = None, hdf5_compression_level: int | None = None, hdf5_chunk_size: int | None = None, gpu_sync_on_exit: bool = False, enable_tags: list[str] | tuple[str, ...] | str | None = None)

Configuration for one profiling manager.

This class centralizes configuration for LIKWID performance counters, buffer limits, and file paths. Each manager owns a separate instance, so independently configured profiling sessions can coexist. Constructing it is purely local: it reads the communicator for rank and size but issues no MPI call of its own, so setup() does not have to be collective.

aggregation_modeproperty#

aggregation_mode: bool

Whether regions retain aggregates instead of individual events.

buffer_limitproperty#

buffer_limit: int

Initial per-region buffer capacity; buffers grow beyond it as needed.

capture_region_sourceproperty#

capture_region_source: bool

Return whether a region’s defining source is captured at creation.

commproperty#

comm: Intercomm | None

MPI communicator or None if MPI is unavailable.

deactivate_file_outputproperty#

deactivate_file_output: bool

Return whether the run writes no HDF5 file at all.

deactivate_profilingproperty#

deactivate_profiling: bool

Return whether profiling is globally turned off.

enable_tagsproperty#

enable_tags: tuple[str, ...]

Opt-in region tags this run records, or an empty tuple.

file_pathproperty#

file_path: str

Global output file path for combined profiling data.

gpu_sync_on_exitproperty#

gpu_sync_on_exit: bool

Whether GPU regions wait for the device before recording CPU end time.

gpu_timing_backendproperty#

gpu_timing_backend

CUDA-event timing backend selector or backend object.

hdf5_chunk_sizeproperty#

hdf5_chunk_size: int | None

Maximum number of events per timestamp chunk.

hdf5_compressionproperty#

hdf5_compression: str | None

Compression filter used for timestamp datasets.

hdf5_compression_levelproperty#

hdf5_compression_level: int | None

Configured GZIP or Zstandard compression level.

labelproperty#

label: str | None

Short name for this run, or None if none was given to setup().

memory_profile_pathproperty#

memory_profile_path: Path | None

Memray capture path, or None when allocation profiling is off.

metadataproperty#

metadata: dict

Environment metadata collected on this rank (hostname, OpenMP threads, …).

metadata_detailproperty#

metadata_detail: str

"full" or "minimal": how much environment metadata is recorded.

output_modeproperty#

output_mode: str

MPI HDF5 output strategy: auto, direct, or parallel.

pausedproperty#

paused: bool

Whether runtime timing collection is temporarily suspended.

perf_eventsproperty#

perf_events: tuple[str, ...]

Requested Linux perf event names, or an empty tuple when disabled.

profile_mpi_callsproperty#

profile_mpi_calls: bool

Whether mpi4py communicator operations are profiled.

recursive_profileproperty#

recursive_profile: bool

Return whether recursive decorator profiling is enabled by default.

start_time_nsproperty#

start_time_ns: int

The run’s start time (ns, perf_counter_ns clock).

The moment the configuration was created. Persisted as metadata and used as the timeline origin when reading the results back.

track_asyncproperty#

track_async: bool

Whether every call records its asyncio task or greenlet.

track_threadsproperty#

track_threads: bool

Whether every call records the thread it ran on.

trackerproperty#

tracker

This run’s :class:~scope_profiler.concurrency.ConcurrencyTracker.

None unless track_threads is set, which is what keeps the default configuration free of any thread, asyncio or greenlet hook.

use_gpu_timingproperty#

use_gpu_timing: bool

Return whether CUDA-event GPU timing is enabled.

use_likwidproperty#

use_likwid: bool

Return whether LIKWID profiling is enabled.

use_line_profilerproperty#

use_line_profiler: bool

Return whether line_profiler profiling is enabled.

use_memrayproperty#

use_memray: bool

Return whether Memray allocation profiling is enabled.

use_nvtxproperty#

use_nvtx: bool

Return whether NVTX annotations are enabled.

collect_likwid_resultsmethod#

def collect_likwid_results(region_names) -> list

Close the LIKWID markers and return the run’s counter results.

Tries three sources, richest first, so a host where LIKWID cannot do the fancy parts still ends up with real numbers in the HDF5 file:

  1. the perfmon read-back, run in a subprocess because it can crash the interpreter outright on hosts that cannot really count;
  2. LIKWID’s marker file, parsed directly --- real values, but no event names or derived metrics;
  3. a marker-API snapshot taken before the markers were closed, for when there is no marker file at all.

See :mod:scope_profiler.likwid_data for why the first one is fenced off.

Parameters

region_names : iterable of str Region names to snapshot via the marker API.

Returns

list of scope_profiler.likwid_data.LikwidRegionResult Empty when LIKWID is disabled, the process was not started under likwid-perfctr -m / likwid-mpirun -marker, or the markers were already collected.

Notes

A process can only do this once. Closing the markers tears the marker API down, and querying it afterwards crashes the interpreter rather than raising, so a second finalize() returns nothing instead of reading again. The counters of the whole process therefore end up in the file written by the first finalize().

likwid_environmentmethod#

def likwid_environment() -> dict

Return the LIKWID_* environment variables of this process.

pylikwid_markerclosemethod#

def pylikwid_markerclose()

Close LIKWID markers to finalize measurement regions.

Idempotent: repeated calls (a second finalize(), say) do nothing.

pylikwid_markerinitmethod#

def pylikwid_markerinit()

Initialize LIKWID markers if LIKWID is enabled.

resetmethodclassmethod#

def reset()

Compatibility no-op retained for callers of older releases.

Configurations are ordinary per-manager objects now, so there is no process-wide instance to reset.

stop_memory_profilingmethod#

def stop_memory_profiling() -> None

Finish Memray’s process-wide allocation capture, if enabled.

ProfilingOptionsclassdataclass#

class ProfilingOptions(file_path: str | None = None, label: str | None = None, use_likwid: bool | None = None, perf_events: list[str] | tuple[str, ...] | str | None = None, use_line_profiler: bool | None = None, use_memray: bool | None = None, memory_profile_path: str | None = None, memray_native_traces: bool | None = None, memray_trace_python_allocators: bool | None = None, memray_follow_fork: bool | None = None, deactivate_profiling: bool | None = None, use_nvtx: bool | None = None, use_gpu_timing: bool | None = None, gpu_timing_backend: Any = None, deactivate_file_output: bool | None = None, recursive_profile: bool | None = None, aggregation_mode: bool | None = None, profile_mpi_calls: bool | None = None, track_threads: bool | None = None, track_async: bool | None = None, capture_region_source: bool | None = None, metadata_detail: str | None = None, buffer_limit: int | None = None, output_mode: str | None = None, hdf5_compression: str | None = None, hdf5_compression_level: int | None = None, hdf5_chunk_size: int | None = None, enable_tags: list[str] | tuple[str, ...] | None = None, memray: MemrayOptions | None = None, gpu: GPUOptions | None = None, hdf5: HDF5Options | None = None, gpu_sync_on_exit: bool | None = None)

A bag of :meth:ProfileManager.setup settings, to reuse or pass around.

Every field mirrors a setup()/session() keyword argument and defaults to None, meaning “unset - let setup()’s own default, or its config_path file, decide”. Pass one to setup() or session() instead of repeating the same handful of keyword arguments at every call site::

options = ProfilingOptions(use_likwid=True, file_path="run.h5")
ProfileManager.setup(options=options)
# ... or, equivalently:
with ProfileManager.session(options=options):
...

An explicit keyword argument passed alongside options still wins over the same field on options, so a shared ProfilingOptions can be reused across runs with one-off overrides::

ProfileManager.setup(options=options, file_path="run_b.h5")

Settings that share a prefix can also be given as groups — :class:MemrayOptions, :class:GPUOptions and :class:HDF5Options — which spell the same settings without the prefix::

options = ProfilingOptions(
file_path="run.h5",
hdf5=HDF5Options(compression="gzip", compression_level=4),
)

Setting one thing both ways raises ValueError rather than picking a winner.

Attributes

file_path : str or None Path to the output profiling data file (default: "profiling_data.h5"). label : str or None Short name for this run (default: None, i.e. the output file’s stem). Post-processing uses it wherever a run has to be named — chart legends, the summary heading, scope-profiler inspect, the JSON statistics - and it is stored in the output file as the label metadata field, so it survives into every later post-processing step. use_likwid : bool or None Enable LIKWID hardware counter collection (default: False). perf_events : sequence of str or None Linux perf_event_open events to collect per region (default: None). use_line_profiler : bool or None Enable line-by-line profiling via line_profiler (default: False). use_memray : bool or None Record process-wide memory allocations with Memray (default: False). deactivate_profiling : bool or None Turn profiling off entirely (setup() default: False). Every region becomes a no-op, so the instrumentation can stay in the code at near-zero cost instead of being removed. use_nvtx : bool or None Add NVTX ranges to profiled regions for NVIDIA Nsight tools (default: False). Requires scope-profiler[nvtx]. use_gpu_timing : bool or None Record CUDA-event elapsed device time for each profiled region (default: False). CPU timestamps are still recorded, so the normal timeline remains enqueue-side timing. gpu_sync_on_exit : bool or None Wait for the GPU device at region exit. Requires use_gpu_timing and removes CPU/GPU overlap; disabled by default. gpu_timing_backend : str, object, or None CUDA-event backend for use_gpu_timing: "auto", "torch", "cupy", or a custom object implementing record_event() and elapsed_time_ns(start_event, end_event) (default: "auto"). deactivate_file_output : bool or None Write no HDF5 file at all (default: False), not even the run metadata. Pair it with finalize(return_results=True) to analyse a run entirely in memory. recursive_profile : bool or None Enable recursive profiling for all decorated functions by default (default: False). Overridable per decorator with @ProfileManager.profile(..., recursive=...). aggregation_mode : bool or None Record only count, inclusive total, minimum, maximum, and exclusive total per region (default: False). Timeline events are unavailable in this mode; it cannot be combined with line, GPU, NVTX, or LIKWID profiling. profile_mpi_calls : bool or None Profile mpi4py operations made through predefined or derived communicators (default: False). mpi4py remains lazily imported. track_threads : bool or None Record which thread each call ran on, and describe every thread the run touched (default: False). Required for correct results from concurrently used regions; see :class:~scope_profiler.region_profiler.ThreadedProfileRegion. track_async : bool or None Also record which asyncio task or greenlet each call ran in, and how much of the call was spent awaiting (default: False). Implies track_threads. capture_region_source : bool or None Record where each region is defined - the with block or the decorated function - once per distinct source file, the first time any of its regions is created (default: False). See :attr:~scope_profiler.region.Region.source_text. Off by default: the cost tracks that file’s total size (one ast.parse + tree walk), so it stays under a millisecond for a typical file but can reach tenths of a second per rank for a single file with thousands of lines, paid independently by every rank. metadata_detail : {“minimal”, “full”} or None How much of the run’s environment is recorded (default: "minimal"). "minimal" stores versions, platform, CPU model and thread/rank counts, and each region’s and line profile’s source file relative to the working directory (or by name alone, outside it). "full" adds the user name, host name, working directory, loaded modules, environment variables such as PATH and VIRTUAL_ENV, SLURM_* variables and absolute source paths; opt into it for runs that stay on the machine they were recorded on. buffer_limit : int or None Initial number of profiling events preallocated per region (default: 1024). Buffers grow on demand, so this is a starting size rather than a limit; raise it for very hot regions to avoid repeated reallocation. output_mode : str or None MPI file writer: "auto", "direct", or "parallel" (default: "auto"). auto prefers MPI-enabled h5py when compatible with the active instrumentation and otherwise lets ranks append directly to one serial-HDF5 file in token order. hdf5_compression : str or None Compression filter for timestamp and GPU-duration datasets: None, "auto", "gzip", "lzf", or "zstd" (default: None). "auto" compresses at publication time, and only when the run is large enough for the saving to repay the write CPU. hdf5_compression_level : int or None GZIP level 0—9 or Zstandard level 1—22 (default: None). hdf5_chunk_size : int or None Maximum events per dataset chunk (default: None). Enables chunked partial reads even without compression. enable_tags : sequence of str or None Opt-in region tags to record (default: None). Regions carrying a tag registered with :meth:ProfileManager.register_opt_in_tag - typically a library’s own instrumentation - stay disabled unless one of their opt-in tags is listed here, in region_tags, or in the comma-separated SCOPE_PROFILER_ENABLE_TAGS environment variable. "*" enables every opt-in tag. memray : MemrayOptions or None The use_memray/memory_profile_path/memray_* settings as one group. gpu : GPUOptions or None The use_gpu_timing/gpu_timing_backend/use_nvtx settings as one group. hdf5 : HDF5Options or None The hdf5_* settings as one group.

aggregation_modeattributeclass attributeinstance attribute#

aggregation_mode: bool | None = None

buffer_limitattributeclass attributeinstance attribute#

buffer_limit: int | None = None

capture_region_sourceattributeclass attributeinstance attribute#

capture_region_source: bool | None = None

deactivate_file_outputattributeclass attributeinstance attribute#

deactivate_file_output: bool | None = None

deactivate_profilingattributeclass attributeinstance attribute#

deactivate_profiling: bool | None = None

enable_tagsattributeclass attributeinstance attribute#

enable_tags: list[str] | tuple[str, ...] | None = None

file_pathattributeclass attributeinstance attribute#

file_path: str | None = None

gpuattributeclass attributeinstance attribute#

gpu: GPUOptions | None = None

gpu_sync_on_exitattributeclass attributeinstance attribute#

gpu_sync_on_exit: bool | None = None

gpu_timing_backendattributeclass attributeinstance attribute#

gpu_timing_backend: Any = None

hdf5attributeclass attributeinstance attribute#

hdf5: HDF5Options | None = None

hdf5_chunk_sizeattributeclass attributeinstance attribute#

hdf5_chunk_size: int | None = None

hdf5_compressionattributeclass attributeinstance attribute#

hdf5_compression: str | None = None

hdf5_compression_levelattributeclass attributeinstance attribute#

hdf5_compression_level: int | None = None

labelattributeclass attributeinstance attribute#

label: str | None = None

memory_profile_pathattributeclass attributeinstance attribute#

memory_profile_path: str | None = None

memrayattributeclass attributeinstance attribute#

memray: MemrayOptions | None = None

memray_follow_forkattributeclass attributeinstance attribute#

memray_follow_fork: bool | None = None

memray_native_tracesattributeclass attributeinstance attribute#

memray_native_traces: bool | None = None

memray_trace_python_allocatorsattributeclass attributeinstance attribute#

memray_trace_python_allocators: bool | None = None

metadata_detailattributeclass attributeinstance attribute#

metadata_detail: str | None = None

output_modeattributeclass attributeinstance attribute#

output_mode: str | None = None

perf_eventsattributeclass attributeinstance attribute#

perf_events: list[str] | tuple[str, ...] | str | None = None

profile_mpi_callsattributeclass attributeinstance attribute#

profile_mpi_calls: bool | None = None

recursive_profileattributeclass attributeinstance attribute#

recursive_profile: bool | None = None

track_asyncattributeclass attributeinstance attribute#

track_async: bool | None = None

track_threadsattributeclass attributeinstance attribute#

track_threads: bool | None = None

use_gpu_timingattributeclass attributeinstance attribute#

use_gpu_timing: bool | None = None

use_likwidattributeclass attributeinstance attribute#

use_likwid: bool | None = None

use_line_profilerattributeclass attributeinstance attribute#

use_line_profiler: bool | None = None

use_memrayattributeclass attributeinstance attribute#

use_memray: bool | None = None

use_nvtxattributeclass attributeinstance attribute#

use_nvtx: bool | None = None

to_kwargsmethod#

def to_kwargs() -> dict

This options’ explicitly-set fields, as setup() keyword arguments.

Group fields are expanded to the flat names they stand for. Setting the same thing both ways raises ValueError rather than picking a winner.

SetupOptionsclass#

class SetupOptions(TypedDict)

Bases: TypedDict

The keyword names :meth:ProfileManager.setup accepts, for typing.

setup() and session() take their settings as **overrides rather than restating 28 keyword parameters, so this is what gives type checkers and editors the names and types of those keywords. It mirrors :class:ProfilingOptions exactly - test_setup_options_match_fields fails the build if the two ever drift.

aggregation_modeattributeinstance attribute#

aggregation_mode: bool

buffer_limitattributeinstance attribute#

buffer_limit: int

capture_region_sourceattributeinstance attribute#

capture_region_source: bool

deactivate_file_outputattributeinstance attribute#

deactivate_file_output: bool

deactivate_profilingattributeinstance attribute#

deactivate_profiling: bool

enable_tagsattributeinstance attribute#

enable_tags: list[str] | tuple[str, ...]

file_pathattributeinstance attribute#

file_path: str

gpu_sync_on_exitattributeinstance attribute#

gpu_sync_on_exit: bool

gpu_timing_backendattributeinstance attribute#

gpu_timing_backend: Any

hdf5_chunk_sizeattributeinstance attribute#

hdf5_chunk_size: int

hdf5_compressionattributeinstance attribute#

hdf5_compression: str

hdf5_compression_levelattributeinstance attribute#

hdf5_compression_level: int

labelattributeinstance attribute#

label: str

memory_profile_pathattributeinstance attribute#

memory_profile_path: str

memray_follow_forkattributeinstance attribute#

memray_follow_fork: bool

memray_native_tracesattributeinstance attribute#

memray_native_traces: bool

memray_trace_python_allocatorsattributeinstance attribute#

memray_trace_python_allocators: bool

metadata_detailattributeinstance attribute#

metadata_detail: str

output_modeattributeinstance attribute#

output_mode: str

perf_eventsattributeinstance attribute#

perf_events: list[str] | tuple[str, ...] | str

profile_mpi_callsattributeinstance attribute#

profile_mpi_calls: bool

recursive_profileattributeinstance attribute#

recursive_profile: bool

track_asyncattributeinstance attribute#

track_async: bool

track_threadsattributeinstance attribute#

track_threads: bool

use_gpu_timingattributeinstance attribute#

use_gpu_timing: bool

use_likwidattributeinstance attribute#

use_likwid: bool

use_line_profilerattributeinstance attribute#

use_line_profiler: bool

use_memrayattributeinstance attribute#

use_memray: bool

use_nvtxattributeinstance attribute#

use_nvtx: bool

load_profiling_configfunction#

def load_profiling_config(path: str | os.PathLike[str]) -> dict

Load the [profiling] table from a TOML settings file.

A top-level table is also accepted for small files. Paths in the file retain TOML’s normal meaning and are interpreted relative to the current working directory, just like paths passed to :meth:ProfileManager.setup.

Settings may be written flat, or grouped into the memray, gpu and hdf5 sub-tables that mirror :class:MemrayOptions, :class:GPUOptions and :class:HDF5Options::

[profiling]
file_path = "run.h5"
[profiling.hdf5]
compression = "gzip"
compression_level = 4

Setting the same thing both ways is an error, not a silent precedence rule.