Can one find out the stack depth from xTaskCreate

mikemoy
Posts: 606
Joined: Fri Jan 12, 2018 9:10 pm

Can one find out the stack depth from xTaskCreate

Postby mikemoy » Sun Mar 17, 2019 5:29 pm

Say a task was created with the following.
xTaskCreate(idleloop, "idleloop", 4096, NULL, 1, &TaskHandle_IdleLoop);

Is there a way within another function to find out what the stack depth (I.E. the 4096) was set to for this task?

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Can one find out the stack depth from xTaskCreate

Postby ESP_Dazz » Mon Mar 18, 2019 1:37 pm

Since the stack depth doesn't change throughout the task's lifetime, you could consider passing the stack depth as a task parameter such as the following:

Code: Select all

xTaskCreate(idleloop, "idleloop", 4096, (void *)4096, 1, &TaskHandle_IdleLoop); 

...

void idleloop(void *arg)
{
    int stack_depth = (int) arg;
}

mikemoy
Posts: 606
Joined: Fri Jan 12, 2018 9:10 pm

Re: Can one find out the stack depth from xTaskCreate

Postby mikemoy » Mon Mar 18, 2019 1:53 pm

Thanks for the suggestion. I probably should elaborate what I am trying to do.
I have created a Thread that simply goes through all my registered tasks and prints the high water mark in a percentage.
So durring run time I can see how close I am getting to the edge if you will.

To do this I need to pass the task handle and its stack allotment. I'd like to avoid passing the stack allotment size to me "GetTaskHighWaterMarkPercent" function, and have it somehow find out what that task was created with.

Code: Select all

UBaseType_t GetTaskHighWaterMark( TaskHandle_t task_handle )
{
  UBaseType_t uxHighWaterMark;
  uxHighWaterMark = uxTaskGetStackHighWaterMark( task_handle );
  return uxHighWaterMark;
}
/* -----------------------------------------------------------------------------
  GetTaskHighWaterMarkPercent( TaskHandle_t task_handle, uint32_t stack_allotment )

 	Input Params:
    - task_handle: The task name you want to examine
   	- stack_allotment:  How much stack space did you allocate to it when you created it

  Returns: float with the % of stacke used
  Example:   printf("Stack Used %04.1f%%\r\n", GetTaskHighWaterMarkPercent(xTask1, 2048) );
  Notes:
 -----------------------------------------------------------------------------*/
float GetTaskHighWaterMarkPercent( TaskHandle_t task_handle, uint32_t stack_allotment )
{
  UBaseType_t uxHighWaterMark;
  uint32_t diff;
  float result;

  uxHighWaterMark = uxTaskGetStackHighWaterMark( task_handle );

  diff = stack_allotment - uxHighWaterMark;

  result = ( (float)diff / (float)stack_allotment ) * 100.0;

  return result;
}

// Spin up the Task
xTaskCreate(heartBeatTask, "heartbeat_task", 1024, NULL, 1, &TaskHandle_heartBeatTask); 

// Where is the Stack at ?
float temp1 = GetTaskHighWaterMarkPercent(TaskHandle_heartBeatTask, 1024);

printf(" %04.1f%%\r\n",temp1);

ESP_Dazz
Posts: 308
Joined: Fri Jun 02, 2017 6:50 am

Re: Can one find out the stack depth from xTaskCreate

Postby ESP_Dazz » Wed Mar 20, 2019 3:42 pm

That might be difficult as the TCB structure in FreeRTOS only stores a pointer to the start of the stack and doesn't actually store the size of the stack. I guess you could try using Thread Local Storage Pointers and set pvValue as the stack size upon task creation. Note that you'll need to enable Thread Local Storage Pointer functionality for FreeRTOS via menuconfig.

Who is online

Users browsing this forum: Vineethad and 210 guests