Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dynamic Systems (MEM, BUF)

Similar presentations


Presentation on theme: "Dynamic Systems (MEM, BUF)"— Presentation transcript:

1 Dynamic Systems (MEM, BUF)
TI BIOS Dynamic Systems (MEM, BUF) 20 September 2018 Dr. Veton Këpuska

2 Introduction In this chapter the ability to create and delete system components will be considered as a way to reduce system size, make better use of on-chip resources, and tailor system components in response to events occurring as the system runs. 20 September 2018 Dr. Veton Këpuska

3 Objectives Contrast static and dynamic system coding benefits
Implement dynamic BIOS object creation and deletion Contrast the BIOS MEM API to malloc and free Contrast MEM API with BUF API Modify static systems to employ dynamic methods 20 September 2018 Dr. Veton Këpuska 2

4 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

5 Outline What are the advantages to adding dynamic coding constructs to my DSP system? 20 September 2018 Dr. Veton Këpuska

6 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

7 Concepts 20 September 2018 Dr. Veton Këpuska

8 Static vs. Dynamic Systems
STATIC Environment Link-time Declare streams Declare buffers Initialize variables Execute Read data Process data Write data DYNAMIC Environment Create Create streams Allocate buffers Initialize variables Execute Read data Process data Write data Delete Delete streams Free buffers 20 September 2018 Dr. Veton Këpuska

9 Static vs Dynamic Systems
Digital Systems: Hardware Organization and Design 9/20/2018 Static vs Dynamic Systems Static Objects – created at link time via TCONF and/or GCONF Dynamic Objects – created at runtime via MOD_create() API Once created, both can be used identically Only dynamic objects may be deleted via MOD_delete() API Static benefits: easy, smaller code size, faster startup Dynamic benefits: smaller RAM budget, reuse of on-chip RAM Dynamic objects allow needs of the running system to determine the style and quantity of objects in place Smaller RAM budget is the result of sharing memory between two threads that run at different times. If the first allocates memory, runs, then frees this memory, a subsequent one can do the same – with the same memory. In complex systems with multiple threads, it is common that given threads only are in use some of the time. Dynamic allocation allows the system architect to take advantage of this fact and specify a RAM capacity smaller than the sum of the total RAM requirements, based on their ability to thus share the RAM. Re-use of on-chip RAM is the same point as above, and is often a high value concern for DSP system designers. Another technique common to RAM reuse in DSP systems is that of ‘overlays’. While this method also allows for memory reuse, it requires that the system integrator and the application authors all plan in advance how to make the overlay usage work correctly. Since this is not often possible, this is why the techniques shown here offer additional value – they allow threads to be written without knowing about other threads in the system and the benefits to still be had – reliably. 20 September 2018 Dr. Veton Këpuska Architecture of a Respresentative 32 Bit Processor

10 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

11 Dynamic Systems Can arrays of RAM be allocated as needed and given back when done ? Can malloc be enhanced to allow requests from a specific memory type? 20 September 2018 Dr. Veton Këpuska

12 Memory Allocation (MEM)
20 September 2018 Dr. Veton Këpuska

13 Dynamic Memory Allocation
Digital Systems: Hardware Organization and Design 9/20/2018 Dynamic Memory Allocation Superset of malloc() function – invokes the DSP/BIOS memory manager Allows selection of heap to draw from plus address alignment Creation of multiple heaps is via GCONF or TCONF Size is in NMADUs and allocation is always an even numbers of words Aligns on even boundaries Returns MEM_ILLEGAL if failure malloc(size) API is translated to MEM_alloc(0,size,0) in BIOS Ptr addr = MEM_alloc(segid, Uns Size, Uns align); Final bullet – about better packing : C malloc() stores array size in address prior to the array granted to the caller Normally, this is convenient, since it allows the system to remember this detail for the user In DSP systems, where sometimes aligned blocks are required, this can present an unexpected problem Imagine you had requested 10 1k blocks, aligned on a 1k boundary (This would be the ideal requirement for circular buffering on 1k blocks – often used in circular buffering – a means of buffering the last N samples of data efficiently) Aligning the first buffer would likely require skipping over the RAM whose addresses are not suited to the alignment requirement. However, the end point of the first block would be an ideal starting point for subsequent blocks – or would it? In the case of C malloc(), the answer is – surprisingly to most users – NO. Why? Recall that regular malloc() stores the size argument in the heap – this means that after making space for the size argument, the next acceptable starting address would be 1023 words away – leaving 1K (-1) holes between each requested block Bottom line: instead of taking ~10.5K (on average) heap to malloc 10 1k blocks on 1k boundaries, we discover it will require over 20K ! Solution? MEM_alloc – since it does not store the size in the heap, the expected result – about 10.5k total – does apply here – which is why MEM_alloc apparently is ‘less automated’ Often students familiar with C malloc will see the extra arg in MEM_free as a detraction What we’ve shown here is that – in fact – it was deliberately designed to be far better in DSP systems 20 September 2018 Dr. Veton Këpuska Architecture of a Respresentative 32 Bit Processor

