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

Allow overwriting max memory on module instantiation #3198

Merged

Conversation

eloparco
Copy link
Contributor

@eloparco eloparco commented Mar 2, 2024

This PR adds a max_memory_pages parameter to module instantiation APIs, to allow overriding the max memory defined in the WASM module.

Sticking to the max memory defined in the module is quite limiting when using shared memory in production. If targeted devices have different memory constraints, many wasm files have to be generated with different max memory values. And device constraints may not be known in advance.

Being able to set the max memory value during module instantiation allows to reuse the same wasm module, e.g. by retrying instantiation with different max memory value.

core/iwasm/aot/aot_runtime.c Outdated Show resolved Hide resolved
core/iwasm/include/wasm_export.h Outdated Show resolved Hide resolved
core/iwasm/include/wasm_export.h Outdated Show resolved Hide resolved
core/iwasm/interpreter/wasm_runtime.c Outdated Show resolved Hide resolved
core/iwasm/interpreter/wasm_runtime.c Outdated Show resolved Hide resolved
@lum1n0us
Copy link
Collaborator

lum1n0us commented Mar 4, 2024

    uint32 max_page_count =
        max_memory_pages == 0 ? memory->mem_max_page_count : max_memory_pages;

Does it suggest that the max_memory_pages (specified by API) can be greater than mem_max_page_count from memory limitation?

@eloparco
Copy link
Contributor Author

eloparco commented Mar 4, 2024

Does it suggest that the max_memory_pages (specified by API) can be greater than mem_max_page_count from memory limitation?

Is that not allowed? Actually I remember that was mentioned https://bytecodealliance.zulipchat.com/#narrow/stream/349267-wasi-threads/topic/rust/near/402263578, but couldn't find it in the specs.

@wenyongh
Copy link
Contributor

wenyongh commented Mar 4, 2024

Does it suggest that the max_memory_pages (specified by API) can be greater than mem_max_page_count from memory limitation?

Is that not allowed? Actually I remember that was mentioned https://bytecodealliance.zulipchat.com/#narrow/stream/349267-wasi-threads/topic/rust/near/402263578, but couldn't find it in the specs.

No, from the comment of Alex, the max_memory_pages specified by the API can be smaller than what it is specified in wasm file, but should not be larger than it. So how about we add checks: if max_memory_pages from API is smaller than memory->init_page_count, then set it memory->init_page_count; and if max_memory_pages is larger than memory->max_page_count, then set it to memory->max_page_count?
@lum1n0us what's your opinion?

@eloparco
Copy link
Contributor Author

eloparco commented Mar 4, 2024

So how about we add checks: if max_memory_pages from API is smaller than memory->init_page_count, then set it memory->init_page_count; and if max_memory_pages is larger than memory->max_page_count, then set it to memory->max_page_count?

Yeah, makes sense, I'll change the code to do that

@eloparco eloparco force-pushed the eloparco/allow-overwriting-max-memory branch from 9eac4f5 to affa7a5 Compare March 4, 2024 10:59
core/iwasm/aot/aot_runtime.c Outdated Show resolved Hide resolved
core/iwasm/common/wasm_runtime_common.c Show resolved Hide resolved
uint32 module_max_page_count)
{
if (max_memory_pages < module_init_page_count) {
LOG_WARNING("Cannot override max memory with value lower than module "
Copy link
Contributor

Choose a reason for hiding this comment

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

Not sure is it better to use LOG_WARNING("warning: ...")?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If the user is overriding the logger (i.e. WAMR_BH_LOG), they probably already have something to distinguish between different log levels: different colors (red for error, yellow for warning) or different log suffixes (e.g. W, E suffixes). So adding the log level to the log message is redundant.

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, got it, thanks.

core/iwasm/interpreter/wasm_runtime.c Outdated Show resolved Hide resolved
core/iwasm/interpreter/wasm_runtime.c Outdated Show resolved Hide resolved
@eloparco eloparco force-pushed the eloparco/allow-overwriting-max-memory branch from affa7a5 to 5282ec8 Compare March 5, 2024 09:01
@eloparco eloparco force-pushed the eloparco/allow-overwriting-max-memory branch from 5282ec8 to 7e1b36a Compare March 5, 2024 09:06
Copy link
Contributor

@wenyongh wenyongh left a comment

Choose a reason for hiding this comment

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

LGTM

uint32 module_max_page_count)
{
if (max_memory_pages < module_init_page_count) {
LOG_WARNING("Cannot override max memory with value lower than module "
Copy link
Contributor

Choose a reason for hiding this comment

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

OK, got it, thanks.

@wenyongh wenyongh merged commit 7692f32 into bytecodealliance:main Mar 5, 2024
406 checks passed
victoryang00 pushed a commit to victoryang00/wamr-aot-gc-checkpoint-restore that referenced this pull request May 27, 2024
…#3198)

This PR adds a max_memory_pages parameter to module instantiation APIs,
to allow overriding the max memory defined in the WASM module.

Sticking to the max memory defined in the module is quite limiting when
using shared memory in production. If targeted devices have different
memory constraints, many wasm files have to be generated with different
max memory values. And device constraints may not be known in advance.

Being able to set the max memory value during module instantiation allows
to reuse the same wasm module, e.g. by retrying instantiation with different
max memory value.
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.

3 participants