]> git.sur5r.net Git - freertos/commitdiff
Add vPortGetHeapStats() function to query heap statistics.
authorrtel <rtel@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Thu, 4 Jul 2019 19:34:48 +0000 (19:34 +0000)
committerrtel <rtel@1d2547de-c912-0410-9cb9-b8ca96c0e9e2>
Thu, 4 Jul 2019 19:34:48 +0000 (19:34 +0000)
git-svn-id: https://svn.code.sf.net/p/freertos/code/trunk@2671 1d2547de-c912-0410-9cb9-b8ca96c0e9e2

FreeRTOS/Source/include/portable.h
FreeRTOS/Source/portable/MemMang/heap_4.c
FreeRTOS/Source/portable/MemMang/heap_5.c

index a1bb4490504e659f0b6acc093b76b54fe07d8a0a..d2baf14a4cfe9c0937c2bd5c58843fd25e0a4e30 100644 (file)
@@ -118,13 +118,26 @@ extern "C" {
        #endif\r
 #endif\r
 \r
-/* Used by heap_5.c. */\r
+/* Used by heap_5.c to define the start address and size of each memory region\r
+that together comprise the total FreeRTOS heap space. */\r
 typedef struct HeapRegion\r
 {\r
        uint8_t *pucStartAddress;\r
        size_t xSizeInBytes;\r
 } HeapRegion_t;\r
 \r
+/* Used to pass information about the heap out of vPortGetHeapStats(). */\r
+typedef struct xHeapStats\r
+{\r
+       size_t xAvailableHeapSpaceInBytes;              /* The total heap size currently available - this is the sum of all the free blocks, not the largest block that can be allocated. */\r
+       size_t xSizeOfLargestFreeBlockInBytes;  /* The maximum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */\r
+       size_t xSizeOfSmallestFreeBlockInBytes; /* The minimum size, in bytes, of all the free blocks within the heap at the time vPortGetHeapStats() is called. */\r
+       size_t xNumberOfFreeBlocks;                             /* The number of free memory blocks within the heap at the time vPortGetHeapStats() is called. */\r
+       size_t xMinimumEverFreeBytesRemaining;  /* The minimum amount of total free memory (sum of all free blocks) there has been in the heap since the system booted. */\r
+       size_t xNumberOfSuccessfulAllocations;  /* The number of calls to pvPortMalloc() that have returned a valid memory block. */\r
+       size_t xNumberOfSuccessfulFrees;                /* The number of calls to vPortFree() that has successfully freed a block of memory. */\r
+} HeapStats_t;\r
+\r
 /*\r
  * Used to define multiple heap regions for use by heap_5.c.  This function\r
  * must be called before any calls to pvPortMalloc() - not creating a task,\r
@@ -138,6 +151,11 @@ typedef struct HeapRegion
  */\r
 void vPortDefineHeapRegions( const HeapRegion_t * const pxHeapRegions ) PRIVILEGED_FUNCTION;\r
 \r
+/*\r
+ * Returns a HeapStats_t structure filled with information about the current\r
+ * heap state.\r
+ */\r
+void vPortGetHeapStats( HeapStats_t *pxHeapStats );\r
 \r
 /*\r
  * Map to the memory management routines required for the port.\r
index 23714eb240737f7b368f5f1fb9c8ba717fd1362f..927064634492a3945d6f0b643e4b00080edbccd3 100644 (file)
@@ -97,10 +97,12 @@ static const size_t xHeapStructSize = ( sizeof( BlockLink_t ) + ( ( size_t ) ( p
 /* Create a couple of list links to mark the start and end of the list. */\r
 static BlockLink_t xStart, *pxEnd = NULL;\r
 \r
-/* Keeps track of the number of free bytes remaining, but says nothing about\r
-fragmentation. */\r
+/* Keeps track of the number of calls to allocate and free memory as well as the\r
+number of free bytes remaining, but says nothing about fragmentation. */\r
 static size_t xFreeBytesRemaining = 0U;\r
 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
+static size_t xNumberOfSuccessfulAllocations = 0;\r
+static size_t xNumberOfSuccessfulFrees = 0;\r
 \r
 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
 member of an BlockLink_t structure is set then the block belongs to the\r
@@ -221,6 +223,7 @@ void *pvReturn = NULL;
                                        by the application and has no "next" block. */\r
                                        pxBlock->xBlockSize |= xBlockAllocatedBit;\r
                                        pxBlock->pxNextFreeBlock = NULL;\r
+                                       xNumberOfSuccessfulAllocations++;\r
                                }\r
                                else\r
                                {\r
@@ -292,6 +295,7 @@ BlockLink_t *pxLink;
                                        xFreeBytesRemaining += pxLink->xBlockSize;\r
                                        traceFREE( pv, pxLink->xBlockSize );\r
                                        prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
+                                       xNumberOfSuccessfulFrees++;\r
                                }\r
                                ( void ) xTaskResumeAll();\r
                        }\r
@@ -326,6 +330,58 @@ void vPortInitialiseBlocks( void )
 }\r
 /*-----------------------------------------------------------*/\r
 \r