14 Dynamic Memory Allocation
Ptr addr = MEM_calloc(Int segid, Uns size, Uns align); MEM_alloc() + clears (zeros) the array MEM_vlloc() + fills array with specified value Replacement for free() C function Complement to MEM_alloc / valloc / calloc APIs Must specify segment and size in addition to ptr to array Removal of auto-store of size arg allows better aligned array packing Ptr_addr = MEM_valloc(segid, size, align, value); Bool status = MEM_free(Int segid, Ptr address, Uns size); 20 September 2018 Dr. Veton Këpuska

15 MEM_valloc (segid, size, align, value);
Syntax addr = MEM_valloc(segid, size, align, value); Parameters Int segid; /* memory segment identifier */ Uns size; /* block size in MADUs */ Uns align; /* block alignment */ Char value; /* character value */ Return Value Void *addr; /* address of allocated block of memory */ Description MEM_valloc uses MEM_alloc to allocate the memory before initializing it to value. 20 September 2018 Dr. Veton Këpuska

16 Memory Heap Configuration
Digital Systems: Hardware Organization and Design 9/20/2018 Memory Heap Configuration GCONF allows any number of memory blocks to be defined for a given system. Each block can contain a heap with a default name of the memory block, or a different one if desired This can also be done with TCONF - RTM in *.c file, obtain access to heap label via : extern Int IRAM; 20 September 2018 Dr. Veton Këpuska Architecture of a Respresentative 32 Bit Processor

