-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Add shared memories #4187
Merged
alexcrichton
merged 47 commits into
bytecodealliance:main
from
abrown:shared-memory-vmcontext
Jun 8, 2022
Merged
Add shared memories #4187
alexcrichton
merged 47 commits into
bytecodealliance:main
from
abrown:shared-memory-vmcontext
Jun 8, 2022
Commits on Jun 6, 2022
-
This change adds the ability to use shared memories in Wasmtime when the [threads proposal] is enabled. Shared memories are annotated as `shared` in the WebAssembly syntax, e.g., `(memory 1 1 shared)`, and are protected from concurrent access during `memory.size` and `memory.grow`. [threads proposal]: https://github.com/WebAssembly/threads/blob/master/proposals/threads/Overview.md In order to implement this in Wasmtime, there are two main cases to cover: - a program may simply create a shared memory and possibly export it; this means that Wasmtime itself must be able to create shared memories - a user may create a shared memory externally and pass it in as an import during instantiation; this is the case when the program contains code like `(import "env" "memory" (memory 1 1 shared))`--this case is handled by a new Wasmtime API type--`SharedMemory` Because of the first case, this change allows any of the current memory-creation mechanisms to work as-is. Wasmtime can still create either static or dynamic memories in either on-demand or pooling modes, and any of these memories can be considered shared. When shared, the `Memory` runtime container will lock appropriately during `memory.size` and `memory.grow` operations; since all memories use this container, it is an ideal place for implementing the locking once and once only. The second case is covered by the new `SharedMemory` structure. It uses the same `Mmap` allocation under the hood as non-shared memories, but allows the user to perform the allocation externally to Wasmtime and share the memory across threads (via an `Arc`). The pointer address to the actual memory is carefully wired through and owned by the `SharedMemory` structure itself. This means that there are differing views of where to access the pointer (i.e., `VMMemoryDefinition`): for owned memories (the default), the `VMMemoryDefinition` is stored directly by the `VMContext`; in the `SharedMemory` case, however, this `VMContext` must point to this separate structure. To ensure that the `VMContext` can always point to the correct `VMMemoryDefinition`, this change alters the `VMContext` structure. Since a `SharedMemory` owns its own `VMMemoryDefinition`, the `defined_memories` table in the `VMContext` becomes a sequence of pointers--in the shared memory case, they point to the `VMMemoryDefinition` owned by the `SharedMemory` and in the owned memory case (i.e., not shared) they point to `VMMemoryDefinition`s stored in a new table, `owned_memories`. This change adds an additional indirection (through the `*mut VMMemoryDefinition` pointer) that could add overhead. Using an imported memory as a proxy, we measured a 1-3% overhead of this approach on the `pulldown-cmark` benchmark. To avoid this, Cranelift-generated code will special-case the owned memory access (i.e., load a pointer directly to the `owned_memories` entry) for `memory.size` so that only shared memories (and imported memories, as before) incur the indirection cost.
Configuration menu - View commit details
-
Copy full SHA for 13113d8 - Browse repository at this point
Copy the full SHA 13113d8View commit details -
Configuration menu - View commit details
-
Copy full SHA for 8e88076 - Browse repository at this point
Copy the full SHA 8e88076View commit details -
Configuration menu - View commit details
-
Copy full SHA for 6281c86 - Browse repository at this point
Copy the full SHA 6281c86View commit details -
Configuration menu - View commit details
-
Copy full SHA for 9a6d871 - Browse repository at this point
Copy the full SHA 9a6d871View commit details -
Configuration menu - View commit details
-
Copy full SHA for 234ea40 - Browse repository at this point
Copy the full SHA 234ea40View commit details -
Configuration menu - View commit details
-
Copy full SHA for 681a627 - Browse repository at this point
Copy the full SHA 681a627View commit details -
Configuration menu - View commit details
-
Copy full SHA for 41a5016 - Browse repository at this point
Copy the full SHA 41a5016View commit details -
Configuration menu - View commit details
-
Copy full SHA for 4cc2ddf - Browse repository at this point
Copy the full SHA 4cc2ddfView commit details -
Configuration menu - View commit details
-
Copy full SHA for 9cc4db0 - Browse repository at this point
Copy the full SHA 9cc4db0View commit details -
Configuration menu - View commit details
-
Copy full SHA for 7a723e1 - Browse repository at this point
Copy the full SHA 7a723e1View commit details -
Configuration menu - View commit details
-
Copy full SHA for 04aaf4f - Browse repository at this point
Copy the full SHA 04aaf4fView commit details -
Configuration menu - View commit details
-
Copy full SHA for 26c22f5 - Browse repository at this point
Copy the full SHA 26c22f5View commit details -
Configuration menu - View commit details
-
Copy full SHA for 188cb65 - Browse repository at this point
Copy the full SHA 188cb65View commit details -
Configuration menu - View commit details
-
Copy full SHA for 06dbb4d - Browse repository at this point
Copy the full SHA 06dbb4dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 2f9d139 - Browse repository at this point
Copy the full SHA 2f9d139View commit details -
Configuration menu - View commit details
-
Copy full SHA for e236a24 - Browse repository at this point
Copy the full SHA e236a24View commit details -
Configuration menu - View commit details
-
Copy full SHA for 0b1f51b - Browse repository at this point
Copy the full SHA 0b1f51bView commit details -
Configuration menu - View commit details
-
Copy full SHA for 90d452e - Browse repository at this point
Copy the full SHA 90d452eView commit details -
fix: fixes based on discussion with Alex
This changes several key parts: - adds memory indexes to imports and exports - makes `VMMemoryDefinition::current_length` an atomic usize
Configuration menu - View commit details
-
Copy full SHA for 7402dc1 - Browse repository at this point
Copy the full SHA 7402dc1View commit details -
Configuration menu - View commit details
-
Copy full SHA for f79b322 - Browse repository at this point
Copy the full SHA f79b322View commit details -
Configuration menu - View commit details
-
Copy full SHA for 25f4a4e - Browse repository at this point
Copy the full SHA 25f4a4eView commit details -
Configuration menu - View commit details
-
Copy full SHA for 09f8d3d - Browse repository at this point
Copy the full SHA 09f8d3dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 6669977 - Browse repository at this point
Copy the full SHA 6669977View commit details -
Configuration menu - View commit details
-
Copy full SHA for bee0ece - Browse repository at this point
Copy the full SHA bee0eceView commit details -
Configuration menu - View commit details
-
Copy full SHA for 12420e4 - Browse repository at this point
Copy the full SHA 12420e4View commit details -
Configuration menu - View commit details
-
Copy full SHA for 1be4ad0 - Browse repository at this point
Copy the full SHA 1be4ad0View commit details -
Configuration menu - View commit details
-
Copy full SHA for e44a58a - Browse repository at this point
Copy the full SHA e44a58aView commit details -
Configuration menu - View commit details
-
Copy full SHA for 14ce663 - Browse repository at this point
Copy the full SHA 14ce663View commit details -
Configuration menu - View commit details
-
Copy full SHA for f087502 - Browse repository at this point
Copy the full SHA f087502View commit details -
Configuration menu - View commit details
-
Copy full SHA for b8ef602 - Browse repository at this point
Copy the full SHA b8ef602View commit details -
Configuration menu - View commit details
-
Copy full SHA for e37f881 - Browse repository at this point
Copy the full SHA e37f881View commit details -
Configuration menu - View commit details
-
Copy full SHA for 87da77a - Browse repository at this point
Copy the full SHA 87da77aView commit details -
Configuration menu - View commit details
-
Copy full SHA for 17f8961 - Browse repository at this point
Copy the full SHA 17f8961View commit details -
Configuration menu - View commit details
-
Copy full SHA for 8d981bf - Browse repository at this point
Copy the full SHA 8d981bfView commit details
Commits on Jun 7, 2022
-
Configuration menu - View commit details
-
Copy full SHA for b154a16 - Browse repository at this point
Copy the full SHA b154a16View commit details -
Configuration menu - View commit details
-
Copy full SHA for e90c3ea - Browse repository at this point
Copy the full SHA e90c3eaView commit details -
Configuration menu - View commit details
-
Copy full SHA for bd6e799 - Browse repository at this point
Copy the full SHA bd6e799View commit details -
Configuration menu - View commit details
-
Copy full SHA for 2bdaaed - Browse repository at this point
Copy the full SHA 2bdaaedView commit details -
Configuration menu - View commit details
-
Copy full SHA for 19f3081 - Browse repository at this point
Copy the full SHA 19f3081View commit details -
Configuration menu - View commit details
-
Copy full SHA for 0a13a7d - Browse repository at this point
Copy the full SHA 0a13a7dView commit details -
Configuration menu - View commit details
-
Copy full SHA for 75ddcd1 - Browse repository at this point
Copy the full SHA 75ddcd1View commit details -
Configuration menu - View commit details
-
Copy full SHA for a96fccd - Browse repository at this point
Copy the full SHA a96fccdView commit details -
Configuration menu - View commit details
-
Copy full SHA for 480a1e1 - Browse repository at this point
Copy the full SHA 480a1e1View commit details -
Configuration menu - View commit details
-
Copy full SHA for be221c7 - Browse repository at this point
Copy the full SHA be221c7View commit details -
Configuration menu - View commit details
-
Copy full SHA for 6868519 - Browse repository at this point
Copy the full SHA 6868519View commit details -
Configuration menu - View commit details
-
Copy full SHA for e8101a0 - Browse repository at this point
Copy the full SHA e8101a0View commit details
Commits on Jun 8, 2022
-
Configuration menu - View commit details
-
Copy full SHA for 9e71b72 - Browse repository at this point
Copy the full SHA 9e71b72View commit details
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.