Repository navigation
control where a heap object is allocated - #11764
lynt-smitka wants to merge 19 commits into
Conversation
The framebuffer length and the DMA command count move out of the constructor into a helper, and picodvi_framebuffer_internal_ram() returns the bytes a display of a given size takes. No change in behaviour.
Since every supervisor allocation starts in internal RAM (adafruit#11176), the USB descriptors, the pystack and the heap's segments land right above the display's buffers at boot, and after release_displays() the heap grows into the freed space. A program that builds the display later then has no room for a 640x480 framebuffer. On boards with PSRAM, picodvi now allocates the buffers of the display it would build at boot even when it does not build it, and a Framebuffer that fits in them uses them instead of allocating. Fixes adafruit#11641.
|
Thank you @lynt-smitka - Tested on two Fruit Jams with a FruitJam() call after boot, 3 runs each.
With a 1080p monitor the boot mode is 720x400x8, so about 296 KB is kept, but 640x480x8 needs about 316 KB. Fruit Jam OS screensavers also change display size at runtime, so a buffer sized to the boot mode can be too small later. Would it work to keep the largest supported mode instead, using That is 320x240x32 at about 325 KB, close to 640x480x8. |
dhalbert
left a comment
There was a problem hiding this comment.
Doesn't this use up heap even when it is never used? I wonder if #11176 should be re-thought, though I'm not sure how to deal with the tradeoff. It requires some clairvoyance that we don't have about what the program wants to do.
|
I think you're right. Holding 309 KB of the 520 KB of RAM for the whole session, even if no display is ever made makes no sense. A softer option might be to leave the memory free and stop the GC from growing into it. On boards with DVI, a new GC heap chunk would go to internal RAM only if a framebuffer-sized hole still remains afterward, otherwise to PSRAM. Python still gets internal RAM first up to that point, and audio or other DMA users can still use the space. The easy option is PSRAM-first again on the DVI boards, but that undoes #11176 on the Fruit Jam. |
|
@dhalbert @mikeysklar I would not go back to the heap in PSRAM: the speed-up from #11176 is large. On a Fruit Jam, memcpy of 16 KB goes from 6 to 109 MB/s, a 38 KB RGB565 fill from 4.2 ms to 0.23 ms, gc.collect() from 30 ms to 8 ms, GIF playback from 24 to 56 fps. When we say the largest mode "takes the heap", it only takes the fast part of the heap in internal RAM, which did not exist before #11176 at all. The heap keeps its size - when internal RAM runs out, allocations spillover to PSRAM as they do now. But holding the largest mode leaves only about 29 KB of fast RAM on a Fruit Jam, and none of the workloads above fit in that, so it would undo most of the gain 😥. That is why I'd propose "heuristic" based on what the firmware already knows at boot:
This doesn't cover a monitor without EDID or one plugged in after boot: there the firmware sees no display, and a program building one after the heap has grown gets the MemoryError it gets today. Setting What do you think? |
|
One change, but I like your suggestions. In case 1, reserve the largest mode too. Fruit Jam OS screensavers switch modes, so reserving only the declared size can still fail. Then Fruit Jam OS can set CIRCUITPY_DISPLAY_WIDTH in its settings.toml and cover the no-monitor-at-boot case. Also, the fake deep sleep wake path calls board_init() again, which leaks the first _boot_ram block. An |
|
@mikeysklar I don't like the case 1 - an app with a 320x240 display is then left with about 29 KB of fast RAM and no way to get more, and the declared size was the one way to say "this is my display". If we go with the largest mode there, I'd add a call to give the RAM back, something like Thanks for the deep sleep guard. |
|
That is fair. I'll drop my case 1 change and keep it as the declared size. Fruit Jam OS can get the largest mode from case 2 instead. It runs at 720x400 but its screensavers switch to 640x480, so it could ship That covers no monitor at boot without a new API, so I'd skip |
With a 1080p display the boot mode is 720x400, 20 KB less than 640x480x8 needs, and Fruit Jam OS switches modes at run time. A display configured with CIRCUITPY_DISPLAY_WIDTH keeps its own size, so the rest of internal RAM stays with the heap. A display built at boot without one gets the largest mode, 320x240 at 32 bits, so the program can switch modes later. With neither, nothing is held.
board_init() runs again when waking from a fake deep sleep, which would allocate a second block and leak the first.
|
Tested your latest commits. Tested b675b48 on a Fruit Jam with a 1080p monitor. Heap grown first, then request_display_config at 720x400, 640x480, 320x240, 640x480, 3 runs each.
The heap gets 241 KB of internal RAM when nothing is reserved. |
|
@mikeysklar I ran some similar tests as well, and everything seemed OK to me. But I'd still like to think more about other possible edge cases and tradeoffs. These aren't easy decisions 🙂. |
|
Why is the display being init so late in this case? Shouldn't it be done in I'm not a huge fan of adding this PR because it adds display specific memory functionality. I'd lean towards what Dan said by just putting the heap back in PSRAM by default. This PR is additional complexity. The reality is that the RP2350 doesn't have enough fast RAM to do everything quickly. Another place to look though may be GC sweep's order of operations to improve cache hits in front of the PSRAM. For example, single reads could be done around the cache in order to preserve metadata in the cache. |
The display's buffers are allocated when the display is built, as before this branch. The four commits that kept them from boot are undone.
Since adafruit#11176 every allocation started in internal RAM, so the heap grew into the space a DMA buffer needs later. A picodvi framebuffer built after start-up then failed with MemoryError while PSRAM was free. Allocations that don't need DMA go to PSRAM first again, as in 10.2.1. Fixes adafruit#11641.
With the heap back in PSRAM, a program can ask for internal RAM again once its display and audio exist. While the property is set, new chunks of the VM heap are taken from DMA-capable memory when it has room. When the chunk asked for does not fit there, the largest one that does is added next to the usual one, so that it is used up first. The property is cleared when the next program starts. Not built where all memory is DMA-capable; the attribute is a read-only False there.
As a constant the attribute cost 40 to 52 bytes on every board, and qtpy_m0_haxpress and trinket_m0_haxpress overflowed by 8 bytes in de_DE.
It was tied to CIRCUITPY_ALL_MEMORY_DMA_CAPABLE. That also built it for ESP32-P4 and ESP32-S31, where it is untested, and left out ESP32-S3 boards with PSRAM. CIRCUITPY_FAST_HEAP lets a port turn it on once it has been tried there. The docs now say that not every board has the property and that it changes nothing without PSRAM.
|
After a long think I agree: going back to the 10.2.1 behaviour is the best thing to do... I was too biased by my own use case with picogame at 320x240. I pushed new commits. The first two take the boot buffers out again and restore the 10.2.1 allocation order, so the heap is in PSRAM by default and #11641 is fixed. Instead of doing it automatically, I'd let the developer say "I know what I'm doing" and ask for the fast heap in the program. That is in the last two commits: import supervisor
# after the display and audio are set up
supervisor.runtime.fast_heap = True
I plan to set it in the picogame libs, right after they set up the display, so games get it without extra code. A 320x240x16 display on a Fruit Jam leaves about 100 KB of internal RAM for the heap. Without a display it is about 250 KB. These are the timings with the display:
In those builds it costs about 330 bytes of flash. I'm currently exploring the options on the ESP32. I'll probably add some more commits soon. |
mikeysklar
left a comment
There was a problem hiding this comment.
I like this compromise.
PSRAM by default keeps things simple and is what Dan and Scott asked for, and fast_heap gives developers a way into fast memory early.
Tested on a Fruit Jam: #11641 fixed
- fast_heap copies 16 KB at 110 MB/s instead of 6.
|
@lynt-smitka This is interesting. The "fast heap" flag could be in |
|
@dhalbert I think that it's better to do this "per app" in the code than "per board" in settings.toml. But ask me again in five minutes 🙂. |
|
My preference would be settings.toml too because then the behavior is fixed for the entire lifetime of the VM. Dynamic is more complicated. Speaking of complicated, maybe the best thing would be to promote (aka cache) certain buffers to internal memory automatically. Certain uses could trigger a "hot memory" path that moves them internally. |
|
If we had a relocating garbage collector that would be great, but that is not happening without a total re-architecture. |
|
There is also this: https://peps.python.org/pep-0723/ It stores metadata in the python source file itself. |
So we could put some (not all) This is a manifestation of a general problem of how to deal with multiple kinds of storage in the Python environment. I was wondering if a decorator could be used in some way here. Maybe the allocation or the constructor could be in its own routine with a special decorator. Not sure if that's better, but it makes it more obvious which code is doing allocation in a special way. |
That's an interesting idea! We could do a context manager sort of thing that could then be a decorator too. So: This is a bit similar to |
|
I like the context manager thing a lot: it scopes the code clearly. |
|
I like it too. Trying to make a prototype. |
A context manager. Buffers of 1 kB and more that are allocated inside its block are placed in internal RAM, in heap areas of their own, or raise MemoryError. Everything else stays where the heap is. On for RP2350. Each buffer gets an area of its size, so the area can go back to the port as soon as that buffer is collected. The heap adds up to 32 kB of them between two collections without collecting first.
The IDF takes internal RAM at any time, for Wi-Fi and BLE. The heap asks the port for the internal RAM of its areas, and Espressif refuses when less than 64 kB would be left.
|
I pushed a prototype of with
Frames per second on a Fruit Jam:
The two do different things. The block moves the buffers you make in it and nothing else, and it gives the RAM back when they are collected (I call this variant "strict"). I also tried a third variant: a block that does both. Buffers get their place in internal RAM, the heap grows there while the block is open, and when internal RAM is full it continues in PSRAM instead of raising (I call it "best effort"). In the games it is as fast as Perhaps the best option would be to use both |
I wonder if this would actually make more sense on the
Why do you have a minimum size? It seems like it could be useful to wrap a tight Python loop that allocates many small objects too. The minimum could be set dynamically by the context manager too. I'd love to see your test code. I care more about how it looks in use really. |
|
I have changed the title of the PR because it moved a lot from the original purpose. Re location of this functionality:
|
The minimum is there because of fragmentation. Small objects made in the block stay in internal RAM after it, and on a Fruit Jam a 640x480 display then no longer fit. With the minimum it does. Without it three of the five games also ran out of internal RAM during import. 1 kB was one of first values I tried and it worked well and nicely rounded, so I kept it. Setting it from the context manager sounds good. I will put the tests on the gist. |
Generally the display should always be allocated early to avoid this. I think Overall, I think we're approaching the point where we're exposing too many internals and folks who want that allocation control may need a different system. So, I'd say let's remove the minimum and make it work with everything going to the "active" region. |
|
@tannewt Ok, less magic then :-) And what to do when internal RAM is full? Should the block raise MemoryError or continue in PSRAM? What do you think is better behavior? |
I'd raise. The block's intent is to allocate memory internally. Folks can catch the exception to fall back if they really want to. Otherwise they may want to change the contents of the block itself. |
The block had a minimum size of 1 kB. Now everything allocated in it goes to internal RAM, and it raises MemoryError when internal RAM has no room. Small objects share heap areas of 16 kB. A buffer of 1 kB or more still gets an area of its own, so that it goes back to the port when the buffer is freed. Allocations in the block remember the last area with free blocks, like ordinary allocations do.
A program that wants its data in internal RAM can run in a microcontroller.internal_memory block.
|
@tannewt I pushed the rework: no minimum, everything allocated in the block goes to internal RAM, and the block raises MemoryError when internal RAM is full. Also removed supervisor.runtime.fast_heap because the block does the same. Fruit Jam, whole game inside the block:
The firmware for the Fruit Jam is 584 bytes larger than main. On the Feather ESP32-S3 TFT the block leaves 68 kB of internal RAM free. That is enough for Wi-Fi and TLS. BLE has to be enabled before a block that fills up, afterwards there is not enough left for it. |
tannewt
left a comment
There was a problem hiding this comment.
A few other comments on how it works. This is core to MP/CP so let's make sure it's what we want.
An allocation in the block that does not fit now collects first and adds an area in internal RAM only after that, like the heap does. The areas in internal RAM are no longer kept for the block. Any allocation can use their free blocks. The block still takes only areas that are in internal RAM, which it tells by their address.
There is a collection before every area the block adds, and a collection looks up the area of every pointer it follows. The areas that small objects share now double in size within a block, from 16 kB up to 64 kB, and halve when internal RAM has no room for one. A buffer gets an area of its own from 4 kB, not from 1 kB.
tannewt
left a comment
There was a problem hiding this comment.
Thanks for the iteration. A few more things. I think we're getting close.
| // A buffer of this size or more gets an area of its own size. An area goes back to the port | ||
| // only when it is empty, so a shared one would stay for the last buffer in it. | ||
| #define GC_INTERNAL_BUFFER_BYTES (4096) |
There was a problem hiding this comment.
Why when the minimum shared size is 4x this?
There was a problem hiding this comment.
It best compromise form the measurements. I tried multiple limits on the Fruit Jam.
- With 16 kB, I made 30 buffers of 4 kB in the block and freed them again. A 640x480 display still did not fit in internal RAM afterwards, because other objects had moved into the areas the buffers had used.
- With 1 kB there were so many areas that a collection took 11 ms instead of 5 ms.
- 4 kB passed both.
| // CIRCUITPY-CHANGE | ||
| #if MICROPY_GC_INTERNAL_AREAS | ||
| if (MP_STATE_MEM(gc_last_free_internal_area) != area) { | ||
| MP_STATE_MEM(gc_last_free_internal_area) = &MP_STATE_MEM(area); |
There was a problem hiding this comment.
Don't you only want to do this if the freed memory was allocated internally?
| // CIRCUITPY-CHANGE | ||
| #if MICROPY_GC_INTERNAL_AREAS | ||
| if (MP_STATE_MEM(gc_last_free_internal_area) != area) { | ||
| MP_STATE_MEM(gc_last_free_internal_area) = &MP_STATE_MEM(area); |
Fixes #11641.
Since #11176 supervisor allocations start in internal RAM, so the heap and the USB descriptors end up around and inside the space a display needs. A program that builds the display later, as
adafruit_fruitjamdoes, then has no contiguous 307200 bytes for 640x480x8.On boards with PSRAM, picodvi now allocates the buffers of the display it would build at boot (the settings.toml size, or 640x480x8) even when it does not build it. A Framebuffer that fits uses them; a bigger one allocates its own.
CIRCUITPY_PICODVI_ENABLE="never"allocates nothing, and boards without PSRAM are unchanged.Tested on a Fruit Jam: 640x480x8 is built fine after 256 KB of heap growth, with or without a display at boot, and after a 320x240x16 display in between.