[ Jira id - ODUHIGH-593 ] Pack and unpack function nomenclature correction
[o-du/l2.git] / src / cm / cm_mem_wl.x
1 /*******************************************************************************
2 ################################################################################
3 #   Copyright (c) [2017-2019] [Radisys]                                        #
4 #                                                                              #
5 #   Licensed under the Apache License, Version 2.0 (the "License");            #
6 #   you may not use this file except in compliance with the License.           #
7 #   You may obtain a copy of the License at                                    #
8 #                                                                              #
9 #       http://www.apache.org/licenses/LICENSE-2.0                             #
10 #                                                                              #
11 #   Unless required by applicable law or agreed to in writing, software        #
12 #   distributed under the License is distributed on an "AS IS" BASIS,          #
13 #   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.   #
14 #   See the License for the specific language governing permissions and        #
15 #   limitations under the License.                                             #
16 ################################################################################
17 *******************************************************************************/
18
19 \f
20 /********************************************************************20**
21  
22      Name:     Lockless Memory Manager 
23  
24      Type:     C include file
25  
26      Desc:     Data structure definitions that are used by 
27                the Lockless Memory Manager.
28  
29      File:     cm_mem_wl.x
30  
31 *********************************************************************21*/
32
33 #ifndef __CMMEMWLX_
34 #define __CMMEMWLX_
35
36 #ifdef __cplusplus
37 extern "C" {
38 #endif
39
40
41 #ifdef SS_LOCKLESS_MEMORY
42 /* typedefs */
43 typedef struct cmMmDynRegCb CmMmDynRegCb;
44 typedef struct cmMmDynBktCb CmMmDynBktCb;
45 typedef struct cmMmMapBkt  CmMmMapBkt;
46 typedef struct cmMmGlobRegCb CmMmGlobRegCb;
47 typedef struct cmMmGlobalBktCb CmMmGlobalBktCb;
48 typedef struct cmMmBlkSetElement CmMmBlkSetElement;
49 typedef Data CmMmEntry;
50  
51 /* Size-To-Bucket map table structure */ 
52 struct cmMmMapBkt
53 {
54    uint16_t   bktIdx;              /* The index to the memory bucket */
55
56 #if (ERRCLASS & ERRCLS_DEBUG)
57    uint16_t   numReq;              /* Number of the allocation request */
58    uint16_t   numFailure;          /* Number of allocation failure form the bucket */
59 #endif
60
61 };
62
63 /* The following structre is used as node to place the first address of the
64  * bucket list in the linkedlist. During initialization, all the buckets are 
65  * divided into limited number sets. The first pointer of the bucket is placed
66  * in the following structure and node is placed in the linked list. Once the one set
67  * get over, the next address in the BktCb is changed to new address */
68 struct cmMmBlkSetElement
69 {
70    CmLList    memSetNode;     /* Linked list of the first pointer of the 
71                                  bucket */
72    uint32_t        numFreeBlks;    /* Number of Free Blocks avilable in the current
73                                  set */
74    CmMmEntry  *nextBktPtr;    /* Address of the first bucket which will be
75                                  used for the allocation */
76 };
77
78 /* The following structure is used for the storing the buckets/pools
79  * this will be present for each bucket which is configured and 
80  * will contains the all the information about the bucket. This
81  * will be used in global region  */
82 struct cmMmGlobalBktCb
83 {
84    uint8_t           poolId;          /* Pool Id */
85    Size         size;            /* Size of the pool */
86    CmLListCp    listValidBktSet; /* LList Cp used to store the nodes of memory element
87                                     node which holds valid pointers */
88    CmLListCp    listFreeBktSet;  /* LList Cp used to store the nodes of memory element
89                                     node which holds invalid information */
90    CmMmEntry    *startAddr;      /* Starting address */
91    CmMmEntry    *next;           /* Next address which will be allocated */
92    uint32_t          numBlks;          /* Total number of buckets configured */
93    uint32_t          bucketSetSize;
94    uint32_t          bktFailCnt;      /* bucket alloc failure count */
95    uint32_t          bktNoFitCnt;     /* number of blocks not fit */
96    SLockId      bucketLock;      /* Lock used for the allocation/freeing of
97                                     the set of buckets */
98 };
99
100 /* The following structure is used for the storing the buckets/pools
101  * this will be present for each bucket which is configured and 
102  * will contains the all the information about the bucket. This
103  * is used in the system taks local region */
104 struct cmMmDynBktCb
105 {
106    uint8_t                 poolId;                   /* Pool Id */
107    Size               size;                     /* Size of the pool */
108    CmLListCp          memBlkSetElem;            /* Memory set element likend list */
109    CmMmBlkSetElement *crntMemBlkSetElem;        /* Pointer to the current memort block set */
110    CmMmEntry         *startAddr;               /* Starting address */
111    CmMmEntry         *next;                    /* Next address which will be allocated */
112    uint32_t               numBktAlloc;              /* Total number of buckets allocted */
113    uint32_t               numBkt;                   /* Total number of buckets configured */
114    uint16_t               bucketSetSize;            /* Size of bucket sets */
115    uint16_t               blkSetRelThreshold;       /* Upper threshold of bucket count upon
116                                                   reaching will free the set to global region */
117    uint16_t               blkSetAcquireThreshold;   /* Lower threshold of bucket count upon
118                                                   reaching will request the set from
119                                                   global region */
120    uint32_t               bktFailCnt;                /* bucket alloc failure count */
121    uint32_t               bktNoFitCnt;               /* number of blocks not fit */
122 };
123
124 /* The individual Task regionCb which will be associate with every system
125  * task which will be created. This contains the mapping of the size to
126  * to bucket index and the different bucket informations.*/
127 #ifdef SS_USE_ICC_MEMORY
128 struct cmMmDynRegCb
129 {
130    Region          region;                   /* Region  Id of the memory */
131    uint16_t             bktSize[CMM_MAX_BKT_ENT]; /* Bucket Size */
132    void            *iccHdl;                  /* Handler used for ICC memory */
133    SsAlloc         alloc;                    /* allocation function pointer */
134    SsFree          free;                     /* deallocation function pointer */
135    SsCtl           ctl;                      /* control request function pointer */
136 };
137 #else
138 struct cmMmDynRegCb
139 {
140    Region          region;                  /* Region  Id of the memory */
141    uint8_t              regType;                 /* Region Type, static or dynamic or global */
142    uint8_t              numBktBlk;               /* Keeps the count of number of bucket set blocks */
143    uint16_t             numBkts;                 /* Number of buckets in the Bucket Pool */
144    Size            bktSize;                 /* Size of the memory used for the bucket pool */
145    uint16_t             bktQnPwr;                /* Quantum size of the bucket pool */
146    Size            bktMaxBlkSize;           /* Maximum size of block in the bucket pool */
147    CmMmDynBktCb    bktTbl[CMM_MAX_BKT_ENT]; /* Pointer to the memory bkt tbl */
148    SsAlloc         alloc;                   /* allocation function pointer */
149    SsFree          free;                    /* deallocation function pointer */
150    SsCtl           ctl;                     /* control request function pointer */
151    CmMmMapBkt      mapTbl[CMM_MAX_MAP_ENT]; /* size-to-bucket map table */
152 };
153 #endif /* SS_USE_ICC_MEMORY */
154
155 /* The global region Cb which holds the entire buckets configured */
156 struct cmMmGlobRegCb
157 {
158    Region           region;                  /* Region  Id of the memory */
159    uint16_t              numBkts;                 /* Number of buckets in the Bucket Pool */
160    CmMmGlobalBktCb  bktTbl[CMM_MAX_BKT_ENT]; /* Pointer to the memory bkt tbl */
161 };
162
163 /* SSI_OPT: Stop */
164
165 /* typedefs */
166 typedef struct cmMmRegCfg    CmMmRegCfg;
167 typedef struct cmMmBktCfg    CmMmBktCfg;
168
169  
170 typedef struct   cmMmRegCb     CmMmRegCb;
171 typedef struct   cmMmHeapCb    CmMmHeapCb;
172 typedef struct   cmMmBkt       CmMmBkt;
173 typedef struct   cmHEntry      CmHEntry;
174 /* cm_mem_x_001.main_9 - addition of new data types for providing 
175 * header for memory blocks in buckets and a hash list to maintain
176 * size vs. numAttempts statistics 
177 */
178  /* cm_mem_x_001.main_11: Additions */
179 #ifdef SS_HISTOGRAM_SUPPORT 
180 typedef struct cmHstGrmHashListCp   CmHstGrmHashListCp;
181 typedef struct cmHstGrmHashListEnt  CmHstGrmHashListEnt;
182 typedef struct cmMemEntries         CmMemEntries;
183 #endif /* SS_HISTOGRAM_SUPPORT */
184  
185 /* forward definitions */
186
187 \f
188 /************************************************************************
189       Common Memory Manager Configuration Data Structures
190 ************************************************************************/
191
192 /* Bucket configuration structure. */
193 struct cmMmBktCfg
194 {
195    Size  size;              /* Size of the memory block */
196    uint32_t   numBlks;           /* Number of the block in the bucket */
197 };
198
199 /* Memory Region configuration structure. */ 
200 struct cmMmRegCfg
201 {
202    Size      size;          /* Size of the memory */
203    Data     *vAddr;         /* Start address of the memory */
204    uint8_t        lType;         /* Lock Type to be used */
205  
206    uint8_t        chFlag;        /* Flag defines the memory region characteristics */
207    Data     *pAddr;         /* Physical address of the memory block: Valid 
208                                if CMM_REG_PHY_VALID bit of chFlag is set */
209    Size      bktQnSize;     /* Quatum size of the memory block */
210    uint16_t       numBkts;       /* Number of buckets in the Bucket Pool */
211  
212    CmMmBktCfg  bktCfg[CMM_MAX_BKT_ENT];  /* Bucket configuration structure */
213 };
214  
215 /************************************************************************
216       Common Memory Manager Implementation Data Structures
217 ************************************************************************/
218 /* cm_mem_x_001.main_11: Additions */
219 #ifdef SS_HISTOGRAM_SUPPORT 
220 struct cmMemEntries /* Entry information */
221 {
222    uint32_t              line; /* Line Number where memory allocation primitive used */
223    uint32_t              allocBytes; /* Granted Byte */
224    uint32_t              allocOverFlow; /* If the value of allocBytes overflows then we use this variable */
225    uint32_t              freedBytes; /* Freed Bytes */
226    uint32_t              freeOverFlow; /* If the value of freeByte overflows then we use this value */
227    uint32_t              wastedBytes; /* Difference between Granted and Requested bytes */
228    uint32_t              bucketAllocReq; /* No of times this Bucket used */
229    uint32_t              bucketFreeReq; /* No of times this Bucket freed */
230    uint32_t              entId; /* Tapa task used this bucket */
231    uint32_t              key; /* combination of file name and line */
232    uint8_t               fileName[CMM_HIST_MAX_FILENAME]; /* File Name where memory allocation primitive used */
233 };
234
235 struct cmHstGrmHashListEnt    /* hash list entry */
236 {
237    CmMemEntries      entries[CMM_HIST_MAX_MEM_ENTRY_PER_BIN]; /* Number of entires in each bin */
238    uint32_t               numOfEntries; /*Number of entries in hash list */
239 };
240
241 struct cmHstGrmHashListCp /* Hash List control pointer */
242 {
243    CmHstGrmHashListEnt   hashList[CMM_HIST_MAX_MEM_BIN]; /* Number of bins in hash list */
244    uint16_t                   totalNumEntries; /* Total number of bins */
245 };
246
247 #endif /* SS_HISTOGRAM_SUPPORT */
248
249 /* cm_mem_x_001.main_9 - addition of memory header for blocks in buckets */
250 #ifdef SSI_DEBUG_LEVEL1
251 /* Memory Block Header */
252 struct cmMmBlkHdr
253 {
254    /* trSignature shall always be maintained as the first element in this structure */
255    uint8_t          trSignature[CMM_TRAMPLING_SIGNATURE_LEN];  /* signature to detect trampling */
256    CmMmBlkHdr  *nextBlk;         /* pointer to the next memory block header */
257    uint32_t         memFlags;         /* Free/Static/Dynamic */
258    Size        requestedSize;    /* requested size for which this mem blk has been allocated */
259 };
260 #endif /* SSI_DEBUG_LEVEL1 */
261
262 /* Memory bucket structure */
263 struct cmMmBkt                /* Bucket Structure */
264 {
265 /* cm_mem_x_001.main_9 - addition of next block pointer */
266 #ifdef SSI_DEBUG_LEVEL1
267    CmMmBlkHdr   *nextBlk;   /* pointer to the next memory block header */
268 #else
269    CmMmEntry   *next;         /* Pointer to the next memory block */
270 #endif /* SSI_DEBUG_LEVEL1 */
271    Size         size;         /* Size of the block */
272    uint32_t          numBlks;      /* Total number of blocks in the bucket */
273    uint32_t          maxAlloc;     /* Maximum number of blocks allocated till now */
274    uint32_t          numAlloc;     /* Number of blocks allocated */
275    SLockId      bktLock;      /* Lock to protect the bucket pool */ 
276    uint32_t          bktFailCnt;   /* bucket alloc failure count */
277    uint32_t          bktNoFitCnt;  /* number of blocks not fit */
278 /* cm_mem_x_001.main_9 - addition of statistics related data */
279 #ifdef SSI_DEBUG_LEVEL1
280    Data         *bktStartPtr;   /* maintains the start address of the memory block in the bucket */
281    StsCntr      numAllocAttempts;   /* total number of allocation attempts */
282    StsCntr      numDeallocAttempts;   /* total number of de-allocation attempts */
283    Size         staticMemUsed;   /* amount of memory used for static allocations */
284    Size         dynamicMemUsed;   /* amount of memory used for dynamic allocations */
285    uint8_t           trampleCount;   /* incremented everytime a trampling is detected in any block of the bucket */
286 #endif /*SSI_DEBUG_LEVEL1*/
287 /* cm_mem_x_001.main_11: Additions */
288 #ifdef SS_HISTOGRAM_SUPPORT 
289   CmHstGrmHashListCp hstGrmHashListCp;
290 #endif /* SS_HISTOGRAM_SUPPORT */
291 };
292
293
294 /* Size-To-Bucket map table structure */ 
295
296 /* Heap entry structure linked in the heap control block */ 
297 struct cmHEntry
298 {
299 /* cm_mem_x_001.main_9 - addition to provide trampling and double-free detections support */
300 #ifdef SSI_DEBUG_LEVEL1
301   /* trSignature shall always be maintianed as the first element in this structure */
302   uint8_t         trSignature[CMM_TRAMPLING_SIGNATURE_LEN];  /* signature to detect trampling */
303   uint32_t        memFlags;          /* flag to maintain the memory status */
304   Size       requestedSize;     /* size of the heap entry block */
305 #endif /* SSI_DEBUG_LEVEL1 */
306   CmHEntry  *next;             /* Pointer to the next entry block */
307   Size       size;             /* size of the heap entry block */
308 };
309
310 /* Heap control block */
311 struct cmMmHeapCb
312 {
313    Data     *vStart;           /* Memory start address */
314    Data     *vEnd;             /* Memory end address */
315    CmHEntry *next;             /* Next heap block entry */
316    Size      avlSize;          /* Total available memory */
317    Size      minSize;          /* Minimum size that can be allocated */
318    SLockId   heapLock;         /* Lock to protect the heap pool */
319 #if (ERRCLASS & ERRCLS_DEBUG)
320    uint16_t       numFragBlk;       /* Number of fragmented block */
321    uint16_t       numReq;           /* Number of allocation request */
322    uint16_t       numFailure;       /* Number of allocation failure */
323 #endif
324    uint32_t       heapAllocCnt;     /* Number of blocks from heap */
325 /* cm_mem_x_001.main_9 - addition of statistics related data */
326 #ifdef SSI_DEBUG_LEVEL1
327    Size     staticHeapMemUsed; /* amount of heap memory used for static allocations */
328    Size     dynamicHeapMemUsed; /* amount of heap memory used for dynamic allocations */
329    Size     nextOffset; /* Offset to the next pointer in structure CmHEntry */
330                         /* Update this in cmMmHeapInit if cmHEntry is changed */
331    StsCntr      numAllocAttempts;   /* total number of allocation attempts in heap */
332    StsCntr      numDeallocAttempts;   /* total number of de-allocation attempts in heap */
333    uint8_t          trampleCount;       /* incremented everytime a trampling is detected in any block of the heap */
334 #endif /* SSI_DEBUG_LEVEL1 */
335  /* cm_mem_x_001.main_11: Additions */
336 #ifdef SS_HISTOGRAM_SUPPORT
337    /* Hash list to support Heap Block histogram */
338    CmHstGrmHashListCp heapHstGrmHashListCp;
339 #endif /* SS_HISTOGRAM_SUPPORT */
340
341 };
342
343 /* cm_mem_x_001.main_9 - addition of hash list for maintaining size vs. numAttempts statistics */
344 #ifdef SSI_DEBUG_LEVEL1
345 struct cmMmHashListEnt    /* hash list entry */
346 {
347    uint32_t  size;             /* pointer to key */
348    uint32_t  numAttempts;      /* number of attempts */
349 };
350
351 struct cmMmHashListCp
352 {
353    CmMmHashListEnt  *hashList;
354    uint16_t  numOfbins;
355    uint16_t  numOfEntries;
356 };
357 #endif /* SSI_DEBUG_LEVEL1 */
358
359 /* Memory region control block */ 
360 struct cmMmRegCb
361 {
362    Region       region;        /* Region  Id of the memory */
363    SRegInfo     regInfo;       /* Region information block */
364
365    uint8_t           chFlag;        /* Flag defines the region characteristics */
366    Data        *pAddr;         /* Physical address of the memory block.
367                                   Valid if CMM_REG_PHY_VALID bit is set */
368  
369    Size         bktSize;       /* Size of the memory used for the bucket pool */
370    uint16_t          bktQnPwr;      /* Quantum size of the bucket pool */
371    Size         bktMaxBlkSize; /* Maximum size of block in the bucket pool */
372    uint16_t          numBkts;       /* Number of buckets in the Bucket Pool */
373
374    CmMmMapBkt   mapTbl[CMM_MAX_MAP_ENT]; /* size-to-bucket map table */
375    CmMmBkt      bktTbl[CMM_MAX_BKT_ENT]; /* Pointer to the memory bkt tbl */
376  
377    Bool         heapFlag;      /* Set to true if the heap pool is configured */
378    Size         heapSize;      /* Size of the heap pool */
379    CmMmHeapCb   heapCb;        /* Heap pool control block */
380 /* cm_mem_x_001.main_9 - addition of hash list control point in region control block */
381 #ifdef SSI_DEBUG_LEVEL1
382    CmMmHashListCp hashListCp;  /* hast list to maintain the size statistics */
383 #endif /* SSI_DEBUG_LEVEL1 */
384 #ifdef SS_MULTICORE_SUPPORT /*cm_mem_x_001.main_10*/
385    SsSTskEntry  *sTsk;
386 #endif /* SS_MULTICORE_SUPPORT */
387
388 };
389
390  /* cm_mem_x_001.main_11: Additions */
391 #ifdef SS_MEM_LEAK_STS
392 typedef struct _memHash
393 {
394    SLockId         memLck;
395    Bool            used;
396    CmHashListCp    memHashCp;
397 }MemHash;
398
399 typedef struct _memAllocInfo
400 {
401    CmHashListEnt         ent;
402    uint32_t                   memAddr;
403    Size                  reqSz;
404    Size                  allocSz;
405    uint16_t                   bktIdx;
406    uint8_t                    moduleId;
407    uint8_t                    bTrcSz;
408    PTR                   backTrace;
409 }MemAllocInfo; 
410
411 typedef struct _memLkCb
412 {
413    Bool        memLkMdlInit;
414    FILE        *fileLkLog;
415    MemHash     memUsrMdl[CM_MEM_USR_MDL][CM_MAX_HASH_PER_TSK];
416 }MemLkCb;
417
418 typedef struct _memUsrMdlStr
419 {
420    Txt   *fPStr;
421    Txt   *mdlStr; 
422 }MemUsrMdlStr;
423
424 #ifdef SS_MEM_LEAK_SOL
425 typedef struct backtrace
426 {
427   Void   **bt_buffer;
428   S32    bt_maxcount;
429   S32    bt_actcount;
430 } Backtrace_t;
431 #endif /* SS_MEM_LEAK_SOL */
432
433 #endif /* SS_MEM_LEAK_STS */
434
435 \f
436 /* functions prototypes */
437  
438 S16 cmMmRegInit   ARGS((
439                          Region region, 
440                          CmMmRegCb *regCb, 
441                          CmMmRegCfg *cfg));
442 S16 cmMmRegDeInit ARGS(( CmMmRegCb *regCb));
443  /* cm_mem_x_001.main_11: Additions */
444 #ifdef SS_MEM_LEAK_STS
445 Void cmInitMemLeakMdl    ARGS((Void));
446 /* cm_mem_x_001.main_12 : Additions */
447 Void cmDeinitMemLeakMdl    ARGS((Void));
448 Void cmStorAllocBlk      ARGS((uint32_t addr, Size reqSz, Size allocSz,
449                                       uint16_t bktIdx));
450 Void cmRlsAllocBlk       ARGS((uint32_t addr));
451 uint8_t   cmMemGetModuleId    ARGS((S8 **funNm, S32 traceSize));
452 S16  cmMemGetStrMtchIdx  ARGS((uint8_t strtIdx, uint8_t endIdx,
453                                       S8 *str, S8 **strLst));
454 Void cmMemOpenMemLkFile  ARGS((S8 *arg));
455 Void SLogLkInfo          ARGS((Void));
456 Void SFlushLkInfo        ARGS((Void));
457
458 #ifdef SS_MEM_LEAK_SOL
459 S32 cmAddrToSymStr       ARGS((Void *pc, S8 *buffer, S32 size));
460 S32 cmLeakCallBack       ARGS((uintptr_t pc, S32 signo, Void *arg));
461 S32 backtrace            ARGS((Void **buffer, S32 count));
462 #endif /* SS_MEM_LEAK_SOL */
463
464 #endif /* SS_MEM_LEAK_STS */
465 /* cm_mem_x_001.main_9 - addition of an API prototype for sanity check */
466 #ifdef SSI_DEBUG_LEVEL1
467 S16 cmMmRegIsBlkSane ARGS((CmMmBlkHdr *blkPtr));
468 #endif /* SSI_DEBUG_LEVEL1 */
469
470
471 Void DumpLayersDebugInformation        ARGS((Void));
472 Void DumpSSIDemandQDebugInformation    ARGS((Void));
473 Void DumpPDCPDlDebugInformation        ARGS((Void));
474 Void DumpPDCPUlDebugInformation        ARGS((Void));
475 Void DumpRLCDlDebugInformation         ARGS((Void));
476 Void DumpRLCUlDebugInformation         ARGS((Void));
477 Void DumpRLCUlDebugInformation         ARGS((Void));
478 Void printMacCellInfo                  ARGS((Void));
479 Void printSchCellInfo                  ARGS((Void));
480
481 uint32_t isMemThreshReached    ARGS((Region region));
482
483 S16 cmMmStatRegInit       ARGS((Region region, CmMmRegCb *regCb, CmMmRegCfg *cfg));
484 S16 cmMmGlobRegInit       ARGS((CmMmGlobRegCb *regCb));
485 S16 cmMmDynRegInit        ARGS((CmMmDynRegCb  *regCb));
486 #ifdef USE_MALLOC
487 S16 ssGetDynMemBlkSet  ARGS((uint8_t bktIdx, CmMmBlkSetElement *dynMemSetElem));
488 S16 ssPutDynMemBlkSet  ARGS((uint8_t bktIdx, CmMmBlkSetElement *dynMemSetElem));
489 #else
490 S16 ssPutDynMemBlkSet ARGS((uint8_t bktIdx,CmMmBlkSetElement *dynMemSetElem,
491                                    uint32_t doNotBlockForLock));
492 S16 ssGetDynMemBlkSet  ARGS((uint8_t bktIdx, CmMmBlkSetElement *dynMemSetElem,
493                                    uint32_t doNotBlockForLock));
494 #endif /* USE_MALLOC */
495 #endif /* SS_LOCKLESS_MEMORY */
496 #ifdef __cplusplus
497 }
498 #endif
499
500 #ifdef T2K_MEM_LEAK_DBG
501 #endif
502 #endif
503
504 /**********************************************************************
505          End of file
506 *********************************************************************/
507