Skip to content

runtime: ensure some headroom for the GC to run #2884

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

Merged
merged 1 commit into from
Sep 16, 2022
Merged

Conversation

aykevl
Copy link
Member

@aykevl aykevl commented May 31, 2022

The GC was originally designed for systems with a fixed amount of memory, like baremetal systems. Therefore, it just used what it could and ran a GC cycle when out of memory.

Other systems (like Linux or WebAssembly) are different. In those systems, it is possible to grow the amount of memory on demand. But the GC only actually grew the heap when it was really out of memory, not when it was getting very close to being out of memory.

This patch fixes this by ensuring there is at least 33% headroom for the GC. This means that programs can allocate around 50% more than what was live in the last GC cycle. It should fix a performance cliff when a program is almost, but not entirely, out of memory and the GC has to run almost every heap allocation. I believe I've seen this issue pop up from time to time (symptoms: very slow program that gets fast again with minor modifications and is stuck in GC somehow).

I did a code size comparison. On Linux/MacOS/Windows/WebAssembly, there is a small increase in size (around 50-200 bytes). On baremetal system, there is no change at all except for one oddball test (tinygo build -size short -o test.hex -target=arduino -scheduler=tasks examples/blinky1). Not sure why: the IR is almost identical except for one difference in the switch of the runtime.sweep function. I guess I'm going to blame the AVR backend and say it's too small of an issue to care about.

@deadprogram
Copy link
Member

@dkegel-fastly
Copy link
Contributor

FWIW in the case I ran into, there was still plenty of system RAM, so the benefit of this patch may have been caused by the code's slightly different strategy rather than the large amount of headroom it tried to maintain. So maybe

freeBytes < heapSize/100 

would do... though I have no idea whether it would prevent the OOM observed in the CI log.

@dgryski
Copy link
Member

dgryski commented Jun 7, 2022

Do we have any numbers to demonstrate an improvement here? (Not saying this isn't a good idea, just wondering if there was a particular test or case or equivalent we could look through the gc debug log and say "Yup, this is better now.")

@dkegel-fastly
Copy link
Contributor

If there is a particular log you want, I could get it on the affected machine...

@aykevl
Copy link
Member Author

aykevl commented Jun 10, 2022

FWIW in the case I ran into, there was still plenty of system RAM

Yes, that's the problem this patch tries to fix. It didn't grow the heap when the runtime still had heap space left in its allocated slice of system RAM, however small. It will run much more efficiently when it has a bit more headroom.

Do we have any numbers to demonstrate an improvement here? (Not saying this isn't a good idea, just wondering if there was a particular test or case or equivalent we could look through the gc debug log and say "Yup, this is better now.")

It's not very deterministic, which makes it hard to reproduce. But you could for example look at how often it allocates and how often it runs the GC (the number of allocs should be a lot bigger than the number of GC cycles).

https://github.com/tinygo-org/tinygo/runs/6680169891?check_suite_focus=true#step:19:51

panic: runtime error: out of memory

Yes... I need to look into that. I don't quite understand why it's running out of memory here (and whether this panic is even correct).

@aykevl
Copy link
Member Author

aykevl commented Jun 15, 2022

I tried the fix in #2909 but unfortunately it doesn't help.

Copy link
Member

@dgryski dgryski left a comment

Choose a reason for hiding this comment

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

LGTM

The GC was originally designed for systems with a fixed amount of
memory, like baremetal systems. Therefore, it just used what it could
and ran a GC cycle when out of memory.

Other systems (like Linux or WebAssembly) are different. In those
systems, it is possible to grow the amount of memory on demand. But the
GC only actually grew the heap when it was really out of memory, not
when it was getting very close to being out of memory.

This patch fixes this by ensuring there is at least 33% headroom for the
GC. This means that programs can allocate around 50% more than what was
live in the last GC cycle. It should fix a performance cliff when a
program is almost, but not entirely, out of memory and the GC has to run
almost every heap allocation.
@aykevl
Copy link
Member Author

aykevl commented Sep 15, 2022

I found a pretty stupid bug, it should be fixed now.

Copy link
Member

@dgryski dgryski left a comment

Choose a reason for hiding this comment

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

LGTM

@deadprogram
Copy link
Member

Thanks for the headroom @aykevl 🎈 and to @dgryski for reviewing. Merging!

@deadprogram deadprogram merged commit 91104b2 into dev Sep 16, 2022
@deadprogram deadprogram deleted the gc-headroom branch September 16, 2022 09:11
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.

4 participants