# `Alternera.Plugin.Provisional.Reductions`
[🔗](https://github.com/10e-ab/alternera-sdk/blob/v0.9.0/alternera_plugin_sdk/lib/alternera/plugin/provisional/reductions.ex#L1)

Provisional: added for Script, and a change to it needs a changelog line but
no new major (`docs/USER_PLUGINS_DESIGN.md`). The namespace has nothing to do
with the context's `:provisional?` key.

A pipeline step's reductions: the BEAM's work counter, the figure builds
are compared on (no clock in it, so it does not move with the machine's
load). `Pipelines.Runner` takes `now/0` before and after each step in the
process that runs it and records the difference on the step's outcome
(`reductions`, beside `time_us`).

Some of a step's work runs in a process of its own: a Script call runs in
a sandbox process (`Plugins.Script.Sandbox`), which reports its own count
with its result. The process that waited for it adds that count here
(`add_elsewhere/1`), and `now/0` is this process's own count plus what it
has been told of, so the step's difference covers both without every
plugin result carrying a number back. A step on a body held on disk runs
in a task of the runner's, which reports its count the same way. The running sum lives in the
process dictionary under one key, read and written only by this module:
it is measurement context of the kind `Logger` metadata is, and a
process that never ran a sandbox call never has it. It only grows: a
step's count is a difference of two readings, never the sum itself.

A helper process that is killed (a Script call stopped at its deadline
or its memory limit, or one that crashed) sends no count, so that step
records only what the waiting process spent: an undercount, on a step
that failed anyway.

`:erlang.process_info/2` for `:reductions` is a BIF that reads a counter
in the process's own structure; a step costs two of them.

## Work outside the BEAM

Reductions count only what runs on the BEAM's schedulers. A step whose
work runs elsewhere (a WebAssembly guest on its runtime's own threads or
in a process of the operating system, a plugin host's call) says so with
`add_outside/2`: what ran there, by a name a person reads ("WebAssembly",
"Camel host"), and for how long, in wall-clock microseconds. The runner
takes it onto the step's outcome (`take_outside/0`, `outside_us` and
`outside`), so a count that is not the whole of the step's work is
marked as such where it is shown. Only the time the work ran outside is
added: whatever the step's process did meanwhile (serving a guest's host
calls, say) is already in the count.

It lives in the process dictionary under a key of its own, beside the
helpers' count, and unlike that it is taken, not read: `take_outside/0`
answers what was added since the last take and starts again, and
`put_outside/1` gives back what an enclosing step had added before a
step it runs (a pipeline call) took its own.

# `outside`

```elixir
@type outside() :: %{us: non_neg_integer(), kinds: [String.t()]}
```

Work outside the BEAM's schedulers: `us`, its wall-clock microseconds,
and `kinds`, the names of what ran it, sorted and each once.

# `add_elsewhere`

```elixir
@spec add_elsewhere(non_neg_integer()) :: :ok
```

Counts `reductions` that a helper process spent on this process's behalf
(a Script call's sandbox process) toward `now/0`.

# `add_outside`

```elixir
@spec add_outside(String.t(), integer()) :: :ok
```

Counts `us` wall-clock microseconds of the step's work that ran outside
the BEAM's schedulers, in `kind` (a name a person reads, such as
"WebAssembly"), toward the step's outcome. A step that ran nothing
outside never calls it, and its count stays unmarked.

# `measure`

```elixir
@spec measure((-&gt; result)) :: {non_neg_integer(), result} when result: term()
```

Runs `fun` and returns `{reductions, result}`, its reductions as `now/0`
counts them (the shape of `:timer.tc/1`).

# `note_outside`

```elixir
@spec note_outside(outside() | nil) :: :ok
```

Adds `outside`, what another process reported (`take_outside/0` there),
as `add_outside/2` does; `nil` adds nothing.

# `now`

```elixir
@spec now() :: non_neg_integer()
```

This process's reductions, plus those of the helper processes whose work
it waited for (`add_elsewhere/1`).

# `own`

```elixir
@spec own() :: non_neg_integer()
```

The calling process's own reductions so far (a helper process reports these).

# `put_outside`

```elixir
@spec put_outside(outside() | nil) :: :ok
```

Puts back `outside`, what an enclosing step had added before a step it
ran took its own (`take_outside/0`); `nil` leaves nothing.

# `take_outside`

```elixir
@spec take_outside() :: outside() | nil
```

What was added outside the BEAM since the last take, or `nil` when
nothing was, and starts again from nothing.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
