]> git.sur5r.net Git - freertos/blobdiff - Source/tasks.c
Added run time stats functions.
[freertos] / Source / tasks.c
index d84e2f70ebfc88ae7a0c5883c7a5da06f4e9776a..62b0a30709f5b8bbeb8e2e055a9724468a1cc995 100644 (file)
@@ -3,20 +3,20 @@
 \r
        This file is part of the FreeRTOS.org distribution.\r
 \r
-       FreeRTOS.org is free software; you can redistribute it and/or modify it \r
+       FreeRTOS.org is free software; you can redistribute it and/or modify it\r
        under the terms of the GNU General Public License (version 2) as published\r
        by the Free Software Foundation and modified by the FreeRTOS exception.\r
 \r
        FreeRTOS.org is distributed in the hope that it will be useful, but WITHOUT\r
-       ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or \r
-       FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for \r
+       ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or\r
+       FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for\r
        more details.\r
 \r
-       You should have received a copy of the GNU General Public License along \r
-       with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59 \r
+       You should have received a copy of the GNU General Public License along\r
+       with FreeRTOS.org; if not, write to the Free Software Foundation, Inc., 59\r
        Temple Place, Suite 330, Boston, MA  02111-1307  USA.\r
 \r
-       A special exception to the GPL is included to allow you to distribute a \r
+       A special exception to the GPL is included to allow you to distribute a\r
        combined work that includes FreeRTOS.org without being obliged to provide\r
        the source code for any proprietary components.  See the licensing section\r
        of http://www.FreeRTOS.org for full details.\r
@@ -85,16 +85,20 @@ typedef struct tskTaskControlBlock
        #endif\r
 \r
        #if ( configUSE_TRACE_FACILITY == 1 )\r
-               unsigned portBASE_TYPE  uxTCBNumber;            /*< This is used for tracing the scheduler and making debugging easier only. */\r
+               unsigned portBASE_TYPE  uxTCBNumber;    /*< This is used for tracing the scheduler and making debugging easier only. */\r
        #endif  \r
                \r
        #if ( configUSE_MUTEXES == 1 )\r
-               unsigned portBASE_TYPE uxBasePriority;\r
+               unsigned portBASE_TYPE uxBasePriority;  /*< The priority last assigned to the task - used by the priority inheritance mechanism. */\r
        #endif\r
 \r
        #if ( configUSE_APPLICATION_TASK_TAG == 1 )\r
                pdTASK_HOOK_CODE pxTaskTag;\r
        #endif\r
+\r
+       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+               unsigned portLONG ulRunTimeCounter;                     /*< Used for calculating how much CPU time each task is utilising. */\r
+       #endif\r
                \r
 } tskTCB;\r
 \r
@@ -144,6 +148,14 @@ static volatile portBASE_TYPE xMissedYield                                         = ( portBASE_TYPE ) pdFALSE;
 static volatile portBASE_TYPE xNumOfOverflows                                  = ( portBASE_TYPE ) 0;\r
 static unsigned portBASE_TYPE uxTaskNumber                                             = ( unsigned portBASE_TYPE ) 0;\r
 \r
+#if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+\r
+       static portCHAR pcStatsString[ 50 ];\r
+       static unsigned portLONG ulTaskSwitchedInTime = 0UL;    /*< Holds the value of a timer/counter the last time a task was switched in. */\r
+       static void prvGenerateRunTimeStatsForTasksInList( const signed portCHAR *pcWriteBuffer, xList *pxList, unsigned portLONG ulTotalRunTime );\r
+\r
+#endif\r
+\r
 /* Debugging and trace facilities private variables and macros. ------------*/\r
 \r
 /*\r
@@ -172,6 +184,7 @@ static unsigned portBASE_TYPE uxTaskNumber                                          = ( unsigned portBASE_TYPE ) 0;
        static signed portBASE_TYPE xTracing = pdFALSE;\r
        static unsigned portBASE_TYPE uxPreviousTask = 255;\r
        static portCHAR pcStatusString[ 50 ];\r
+\r
 #endif\r
 \r
 /*-----------------------------------------------------------*/\r
@@ -303,7 +316,9 @@ static portTASK_FUNCTION_PROTO( prvIdleTask, pvParameters );
  * allocated by calls to pvPortMalloc from within the tasks application code).\r
  */\r
 #if ( ( INCLUDE_vTaskDelete == 1 ) || ( INCLUDE_vTaskCleanUpResources == 1 ) )\r
+\r
        static void prvDeleteTCB( tskTCB *pxTCB );\r
+\r
 #endif\r
 \r
 /*\r
@@ -826,7 +841,7 @@ tskTCB * pxNewTCB;
                        /* If null is passed in here then we are suspending ourselves. */\r
                        pxTCB = prvGetTCBFromHandle( pxTaskToSuspend );\r
 \r
-                       traceTASK_SUSPEND( pxTaskToSuspend );\r
+                       traceTASK_SUSPEND( pxTCB );\r
 \r
                        /* Remove task from the ready/delayed list and place in the     suspended list. */\r
                        vListRemove( &( pxTCB->xGenericListItem ) );\r
@@ -987,6 +1002,11 @@ portBASE_TYPE xReturn;
                xSchedulerRunning = pdTRUE;\r
                xTickCount = ( portTickType ) 0;\r
 \r
+               /* If configGENERATE_RUN_TIME_STATS is defined then the following\r
+               macro must be defined to configure the timer/counter used to generate\r
+               the run time counter time base. */\r
+               portCONFIGURE_TIMER_FOR_RUN_TIME_STATS();\r
+\r
                /* Setting up the timer tick is hardware specific and thus in the\r
                portable interface. */\r
                if( xPortStartScheduler() )\r
@@ -1126,7 +1146,7 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
-#if ( ( configUSE_TRACE_FACILITY == 1 ) && ( INCLUDE_vTaskDelete == 1 ) && ( INCLUDE_vTaskSuspend == 1 ) )\r
+#if ( configUSE_TRACE_FACILITY == 1 )\r
 \r
        void vTaskList( signed portCHAR *pcWriteBuffer )\r
        {\r
@@ -1165,15 +1185,88 @@ unsigned portBASE_TYPE uxTaskGetNumberOfTasks( void )
                                prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, tskBLOCKED_CHAR );\r
                        }\r
 \r
-                       if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )\r
+                       #if( INCLUDE_vTaskDelete == 1 )\r
                        {\r
-                               prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );\r
+                               if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )\r
+                               {\r
+                                       prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, tskDELETED_CHAR );\r
+                               }\r
                        }\r
+                       #endif\r
 \r
-                       if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )\r
+                       #if ( INCLUDE_vTaskSuspend == 1 )\r
                        {\r
-                               prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );\r
+                               if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )\r
+                               {\r
+                                       prvListTaskWithinSingleList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, tskSUSPENDED_CHAR );\r
+                               }\r
                        }\r
+                       #endif\r
+               }\r
+        xTaskResumeAll();\r
+       }\r
+\r
+#endif\r
+/*----------------------------------------------------------*/\r
+\r
+#if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+\r
+       void vTaskGetRunTimeStats( signed portCHAR *pcWriteBuffer )\r
+       {\r
+       unsigned portBASE_TYPE uxQueue;\r
+       unsigned portLONG ulTotalRunTime = portGET_RUN_TIME_COUNTER_VALUE();\r
+\r
+               /* This is a VERY costly function that should be used for debug only.\r
+               It leaves interrupts disabled for a LONG time. */\r
+\r
+        vTaskSuspendAll();\r
+               {\r
+                       /* Run through all the lists that could potentially contain a TCB,\r
+                       generating a table of run timer percentages in the provided \r
+                       buffer. */\r
+\r
+                       pcWriteBuffer[ 0 ] = ( signed portCHAR ) 0x00;\r
+                       strcat( ( portCHAR * ) pcWriteBuffer, ( const portCHAR * ) "\r\n" );\r
+\r
+                       uxQueue = uxTopUsedPriority + 1;\r
+\r
+                       do\r
+                       {\r
+                               uxQueue--;\r
+\r
+                               if( !listLIST_IS_EMPTY( &( pxReadyTasksLists[ uxQueue ] ) ) )\r
+                               {\r
+                                       prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &( pxReadyTasksLists[ uxQueue ] ), ulTotalRunTime );\r
+                               }\r
+                       }while( uxQueue > ( unsigned portSHORT ) tskIDLE_PRIORITY );\r
+\r
+                       if( !listLIST_IS_EMPTY( pxDelayedTaskList ) )\r
+                       {\r
+                               prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxDelayedTaskList, ulTotalRunTime );\r
+                       }\r
+\r
+                       if( !listLIST_IS_EMPTY( pxOverflowDelayedTaskList ) )\r
+                       {\r
+                               prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) pxOverflowDelayedTaskList, ulTotalRunTime );\r
+                       }\r
+\r
+                       #if ( INCLUDE_vTaskDelete == 1 )\r
+                       {\r
+                               if( !listLIST_IS_EMPTY( &xTasksWaitingTermination ) )\r
+                               {\r
+                                       prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xTasksWaitingTermination, ulTotalRunTime );\r
+                               }\r
+                       }\r
+                       #endif\r
+\r
+                       #if ( INCLUDE_vTaskSuspend == 1 )\r
+                       {\r
+                               if( !listLIST_IS_EMPTY( &xSuspendedTaskList ) )\r
+                               {\r
+                                       prvGenerateRunTimeStatsForTasksInList( pcWriteBuffer, ( xList * ) &xSuspendedTaskList, ulTotalRunTime );\r
+                               }\r
+                       }\r
+                       #endif\r
                }\r
         xTaskResumeAll();\r
        }\r
