@@ -193,4 +193,69 @@ uint32_t PIOS_TASK_MONITOR_GetZeroLoadTicksCount()
193193 return zeroLoadIdleCounterPerSec ;
194194}
195195
196+ /*
197+ * print a human readable table of run time stats
198+ * information is generated from raw data provided by uxTaskGetSystemState().
199+ * The human readable table is written to pcWriteBuffer. (see the vTaskList()
200+ * API function which actually does just this).
201+ */
202+ void PIOS_TASK_MONITOR_GetRunTimeStats (/*signed*/ char * pcWriteBuffer ) {
203+ TaskStatus_t * pxTaskStatusArray ;
204+ volatile UBaseType_t uxArraySize , x ;
205+ unsigned long ulTotalRunTime , ulStatsAsPercentage ;
206+
207+ /* Make sure the write buffer does not contain a string. */
208+ * pcWriteBuffer = 0x00 ;
209+
210+ /* Take a snapshot of the number of tasks in case it changes while this
211+ function is executing. */
212+ uxArraySize = uxTaskGetNumberOfTasks ();
213+
214+ /* Allocate a TaskStatus_t structure for each task. An array could be
215+ allocated statically at compile time. */
216+ pxTaskStatusArray = pvPortMalloc ( uxArraySize * sizeof ( TaskStatus_t ) );
217+
218+ if ( pxTaskStatusArray != NULL ) {
219+ /* Generate raw status information about each task. */
220+ uxArraySize = uxTaskGetSystemState ( pxTaskStatusArray ,
221+ uxArraySize ,
222+ & ulTotalRunTime );
223+
224+ /* For percentage calculations. */
225+ ulTotalRunTime /= 100UL ;
226+
227+ /* Avoid divide by zero errors. */
228+ if ( ulTotalRunTime > 0 ) {
229+ /* For each populated position in the pxTaskStatusArray array,
230+ format the raw data as human readable ASCII data. */
231+ for ( x = 0 ; x < uxArraySize ; x ++ ) {
232+ /* What percentage of the total run time has the task used?
233+ This will always be rounded down to the nearest integer.
234+ ulTotalRunTimeDiv100 has already been divided by 100. */
235+ ulStatsAsPercentage =
236+ pxTaskStatusArray [ x ].ulRunTimeCounter / ulTotalRunTime ;
237+
238+ if ( ulStatsAsPercentage > 0UL ) {
239+ sprintf ( pcWriteBuffer , "%s:%d,pct%d \n" ,
240+ pxTaskStatusArray [ x ].pcTaskName ,
241+ pxTaskStatusArray [ x ].ulRunTimeCounter ,
242+ ulStatsAsPercentage );
243+ }
244+ else {
245+ /* If the percentage is zero here then the task has
246+ consumed less than 1% of the total run time. */
247+ sprintf ( pcWriteBuffer , "%s:%d,<1% \n" ,
248+ pxTaskStatusArray [ x ].pcTaskName ,
249+ pxTaskStatusArray [ x ].ulRunTimeCounter );
250+ }
251+
252+ pcWriteBuffer += strlen ( ( char * ) pcWriteBuffer );
253+ }
254+ }
255+
256+ /* The array is no longer needed, free the memory it consumes. */
257+ vPortFree ( pxTaskStatusArray );
258+ }
259+ }
260+
196261#endif // PIOS_INCLUDE_TASK_MONITOR
0 commit comments