Jump to content

Potential Uinentended Stack Size on FreeRTOS Platforms


Recommended Posts

Posted (edited)

The xTaskCreate function is called in ugfx from the GOS layer when creating a thread. This function will be called to create the GTIMER thread with a default intended stack size of 2048 bytes. However, the usStackDepth as defined by FreeRTOS is:

// usStackDepth    - the stack size DEFINED IN WORDS.

Which will be dependent on the platform and port base type size. On my current 32 bit Cortex-M0+ platform, passing a stack size of 2048 will actually allocate 8192 bytes on the FreeRTOS heap! I was wondering why ugfx was gobbling up so much memory in this thread. I have reduced this by defining the GTIMER_THREAD_WORKAREA_SIZE symbol as 512, however, just something to keep in mind as I'm sure this wasn't intended.

Edited by kengineer
grammar corrections
Posted

I suggest that in the gfxCreateThread function we should divide by sizeof (WORD) before passing it to xCreateThread. That will keep the meaning of the stack size passed to gfxCreateThread consistent accross platforms.

If we do so, is WORD a defined FreeRTOS type or should we use sizeof (int) or something else?

Thanks for finding this!

 

Posted

Unfortunately it seems like FreeRTOS doesn't provide a way to query/retrieve the word size - but I might be wrong. Using sizeof(int) appears "wrong" to me because it's purely the choice of the compiler how wide an int is. Using the same processor/target with two different compilers might yield two different sizeof(int) values. I agree that most compilers probably show consistent behavior here but we currently have a list of 74 compilers that are supported by µGFX (and a subset which is actively tested). Therefore, having a solution that works with just most compilers seems inadequate to me. Unless of course FreeRTOS uses sizeof(int) itself internally to determine the word size. Does anybody know?

Thank you for bringing this to our attention. Much appreciated!

Posted (edited)

I believe it may be defined by the FreeRTOS type StackType_t which is defined in the individual port of FreeRTOS. Looks like they get sizeof and multiply it by the stack depth passed when they malloc tasks for creation. This is defined is the tasks.c which is the generic (not platform specific) set of functions:

 

//(tasks.c prvAllocateTCBAndStack function)

/* Allocate space for the stack used by the task being created.
		The base of the stack memory stored in the TCB so the task can
		be deleted later if required. */
		pxNewTCB->pxStack = ( StackType_t * ) pvPortMallocAligned( ( ( ( size_t ) usStackDepth ) * sizeof( StackType_t ) ), puxStackBuffer );

 

In the port for my platform, this is defined and typedef'd as follows:

#define portSTACK_TYPE	uint32_t

typedef portSTACK_TYPE StackType_t;

 

Since each FreeRTOS port adjusts this type for the destination platform/compiler, I would think that using sizeof(StackType_t) would be sufficient.

Edited by kengineer
Added more details
Posted

This has now been updated in the master repository.

As I don't use FreeRTOS myself can you please check that I got it right?

Thanks for finding this and your assistance in solving it.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...