Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wasmtime(pooling allocator): Batch decommits #8590

Merged
merged 1 commit into from May 14, 2024

Conversation

fitzgen
Copy link
Member

@fitzgen fitzgen commented May 9, 2024

This introduces a DecommitQueue for batching decommits together in the pooling allocator:

  • Deallocating a memory/table/stack enqueues their associated regions of memory for decommit; it no longer immediately returns the associated slot to the pool's free list. If the queue's length has reached the configured batch size, then we flush the queue by running all the decommits, and finally returning the memory/table/stack slots to their respective pools and free lists.

  • Additionally, if allocating a new memory/table/stack fails because the free list is empty (aka we've reached the max concurrently-allocated limit for this entity) then we fall back to a slow path before propagating the error. This slow path flushes the decommit queue and then retries allocation, hoping that the queue flush reclaimed slots and made them available for this fallback allocation attempt. This involved defining a new PoolConcurrencyLimitError to match on, which is also exposed in the public embedder API.

It is also worth noting that we always use this new decommit queue now. To keep the existing behavior, where e.g. a memory's decommits happen immediately on deallocation, you can use a batch size of one. This effectively disables queuing, forcing all decommits to be flushed immediately.

The default decommit batch size is one.

This commit, with batch size of one, consistently gives me an increase on wasmtime serve's requests-per-second versus its parent commit, as measured by benches/wasmtime-serve-rps.sh. I get ~39K RPS on this commit compared to ~35K RPS on the parent commit. This is quite puzzling to me. I was expecting no change, and hoping there wouldn't be a regression. I was not expecting a speed up. I cannot explain this result at this time.

@fitzgen fitzgen requested review from a team as code owners May 9, 2024 21:22
@fitzgen fitzgen requested review from alexcrichton and removed request for a team May 9, 2024 21:22
@fitzgen fitzgen force-pushed the decommit-queue branch 2 times, most recently from e99a95b to 1f95aa7 Compare May 9, 2024 21:41
Copy link
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.

Thanks and looks good! I've got some thoughts on refactorings below, but otherwise can you additionally add some tests that exercise the if-empty-then-flush-and-try-again path? Basically a test that fails if that path isn't there but succeeds if it is.

This is quite puzzling to me. I was expecting no change, and hoping there wouldn't be a regression. I was not expecting a speed up. I cannot explain this result at this time.

I think I've seen things to this effect historically where very-close-together madvises are slightly more optimal sometimes. The theory is that the kernel didn't have a chance to run any other threads between two calls to madvise so the second one can skip IPIs since the kernel dynamically knows that no other cores need to be shot down, but that's only a guess.

@github-actions github-actions bot added fuzzing Issues related to our fuzzing infrastructure wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime labels May 10, 2024
Copy link

Subscribe to Label Action

cc @fitzgen

This issue or pull request has been labeled: "fuzzing", "wasmtime:api", "wasmtime:config"

Thus the following users have been cc'd because of the following labels:

  • fitzgen: fuzzing

To subscribe or unsubscribe from this label, edit the .github/subscribe-to-label.json configuration file.

Learn more.

Copy link

github-actions bot commented May 10, 2024

Label Messager: wasmtime:config

It looks like you are changing Wasmtime's configuration options. Make sure to
complete this check list:

  • If you added a new Config method, you wrote extensive documentation for
    it.

    Our documentation should be of the following form:

    Short, simple summary sentence.
    
    More details. These details can be multiple paragraphs. There should be
    information about not just the method, but its parameters and results as
    well.
    
    Is this method fallible? If so, when can it return an error?
    
    Can this method panic? If so, when does it panic?
    
    # Example
    
    Optional example here.
    
  • If you added a new Config method, or modified an existing one, you
    ensured that this configuration is exercised by the fuzz targets.

    For example, if you expose a new strategy for allocating the next instance
    slot inside the pooling allocator, you should ensure that at least one of our
    fuzz targets exercises that new strategy.

    Often, all that is required of you is to ensure that there is a knob for this
    configuration option in wasmtime_fuzzing::Config (or one
    of its nested structs).

    Rarely, this may require authoring a new fuzz target to specifically test this
    configuration. See our docs on fuzzing for more details.

  • If you are enabling a configuration option by default, make sure that it
    has been fuzzed for at least two weeks before turning it on by default.


To modify this label's message, edit the .github/label-messager/wasmtime-config.md file.

To add new label messages or remove existing label messages, edit the
.github/label-messager.json configuration file.

Learn more.

@fitzgen fitzgen force-pushed the decommit-queue branch 3 times, most recently from 2913b76 to aa7508d Compare May 10, 2024 20:26
@fitzgen
Copy link
Member Author

fitzgen commented May 10, 2024

@alexcrichton I think this is ready for re-review!

Copy link
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.

Nice!

@alexcrichton alexcrichton added this pull request to the merge queue May 10, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 10, 2024
@jameysharp
Copy link
Contributor

Looks like the reason CI is failing is that we can only use libc::iovec on cfg(unix). Elsewhere I guess we should define an alternative struct iovec with the same fields? We need it to match system ABI if we use it in syscalls but on non-Unix (non-Linux, even) any pair of pointer and length will do.

@fitzgen fitzgen enabled auto-merge May 13, 2024 16:01
@fitzgen fitzgen added this pull request to the merge queue May 13, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 13, 2024
@fitzgen fitzgen enabled auto-merge May 13, 2024 18:16
@fitzgen fitzgen added this pull request to the merge queue May 13, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 13, 2024
@fitzgen fitzgen enabled auto-merge May 13, 2024 18:36
@fitzgen fitzgen added this pull request to the merge queue May 13, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks May 13, 2024
@fitzgen fitzgen enabled auto-merge May 13, 2024 19:22
@fitzgen fitzgen force-pushed the decommit-queue branch 2 times, most recently from 3cab360 to b457df9 Compare May 13, 2024 22:31
This introduces a `DecommitQueue` for batching decommits together in the pooling
allocator:

* Deallocating a memory/table/stack enqueues their associated regions of memory
  for decommit; it no longer immediately returns the associated slot to the
  pool's free list. If the queue's length has reached the configured batch size,
  then we flush the queue by running all the decommits, and finally returning
  the memory/table/stack slots to their respective pools and free lists.

* Additionally, if allocating a new memory/table/stack fails because the free
  list is empty (aka we've reached the max concurrently-allocated limit for this
  entity) then we fall back to a slow path before propagating the error. This
  slow path flushes the decommit queue and then retries allocation, hoping that
  the queue flush reclaimed slots and made them available for this fallback
  allocation attempt. This involved defining a new `PoolConcurrencyLimitError`
  to match on, which is also exposed in the public embedder API.

It is also worth noting that we *always* use this new decommit queue now. To
keep the existing behavior, where e.g. a memory's decommits happen immediately
on deallocation, you can use a batch size of one. This effectively disables
queueing, forcing all decommits to be flushed immediately.

The default decommit batch size is one.

This commit, with batch size of one, consistently gives me an increase on
`wasmtime serve`'s requests-per-second versus its parent commit, as measured by
`benches/wasmtime-serve-rps.sh`. I get ~39K RPS on this commit compared to ~35K
RPS on the parent commit. This is quite puzzling to me. I was expecting no
change, and hoping there wouldn't be a regression. I was not expecting a speed
up. I cannot explain this result at this time.

prtest:full

Co-Authored-By: Jamey Sharp <jsharp@fastly.com>
@fitzgen fitzgen added this pull request to the merge queue May 14, 2024
Merged via the queue into bytecodealliance:main with commit e1f8b9b May 14, 2024
52 checks passed
@fitzgen fitzgen deleted the decommit-queue branch May 14, 2024 00:46
fitzgen added a commit to fitzgen/wasmtime that referenced this pull request May 14, 2024
This introduces a `DecommitQueue` for batching decommits together in the pooling
allocator:

* Deallocating a memory/table/stack enqueues their associated regions of memory
  for decommit; it no longer immediately returns the associated slot to the
  pool's free list. If the queue's length has reached the configured batch size,
  then we flush the queue by running all the decommits, and finally returning
  the memory/table/stack slots to their respective pools and free lists.

* Additionally, if allocating a new memory/table/stack fails because the free
  list is empty (aka we've reached the max concurrently-allocated limit for this
  entity) then we fall back to a slow path before propagating the error. This
  slow path flushes the decommit queue and then retries allocation, hoping that
  the queue flush reclaimed slots and made them available for this fallback
  allocation attempt. This involved defining a new `PoolConcurrencyLimitError`
  to match on, which is also exposed in the public embedder API.

It is also worth noting that we *always* use this new decommit queue now. To
keep the existing behavior, where e.g. a memory's decommits happen immediately
on deallocation, you can use a batch size of one. This effectively disables
queueing, forcing all decommits to be flushed immediately.

The default decommit batch size is one.

This commit, with batch size of one, consistently gives me an increase on
`wasmtime serve`'s requests-per-second versus its parent commit, as measured by
`benches/wasmtime-serve-rps.sh`. I get ~39K RPS on this commit compared to ~35K
RPS on the parent commit. This is quite puzzling to me. I was expecting no
change, and hoping there wouldn't be a regression. I was not expecting a speed
up. I cannot explain this result at this time.

prtest:full

Co-authored-by: Jamey Sharp <jsharp@fastly.com>
alexcrichton added a commit to alexcrichton/wasmtime that referenced this pull request May 14, 2024
Fixes an accidental fuzz regression from bytecodealliance#8590 where error messages were
changed slightly.
github-merge-queue bot pushed a commit that referenced this pull request May 14, 2024
Fixes an accidental fuzz regression from #8590 where error messages were
changed slightly.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fuzzing Issues related to our fuzzing infrastructure wasmtime:api Related to the API of the `wasmtime` crate itself wasmtime:config Issues related to the configuration of Wasmtime
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants