Skip to content

add monotonic clock "pause" feature to p1->p2 adapter#13563

Merged
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
dicej:p1-adapter-pause-monotonic-clock
Jun 5, 2026
Merged

add monotonic clock "pause" feature to p1->p2 adapter#13563
alexcrichton merged 1 commit into
bytecodealliance:mainfrom
dicej:p1-adapter-pause-monotonic-clock

Conversation

@dicej
Copy link
Copy Markdown
Contributor

@dicej dicej commented Jun 4, 2026

This adds a new adapter_monotonic_clock_set_paused export to the adapter which toggles whether clock_time_get will call monotonic_clock::now when called with CLOCKID_MONOTONIC versus returning a cached value instead.

This is a workaround for guest language runtimes whose cabi_realloc implementations may call clock_time_get. Since calling imports from cabi_realloc is disallowed, this will trap if the adapter calls monotonic_clock::now. We can avoid the trap by using a cached value instead.

This helps us address
bytecodealliance/componentize-go#56. In that case, cabi_realloc is implemented by calling
unsafe.SliceData to allocate a segment of the appropriate size and alignment from the GC-managed heap and then pinning it using pinner.Pin until it is no longer needed. However, such an allocation may trigger a GC under memory pressure, and as of this writing the Go collector calls clock_time_get to measure time spent in various stages of the GC process.

An alternative fix for the componentize-go issue would be to modify the Go runtime to avoid such calls on WASI during GC, but getting that upstream is likely to be significantly more difficult than working around it in the adapter. If and when Go supports WASIp3 or later natively, the adapter will no longer be used, in which case we will certainly address this upstream.

This adds a new `adapter_monotonic_clock_set_paused` export to the adapter which
toggles whether `clock_time_get` will call `monotonic_clock::now` when called
with `CLOCKID_MONOTONIC` versus returning a cached value instead.

This is a workaround for guest language runtimes whose `cabi_realloc`
implementations may call `clock_time_get`.  Since calling imports from
`cabi_realloc` is disallowed, this will trap if the adapter calls
`monotonic_clock::now`.  We can avoid the trap by using a cached value instead.

This helps us address
bytecodealliance/componentize-go#56.  In that case,
`cabi_realloc` is implemented by calling
[unsafe.SliceData](https://pkg.go.dev/unsafe#SliceData) to allocate a segment of
the appropriate size and alignment from the GC-managed heap and then pinning it
using [pinner.Pin](https://pkg.go.dev/runtime#Pinner.Pin) until it is no longer
needed.  However, such an allocation may trigger a GC under memory pressure, and
as of this writing the Go collector calls `clock_time_get` to measure time spent
in various stages of the GC process.

An alternative fix for the `componentize-go` issue would be to modify the Go
runtime to avoid such calls on WASI during GC, but getting that upstream is
likely to be significantly more difficult than working around it in the adapter.
If and when Go supports WASIp3 or later natively, the adapter will no longer be
used, in which case we will certainly address this upstream.
@dicej dicej requested a review from a team as a code owner June 4, 2026 22:34
@dicej dicej requested review from pchickey and removed request for a team June 4, 2026 22:34
Copy link
Copy Markdown
Member

@alexcrichton alexcrichton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not thrilled about it, but I suspect you aren't either, and so long as Go is an unchangable monolith I don't think there are many other realistic options on the table.

@alexcrichton alexcrichton added this pull request to the merge queue Jun 4, 2026
Merged via the queue into bytecodealliance:main with commit ff639ef Jun 5, 2026
51 checks passed
dicej added a commit to dicej/go-pkg that referenced this pull request Jun 5, 2026
See bytecodealliance/wasmtime#13563 for details and note
that this will require using an up-to-date (and currently not-yet-released)
version of the `wasi_snapshot_preview1` adapter.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants