Alternera.Plugin.Provisional.HeapPeak (alternera_plugin_sdk v0.9.0)

Copy Markdown View Source

Provisional: added for the runner and Script, and a change to it needs a changelog line but no new major (docs/USER_PLUGINS_DESIGN.md).

The most memory a step's process reached, as its heap limit (StepHeap, max_heap_size with include_shared_binaries) counts it: the heap's blocks, the old heap's, the heap fragments and the reference-counted binaries the process holds. The BEAM keeps no peak, and a reading taken now and then misses a parse that grows and is collected in between, so the process's garbage collections are traced and the largest size any collection reports, before or after it, is kept, with the size when the meter is stopped. The VM checks max_heap_size at the same collections, so the peak and the limit are the same kind of number. The collection the VM kills a process at reports nothing, so the trace of a process killed at its limit ends at the collection before, often half the limit or less: a caller that knows the process was killed for its memory notes the limit as its peak (note/2), which the kill shows it reached.

The collections of every metered process go to one process, HeapPeak.Collector, on one trace session it keeps for the node (:trace.session_create/3, which leaves any other tracer of a process alone): a meter turns that session's trace on for its process and off again. A meter is started by the process that waits for the step (start/0, which costs nothing yet), so a step's process killed at its limit still leaves its peak with the collector; the step's process puts itself under it first thing (watch/2), before it allocates, and the waiting process reads the peak with stop/1. The collector drops a meter on stop/1, or when the process that started it goes. A meter costs three calls to the collector and a message per collection, a few hundred on a step that parses tens of megabytes, so it is started only for work on a body over the inline threshold: the runner meters a step it runs under a heap limit, the Script sandbox a call that takes in more than the threshold (Plugin.Host.inline_threshold_bytes/0). watch/2, done/1 and stop/1 take nil, the meter not started, and do nothing.

A step's peak travels like its reductions (Reductions): the process that waited notes it (note/2), with the limit the process ran under, and the runner takes it after the step (take/0), so no plugin result carries it. Two notes in one step (a step on a held body whose Script call ran in a sandbox of its own) keep the larger peak and its limit.

Summary

Types

A meter (start/0); nil where none was started.

A step's noted peak and the limit it ran under, in bytes (take/0).

Functions

Hands meter the calling process's size now, returning once it has it: called by the watched process at the end of its work, since a process that never collected garbage reported nothing, and one that has exited can no longer be read.

Notes what take/0 returned elsewhere (nil: nothing).

Notes a peak of peak_bytes under a limit of limit_bytes (nil: none) for the step the calling process is running; take/0 reads it.

Starts a meter owned by the calling process; it measures nothing until watch/2.

The peak, in bytes, of the process meter watched (0 when it watched none), and the meter's end.

The peak noted since the last take/0, cleared; nil when none was.

Puts pid under meter, returning once its collections are traced: called by the step's process on itself before it does any work.

Types

meter()

@opaque meter()

A meter (start/0); nil where none was started.

noted()

@type noted() :: %{peak_bytes: non_neg_integer(), limit_bytes: pos_integer() | nil}

A step's noted peak and the limit it ran under, in bytes (take/0).

Functions

done(arg1)

@spec done(meter()) :: :ok

Hands meter the calling process's size now, returning once it has it: called by the watched process at the end of its work, since a process that never collected garbage reported nothing, and one that has exited can no longer be read.

note(arg1)

@spec note(noted() | nil) :: :ok

Notes what take/0 returned elsewhere (nil: nothing).

note(peak_bytes, limit_bytes)

@spec note(non_neg_integer(), pos_integer() | nil) :: :ok

Notes a peak of peak_bytes under a limit of limit_bytes (nil: none) for the step the calling process is running; take/0 reads it.

start()

@spec start() :: meter()

Starts a meter owned by the calling process; it measures nothing until watch/2.

stop(arg1)

@spec stop(meter()) :: non_neg_integer()

The peak, in bytes, of the process meter watched (0 when it watched none), and the meter's end.

take()

@spec take() :: noted() | nil

The peak noted since the last take/0, cleared; nil when none was.

watch(arg1, pid)

@spec watch(meter(), pid()) :: :ok

Puts pid under meter, returning once its collections are traced: called by the step's process on itself before it does any work.