+void vPortGetHeapStats( HeapStats_t *pxHeapStats )\r
+{\r
+BlockLink_t *pxBlock;\r
+size_t xBlocks = 0, xMaxSize = 0, xMinSize = 0;\r
+\r
+       vTaskSuspendAll();\r
+       {\r
+               pxBlock = xStart.pxNextFreeBlock;\r
+\r
+               /* pxBlock will be NULL if the heap has not been initialised.  The heap\r
+               is initialised automatically when the first allocation is made. */\r
+               if( pxBlock != NULL )\r
+               {\r
+                       do\r
+                       {\r
+                               /* Increment the number of blocks and record the largest block seen\r
+                               so far. */\r
+                               xBlocks++;\r
+\r
+                               if( pxBlock->xBlockSize > xMaxSize )\r
+                               {\r
+                                       xMaxSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               if( pxBlock->xBlockSize < xMinSize )\r
+                               {\r
+                                       xMinSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               /* Move to the next block in the chain until the last block is\r
+                               reached. */\r
+                               pxBlock = pxBlock->pxNextFreeBlock;\r
+                       } while( pxBlock != pxEnd );\r
+               }\r
+       }\r
+       xTaskResumeAll();\r
+\r
+       pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;\r
+       pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;\r
+       pxHeapStats->xNumberOfFreeBlocks = xBlocks;\r
+\r
+       taskENTER_CRITICAL();\r
+       {\r
+               pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;\r
+               pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;\r
+               pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;\r
+               pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;\r
+       }\r
+       taskEXIT_CRITICAL();\r
+}\r
+/*-----------------------------------------------------------*/\r
+\r
 static void prvHeapInit( void )\r
 {\r
 BlockLink_t *pxFirstFreeBlock;\r
index 8e50762360bd1b920bd6a390cb551834d4b7733a..614210edf3e5f0c01efedc17325aec740f0e9b2b 100644 (file)
@@ -116,10 +116,12 @@ static const size_t xHeapStructSize       = ( sizeof( BlockLink_t ) + ( ( size_t ) ( p
 /* Create a couple of list links to mark the start and end of the list. */\r
 static BlockLink_t xStart, *pxEnd = NULL;\r
 \r
-/* Keeps track of the number of free bytes remaining, but says nothing about\r
-fragmentation. */\r
+/* Keeps track of the number of calls to allocate and free memory as well as the\r
+number of free bytes remaining, but says nothing about fragmentation. */\r
 static size_t xFreeBytesRemaining = 0U;\r
 static size_t xMinimumEverFreeBytesRemaining = 0U;\r
+static size_t xNumberOfSuccessfulAllocations = 0;\r
+static size_t xNumberOfSuccessfulFrees = 0;\r
 \r
 /* Gets set to the top bit of an size_t type.  When this bit in the xBlockSize\r
 member of an BlockLink_t structure is set then the block belongs to the\r
@@ -231,6 +233,7 @@ void *pvReturn = NULL;
                                        by the application and has no "next" block. */\r
                                        pxBlock->xBlockSize |= xBlockAllocatedBit;\r
                                        pxBlock->pxNextFreeBlock = NULL;\r
+                                       xNumberOfSuccessfulAllocations++;\r
                                }\r
                                else\r
                                {\r
@@ -301,6 +304,7 @@ BlockLink_t *pxLink;
                                        xFreeBytesRemaining += pxLink->xBlockSize;\r
                                        traceFREE( pv, pxLink->xBlockSize );\r
                                        prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );\r
+                                       xNumberOfSuccessfulFrees++;\r
                                }\r
                                ( void ) xTaskResumeAll();\r
                        }\r
@@ -482,4 +486,56 @@ const HeapRegion_t *pxHeapRegion;
        /* Work out the position of the top bit in a size_t variable. */\r
        xBlockAllocatedBit = ( ( size_t ) 1 ) << ( ( sizeof( size_t ) * heapBITS_PER_BYTE ) - 1 );\r
 }\r
+/*-----------------------------------------------------------*/\r
+\r
+void vPortGetHeapStats( HeapStats_t *pxHeapStats )\r
+{\r
+       BlockLink_t *pxBlock;\r
+       size_t xBlocks = 0, xMaxSize = 0, xMinSize = 0;\r
+\r
+       vTaskSuspendAll();\r
+       {\r
+               pxBlock = xStart.pxNextFreeBlock;\r
+\r
+               /* pxBlock will be NULL if the heap has not been initialised.  The heap\r
+               is initialised automatically when the first allocation is made. */\r
+               if( pxBlock != NULL )\r
+               {\r
+                       do\r
+                       {\r
+                               /* Increment the number of blocks and record the largest block seen\r
+                               so far. */\r
+                               xBlocks++;\r
+\r
+                               if( pxBlock->xBlockSize > xMaxSize )\r
+                               {\r
+                                       xMaxSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               if( pxBlock->xBlockSize < xMinSize )\r
+                               {\r
+                                       xMinSize = pxBlock->xBlockSize;\r
+                               }\r
+\r
+                               /* Move to the next block in the chain until the last block is\r
+                               reached. */\r
+                               pxBlock = pxBlock->pxNextFreeBlock;\r
+                       } while( pxBlock != pxEnd );\r
+               }\r
+       }\r
+       xTaskResumeAll();\r
+\r
+       pxHeapStats->xSizeOfLargestFreeBlockInBytes = xMaxSize;\r
+       pxHeapStats->xSizeOfSmallestFreeBlockInBytes = xMinSize;\r
+       pxHeapStats->xNumberOfFreeBlocks = xBlocks;\r
+\r
+       taskENTER_CRITICAL();\r
+       {\r
+               pxHeapStats->xAvailableHeapSpaceInBytes = xFreeBytesRemaining;\r
+               pxHeapStats->xNumberOfSuccessfulAllocations = xNumberOfSuccessfulAllocations;\r
+               pxHeapStats->xNumberOfSuccessfulFrees = xNumberOfSuccessfulFrees;\r
+               pxHeapStats->xMinimumEverFreeBytesRemaining = xMinimumEverFreeBytesRemaining;\r
+       }\r
+       taskEXIT_CRITICAL();\r
+}\r
 \r