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

Enable concurrent instantiation, and release #342

Merged
merged 17 commits into from
Mar 7, 2022
Merged

Enable concurrent instantiation, and release #342

merged 17 commits into from
Mar 7, 2022

Conversation

mathetake
Copy link
Member

@mathetake mathetake commented Mar 7, 2022

This makes sure that concurrent use of Instantiate and
ReleaseModuleInstance is goroutine-safe.

Note that it is still not safe to expose Release in the public API
as we haven't taken into account the outstanding calls. That
would be addressed in the follow-up commit.

part of #293.
Signed-off-by: Takeshi Yoneda [email protected]

@mathetake
Copy link
Member Author

ok cool no race detected

$ go test -race -run ^TestStore_concurrent$ github.com/tetratelabs/wazero/internal/wasm -count=100 
ok      github.com/tetratelabs/wazero/internal/wasm     19.425s

// This case other modules are importing this module instance and still alive.
return fmt.Errorf("%d modules import this and need to be closed first", instance.refCount)
return fmt.Errorf("%d modules import this and need to be closed first", instance.importedCount)
}

// TODO: check outstanding calls and wait until they exit.
Copy link
Member Author

Choose a reason for hiding this comment

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

this will be resolved in the follow-up and then we could expose Close (or whatever we call) in the public API

@mathetake mathetake marked this pull request as ready for review March 7, 2022 09:28
@mathetake mathetake changed the title Enable concurrent instantiation and compilation Enable concurrent instantiation and compilation / Goroutine-safe Release Mar 7, 2022
@mathetake mathetake changed the title Enable concurrent instantiation and compilation / Goroutine-safe Release Enable concurrent instantiation, and release Mar 7, 2022
@codefromthecrypt
Copy link
Contributor

I'm going to add some commits instead of making review comments (or at least to reduce them)

mathetake added 14 commits March 7, 2022 19:14
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Takeshi Yoneda <[email protected]>
Signed-off-by: Adrian Cole <[email protected]>
Copy link
Contributor

@codefromthecrypt codefromthecrypt left a comment

Choose a reason for hiding this comment

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

one main question on the impl, otherwise works for me!

PS I think in the future I'd also like to understand why the store can't partition data by module as it seems the locks would be less blinding. I guess there are store-wide type indexes that must be coherent across modules? If this is already explained in RATIONALE never mind, but one day if it isn't let's add something on the data partitioning strategy as it impacts locking.

// mux is used to guard the fields from concurrent access.
mux sync.Mutex

// dependentCount is the current number of modules which import this module. On Store.ReleaseModule, this number
Copy link
Contributor

Choose a reason for hiding this comment

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

notably renamed these two fields

internal/wasm/store.go Outdated Show resolved Hide resolved
Signed-off-by: Adrian Cole <[email protected]>
@mathetake
Copy link
Member Author

why the store can't partition data by module as it seems the locks would be less blinding.

Theoretically can be partitioned, but a live instance must be assigned a unique addr (funcaddr, memaddr, etc in the spec https://www.w3.org/TR/wasm-core-1/#addresses%E2%91%A0), and notably the function calls were made via funcaddr (which engine maps to the a *compiledFunction). In the spec rather aggressively, a module instance only holds addresses (https://www.w3.org/TR/wasm-core-1/#module-instances%E2%91%A0).

But yeah I believe there's some way to work around this (especially once we are able to do precompilation namely after we could decouple compiledFunction from runtime representations).

Signed-off-by: Takeshi Yoneda <[email protected]>
@mathetake
Copy link
Member Author

Raise a follow-up for the ps comment! #343

@codefromthecrypt
Copy link
Contributor

thanks for the work on this, getting close to safe Close!

mathetake added a commit that referenced this pull request Mar 9, 2022
This commit adds ModuleEngine interface which is used to
make calls and created per-module instance. Notably, this
enables us to remove the necessity for store to holds
FunctionInstances and FunctionIndex.

This is a follow-up from #342

Signed-off-by: Takeshi Yoneda <[email protected]>
Co-authored-by: Adrian Cole <[email protected]>
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