@@ -1394,6 +1487,21 @@ void vTaskSwitchContext( void )
 {\r
        traceTASK_SWITCHED_OUT();\r
 \r
+       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+       {\r
+               unsigned portLONG ulTempCounter = portGET_RUN_TIME_COUNTER_VALUE();\r
+\r
+                       /* Add the amount of time the task has been running to the accumulated\r
+                       time so far.  The time the task started running was stored in \r
+                       ulTaskSwitchedInTime.  Note that there is no overflow protection here\r
+                       so count values are only valid until the timer overflows.  Generally\r
+                       this will be about 1 hour assuming a 1uS timer increment. */\r
+                       pxCurrentTCB->ulRunTimeCounter += ( ulTempCounter - ulTaskSwitchedInTime );\r
+                       ulTaskSwitchedInTime = ulTempCounter;\r
+       }\r
+       #endif\r
+\r
+\r
        if( uxSchedulerSuspended != ( unsigned portBASE_TYPE ) pdFALSE )\r
        {\r
                /* The scheduler is currently suspended - do not allow a context\r
@@ -1709,6 +1817,12 @@ static void prvInitialiseTCBVariables( tskTCB *pxTCB, const signed portCHAR * co
                pxTCB->pxTaskTag = NULL;\r
        }\r
        #endif  \r
+\r
+       #if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+       {\r
+               pxTCB->ulRunTimeCounter = 0UL;\r
+       }\r
+       #endif\r
 }\r
 /*-----------------------------------------------------------*/\r
 \r
@@ -1833,6 +1947,38 @@ tskTCB *pxNewTCB;
 #endif\r
 /*-----------------------------------------------------------*/\r
 \r
+#if ( configGENERATE_RUN_TIME_STATS == 1 )\r
+\r
+       static void prvGenerateRunTimeStatsForTasksInList( const signed portCHAR *pcWriteBuffer, xList *pxList, unsigned portLONG ulTotalRunTime )\r
+       {\r
+       volatile tskTCB *pxNextTCB, *pxFirstTCB;\r
+       unsigned portLONG ulStatsAsPercentage;\r
+\r
+               /* Write the run time stats of all the TCB's in pxList into the buffer. */\r
+               listGET_OWNER_OF_NEXT_ENTRY( pxFirstTCB, pxList );\r
+               do\r
+               {\r
+                       listGET_OWNER_OF_NEXT_ENTRY( pxNextTCB, pxList );\r
+\r
+                       ulStatsAsPercentage = ( 100UL * pxNextTCB->ulRunTimeCounter ) / ulTotalRunTime;\r
+\r
+                       if( ulStatsAsPercentage > 0UL )\r
+                       {\r
+                               sprintf( pcStatsString, ( portCHAR * ) "%s\t\t\t%lu\t\t\t%lu%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter, ulStatsAsPercentage );\r
+                       }\r
+                       else\r
+                       {\r
+                               sprintf( pcStatsString, ( portCHAR * ) "%s\t\t\t%lu\t\t\t< 1%%\r\n", pxNextTCB->pcTaskName, pxNextTCB->ulRunTimeCounter );\r
+                       }\r
+\r
+                       strcat( ( portCHAR * ) pcWriteBuffer, ( portCHAR * ) pcStatsString );\r
+\r
+               } while( pxNextTCB != pxFirstTCB );\r
+       }\r
+\r
+#endif\r
+/*-----------------------------------------------------------*/\r
+\r
 #if ( ( configUSE_TRACE_FACILITY == 1 ) || ( INCLUDE_uxTaskGetStackHighWaterMark == 1 ) )\r
 \r
        unsigned portSHORT usTaskCheckFreeStackSpace( const unsigned portCHAR * pucStackByte )\r