17 Memory Status Interrogation
struct MEM_Stat { Uns size; // original size of heap Uns used // number of MADUs used in heap Uns length; // largest contiguous block available } status = MEM_stat(segid, statbuf); Used to query the status of specified heap Helpful for diagnostics Could be used to actively manage heap usage in sophisticated system Cannot be called from a SWI or HWI; TSK scheduler must be enabled Same information available in CCS Kernel/Object View window 20 September 2018 Dr. Veton Këpuska

18 Memory Status Interrogation
Digital Systems: Hardware Organization and Design 9/20/2018 Memory Status Interrogation Here we start to talk about dynamic memory allocation managed by the C language. For example, malloc() reserves memory on a special stack called a heap. When we are done with it, we call function free(), which "places" this memory back in the same physical space for us to use later in our system. 20 September 2018 Dr. Veton Këpuska Architecture of a Respresentative 32 Bit Processor

19 Dynamic Memory Considerations...
Non-Reentrant Cannot be called from within HWI or SWI Non-Deterministic: Memory manager traverses linked list of free blocks for each allocate and delete Recommended: Allocate and free memory during a background process or startup (pre-realtime) Fragmentation: After repeated allocation and freeing of memory large contiguous blocks of memory may not be available to allocate To minimize memory fragmentation: Allocate smaller, equal-sized blocks of memory from one memory segment and larger equal-sized blocks of memory from a second segment, etc. 20 September 2018 Dr. Veton Këpuska

20 Dynamic Memory Considerations...
For all the above concerns: Consider using BUF API 20 September 2018 Dr. Veton Këpuska

21 MEM API Review MEM API Description MEM_alloc
Allocate memory from specified heap MEM_calloc MEM_alloc + clear (zero) the array MEM_valloc MEM_alloc + fill array with specified value MEM_free Return MEM_alloc’d array to specified heap MEM_stat Return the status of a memory segment MEM_define Define a new memory segment MEM_redefine Redefine an existing memory segment segid = MEM_define(base, length, attrs); MEM_redefine(segid, base, length); 20 September 2018 Dr. Veton Këpuska

22 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

23 Dynamic Systems Can BIOS objects occupy RAM only when needed?
20 September 2018 Dr. Veton Këpuska

24 Dynamic Objects 20 September 2018 Dr. Veton Këpuska

25 Dynamically Creating DSP/BIOS Objects
XXX_create Allocates memory for object out of segment defined in config tool Returns a XXX_Handle to the created object XXX_delete Frees the object’s memory Argument: object handle; return type: void 20 September 2018 Dr. Veton Këpuska

26 Dynamically Creating DSP/BIOS Objects
Modules supporting dynamic create and delete SWI TSK SIO SEM BUF MSGQ QUE MBX LCK GIO #define COUNT 0 #include <sem.h> SEM_Handle hMySem; hMySem = SEM_create(COUNT,NULL); SEM_post(hMySem); SEM_delete(hMySem); C X D 20 September 2018 Dr. Veton Këpuska

27 Dynamic Object Creation and Deletion
hSwi = SWI_create(attrs); // attrs shown in the next slide below hTask = TSK_create(fxn, attrs, [arg,] ...); hStrm = SIO_create(name, mode, bufsize, attrs); hSem = SEM_create(count, attrs); hBuf = BUF_create(numbuff, size, align, attrs); hQueue = QUE_create(attrs); hMbx = MBX_create(msgsize, mbxlength, attrs); // see below hLock = LCK_create(attrs); hGio = GIO_create(name,mode,*status,chanParams,*attrs) MOD_create API all return the handle to the new object Arguments mirror those from static configuration Many ‘attrs’ arguments are ‘placeholders’ for possible future definition SEM, BUF, SIO, TSK examples appear elsewhere in this chapter 20 September 2018 Dr. Veton Këpuska

28 Dynamic Object Creation and Deletion
struct SWI_Attrs { SWI_Fxn fxn; Arg arg0; Arg arg1; Int priority; Uns mailbox; }; Uns msgsize; // size of message Uns mbxlength; // length of mailbox MBX_Attrs *attrs; // pointer to mailbox attributes struct MBX_Attrs { Int segid; // heap to malloc from }; 20 September 2018 Dr. Veton Këpuska

29 hStrm = SIO_create(name, mode, uBufSize, pAttrs);
Dynamic Stream API hStrm = SIO_create(name, mode, uBufSize, pAttrs); #include <sio.h> #define BUFSIZE 100 SIO_Handle hStrmIn, hStrmOut; SIO_Attrs attrs = SIO_ATTRS; attrs.nbufs = 3; hStrmIn = SIO_create("/a2d", SIO_INPUT, BUFSIZE, &attrs); hStrmOut = SIO_create("/d2a", SIO_OUTPUT, BUFSIZE, &attrs); // use In and Out streams as desired ... SIO_idle(hStrmIn); SIO_idle(hStrmOut); SIO_delete(hStrmIn); SIO_delete(hStrmOut); C X D 20 September 2018 Dr. Veton Këpuska

30 Dynamic Stream API SIO_Handle hStrm; // stream object handle
String name; // name of device Int mode; // SIO_INPUT or SIO_OUTPUT Uns uBufSize; // stream buffer size SIO_Attrs *pAttrs; // pointer to stream attributes struct SIO_Attrs { Int nbufs; Int segid; Int align; Bool flush; Uns model; Uns timeout; DEV_Callback callback } 20 September 2018 Dr. Veton Këpuska

31 Stream Attributes Structure
struct SIO_Attrs { SIO_ATTRS Int nbufs; 2 max number of buffers to queue Int segid; 0 memory segment for buffers Int align; 0 alignment of buffers Bool flush; FALSE or TRUE; Block in DEV_idle? Uns model; SIO_STANDARD or SIO_ISSUERECLAIM; stream model Uns timeout; SYS_FOREVER max # system ticks to wait when blocking DEV_Callback *callback; NULL callback fxn – most useful for SIO to SWI } SIO_Attrs; 20 September 2018 Dr. Veton Këpuska

32 hTsk = TSK_create(fxn, attrs, [arg,] ...);
Dynamic Task API hTsk = TSK_create(fxn, attrs, [arg,] ...); #include <tsk.h> TSK_Handle hMyTsk; TSK_Attrs attrs = TSK_ATTRS; attrs.priority = 3; hMyTsk = TSK_create((Fxn)myCode, attrs); // “MyTsk” is now active in system with priority = 3 ... TSK_delete(hMyTsk); C X D 20 September 2018 Dr. Veton Këpuska

33 Digital Systems: Hardware Organization and Design
9/20/2018 Dynamic Task API Task cannot call TSK_delete() on itself TSK_delete is never intrinsically called by BIOS struct TSK_Attrs { Int priority; // task priority Ptr stack; // pre-allocated stack Uns stacksize; // size of stack in MAU Int stackseg; // segment to allocate Ptr environ; // global data structure String name; // printable name Bool exitflag; // needs to exit to terminate? }; Detail to mention optionally – task creation can employ optional ‘arg’ values, as implied in the API layout at the top of this slide. This ability is identically available in both dymanic mode as seen here, and in static mode, as per module 5 via the GCONF/TCONF interfaces. 20 September 2018 Dr. Veton Këpuska Architecture of a Respresentative 32 Bit Processor

34 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

35 Buffer Pools - BUF How can threads share arrays without issues of speed, determinism, or fragmentation? 20 September 2018 Dr. Veton Këpuska

36 Buffer Pools BUF 20 September 2018 Dr. Veton Këpuska

37 BUF Concepts Buffer Pool buf SWI BUF_alloc TSK BUF_free BUF_create
BUF_delete 20 September 2018 Dr. Veton Këpuska

38 BUF Concepts Buffer pools contain a specified number of equal size buffers Any number of pools can be created Creation can be static (via TCONF) or dynamic (BUF_create / BUF_delete) Buffers are allocated from a pool and freed back when no longer needed Buffers can be shared between threads Buffer pool API are faster and smaller than malloc-type operations In addition, BUF_alloc and BUF_free are deterministic (unlike malloc) BUF API have no reentrancy or fragmentation issues Tip – larger buffers can be semi-filled by threads with smaller data blocks Cache and EDMA can cause coherency issues when using BUF or MEM API 20 September 2018 Dr. Veton Këpuska

39 BUF_alloc() & BUF_free()
pMyBuf = BUF_alloc(hPool) get a buffer from buffer pool hPool' and initializes ‘pMyBuf' with the base address of the borrowed buffer bStatus = BUF_free(hPool, pMyBuf) returns the buffer pointed to by ‘pMyBuf' to pool ‘hPool' Complement to BUF_alloc example of use: extern BUF_Obj bufferPool; BUF_Handle hPool = &bufferPool; Ptr pMyBuf; pMyBuf = BUF_alloc(hPool); if (pMyBuf == NULL ) { SYS_abort("BUF_alloc failed");} // thread can use buffer freely now... bStatus = BUF_free(hPool, pMyBuf); if(bStatus==0) { LOG_printf(&trace,"BUF_delete failed!"); } 20 September 2018 Dr. Veton Këpuska

40 BUF_maxbuff() & BUF_stat()
uCount = BUF_maxbuff(hPool); Returns maximum number of free buffers available currently Useful for fundamental system tuning and diagnostics BUF_stat(hPool, pBufInfo); Information from the pool object is copied to the BufInfo structure Useful for additional system tuning and diagnostics example of use: typedef struct BUF_Stat { MEM_sizep postalignsize; // Size after align MEM_sizep size; // Original size of buffer Uns totalbuffers; // Total # of buffers in pool Uns freebuffers; // # of free buffers in pool } BUF_Stat; BUF_Stat bufInfo; extern BUF_Obj bufferPool; BUF_Handle hPool = &bufferPool; BUF_stat(hPool, &bufInfo); LOG_printf(&trace, "Free buffers Available: %d",bufInfo.freebuffers); 20 September 2018 Dr. Veton Këpuska

41 GCONF Creation of Buffer Pool
Creating a BUF 1. right click on BUF mgr 2. select “insert BUF” 3. type BUF name 4. right click on new BUF 5. select “properties” 6. indicate desired Memory segment Number of buffers Size of buffers Alignment of buffers Gray boxes indicate effective pool and buffer sizes 20 September 2018 Dr. Veton Këpuska

42 TCONF Creation of Buffer Pool
TCONF programming essentially mirrors the BUF_create arguments: BUF.OBJMEMSEG = prog.get(“IRAM"); // mem segment for all pool objects var myPool = BUF.create("myBuf"); myPool.bufCount = 8; // number of buffers in 'myPool' pool myPool.size = 512*sizeof(short); // size of each 'myPool' buffer in MADUs myPool.align = 2; // alignment applied to each buffer in myPool myPool.bufSeg = prog.get(“IRAM"); // mem segment for 'myPool' buffers myPool = BUF_create(numbuf, size, align, attrs) typedef struct BUF_Attrs { Int segid; } BUF_Attrs; 20 September 2018 Dr. Veton Këpuska

43 BUF_create() & BUF_delete()
hPool = BUF_create(numbuf, size, align, attrs) Dynamically create a buffer pool Arguments: number of buffers in pool, their size and alignment, heap to draw from Returns handle to pool object Calls MEM_alloc() to obtain memory from heap bStatus = BUF_delete(hPool) Frees a dynamically created buffer pool back to heap Complement to BUF_create Status: 1=success, 0=fail Calls MEM_free to implement example of use: typedef struct BUF_Attrs { Int segid; } BUF_Attrs; 20 September 2018 Dr. Veton Këpuska

44 BUF_create() & BUF_delete()
BUF_Handle hPool; BUF_Attrs *myAttrs; myAttrs = &BUF_ATTRS; hPool=BUF_create(8, 1024, 2, &myAttrs); if( hPool == NULL ){LOG_printf(&trace,"BUF_create failed!");} // buffer pool can now be used as long as desired... bStatus = BUF_delete(hPool); if( bStatus == 0 ){LOG_printf(&trace,"BUF_delete failed!");} 20 September 2018 Dr. Veton Këpuska

45 BUF API Review BUF API Description BUF_alloc
Allocate a buffer from the pool BUF_free Return a buffer to the pool BUF_create Dynamically create a pool of buffers BUF_delete Delete a dynamically created buffer pool BUF_maxbuff Interrogate maximum # buffers taken from pool BUF_stat Get pool info (buf size, # free bufs, total # bufs in pool) 20 September 2018 Dr. Veton Këpuska

46 BUF Structures typedef struct BUF_Obj {
Ptr startaddr; // Start addr of buffer pool MEM_sizep size; // Size before alignment MEM_sizep postalignsize; // Size after align Ptr nextfree; // Ptr to next free buffer Uns totalbuffers; // # of buffers in pool Uns freebuffers; // # of free buffers in pool Int segid; // Mem seg for buffer pool } BUF_Obj, *BUF_Handle; typedef struct BUF_Attrs { Int segid; // segment for element allocation } BUF_Attrs; 20 September 2018 Dr. Veton Këpuska

47 BUF Structures BUF_Attrs BUF_ATTRS = { // default attributes 0, };
typedef struct BUF_Stat { MEM_sizep postalignsize; // Size after align MEM_sizep size; // Original size of buffer Uns totalbuffers; // Total buffers in pool Uns freebuffers; // # of free buffers in pool } BUF_Stat; 20 September 2018 Dr. Veton Këpuska

48 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

49 Review of Dynamic Systems
20 September 2018 Dr. Veton Këpuska

50 Review of Dynamic Systems
MOD_create() and MOD_delete() Objects created as/when desired Identical to static objects when executed May be deleted when no longer required Reduced RAM requirement Larger code space, slower startup System can adapt ‘on the fly’ MOD_create() returns a handle to the new object MOD_delete() API are void return with handle arg 20 September 2018 Dr. Veton Këpuska

51 Review of Dynamic Systems
MEM API Allow dynamic array create and delete Superset of malloc() and free() Able to select from multiple heaps + alignment Offers the most flexibility and memory savings ability Issues: determinism, reentrancy, fragmentation BUF API Allows sharing of buffers amongst threads Buffers within a given pool are equal sized Deterministic, reentrant, nonfragmenting 20 September 2018 Dr. Veton Këpuska

52 1 2 3 4 5 6 Objectives Concepts Memory Allocation (MEM)
Dynamic Objects 3 Buffer Pools (BUF) 4 Review 5 Lab 6 20 September 2018 Dr. Veton Këpuska 2

53 Lab 20 September 2018 Dr. Veton Këpuska

54 Lab 10 Apply any one or group of dynamic constructs to the prior lab
Measure speed and memory to see what changes there and were 20 September 2018 Dr. Veton Këpuska

55 Lab10 – Dynamic Systems Use the KOV to observe memory objects in the following lab exercises: Stream Buffer Creation: BUF_alloc() Stream Buffer Deletion : BUF_free() Memory Allocation : MEM_alloc(), MEM_free() Dynamic Task : TSK_create(), _delete() 20 September 2018 Dr. Veton Këpuska

56 Double Buffered SIO Create/Delete
// create phase… hSioIn = SIO_create(“/dioIn” , SIO_INPUT , SIZE, attrs); hSioOut = SIO_create(“/dioOut”, SIO_OUTPUT, SIZE, attrs); pBufIn = MEM_alloc(ISRAM, BFSZ, 2); pBufIn2 = MEM_alloc(ISRAM, BFSZ, 2); pBufOut = MEM_alloc(ISRAM, BKSZ, 2); pBufOut2 = MEM_alloc(ISRAM, BKSZ, 2); // delete phase… status = MEM_free(ISRAM, pBufOut2, BKFZ); status = MEM_free(ISRAM, pBufOut, BKSZ); status = MEM_free(ISRAM, pBufIn2, BFSZ); status = MEM_free(ISRAM, pBufIn, BFSZ); status = SIO_delete(hSioIn); status = SIO_delete(hSioOut); 20 September 2018 Dr. Veton Këpuska

57 BIOS Instrumentation: Dynamic Systems
END 20 September 2018 Dr. Veton Këpuska


Download ppt "Dynamic Systems (MEM, BUF)"

Similar presentations


Ads by Google