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

trim cache parameters for low memory operation #1883

Merged
merged 4 commits into from
Jul 6, 2023
Merged

Conversation

wjhun
Copy link
Contributor

@wjhun wjhun commented Jun 24, 2023

This change reduces baseline memory requirements for various memory caches
under "low memory" conditions. If the function is_low_memory_machine() returns
true, then the following parameters are minimized:

  • the pagecache will use PAGECACHE_LOWMEM_CONTIGUOUS_PAGESIZE (default 128KB)
    as its objcache pagesize instead of PAGESIZE_2M
  • the mcache high order is lowered to 11 (2KB), thus forcing PAGESIZE or
    larger allocations to be made directly from the backing heap - and thus not
    cached, nor available for situations that implement a malloc/free interface
  • similiarly, allocations for lwIP are limited to a maximum size of 2KB

In addition:

  • the runtime tests are stripped by default, saving the unstripped binaries
    under the ".dbg" extension
  • in virtio-net, the size of the backing page allocations for the rx buffer and
    tx completion caches is set according to the rx and tx queue sizes

With these changes, it is possible to run a nanos instance capable of running the
'webs' web server in as little as 16MB of system memory.

@wjhun wjhun requested a review from a team June 24, 2023 20:39
Copy link
Member

@francescolavra francescolavra left a comment

Choose a reason for hiding this comment

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

As a heads up, with this change the tls klib cannot be used anymore in low-memory VMs, which means that such VMs won't be able to use the radar service, and won't be able to use HTTPS URLs to retrieve files or environment variables with the cloud_init klib.
Also, in low-memory VMs writes to network socket file descriptors will be limited to max 2KB (so the netsock and tun runtime tests will fail when run on a 64MB Qemu instance).

This comment no longer applies, since now we allow the use of arbitrary sizes for the malloc-style interface.

src/kernel/init.c Show resolved Hide resolved
src/runtime/heap/mcache.c Outdated Show resolved Hide resolved
src/net/net.c Outdated Show resolved Hide resolved
wjhun added 3 commits July 5, 2023 12:02
In an effort to minimize VM sizes, this change will cause runtime test
executables to be stripped, saving unstripped binaries under the ".dbg"
extension. The LDFLAGS and LIBS variables specified for each program are also
duplicated for the debug build.
This change reduces baseline memory requirements for various memory caches
under "low memory" conditions. If the function is_low_memory_machine() returns
true, then the following parameters are trimmed:

- the pagecache will use PAGECACHE_LOWMEM_CONTIGUOUS_PAGESIZE (default 128KB)
  as its objcache pagesize instead of PAGESIZE_2M
- the mcache high order is lowered to 11 (2KB), thus forcing PAGESIZE or
  larger allocations to be made directly from the backing heap - and thus not
  cached, nor available for situations that implement a malloc/free interface
- similiarly, allocations for lwIP are limited to a maximum size of 2KB
Given that there is limited queueing within the network stack, we can
reasonably predict that only up to virtqueue_entries(vn->rxq) rx buffers will
need to be cached - and similarly for tx handler completions. This adjusts the
objcache parent pagesizes to accommodate only these requirements, regardless
of whether the machine is in a 'low memory' configuration.
This modifies the mcache to allow using a malloc/free-style interface (where
the allocation size is not specified on a free()) when allocation sizes exceed
the largest objcache size. Such use would previously result in an error when
attempting to free such large allocations. To accomplish this, the mcache now
maintains a mapping of large allocation addresses to their corresponding
sizes. When a deallocate with an unspecified (-1ull) size occurs, a table
lookup/removal is made to check if the address corresponds to such a
"fallback" allocation and, if so, recover the allocation size.

The cost of using this fallback table is an insertion when making a
fallback (large) allocation and a table lookup/removal when deallocating a
fallback allocation of a known size, or on any deallocation (free) of an
unknown size. This should not impact the vast majority of mcache uses, for
which 1) the allocation size falls within the domain of the contained
objcaches and 2) the size parameter is valid on deallocation.
@francescolavra
Copy link
Member

LGTM

@wjhun wjhun merged commit 5e66599 into master Jul 6, 2023
@francescolavra francescolavra deleted the low-memory-enhancements branch December 5, 2023 14:59
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