1 /*******************************************************************************
2 ################################################################################
3 # Copyright (c) [2017-2019] [Radisys] #
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 #
9 # http://www.apache.org/licenses/LICENSE-2.0 #
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 *******************************************************************************/
20 /********************************************************************20**
22 Name: Common Memory Manager
26 Desc: Data structure definitions that are used by
27 the Common Memory Manager.
31 Sid: cm_mem.x@@/main/13 - Fri Feb 26 13:16:42 2010
35 *********************************************************************21*/
43 /*cm_mem_x_001.main_12: As these required for the following only */
44 #if (defined(SS_HISTOGRAM_SUPPORT) || defined(SS_MEM_LEAK_STS))
45 #include "cm_hash.h" /* cm_mem_x_001.main_11: Additions */
47 #endif /* SS_HISTOGRAM_SUPPORT */
52 typedef struct cmMmRegCfg CmMmRegCfg;
53 typedef struct cmMmBktCfg CmMmBktCfg;
56 typedef struct cmMmRegCb CmMmRegCb;
57 typedef struct cmMmMapBkt CmMmMapBkt;
58 typedef struct cmMmHeapCb CmMmHeapCb;
59 typedef struct cmMmBkt CmMmBkt;
60 typedef struct cmHEntry CmHEntry;
61 /* cm_mem_x_001.main_9 - addition of new data types for providing
62 * header for memory blocks in buckets and a hash list to maintain
63 * size vs. numAttempts statistics
65 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
66 typedef struct cmMmBlkHdr CmMmBlkHdr;
67 typedef struct cmMmHashListEnt CmMmHashListEnt;
68 typedef struct cmMmHashListCp CmMmHashListCp;
69 #endif /* SSI_DEBUG_LEVEL1 */
70 typedef Data CmMmEntry;
71 /* cm_mem_x_001.main_11: Additions */
72 #ifdef SS_HISTOGRAM_SUPPORT
73 typedef struct cmHstGrmHashListCp CmHstGrmHashListCp;
74 typedef struct cmHstGrmHashListEnt CmHstGrmHashListEnt;
75 typedef struct cmMemEntries CmMemEntries;
76 #endif /* SS_HISTOGRAM_SUPPORT */
78 /* forward definitions */
81 /************************************************************************
82 Common Memory Manager Configuration Data Structures
83 ************************************************************************/
85 /* Bucket configuration structure. */
88 Size size; /* Size of the memory block */
89 U32 numBlks; /* Number of the block in the bucket */
92 /* Memory Region configuration structure. */
95 Size size; /* Size of the memory */
96 Data *vAddr; /* Start address of the memory */
97 U8 lType; /* Lock Type to be used */
99 U8 chFlag; /* Flag defines the memory region characteristics */
100 Data *pAddr; /* Physical address of the memory block: Valid
101 if CMM_REG_PHY_VALID bit of chFlag is set */
102 Size bktQnSize; /* Quatum size of the memory block */
103 U16 numBkts; /* Number of buckets in the Bucket Pool */
105 CmMmBktCfg bktCfg[CMM_MAX_BKT_ENT]; /* Bucket configuration structure */
108 /************************************************************************
109 Common Memory Manager Implementation Data Structures
110 ************************************************************************/
111 /* cm_mem_x_001.main_11: Additions */
112 #ifdef SS_HISTOGRAM_SUPPORT
113 struct cmMemEntries /* Entry information */
115 U32 line; /* Line Number where memory allocation primitive used */
116 U32 allocBytes; /* Granted Byte */
117 U32 allocOverFlow; /* If the value of allocBytes overflows then we use this variable */
118 U32 freedBytes; /* Freed Bytes */
119 U32 freeOverFlow; /* If the value of freeByte overflows then we use this value */
120 U32 wastedBytes; /* Difference between Granted and Requested bytes */
121 U32 bucketAllocReq; /* No of times this Bucket used */
122 U32 bucketFreeReq; /* No of times this Bucket freed */
123 U32 entId; /* Tapa task used this bucket */
124 U32 key; /* combination of file name and line */
125 U8 fileName[CMM_HIST_MAX_FILENAME]; /* File Name where memory allocation primitive used */
128 struct cmHstGrmHashListEnt /* hash list entry */
130 CmMemEntries entries[CMM_HIST_MAX_MEM_ENTRY_PER_BIN]; /* Number of entires in each bin */
131 U32 numOfEntries; /*Number of entries in hash list */
134 struct cmHstGrmHashListCp /* Hash List control pointer */
136 CmHstGrmHashListEnt hashList[CMM_HIST_MAX_MEM_BIN]; /* Number of bins in hash list */
137 U16 totalNumEntries; /* Total number of bins */
140 #endif /* SS_HISTOGRAM_SUPPORT */
142 /* cm_mem_x_001.main_9 - addition of memory header for blocks in buckets */
143 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
144 /* Memory Block Header */
147 /* trSignature shall always be maintained as the first element in this structure */
148 U8 trSignature[CMM_TRAMPLING_SIGNATURE_LEN]; /* signature to detect trampling */
149 CmMmBlkHdr *nextBlk; /* pointer to the next memory block header */
150 U32 memFlags; /* Free/Static/Dynamic */
151 Size requestedSize; /* requested size for which this mem blk has been allocated */
152 #ifdef SS_LIGHT_MEM_LEAK_STS
153 U32 timeStamp; /* Time stamp */
158 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
159 CmHashListEnt ent; /* hash list entry */
160 Size reqSz; /* Requested size */
161 Size allocSz;/* Allocated size */
162 U8 bktIdx; /* Bucket ID */
163 Void *backTrace[BRDCM_MEM_LEAK_BTRACE]; /* Back trace */
166 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
167 typedef struct cmMmBlkTail
169 U8 trSignature[CMM_TRAMPLING_SIGNATURE_LEN]; /* signature to detect trampling */
171 typedef struct memLkInfo
177 #endif /* SSI_DEBUG_LEVEL1 */
179 /* Memory bucket structure */
180 struct cmMmBkt /* Bucket Structure */
182 /* cm_mem_x_001.main_9 - addition of next block pointer */
183 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
184 CmMmBlkHdr *nextBlk; /* pointer to the next memory block header */
185 #if (defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1) || defined (SS_LIGHT_MEM_LEAK_STS))
186 CmMmBlkHdr *lastBlk; /* Pointer to the last memory block header */
189 CmMmEntry *next; /* Pointer to the next memory block */
190 #ifdef SSI_MEM_CORR_PREVENTION
191 CmMmEntry *last; /* Pointer to the last memory block */
193 #ifdef MS_MBUF_CORRUPTION /*PST Should not take code Under this flag*/
194 CmMmEntry *last; /* Pointer to the last memory block */
196 #endif /* SSI_DEBUG_LEVEL1 */
197 Size size; /* Size of the block */
198 U32 numBlks; /* Total number of blocks in the bucket */
199 U32 maxAlloc; /* Maximum number of blocks allocated till now */
200 U32 numAlloc; /* Number of blocks allocated */
201 SLockId bktLock; /* Lock to protect the bucket pool */
202 U32 bktFailCnt; /* bucket alloc failure count */
203 U32 bktNoFitCnt; /* number of blocks not fit */
204 /* cm_mem_x_001.main_9 - addition of statistics related data */
205 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
206 Data *bktStartPtr; /* maintains the start address of the memory block in the bucket */
207 StsCntr numAllocAttempts; /* total number of allocation attempts */
208 StsCntr numDeallocAttempts; /* total number of de-allocation attempts */
209 Size staticMemUsed; /* amount of memory used for static allocations */
210 Size dynamicMemUsed; /* amount of memory used for dynamic allocations */
211 U8 trampleCount; /* incremented everytime a trampling is detected in any block of the bucket */
212 #endif /*SSI_DEBUG_LEVEL1*/
213 /* cm_mem_x_001.main_11: Additions */
214 #ifdef SS_HISTOGRAM_SUPPORT
215 CmHstGrmHashListCp hstGrmHashListCp;
216 #endif /* SS_HISTOGRAM_SUPPORT */
220 /* Size-To-Bucket map table structure */
223 U16 bktIdx; /* The index to the memory bucket */
225 #if (ERRCLASS & ERRCLS_DEBUG)
226 U16 numReq; /* Number of the allocation request */
227 U16 numFailure; /* Number of allocation failure form the bucket */
232 /* Heap entry structure linked in the heap control block */
235 /* cm_mem_x_001.main_9 - addition to provide trampling and double-free detections support */
236 #ifdef SSI_DEBUG_LEVEL1
237 /* trSignature shall always be maintianed as the first element in this structure */
238 U8 trSignature[CMM_TRAMPLING_SIGNATURE_LEN]; /* signature to detect trampling */
239 U32 memFlags; /* flag to maintain the memory status */
240 Size requestedSize; /* size of the heap entry block */
241 #endif /* SSI_DEBUG_LEVEL1 */
242 CmHEntry *next; /* Pointer to the next entry block */
243 Size size; /* size of the heap entry block */
246 /* Heap control block */
249 Data *vStart; /* Memory start address */
250 Data *vEnd; /* Memory end address */
251 CmHEntry *next; /* Next heap block entry */
252 Size avlSize; /* Total available memory */
253 Size minSize; /* Minimum size that can be allocated */
254 SLockId heapLock; /* Lock to protect the heap pool */
255 #if (ERRCLASS & ERRCLS_DEBUG)
256 U16 numFragBlk; /* Number of fragmented block */
257 U16 numReq; /* Number of allocation request */
258 U16 numFailure; /* Number of allocation failure */
260 U32 heapAllocCnt; /* Number of blocks from heap */
261 /* cm_mem_x_001.main_9 - addition of statistics related data */
262 #ifdef SSI_DEBUG_LEVEL1
263 Size staticHeapMemUsed; /* amount of heap memory used for static allocations */
264 Size dynamicHeapMemUsed; /* amount of heap memory used for dynamic allocations */
265 Size nextOffset; /* Offset to the next pointer in structure CmHEntry */
266 /* Update this in cmMmHeapInit if cmHEntry is changed */
267 StsCntr numAllocAttempts; /* total number of allocation attempts in heap */
268 StsCntr numDeallocAttempts; /* total number of de-allocation attempts in heap */
269 U8 trampleCount; /* incremented everytime a trampling is detected in any block of the heap */
270 #endif /* SSI_DEBUG_LEVEL1 */
271 /* cm_mem_x_001.main_11: Additions */
272 #ifdef SS_HISTOGRAM_SUPPORT
273 /* Hash list to support Heap Block histogram */
274 CmHstGrmHashListCp heapHstGrmHashListCp;
275 #endif /* SS_HISTOGRAM_SUPPORT */
279 /* cm_mem_x_001.main_9 - addition of hash list for maintaining size vs. numAttempts statistics */
280 #ifdef SSI_DEBUG_LEVEL1
281 struct cmMmHashListEnt /* hash list entry */
283 U32 size; /* pointer to key */
284 U32 numAttempts; /* number of attempts */
287 struct cmMmHashListCp
289 CmMmHashListEnt *hashList;
293 #endif /* SSI_DEBUG_LEVEL1 */
295 /* Memory region control block */
298 Region region; /* Region Id of the memory */
299 SRegInfo regInfo; /* Region information block */
301 U8 chFlag; /* Flag defines the region characteristics */
302 Data *pAddr; /* Physical address of the memory block.
303 Valid if CMM_REG_PHY_VALID bit is set */
305 Size bktSize; /* Size of the memory used for the bucket pool */
306 U16 bktQnPwr; /* Quantum size of the bucket pool */
307 Size bktMaxBlkSize; /* Maximum size of block in the bucket pool */
308 U16 numBkts; /* Number of buckets in the Bucket Pool */
310 CmMmMapBkt mapTbl[CMM_MAX_MAP_ENT]; /* size-to-bucket map table */
311 CmMmBkt bktTbl[CMM_MAX_BKT_ENT]; /* Pointer to the memory bkt tbl */
313 Bool heapFlag; /* Set to true if the heap pool is configured */
314 Size heapSize; /* Size of the heap pool */
315 CmMmHeapCb heapCb; /* Heap pool control block */
316 /* cm_mem_x_001.main_9 - addition of hash list control point in region control block */
317 #ifdef SSI_DEBUG_LEVEL1
318 CmMmHashListCp hashListCp; /* hast list to maintain the size statistics */
319 #endif /* SSI_DEBUG_LEVEL1 */
320 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
321 CmHashListCp brdcmSsiLstCp; /* hast list to maintain the SSI memory information for Broadcom */
323 #ifdef SS_MULTICORE_SUPPORT /*cm_mem_x_001.main_10*/
325 #endif /* SS_MULTICORE_SUPPORT */
329 #ifdef SS_LIGHT_MEM_LEAK_STS
330 typedef struct allocInfo
336 typedef struct memLeakCb
340 AllocInfo allocInfo[CM_MAX_ALLOC_ENTERIES];
341 U32 queue[CM_MAX_ALLOC_ENTERIES];
346 EXTERN Void cmInitMemLeak ARGS((Void));
347 EXTERN Void cmDeinitMemLeak ARGS((Void));
348 EXTERN Void cmStartStopLeakLog ARGS((Void));
349 EXTERN U32 cmStorAllocBlk ARGS((Void *addr));
350 EXTERN Void cmRlsAllocBlk ARGS((U32 indx));
351 EXTERN Void cmPrintLeakLog ARGS((Void));
352 #endif /* SS_LIGHT_MEM_LEAK_STS */
354 /* cm_mem_x_001.main_11: Additions */
355 #ifdef SS_MEM_LEAK_STS
356 typedef struct _memHash
360 CmHashListCp memHashCp;
363 typedef struct _memAllocInfo
379 typedef struct _memLkCb
383 MemHash memUsrMdl[CM_MEM_USR_MDL][CM_MAX_HASH_PER_TSK];
386 typedef struct _memUsrMdlStr
392 #ifdef SS_MEM_LEAK_SOL
393 typedef struct backtrace
399 #endif /* SS_MEM_LEAK_SOL */
401 #endif /* SS_MEM_LEAK_STS */
403 #ifdef T2K_MEM_LEAK_DBG
404 #define T2K_MEM_LEAK_INFO_TABLE_SIZE 8388606
405 /* osCp.wls.allocAddr is the starting address allocated by WLS,
406 * whenever that changes pleasse change here */
407 U64 t2k_MEM_LEAK_START_ADDR;
410 typedef struct _t2kMemLeakInfo
412 U64 address; /* 0 means it is free */
413 U32 size; /* size of the block */
414 U32 age; /* in ticks, when it was allocated */
415 U32 lineNo; /* lineNo in the file from where allocation was done */
416 char* fileName; /* from where allocation was done */
417 U32 prevRemLineNo; /* lineNo in the file from where allocation was done */
418 char* prevRemFileName; /* from where allocation was done */
419 U32 lastDelLineNum; /* lineNo in the file from where allocation was done */
420 char* lastDelFileName; /* from where allocation was done */
423 typedef struct _regMemLeakInfo
426 T2kMeamLeakInfo *gMemLeakInfo[SS_MAX_REGS]; /* Allocate using calloc(system Call ) size T2K_MEM_LEAK_INFO_TABLE_SIZE*/
427 U64 regStartAddr[SS_MAX_REGS]; /* start Address of Region */
428 pthread_mutex_t memLock[SS_MAX_REGS]; /* Region Lock */
431 #endif /*T2K_MEM_LEAK_DBG */
434 /* functions prototypes */
436 EXTERN S16 cmMmRegInit ARGS((
440 EXTERN S16 cmMmRegDeInit ARGS(( CmMmRegCb *regCb));
441 /* cm_mem_x_001.main_11: Additions */
442 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL2
443 EXTERN Void cmRlsAllocBlk ARGS((CmMmBlkHdr *ptrHdr, CmMmRegCb *regCb));
444 EXTERN Void cmStorAllocBlk ARGS((CmMmBlkHdr *ptrHdr, Size reqSz, Size allocSz,
445 U16 bktIdx, CmMmRegCb *regCb));
447 #ifdef SS_MEM_LEAK_STS
448 EXTERN Void cmInitMemLeakMdl ARGS((Void));
449 /* cm_mem_x_001.main_12 : Additions */
450 EXTERN Void cmDeinitMemLeakMdl ARGS((Void));
452 EXTERN Void cmStorAllocBlk ARGS((U64 addr, Size reqSz, Size allocSz,
454 EXTERN Void cmRlsAllocBlk ARGS((U64 addr));
456 EXTERN Void cmStorAllocBlk ARGS((U32 addr, Size reqSz, Size allocSz,
458 EXTERN Void cmRlsAllocBlk ARGS((U32 addr));
460 EXTERN U8 cmMemGetModuleId ARGS((S8 **funNm, S32 traceSize));
461 EXTERN S16 cmMemGetStrMtchIdx ARGS((U8 strtIdx, U8 endIdx,
462 S8 *str, S8 **strLst));
463 EXTERN Void cmMemOpenMemLkFile ARGS((S8 *arg));
464 EXTERN Void SLogLkInfo ARGS((Void));
465 EXTERN Void SFlushLkInfo ARGS((Void));
467 #ifdef SS_MEM_LEAK_SOL
468 EXTERN S32 cmAddrToSymStr ARGS((Void *pc, S8 *buffer, S32 size));
469 EXTERN S32 cmLeakCallBack ARGS((uintptr_t pc, S32 signo, Void *arg));
470 EXTERN S32 backtrace ARGS((Void **buffer, S32 count));
471 #endif /* SS_MEM_LEAK_SOL */
473 #endif /* SS_MEM_LEAK_STS */
474 /* cm_mem_x_001.main_9 - addition of an API prototype for sanity check */
475 #ifdef SSI_DEBUG_LEVEL1
476 EXTERN S16 cmMmRegIsBlkSane ARGS((CmMmBlkHdr *blkPtr));
477 #endif /* SSI_DEBUG_LEVEL1 */
479 #ifdef BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1
480 EXTERN S16 cmMmRegIsBlkSane ARGS((CmMmBlkHdr *blkPtr, Size size));
482 #ifdef SS_HISTOGRAM_SUPPORT
483 #ifdef SSI_DEBUG_LEVEL1
484 EXTERN S16 cmAllocNL ARGS((Void *regionCb, Size *size, U32 flags, Data **ptr, U32 memType, U32 line, U8 *fileName, U8 entId, Bool hstReg));
486 EXTERN S16 cmAllocNL ARGS((Void *regionCb, Size *size, U32 flags, Data **ptr, U32 line, U8 *fileName, U8 entId, Bool hstReg));
488 #elif defined(SS_LIGHT_MEM_LEAK_STS)
489 EXTERN S16 cmAllocNL ARGS((Void *regionCb,Size *size,U32 flags,Data **ptr,U32 memType,U32 lineNo,U8 *funcName));
491 #if (defined(SSI_DEBUG_LEVEL1) || defined(BRDCM_SSI_MEM_LEAK_DEBUG_LEVEL1))
492 EXTERN S16 cmAllocNL ARGS((Void *regionCb, Size *size, U32 flags, Data **ptr, U32 memType));
494 EXTERN S16 cmAllocNL ARGS((Void *regionCb, Size *size, U32 flags, Data **ptr));
498 #ifdef SS_HISTOGRAM_SUPPORT
499 EXTERN S16 cmFreeNL ARGS((Void *regionCb, Data *ptr, Size size, U32 line, U8 *fileName, U8 entId, Bool hstReg));
500 #elif defined(SS_LIGHT_MEM_LEAK_STS)
501 EXTERN S16 cmFreeNL ARGS((Void *regionCb,Data *ptr, Size size,U32 lineNo,U8 *funcName));
503 EXTERN S16 cmFreeNL ARGS((Void *regionCb, Data *ptr, Size size));
512 /**********************************************************************
514 **********************************************************************/