Reducing Stack size

paulgigliotti wrote on Sunday, March 05, 2017:

I am aware of tools for seeing how much stack is used by task. I am looking for some general guidelines on methods to reduce stack requirements. I am less concerned with the stack size, than by the time it takes to push data on to the stack upon a context switch. I have several tasks that wake up on a QueueRecieve. In these tasks, I have declared a buffer, into which the QueueReceive copies the data. The task processes the data, at at the end of the loop, returns to the QueueReceive, where it will become blocked, and wait for the next packet. What I am wondering is will the contents of the buffer (just processed) be pushed on to the stack. I no longer need this data, and moving it to the stack would be a waste of processor band width.
If the data does not get stored, then fine. If not, would it be more advantages to create the buffer outside the process, and pass the address of the buffer into the task as a paramater. Would this then prevent the contents of the buffer from being pushed onto the stack?

richarddamon wrote on Sunday, March 05, 2017:

The time to do a context switch is NOT dependent on the amount of data that is on the stack. Each task has its own stack space, that remains were it is, and the context switch basically saves the register set, saves the value of the Stack Pointer into the tasls TCB, and then loads the stack pointer for the new task, and restores the registar set and resumes execution.

paulgigliotti wrote on Thursday, May 11, 2017:

Thanks for that. Ok, independant of context switch, what are things to consider to reduce stack size. We are seeing some stack overflow issues that we are chasing down and looking for ways to reduce the amount of stack space required.

rtel wrote on Friday, May 12, 2017:

The stack usage is really nothing to do with the RTOS.

If you take a non-RTOS program and execute it then the amount of stack
it uses is dependent on the function call depth, the compiler used, the
compiler optimisation, and the local variables you declare. If you then
take the same program and run it under the RTOS the stack usage will be
very approximately 70 words larger. The small number of additional
words are used to hold the task’s context when the task is not running.

If you are using GCC then, with the right command line option (which I
forget but are easy to look up), you will get a file that tells you the
stack usage of every function as part of the build output.

You can also use uxTaskGetStackHighWaterMark() to query stack usage.