Skip to content

Conversation

@lann
Copy link
Contributor

@lann lann commented Aug 21, 2025

This exposes some basic runtime metrics derived from the internal state of a PoolingInstanceAllocator.

Two new atomics were added to PoolingInstanceAllocator: live_memories and live_tables. While these counts could be derived from existing state it would require acquiring mutexes on some inner state.

Fixes #11449

@lann lann requested a review from a team as a code owner August 21, 2025 16:13
@lann lann requested review from pchickey and removed request for a team August 21, 2025 16:13
@lann lann marked this pull request as draft August 21, 2025 16:20
@lann lann force-pushed the pooling-metrics branch from 30f7954 to 2b497ed Compare August 21, 2025 16:44
@lann lann marked this pull request as ready for review August 21, 2025 16:45
Ok(result) => return Ok(result),
Err(e) => e,
};
self.live_memories.fetch_add(1, Ordering::AcqRel);
Copy link
Contributor Author

@lann lann Aug 21, 2025

Choose a reason for hiding this comment

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

I was on the fence about using Ordering::Relaxed for these, but if someone ends up using metrics in actual business logic (like server backpressure) it would be a pretty nasty surprise for these to underflow...

Copy link
Member

Choose a reason for hiding this comment

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

I'd actually opt towards using Relaxed here since there's explicitly no data dependency with anything else in the system (e.g. this isn't looking for memory effects of another operation or publishing its own memory effects). Could you detail more about the underflow point though? How would atomic ordering affect that?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sounds good. I don't really have a good grasp of relaxed atomic semantics so I just pessimistically assume that a concurrent thread would be able to go back in time and kill its own grandparent or whatever.

Copy link
Member

Choose a reason for hiding this comment

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

Heh nah makes sense. I'm by no means an expert but I'm confident enough in my understanding that if atomics are used for things like metric purposes it's fine to use relaxed. Relaxed ordering, AFAIK, basically means that you get no guarantees about other memory locations in the program when you observe a value. For the memory location in question, though, everything is still just as atomic as you'd expect (e.g. fetch-and-add still does that, it doesn't like switch to non-atomics). This happens because relaxed atomics can be reordered around each other by the compiler, so you can't for example say "I saw N memories in use, therefore I must see N+1 tables in use next", that's not valid.

Copy link
Contributor Author

@lann lann Aug 21, 2025

Choose a reason for hiding this comment

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

Yeah makes sense. I guess I'm not entirely clear on what makes increment_component_instance_count different here (above) that it would need AcqRel?

Copy link
Member

Choose a reason for hiding this comment

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

Oh it's not, the main difference is I saw this here and no one saw it there most likely. There's no need for that to use AcqRel either and IMO it should also be using Relaxed

@github-actions github-actions bot added the wasmtime:api Related to the API of the `wasmtime` crate itself label Aug 21, 2025
Ok(result) => return Ok(result),
Err(e) => e,
};
self.live_memories.fetch_add(1, Ordering::AcqRel);
Copy link
Member

Choose a reason for hiding this comment

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

I'd actually opt towards using Relaxed here since there's explicitly no data dependency with anything else in the system (e.g. this isn't looking for memory effects of another operation or publishing its own memory effects). Could you detail more about the underflow point though? How would atomic ordering affect that?

@lann lann force-pushed the pooling-metrics branch 2 times, most recently from b2a427f to fc3f429 Compare August 21, 2025 19:18
@alexcrichton alexcrichton enabled auto-merge August 21, 2025 19:27
@alexcrichton alexcrichton added this pull request to the merge queue Aug 21, 2025
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Aug 21, 2025
@alexcrichton
Copy link
Member

While the vet error is spurious, the miri failure looks legitimate.

@lann
Copy link
Contributor Author

lann commented Aug 22, 2025

This does indeed fail under miri locally:

Engine::new(&Config::new().allocation_strategy(InstanceAllocationStrategy::pooling()))
.unwrap();

Presumably I can work around this by overriding some default config, but is it a bug that the defaults don't work with miri?

@lann
Copy link
Contributor Author

lann commented Aug 22, 2025

Oh well I guess that's pretty straightforward:

if cfg!(miri) {
Tunables::default_miri()

Any preference on how to fix this?

  • Add a cfg!(miri) for default max_memory_size
  • Just set max_memory_size in the test

@alexcrichton
Copy link
Member

The main test suite has a helper function for a "small pool config" which could be modeled here as well, but this test isn't too interesting for miri so it's also fine to slap a #[cfg_attr(miri, ignore)] on the test

This exposes some basic runtime metrics derived from the internal state
of a `PoolingInstanceAllocator`.

Two new atomics were added to PoolingInstanceAllocator: `live_memories`
and `live_tables`. While these counts could be derived from existing
state it would require acquiring mutexes on some inner state.
@lann lann force-pushed the pooling-metrics branch from fc3f429 to c1b726c Compare August 22, 2025 15:51
@alexcrichton alexcrichton enabled auto-merge August 22, 2025 15:57
@alexcrichton alexcrichton added this pull request to the merge queue Aug 22, 2025
Merged via the queue into bytecodealliance:main with commit becdee5 Aug 22, 2025
44 checks passed
bongjunj pushed a commit to prosyslab/wasmtime that referenced this pull request Oct 20, 2025
This exposes some basic runtime metrics derived from the internal state
of a `PoolingInstanceAllocator`.

Two new atomics were added to PoolingInstanceAllocator: `live_memories`
and `live_tables`. While these counts could be derived from existing
state it would require acquiring mutexes on some inner state.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

wasmtime:api Related to the API of the `wasmtime` crate itself

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Expose pooling allocator runtime metrics

2 participants