APM/main/esp32ai.c
// This example demonstrates how a human readable table of run time stats
// information is generated from raw data provided by uxTaskGetSystemState().
// The human readable table is written to pcWriteBuffer
This example demonstrates how a human readable table of run time stats
information is generated from raw data provided by uxTaskGetSystemState().
The human readable table is written to pcWriteBuffer
This example demonstrates how a human readable table of run time stats information is generated from raw data provided by uxTaskGetSystemState () . The human readable table is written to pcWriteBuffer
void local_vTaskGetRunTimeStats( char *pcWriteBuffer )
{
	TaskStatus_t *pxTaskStatusArray;
	volatile UBaseType_t uxArraySize, x;
	uint32_t ulTotalRunTime;	
	float fltStatsAsPercentage;		
	float fltRunTimeTicks;
	size_t heapMin, heapNow;
	uint32_t ulAccumCounter;
	*pcWriteBuffer = 0x00;
	uxArraySize = uxTaskGetNumberOfTasks();
	pxTaskStatusArray = pvPortMalloc( uxArraySize * sizeof( TaskStatus_t ) );
	if( pxTaskStatusArray != NULL )
	{
		uxArraySize = uxTaskGetSystemState( pxTaskStatusArray, uxArraySize, &ulTotalRunTime );
		fltRunTimeTicks = (float)(ulTotalRunTime)/1000000.0;
		sprintf( pcWriteBuffer, "\nSecurity Metrics:\n Runtime = %.1f\n", fltRunTimeTicks);
		pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
		ulTotalRunTime /= 100UL;
		if( ulTotalRunTime > 0 )
		{
			ulAccumCounter = 0;
			for( x = 0; x < uxArraySize; x++ )
			{
				#ifdef PRINT_TASK_STATS				
					fltStatsAsPercentage = (float)pxTaskStatusArray[ x ].ulRunTimeCounter / (float)ulTotalRunTime;		
					if( fltStatsAsPercentage > 0.1 )
					{
						sprintf( pcWriteBuffer, "%s\t\t%d\t\t%.2f%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter, fltStatsAsPercentage );
					}
					else
					{
					}
					pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
				#endif	
				if ((strcasecmp(pxTaskStatusArray[ x ].pcTaskName, "MainTask") == 0) ||
					(strcasecmp(pxTaskStatusArray[ x ].pcTaskName, "MqttTask") == 0) ||
					(strcasecmp(pxTaskStatusArray[ x ].pcTaskName, "wifi") == 0) ||
					(strcasecmp(pxTaskStatusArray[ x ].pcTaskName, "SoundTask") == 0) )
				{
					ulAccumCounter += pxTaskStatusArray[ x ].ulRunTimeCounter;
				}
			}
			fltStatsAsPercentage = (float)ulAccumCounter / (float)ulTotalRunTime;		
			sprintf( pcWriteBuffer, " CPU = %.2f\r\n", fltStatsAsPercentage );
			pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
		}	
	}
	vPortFree( pxTaskStatusArray );
	heapNow = xPortGetFreeHeapSize();
	heapMin = xPortGetMinimumEverFreeHeapSize();
	sprintf( pcWriteBuffer, " Heap ratio = %.5f\r\n", (float)(heapNow-heapMin)/(float)heapMin);		
	pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
	sprintf( pcWriteBuffer, " Processes = %d\n \n", uxArraySize );
	pcWriteBuffer += strlen( ( char * ) pcWriteBuffer );
}
void local_vTaskGetRunTimeStats ( char * pcWriteBuffer ) { TaskStatus_t * pxTaskStatusArray ; volatile UBaseType_t uxArraySize , x ; uint32_t ulTotalRunTime ; float fltStatsAsPercentage ; float fltRunTimeTicks ; size_t heapMin , heapNow ; uint32_t ulAccumCounter ; * pcWriteBuffer = 0x00 ; uxArraySize = uxTaskGetNumberOfTasks ( ) ; pxTaskStatusArray = pvPortMalloc ( uxArraySize * sizeof ( TaskStatus_t ) ) ; if ( pxTaskStatusArray != NULL ) { uxArraySize = uxTaskGetSystemState ( pxTaskStatusArray , uxArraySize , & ulTotalRunTime ) ; fltRunTimeTicks = ( float ) ( ulTotalRunTime ) / 1000000.0 ; sprintf ( pcWriteBuffer , " \n \n \n " , fltRunTimeTicks ) ; pcWriteBuffer += strlen ( ( char * ) pcWriteBuffer ) ; ulTotalRunTime /= 100UL ; if ( ulTotalRunTime > 0 ) { ulAccumCounter = 0 ; for ( x = 0 ; x < uxArraySize ; x ++ ) { #ifdef PRINT_TASK_STATS fltStatsAsPercentage = ( float ) pxTaskStatusArray [ x ] . ulRunTimeCounter / ( float ) ulTotalRunTime ; if ( fltStatsAsPercentage > 0.1 ) { sprintf ( pcWriteBuffer , " \t \t \t \t \r \n " , pxTaskStatusArray [ x ] . pcTaskName , pxTaskStatusArray [ x ] . ulRunTimeCounter , fltStatsAsPercentage ) ; } else { } pcWriteBuffer += strlen ( ( char * ) pcWriteBuffer ) ; #endif if ( ( strcasecmp ( pxTaskStatusArray [ x ] . pcTaskName , " " ) == 0 ) || ( strcasecmp ( pxTaskStatusArray [ x ] . pcTaskName , " " ) == 0 ) || ( strcasecmp ( pxTaskStatusArray [ x ] . pcTaskName , " " ) == 0 ) || ( strcasecmp ( pxTaskStatusArray [ x ] . pcTaskName , " " ) == 0 ) ) { ulAccumCounter += pxTaskStatusArray [ x ] . ulRunTimeCounter ; } } fltStatsAsPercentage = ( float ) ulAccumCounter / ( float ) ulTotalRunTime ; sprintf ( pcWriteBuffer , " \r \n " , fltStatsAsPercentage ) ; pcWriteBuffer += strlen ( ( char * ) pcWriteBuffer ) ; } } vPortFree ( pxTaskStatusArray ) ; heapNow = xPortGetFreeHeapSize ( ) ; heapMin = xPortGetMinimumEverFreeHeapSize ( ) ; sprintf ( pcWriteBuffer , " \r \n " , ( float ) ( heapNow - heapMin ) / ( float ) heapMin ) ; pcWriteBuffer += strlen ( ( char * ) pcWriteBuffer ) ; sprintf ( pcWriteBuffer , " \n \n " , uxArraySize ) ; pcWriteBuffer += strlen ( ( char * ) pcWriteBuffer ) ; }
This example demonstrates how a human readable table of run time stats
information is generated from raw data provided by uxTaskGetSystemState().
This example demonstrates how a human readable table of run time stats information is generated from raw data provided by uxTaskGetSystemState () .
//, ulStatsAsPercentage; // use a float to display fractional units // Make sure the write buffer does not contain a string. // Take a snapshot of the number of tasks in case it changes while this // function is executing. // Allocate a TaskStatus_t structure for each task.  An array could be // allocated statically at compile time. // Generate raw status information about each task. // For percentage calculations. // Avoid divide by zero errors. // For each populated position in the pxTaskStatusArray array, // format the raw data as human readable ASCII data // What percentage of the total run time has the task used? // This will always be rounded down to the nearest integer. // ulTotalRunTimeDiv100 has already been divided by 100. // see in fractional units // If the percentage is zero here then the task has // consumed less than 1% of the total run time, so don't bother outputting it //sprintf( pcWriteBuffer, "%s\t\t%d\t\t<1%%\r\n", pxTaskStatusArray[ x ].pcTaskName, pxTaskStatusArray[ x ].ulRunTimeCounter ); // now, let's just calculate the needed metrics // see in fractional units // The array is no longer needed, free the memory it consumes. //---------------------------------- // current value is X percent higher than minimum value //----------------------------------