Allocated vs Free ??

ChibiOS public support forum for all topics not covered by a specific support forum.

Moderators: RoccoMarco, lbednarz, utzig, tfAteba, barthess

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Allocated vs Free ??

Postby Tabulous » Thu Dec 09, 2021 3:57 pm

Anyone know why this would happen ?

some threads are showing more free memory than they where allocated ?

i,e FTP allocated 768 bytes for thread
static THD_WORKING_AREA(FTPWA, 768);


below it shows free 776 ????
[ addr ] [stack ] [free] [pr] [rf] [ name ] [ state ] [ time ] [ % ]
20001600 200006F4 472 64 1 main SLEEPING 284 0.14
200016A8 20001764 148 1 1 idle READY 198813 98.10
20015318 20015524 384 66 1 wdgd SLEEPING 459 0.22
20010DA8 2001100C 572 4 1 tstd SLEEPING 0 0.00
2000F2C0 2000F4D4 328 64 1 clkd WTSEM 24 0.01
20011580 20011894 648 64 1 eepd SLEEPING 1 0.00
2000F618 2000F804 356 3 1 alsd SLEEPING 2 0.00
20015030 200151D4 280 3 1 ledd READY 64 0.03
200019F8 20001B64 184 2 1 usbd READY 34 0.01
2000F9A8 2000FB44 296 64 1 psud SLEEPING 0 0.00
20010038 2001025C 288 67 1 imud WTSEM 1626 0.80
2001BBF8 2001BF4C 812 64 1 timd SLEEPING 0 0.00
2001E270 2001E5C4 440 64 1 osdd SLEEPING 67 0.03
2000FC60 2000FE04 380 64 1 bled SLEEPING 0 0.00
2001CC88 2001CF2C 636 65 1 dbh1 WTSEM 0 0.00
2001C758 2001C9FC 636 65 1 dbh2 WTSEM 0 0.00
2001E768 2001EA04 628 64 1 owbd SLEEPING 0 0.00
2001F5D8 2001F764 280 3 1 fota SLEEPING 1 0.00
200148F8 20014A94 372 64 1 exti SLEEPING 0 0.00
20014C08 20014EB4 644 65 1 trip WTSEM 0 0.00
20003968 20003CF4 360 64 1 gnsd WTANDEVT 1151 0.56
20012728 2001293C 392 63 1 smsd SLEEPING 3 0.00
20016750 20016AE4 776 64 1 ftpd SLEEPING 5 0.00
20014508 2001479C 520 64 1 gprs SLEEPING 3 0.00
2000EEB8 2000F16C 408 64 1 athl WTANDEVT 15 0.00
2000E6E8 2000E8E4 304 65 1 modm SLEEPING 5 0.00
200106A0 20010A04 256 64 1 evtd WTSEM 84 0.04
2000E180 2000E31C 232 3 1 cons SLEEPING 4 0.00
2001F970 2001FB54 348 3 1 shel CURRENT 199 0.09

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Allocated vs Free ??

Postby Giovanni » Thu Dec 09, 2021 7:37 pm

Hi,

That 768 is the guaranteed minimum available to the thread, the actual area size is larger because it stores space for interrupts, various structures and context switch data. Look at the THD_WORKING_AREA macro implementation, it is quite complex, it also varies based on selected options.

Giovanni

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Re: Allocated vs Free ??

Postby Tabulous » Fri Dec 10, 2021 11:21 am

Thanks for your response, makes sense.

So to optimize what is needed for the THD_WORKING_AREA() declarations, what would you recommend ?

Basically, i want to pullback as much unused memory as i can.

I see CH_DBG_FILL_THREADS fills the thread stack with CH_DBG_STACK_FILL_VALUE.
If i use this and then exercise all the threads to the maximum, this should show what is not been used ?

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Allocated vs Free ??

Postby Giovanni » Fri Dec 10, 2021 12:06 pm

Hi,

The filler is useful for determining how much space has been effectively used so you can optimize usage. Another way is to enable stack checking WITH MPU, that makes sure that any overflow is immediately detected if you "optimized" too much.

Note that the stack usage varies with compiler version and compiler options, so do that only at the very end of your development cycle.

Giovanni

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Re: Allocated vs Free ??

Postby Tabulous » Mon Dec 13, 2021 12:52 pm

Pre cursor this is an old version of Chibios(3.1.5)

So if you declare :
static THD_WORKING_AREA(FTPWA, 768);

This would mean 768bytes of user area + all the OS thread overhead(ctx etc).

So to find out how much space is needed out of the 768, what would you suggest as its not really clear to me.

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Allocated vs Free ??

Postby Giovanni » Mon Dec 13, 2021 3:58 pm

Hi,

By filling the area you can inspect it using the debugger and understand how much of it is unused. Extra areas are placed on top so start looking from the base address.

Giovanni

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Re: Allocated vs Free ??

Postby Tabulous » Mon Dec 13, 2021 4:50 pm

What i'm wanting to do is be able to display via the console/shell at run time.

what i'm finding difficult is, when a thread is created, the amount of user space allocated does not seem to be stored, thus can one calculate ?

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Allocated vs Free ??

Postby Giovanni » Mon Dec 13, 2021 5:19 pm

You can calculate at runtime exactly like you would do manually, start scanning from thread base.

Somebody proposed some code on the forum, it could be worth a search.

Giovanni

Tabulous
Posts: 509
Joined: Fri May 03, 2013 12:02 pm
Has thanked: 7 times
Been thanked: 17 times

Re: Allocated vs Free ??

Postby Tabulous » Mon Dec 13, 2021 5:27 pm

what varible would be threadbase ?

p_ctx.r13 ?
p_stklimit ?

and where would you know the size of what you have declared in static THD_WORKING_AREA(FTPWA, 768) ?

User avatar
Giovanni
Site Admin
Posts: 14457
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1076 times
Been thanked: 922 times
Contact:

Re: Allocated vs Free ??

Postby Giovanni » Mon Dec 13, 2021 5:29 pm

Tabulous wrote:what varible would be threadbase ?

p_ctx.r13 <- top
p_stklimit <- base

and where would you know the size of what you have declared in static THD_WORKING_AREA(FTPWA, 768) ?


Note that for the current thread there is no "top" because it is running, do that only for non-running threads.

Giovanni


Return to “General Support”

Who is online

Users browsing this forum: No registered users and 46 guests