Upload
mohammad-mohsen-amiri
View
18
Download
3
Embed Size (px)
DESCRIPTION
tornado train workshop 9
Citation preview
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-1
Chapter
9
Memory
Memory management in VxWorks
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-2
Typical Memory Layout
VxWorks
sysMemTop( )
FREE_RAM_ADRS
LOCAL_MEM_LOCAL_ADRS
RAM_LOW_ADRS
sysPhysMemTop( )
WDB_POOL_SIZE
USER_RESERVED_MEM
SystemMemory
Pool
• Many of the above macros are parameters for the component /hardware/
memory/BSP Memory Configuration.
• The macro LOCAL_MEM_LOCAL_ADRS is the address of the start of RAM
on your board. It is typically equal to 0.
• The macro RAM_LOW_ADRS is the address where VxWorks is loaded.
• FREE_RAM_ADRS is the address of the end of the VxWorks system image.
• Most BSP’s define sysPhysMemTop( ) as the top of physical RAM;
sysMemTop( ) is always the top of the system memory pool. The
difference is the configurable constant USER_RESERVED_MEM.
• Low memory often contains the interrupt vector table, and space for the
boot line and an exception message. More detailed memory maps are
available in the Programmer’s Guide appendix for your CPU architecture.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-3
Target Server Memory Pool
• A pool of memory on the target reserved for use by the
Tornado tools:
● Dynamic loading of object modules.
● Passing string arguments to tasks spawned on target.
● Creation of variables from WindSh.
• The target server manages the pool, keeping overhead
such as block lists on the host.
• The initial size of the target server memory pool is
configured by WDB_POOL_SIZE. The default is 1/16 of
sysMemTop ( ) - FREE_RAM_ADRS.
• Additional memory is silently allocated from the
system memory pool if needed.
• The target server memory pool helps to reduce memory fragmentation
on the target.
• WDB_POOL_SIZE is not a component parameter, but can be configured
.../target/config/comps/src/configAll.h.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-4
System Memory Pool
• Used for dynamic memory allocation in programs:
● malloc( ).● Creating tasks (stack and TCB).
● VxWorks memory requests.
• Initialized at system start-up.
● Can modify USER_RESERVED_MEM to reserve memoryfor application-specific use.
● May need to modify sysPhysMemTop( ) (or justLOCAL_MEM_SIZE) when adding memory to yourboard. Check your BSP documentation.
• To add off-board memory:
void memAddToPool (pPool, poolSize)
pPool must be the local address of the memory.
• USER_RESERVED_MEM, a parameter for the component /hardware/memory/
BSP Memory Configuration, specifies how many bytes below the top of
physical RAM the system memory pool ends. The default value is 0.
• The local address of off-board memory on a VME bus can be computed
as follows:
● Read hardware doc to find the VMEbus address of the memory.
● Use sysBusToLocalAdrs( ) to compute the local addresscorresponding to the VMEbus address.
• Adding off-board memory to the system memory pool is usually a bad
idea. A subsequent call to malloc() will give you a block of either on-
board or off-board memory, leaving uncertainty about the worst-case
access time to this block. Instead, consider creating a separate partition
for this off-board memory.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-5
Allocating/Releasing Memory
• To dynamically allocate memory:
void *malloc (nBytes)Returns a pointer to the newly allocated memory orNULL on error.
• Uses first-fit algorithm.
● Free memory is stored in a linked list.
● Some (small) overhead for each malloc( ).
• To release allocated memory:
void free (ptr)
Adjacent blocks are coalesced.
• For 68k boards the overhead for malloc( ) is:
● 8 byte header per malloc( ).● Always rounds up to a 4 byte boundary.
● When block is free, 8 bytes within body may be used for free listlinks.
● Minimum block size is 8 bytes (header) + 8 bytes (free list links) = 16bytes.
• See the memPartLib man page for more information about the
overhead for your architecture.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-6
Debugging Options
• Default malloc( ) debugging: If request too large, log an
error message.
• Default free( ) debugging:
● Check block for consistency.
● If corrupted: suspend task, log error message.
• Can change default debugging options with:
void memOptionsSet (options)
• Options can be:
+ MEM_ALLOC_ERROR_LOG_FLAG- MEM_ALLOC_ERROR_SUSPEND_FLAG+ MEM_BLOCK_CHECK+ MEM_BLOCK_ERROR_LOG_FLAG+ MEM_BLOCK_ERROR_SUSPEND_FLAG
• Bitwise OR options together to get more than one.
• Setting any MEM_BLOCK_XXX option automatically causes the
MEM_BLOCK_CHECK option to be set.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-7
Examining Memory
• Use the Browser.
• Enter the memory partition ID in the Show box.
Free Blocks
System Memory
Currently
Free List
Pool Size
Allocated
TotalAllocated
• The VxWorks system memory partition ID is memSysPartId.
• Total target memory usage within both the target server pool and the
system memory pool is displayed with bar graphs in the browser.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-8
Additional System MemoryManagement Routines
void * calloc (nElems, size) Allocate zeroed memoryfor an array.
void * realloc (ptr, newSize) Resize an allocated block.The block may be moved.
int memFindMax( ) Returns the size of thelargest free block insystem memory.
• A possible race condition can arise between calls to memFindMax( ) and
malloc( ).
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-9
Fine Tuning
• For fast, deterministic allocation of fixed size buffers,
use message queues instead of malloc( ).
message queue
buffers
...
...ptr ptrptrbufReturn( ) bufGet( )
• Create a library “bufferLib” of routines for managing your buffers. For
example:
/* pBlock points to memory for array of buffers (NULL => malloc) *//* Put addresses of buffers in a message queue *//* Returned BUF_ID contains the newly created msgQId */BUF_ID bufCreate (pBlock, numBufs, bufSize);
/* Calls msgQReceive() to get next buffer */void *bufGet (bufId);
/* Calls msgQSend() to return the buffer */void bufReturn (bufId, ptr);
• Alternative: Implement your own fixed-size block allocator, guarding
the block free-list with a mutex, taskLock(), or intLock().
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-10
Generic Partition Manager
• VxWorks provides low-level routines to create and
manipulate alternate memory pools.
• High-level routines like malloc( ) and free( ) call these
lower level routines, specifying the system memory
pool.
• Application may use alternate memory partitions to
reduce fragmentation.
• Application may use alternate memory partitions to
manage memory with different properties.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-11
Creating a Memory Partition
PART_ID memPartCreate (pPool, size)
pPool Pointer to memory for this partition.
size Size of memory partition in bytes.
• Returns a partition id (PART_ID), or NULL on error.
• The memory for this partition (pPool) may be taken
from:
● A separate memory board.
● A block allocated from the system memory partition.
● The top of the CPU board’s RAM.
• PART_ID, defined in memLib.h, is a handle to the memory partition
being created.
• Specify nonzero USER_RESERVED_MEM to reserve memory at the top of
RAM for your application’s exclusive use.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-12
Managing Memory Partitions
• System partition management routines call routines
listed below, specifying the PART_ID as memSysPartId.
Generic System Memory Pool
memPartAlloc( ) malloc( )
memPartFree( ) free( )
memPartShow( ) memShow( )
memPartAddToPool( ) memAddToPool( )
memPartOptionsSet( ) memOptionsSet( )
memPartRealloc( ) realloc( )
memPartFindMax( ) memFindMax( )
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-13
Example Creating a MemoryPartition
->partId =memPartCreate(pMemory,100000)
new symbol “partId” added to symbol table.partId = 0x23ff318: value = 37745448 = 0x23ff328 =partId + 0x10
->ptr=memPartAlloc(partId,200)new symbol “ptr” added to symbol table.ptr = 0x23ff2ec: value = 37652632 = 0x23e8898
->show partId status bytes blocks ave block max block ------------ ---------------- -----------current free 99776 1 99776 99776 alloc 208 1 208 -cumulative alloc 208 1 208 -
• Partition information can also be obtained using the memory-partition
browser.
Wind River SystemsTornado Training Workshop © Copyright Wind River Systems 9-14
Summary
• Standard C routines are used for dynamic memory
allocation.
• To configure the system memory pool:
● Modify sysPhysMemTop ( ).● Specify USER_RESERVED_MEM.
● Call memAddToPool( ).
• For fast, deterministic allocation of fixed size buffers,
use message queues instead of malloc( ).
• Create separate memory partition for off-board
memory, or to help reduce fragmentation.