Memory optimization

lg.lindstrom
Posts: 48
Joined: Fri Sep 17, 2021 4:02 pm

Memory optimization

Postby lg.lindstrom » Wed Feb 01, 2023 6:26 pm

Hi
I am getting low on IRAM. Optimization reduced the usage but still on the edge. I am using a lot of components from ESP-IDF but not everything at the same time.

Below is a "functional description of my architecture.

Code: Select all

main:
	state = get_app_state_to_run;
	switch(state) {
		case A: runA  (uses I2C).
		case B: runB  (uses Nimble BLE, I2C )
		case C: runC (uses WIFI,ESP-MQTT, NTP, I2C)
	}
	
	if ( no_state_change) 
		just_wait;
	else
		save_new_state
		restart_from_main.
I assume that the static analyze of IRAM usage summarize it all, i.e I2C+Nimble BLE+WIFI+ESP-MQTT+NTP.

This will never happen so the indicated IRAM usage is much to high.

Before the optimization I was using to much IRAM so I could not flash my device.
As I describer the actual usage is lower then what the static analyze shows.

Is there a way to disable this IRAM check so I can load my code even if the analyze says I use to much.

MicroController
Posts: 1688
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Memory optimization

Postby MicroController » Thu Feb 09, 2023 5:37 pm

Hi.

If by "IRAM" you are actually referring to "Instruction RAM" (not generally "internal RAM"), I believe you're out of luck.
AFAICT, whether a function/ISR is located in IRAM or not is statically defined at build-time, IRAM space is statically allocated to pieces of code and that allocation cannot change during runtime. There is no way to "unload" such a function/ISR from IRAM and load in another one in its place at runtime.
It should be possible to "manually" copy code from flash to IRAM as&when needed, but that would open another can of worms with regard to building/linking and relocation, not to forget the necessary memory management. Obviously, the former issue is solvable somehow as proven by the existence of IRAM functions...

MicroController
Posts: 1688
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Memory optimization

Postby MicroController » Sun Feb 12, 2023 11:45 am

It seems that using OVERLAYs in the liker script would take care of the build-time stuff (see https://ftp.gnu.org/old-gnu/Manuals/ld- ... ld_22.html), allowing you to build/link multiple sections which expect to be all executed from the same address at runtime. With that, you'd "only" have to copy the desired section from flash to IRAM when needed and make sure that everybody knows which function is currently loaded, or rather, which one is not, so as to not call the wrong code.

Would be interesting to explore, but "hacking" the IDF to join that game is probably not worth it.

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: Memory optimization

Postby ESP_Sprite » Sun Feb 12, 2023 2:16 pm

Note that stuff is generally in IRAM because at the time it needs to be called, the flash memory may be unavailable... so using overlays wouldn't work either in that case.

MicroController
Posts: 1688
Joined: Mon Oct 17, 2022 7:38 pm
Location: Europe, Germany

Re: Memory optimization

Postby MicroController » Thu Feb 16, 2023 3:54 pm

Not sure if many people are aware of that, i.e. the point that they should only select code for IRAM which needs to be able to run during write accesses to flash (mainly OTA or nvs, if I'm not mistaken). I know it's documented, but maybe not as clearly as it could be.

@ESP_Sprite In your experience, is reduced ISR latency/jitter another valid reason to go to IRAM, or does it not really make a difference?

ESP_Sprite
Posts: 9708
Joined: Thu Nov 26, 2015 4:08 am

Re: Memory optimization

Postby ESP_Sprite » Fri Feb 17, 2023 6:30 am

MicroController wrote:
Thu Feb 16, 2023 3:54 pm
@ESP_Sprite In your experience, is reduced ISR latency/jitter another valid reason to go to IRAM, or does it not really make a difference?
Yes, it's also a reason some people put stuff in IRAM: if not there, it has to come from flash via the cache and if it's a relatively lightly used function, it may be pushed out of cache which means it needs to be loaded from flash again (slower) when used. Putting the routine in IRAM stops that from happening. However, functions for which this is done tend to be pretty small.

Who is online

Users browsing this forum: No registered users and 55 guests