17d0a6c19SBarry Smith 2e5c89e4eSSatish Balay /* 3e5c89e4eSSatish Balay Interface to malloc() and free(). This code allows for 4e5c89e4eSSatish Balay logging of memory usage and some error checking 5e5c89e4eSSatish Balay */ 6c6db04a5SJed Brown #include <petscsys.h> /*I "petscsys.h" I*/ 7665c2dedSJed Brown #include <petscviewer.h> 8e5c89e4eSSatish Balay #if defined(PETSC_HAVE_MALLOC_H) 9e5c89e4eSSatish Balay #include <malloc.h> 10e5c89e4eSSatish Balay #endif 11e5c89e4eSSatish Balay 12e5c89e4eSSatish Balay 13e5c89e4eSSatish Balay /* 14e5c89e4eSSatish Balay These are defined in mal.c and ensure that malloced space is PetscScalar aligned 15e5c89e4eSSatish Balay */ 16efca3c55SSatish Balay extern PetscErrorCode PetscMallocAlign(size_t,int,const char[],const char[],void**); 17efca3c55SSatish Balay extern PetscErrorCode PetscFreeAlign(void*,int,const char[],const char[]); 183221ece2SMatthew G. Knepley extern PetscErrorCode PetscReallocAlign(size_t,int,const char[],const char[],void**); 19efca3c55SSatish Balay extern PetscErrorCode PetscTrMallocDefault(size_t,int,const char[],const char[],void**); 20efca3c55SSatish Balay extern PetscErrorCode PetscTrFreeDefault(void*,int,const char[],const char[]); 213221ece2SMatthew G. Knepley extern PetscErrorCode PetscTrReallocDefault(size_t,int,const char[],const char[],void**); 22e5c89e4eSSatish Balay 23e5c89e4eSSatish Balay 240700a824SBarry Smith #define CLASSID_VALUE ((PetscClassId) 0xf0e0d0c9) 250700a824SBarry Smith #define ALREADY_FREED ((PetscClassId) 0x0f0e0d9c) 26e5c89e4eSSatish Balay 27e5c89e4eSSatish Balay typedef struct _trSPACE { 28e5c89e4eSSatish Balay size_t size; 29e5c89e4eSSatish Balay int id; 30e5c89e4eSSatish Balay int lineno; 31e5c89e4eSSatish Balay const char *filename; 32e5c89e4eSSatish Balay const char *functionname; 330700a824SBarry Smith PetscClassId classid; 348bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 35e5c89e4eSSatish Balay PetscStack stack; 36e5c89e4eSSatish Balay #endif 37e5c89e4eSSatish Balay struct _trSPACE *next,*prev; 38e5c89e4eSSatish Balay } TRSPACE; 39e5c89e4eSSatish Balay 4025b53cc9SJed Brown /* HEADER_BYTES is the number of bytes in a PetscMalloc() header. 4125b53cc9SJed Brown It is sizeof(TRSPACE) padded to be a multiple of PETSC_MEMALIGN. 4225b53cc9SJed Brown */ 43e5c89e4eSSatish Balay 44a64a8e02SBarry Smith #define HEADER_BYTES ((sizeof(TRSPACE)+(PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1)) 45e5c89e4eSSatish Balay 46e5c89e4eSSatish Balay 4725b53cc9SJed Brown /* This union is used to insure that the block passed to the user retains 4825b53cc9SJed Brown a minimum alignment of PETSC_MEMALIGN. 4925b53cc9SJed Brown */ 50e5c89e4eSSatish Balay typedef union { 51e5c89e4eSSatish Balay TRSPACE sp; 5225b53cc9SJed Brown char v[HEADER_BYTES]; 53e5c89e4eSSatish Balay } TrSPACE; 54e5c89e4eSSatish Balay 55b022a5c1SBarry Smith 56e5c89e4eSSatish Balay static size_t TRallocated = 0; 57e5c89e4eSSatish Balay static int TRfrags = 0; 58f0ba7cfcSLisandro Dalcin static TRSPACE *TRhead = NULL; 59e5c89e4eSSatish Balay static int TRid = 0; 60ace3abfcSBarry Smith static PetscBool TRdebugLevel = PETSC_FALSE; 61e5c89e4eSSatish Balay static size_t TRMaxMem = 0; 62e5c89e4eSSatish Balay /* 63e5c89e4eSSatish Balay Arrays to log information on all Mallocs 64e5c89e4eSSatish Balay */ 65f0ba7cfcSLisandro Dalcin static int PetscLogMallocMax = 10000; 66f0ba7cfcSLisandro Dalcin static int PetscLogMalloc = -1; 67574034a9SJed Brown static size_t PetscLogMallocThreshold = 0; 68e5c89e4eSSatish Balay static size_t *PetscLogMallocLength; 69efca3c55SSatish Balay static const char **PetscLogMallocFile,**PetscLogMallocFunction; 70e5c89e4eSSatish Balay 71e5c89e4eSSatish Balay #undef __FUNCT__ 72b022a5c1SBarry Smith #define __FUNCT__ "PetscSetUseTrMalloc_Private" 73b022a5c1SBarry Smith PetscErrorCode PetscSetUseTrMalloc_Private(void) 74b022a5c1SBarry Smith { 75b022a5c1SBarry Smith PetscErrorCode ierr; 76b022a5c1SBarry Smith 77b022a5c1SBarry Smith PetscFunctionBegin; 78b022a5c1SBarry Smith ierr = PetscMallocSet(PetscTrMallocDefault,PetscTrFreeDefault);CHKERRQ(ierr); 793221ece2SMatthew G. Knepley PetscTrRealloc = PetscTrReallocDefault; 80a297a907SKarl Rupp 81b022a5c1SBarry Smith TRallocated = 0; 82b022a5c1SBarry Smith TRfrags = 0; 83f0ba7cfcSLisandro Dalcin TRhead = NULL; 84b022a5c1SBarry Smith TRid = 0; 85b022a5c1SBarry Smith TRdebugLevel = PETSC_FALSE; 86b022a5c1SBarry Smith TRMaxMem = 0; 87b022a5c1SBarry Smith PetscLogMallocMax = 10000; 88b022a5c1SBarry Smith PetscLogMalloc = -1; 89b022a5c1SBarry Smith PetscFunctionReturn(0); 90b022a5c1SBarry Smith } 91b022a5c1SBarry Smith 92b022a5c1SBarry Smith #undef __FUNCT__ 93e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocValidate" 94e5c89e4eSSatish Balay /*@C 95e5c89e4eSSatish Balay PetscMallocValidate - Test the memory for corruption. This can be used to 96e5c89e4eSSatish Balay check for memory overwrites. 97e5c89e4eSSatish Balay 98e5c89e4eSSatish Balay Input Parameter: 99e5c89e4eSSatish Balay + line - line number where call originated. 100e5c89e4eSSatish Balay . function - name of function calling 101efca3c55SSatish Balay - file - file where function is 102e5c89e4eSSatish Balay 103e5c89e4eSSatish Balay Return value: 104e5c89e4eSSatish Balay The number of errors detected. 105e5c89e4eSSatish Balay 106e5c89e4eSSatish Balay Output Effect: 107e5c89e4eSSatish Balay Error messages are written to stdout. 108e5c89e4eSSatish Balay 109e5c89e4eSSatish Balay Level: advanced 110e5c89e4eSSatish Balay 111e5c89e4eSSatish Balay Notes: 112e5c89e4eSSatish Balay You should generally use CHKMEMQ as a short cut for calling this 113e5c89e4eSSatish Balay routine. 114e5c89e4eSSatish Balay 115efca3c55SSatish Balay The line, function, file are given by the C preprocessor as 116efca3c55SSatish Balay __LINE__, __FUNCT__, __FILE__ 117e5c89e4eSSatish Balay 118e5c89e4eSSatish Balay The Fortran calling sequence is simply PetscMallocValidate(ierr) 119e5c89e4eSSatish Balay 120e5c89e4eSSatish Balay No output is generated if there are no problems detected. 121e5c89e4eSSatish Balay 122e5c89e4eSSatish Balay .seealso: CHKMEMQ 123e5c89e4eSSatish Balay 124e5c89e4eSSatish Balay @*/ 125efca3c55SSatish Balay PetscErrorCode PetscMallocValidate(int line,const char function[],const char file[]) 126e5c89e4eSSatish Balay { 1276c093d5bSvictor TRSPACE *head,*lasthead; 128e5c89e4eSSatish Balay char *a; 1290700a824SBarry Smith PetscClassId *nend; 130e5c89e4eSSatish Balay 131e5c89e4eSSatish Balay PetscFunctionBegin; 1326c093d5bSvictor head = TRhead; lasthead = NULL; 133e5c89e4eSSatish Balay while (head) { 1340700a824SBarry Smith if (head->classid != CLASSID_VALUE) { 135efca3c55SSatish Balay (*PetscErrorPrintf)("PetscMallocValidate: error detected at %s() line %d in %s\n",function,line,file); 136e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory at address %p is corrupted\n",head); 137e5c89e4eSSatish Balay (*PetscErrorPrintf)("Probably write past beginning or end of array\n"); 138efca3c55SSatish Balay if (lasthead) (*PetscErrorPrintf)("Last intact block allocated in %s() line %d in %s\n",lasthead->functionname,lasthead->lineno,lasthead->filename); 139e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC," "); 140e5c89e4eSSatish Balay } 141e5c89e4eSSatish Balay a = (char*)(((TrSPACE*)head) + 1); 1420700a824SBarry Smith nend = (PetscClassId*)(a + head->size); 1430700a824SBarry Smith if (*nend != CLASSID_VALUE) { 144efca3c55SSatish Balay (*PetscErrorPrintf)("PetscMallocValidate: error detected at %s() line %d in %s\n",function,line,file); 145e5c89e4eSSatish Balay if (*nend == ALREADY_FREED) { 146e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory [id=%d(%.0f)] at address %p already freed\n",head->id,(PetscLogDouble)head->size,a); 147e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC," "); 148e5c89e4eSSatish Balay } else { 149e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory [id=%d(%.0f)] at address %p is corrupted (probably write past end of array)\n",head->id,(PetscLogDouble)head->size,a); 150efca3c55SSatish Balay (*PetscErrorPrintf)("Memory originally allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 151e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC," "); 152e5c89e4eSSatish Balay } 153e5c89e4eSSatish Balay } 1546c093d5bSvictor lasthead = head; 155e5c89e4eSSatish Balay head = head->next; 156e5c89e4eSSatish Balay } 157e5c89e4eSSatish Balay PetscFunctionReturn(0); 158e5c89e4eSSatish Balay } 159e5c89e4eSSatish Balay 160e5c89e4eSSatish Balay #undef __FUNCT__ 161e5c89e4eSSatish Balay #define __FUNCT__ "PetscTrMallocDefault" 162e5c89e4eSSatish Balay /* 163e5c89e4eSSatish Balay PetscTrMallocDefault - Malloc with tracing. 164e5c89e4eSSatish Balay 165e5c89e4eSSatish Balay Input Parameters: 166e5c89e4eSSatish Balay + a - number of bytes to allocate 167e5c89e4eSSatish Balay . lineno - line number where used. Use __LINE__ for this 168e5c89e4eSSatish Balay . function - function calling routine. Use __FUNCT__ for this 169efca3c55SSatish Balay - filename - file name where used. Use __FILE__ for this 170e5c89e4eSSatish Balay 171e5c89e4eSSatish Balay Returns: 172e5c89e4eSSatish Balay double aligned pointer to requested storage, or null if not 173e5c89e4eSSatish Balay available. 174e5c89e4eSSatish Balay */ 175efca3c55SSatish Balay PetscErrorCode PetscTrMallocDefault(size_t a,int lineno,const char function[],const char filename[],void **result) 176e5c89e4eSSatish Balay { 177e5c89e4eSSatish Balay TRSPACE *head; 178e5c89e4eSSatish Balay char *inew; 179e5c89e4eSSatish Balay size_t nsize; 180e5c89e4eSSatish Balay PetscErrorCode ierr; 181e5c89e4eSSatish Balay 182e5c89e4eSSatish Balay PetscFunctionBegin; 183f0ba7cfcSLisandro Dalcin /* Do not try to handle empty blocks */ 184f0ba7cfcSLisandro Dalcin if (!a) { *result = NULL; PetscFunctionReturn(0); } 185f0ba7cfcSLisandro Dalcin 186e5c89e4eSSatish Balay if (TRdebugLevel) { 187efca3c55SSatish Balay ierr = PetscMallocValidate(lineno,function,filename); if (ierr) PetscFunctionReturn(ierr); 188e5c89e4eSSatish Balay } 189e5c89e4eSSatish Balay 19025b53cc9SJed Brown nsize = (a + (PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1); 191efca3c55SSatish Balay ierr = PetscMallocAlign(nsize+sizeof(TrSPACE)+sizeof(PetscClassId),lineno,function,filename,(void**)&inew);CHKERRQ(ierr); 192e5c89e4eSSatish Balay 193e5c89e4eSSatish Balay head = (TRSPACE*)inew; 194e5c89e4eSSatish Balay inew += sizeof(TrSPACE); 195e5c89e4eSSatish Balay 196e5c89e4eSSatish Balay if (TRhead) TRhead->prev = head; 197e5c89e4eSSatish Balay head->next = TRhead; 198e5c89e4eSSatish Balay TRhead = head; 199f0ba7cfcSLisandro Dalcin head->prev = NULL; 200e5c89e4eSSatish Balay head->size = nsize; 201e5c89e4eSSatish Balay head->id = TRid; 202e5c89e4eSSatish Balay head->lineno = lineno; 203e5c89e4eSSatish Balay 204e5c89e4eSSatish Balay head->filename = filename; 205e5c89e4eSSatish Balay head->functionname = function; 2060700a824SBarry Smith head->classid = CLASSID_VALUE; 2070700a824SBarry Smith *(PetscClassId*)(inew + nsize) = CLASSID_VALUE; 208e5c89e4eSSatish Balay 209e5c89e4eSSatish Balay TRallocated += nsize; 210a297a907SKarl Rupp if (TRallocated > TRMaxMem) TRMaxMem = TRallocated; 211e5c89e4eSSatish Balay TRfrags++; 212e5c89e4eSSatish Balay 2138bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 21476386721SLisandro Dalcin if (PetscStackActive()) { 2155c25fcd7SBarry Smith ierr = PetscStackCopy(petscstack,&head->stack);CHKERRQ(ierr); 2162c9581d2SBarry Smith /* fix the line number to where the malloc() was called, not the PetscFunctionBegin; */ 2172c9581d2SBarry Smith head->stack.line[head->stack.currentsize-2] = lineno; 2189de0f6ecSBarry Smith } else { 2199de0f6ecSBarry Smith head->stack.currentsize = 0; 22076386721SLisandro Dalcin } 221e5c89e4eSSatish Balay #endif 222e5c89e4eSSatish Balay 223e5c89e4eSSatish Balay /* 224e5c89e4eSSatish Balay Allow logging of all mallocs made 225e5c89e4eSSatish Balay */ 226574034a9SJed Brown if (PetscLogMalloc > -1 && PetscLogMalloc < PetscLogMallocMax && a >= PetscLogMallocThreshold) { 227e5c89e4eSSatish Balay if (!PetscLogMalloc) { 228e5c89e4eSSatish Balay PetscLogMallocLength = (size_t*)malloc(PetscLogMallocMax*sizeof(size_t)); 229e32f2f54SBarry Smith if (!PetscLogMallocLength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 230a297a907SKarl Rupp 231a2ea699eSBarry Smith PetscLogMallocFile = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 232e32f2f54SBarry Smith if (!PetscLogMallocFile) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 233a297a907SKarl Rupp 234a2ea699eSBarry Smith PetscLogMallocFunction = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 235e32f2f54SBarry Smith if (!PetscLogMallocFunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 236e5c89e4eSSatish Balay } 237e5c89e4eSSatish Balay PetscLogMallocLength[PetscLogMalloc] = nsize; 238e5c89e4eSSatish Balay PetscLogMallocFile[PetscLogMalloc] = filename; 239e5c89e4eSSatish Balay PetscLogMallocFunction[PetscLogMalloc++] = function; 240e5c89e4eSSatish Balay } 241e5c89e4eSSatish Balay *result = (void*)inew; 242e5c89e4eSSatish Balay PetscFunctionReturn(0); 243e5c89e4eSSatish Balay } 244e5c89e4eSSatish Balay 245e5c89e4eSSatish Balay 246e5c89e4eSSatish Balay #undef __FUNCT__ 247e5c89e4eSSatish Balay #define __FUNCT__ "PetscTrFreeDefault" 248e5c89e4eSSatish Balay /* 249e5c89e4eSSatish Balay PetscTrFreeDefault - Free with tracing. 250e5c89e4eSSatish Balay 251e5c89e4eSSatish Balay Input Parameters: 252e5c89e4eSSatish Balay . a - pointer to a block allocated with PetscTrMalloc 253e5c89e4eSSatish Balay . lineno - line number where used. Use __LINE__ for this 254e5c89e4eSSatish Balay . function - function calling routine. Use __FUNCT__ for this 255e5c89e4eSSatish Balay . file - file name where used. Use __FILE__ for this 256e5c89e4eSSatish Balay */ 257efca3c55SSatish Balay PetscErrorCode PetscTrFreeDefault(void *aa,int line,const char function[],const char file[]) 258e5c89e4eSSatish Balay { 259e5c89e4eSSatish Balay char *a = (char*)aa; 260e5c89e4eSSatish Balay TRSPACE *head; 261e5c89e4eSSatish Balay char *ahead; 262e5c89e4eSSatish Balay PetscErrorCode ierr; 2630700a824SBarry Smith PetscClassId *nend; 264e5c89e4eSSatish Balay 265e5c89e4eSSatish Balay PetscFunctionBegin; 266e5c89e4eSSatish Balay /* Do not try to handle empty blocks */ 26749d7da52SJed Brown if (!a) PetscFunctionReturn(0); 268e5c89e4eSSatish Balay 269e5c89e4eSSatish Balay if (TRdebugLevel) { 270efca3c55SSatish Balay ierr = PetscMallocValidate(line,function,file);CHKERRQ(ierr); 271e5c89e4eSSatish Balay } 272e5c89e4eSSatish Balay 273e5c89e4eSSatish Balay ahead = a; 274e5c89e4eSSatish Balay a = a - sizeof(TrSPACE); 275e5c89e4eSSatish Balay head = (TRSPACE*)a; 276e5c89e4eSSatish Balay 2770700a824SBarry Smith if (head->classid != CLASSID_VALUE) { 278efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 279e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block at address %p is corrupted; cannot free;\nmay be block not allocated with PetscMalloc()\n",a); 280e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Bad location or corrupted memory"); 281e5c89e4eSSatish Balay } 2820700a824SBarry Smith nend = (PetscClassId*)(ahead + head->size); 2830700a824SBarry Smith if (*nend != CLASSID_VALUE) { 284e5c89e4eSSatish Balay if (*nend == ALREADY_FREED) { 285efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 286e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p was already freed\n",head->id,(PetscLogDouble)head->size,a + sizeof(TrSPACE)); 287e5c89e4eSSatish Balay if (head->lineno > 0 && head->lineno < 50000 /* sanity check */) { 288efca3c55SSatish Balay (*PetscErrorPrintf)("Block freed in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 289e5c89e4eSSatish Balay } else { 290efca3c55SSatish Balay (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,-head->lineno,head->filename); 291e5c89e4eSSatish Balay } 292e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Memory already freed"); 293e5c89e4eSSatish Balay } else { 294e5c89e4eSSatish Balay /* Damaged tail */ 295efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 296e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p is corrupted (probably write past end of array)\n",head->id,(PetscLogDouble)head->size,a); 297efca3c55SSatish Balay (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 298e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Corrupted memory"); 299e5c89e4eSSatish Balay } 300e5c89e4eSSatish Balay } 301e5c89e4eSSatish Balay /* Mark the location freed */ 302e5c89e4eSSatish Balay *nend = ALREADY_FREED; 303e5c89e4eSSatish Balay /* Save location where freed. If we suspect the line number, mark as allocated location */ 304e5c89e4eSSatish Balay if (line > 0 && line < 50000) { 305e5c89e4eSSatish Balay head->lineno = line; 306e5c89e4eSSatish Balay head->filename = file; 307e5c89e4eSSatish Balay head->functionname = function; 308e5c89e4eSSatish Balay } else { 309e5c89e4eSSatish Balay head->lineno = -head->lineno; 310e5c89e4eSSatish Balay } 311e5c89e4eSSatish Balay /* zero out memory - helps to find some reuse of already freed memory */ 312e5c89e4eSSatish Balay ierr = PetscMemzero(aa,head->size);CHKERRQ(ierr); 313e5c89e4eSSatish Balay 314e5c89e4eSSatish Balay TRallocated -= head->size; 315e5c89e4eSSatish Balay TRfrags--; 316e5c89e4eSSatish Balay if (head->prev) head->prev->next = head->next; 317e5c89e4eSSatish Balay else TRhead = head->next; 318e5c89e4eSSatish Balay 319e5c89e4eSSatish Balay if (head->next) head->next->prev = head->prev; 320efca3c55SSatish Balay ierr = PetscFreeAlign(a,line,function,file);CHKERRQ(ierr); 321e5c89e4eSSatish Balay PetscFunctionReturn(0); 322e5c89e4eSSatish Balay } 323e5c89e4eSSatish Balay 324e5c89e4eSSatish Balay 3253221ece2SMatthew G. Knepley 3263221ece2SMatthew G. Knepley #undef __FUNCT__ 3273221ece2SMatthew G. Knepley #define __FUNCT__ "PetscTrReallocDefault" 3283221ece2SMatthew G. Knepley /* 3293221ece2SMatthew G. Knepley PetscTrReallocDefault - Realloc with tracing. 3303221ece2SMatthew G. Knepley 3313221ece2SMatthew G. Knepley Input Parameters: 3323221ece2SMatthew G. Knepley + len - number of bytes to allocate 3333221ece2SMatthew G. Knepley . lineno - line number where used. Use __LINE__ for this 3343221ece2SMatthew G. Knepley . function - function calling routine. Use __FUNCT__ for this 3353221ece2SMatthew G. Knepley . filename - file name where used. Use __FILE__ for this 3363221ece2SMatthew G. Knepley - result - double aligned pointer to initial storage. 3373221ece2SMatthew G. Knepley 3383221ece2SMatthew G. Knepley Output Parameter: 3393221ece2SMatthew G. Knepley . result - double aligned pointer to requested storage, or null if not available. 3403221ece2SMatthew G. Knepley 3413221ece2SMatthew G. Knepley Level: developer 3423221ece2SMatthew G. Knepley 3433221ece2SMatthew G. Knepley .seealso: PetscTrMallocDefault(), PetscTrFreeDefault() 3443221ece2SMatthew G. Knepley */ 3453221ece2SMatthew G. Knepley PetscErrorCode PetscTrReallocDefault(size_t len, int lineno, const char function[], const char filename[], void **result) 3463221ece2SMatthew G. Knepley { 3473221ece2SMatthew G. Knepley char *a = (char *) *result; 3483221ece2SMatthew G. Knepley TRSPACE *head; 3493221ece2SMatthew G. Knepley char *ahead, *inew; 3503221ece2SMatthew G. Knepley PetscClassId *nend; 3513221ece2SMatthew G. Knepley size_t nsize; 3523221ece2SMatthew G. Knepley PetscErrorCode ierr; 3533221ece2SMatthew G. Knepley 3543221ece2SMatthew G. Knepley PetscFunctionBegin; 355*c22f1541SToby Isaac /* Realloc to zero = free */ 356*c22f1541SToby Isaac if (!len) { 357*c22f1541SToby Isaac ierr = PetscTrFreeDefault(*result,lineno,function,filename);CHKERRQ(ierr); 358*c22f1541SToby Isaac *result = NULL; 359*c22f1541SToby Isaac PetscFunctionReturn(0); 360*c22f1541SToby Isaac } 3613221ece2SMatthew G. Knepley 3623221ece2SMatthew G. Knepley if (TRdebugLevel) {ierr = PetscMallocValidate(lineno,function,filename); if (ierr) PetscFunctionReturn(ierr);} 3633221ece2SMatthew G. Knepley 3643221ece2SMatthew G. Knepley ahead = a; 3653221ece2SMatthew G. Knepley a = a - sizeof(TrSPACE); 3663221ece2SMatthew G. Knepley head = (TRSPACE *) a; 3673221ece2SMatthew G. Knepley inew = a; 3683221ece2SMatthew G. Knepley 3693221ece2SMatthew G. Knepley if (head->classid != CLASSID_VALUE) { 3703221ece2SMatthew G. Knepley (*PetscErrorPrintf)("PetscTrReallocDefault() called from %s() line %d in %s\n",function,lineno,filename); 3713221ece2SMatthew G. Knepley (*PetscErrorPrintf)("Block at address %p is corrupted; cannot free;\nmay be block not allocated with PetscMalloc()\n",a); 3723221ece2SMatthew G. Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Bad location or corrupted memory"); 3733221ece2SMatthew G. Knepley } 3743221ece2SMatthew G. Knepley nend = (PetscClassId *)(ahead + head->size); 3753221ece2SMatthew G. Knepley if (*nend != CLASSID_VALUE) { 3763221ece2SMatthew G. Knepley if (*nend == ALREADY_FREED) { 3773221ece2SMatthew G. Knepley (*PetscErrorPrintf)("PetscTrReallocDefault() called from %s() line %d in %s\n",function,lineno,filename); 3783221ece2SMatthew G. Knepley (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p was already freed\n",head->id,(PetscLogDouble)head->size,a + sizeof(TrSPACE)); 3793221ece2SMatthew G. Knepley if (head->lineno > 0 && head->lineno < 50000 /* sanity check */) { 3803221ece2SMatthew G. Knepley (*PetscErrorPrintf)("Block freed in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 3813221ece2SMatthew G. Knepley } else { 3823221ece2SMatthew G. Knepley (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,-head->lineno,head->filename); 3833221ece2SMatthew G. Knepley } 3843221ece2SMatthew G. Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Memory already freed"); 3853221ece2SMatthew G. Knepley } else { 3863221ece2SMatthew G. Knepley /* Damaged tail */ 3873221ece2SMatthew G. Knepley (*PetscErrorPrintf)("PetscTrReallocDefault() called from %s() line %d in %s\n",function,lineno,filename); 3883221ece2SMatthew G. Knepley (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p is corrupted (probably write past end of array)\n",head->id,(PetscLogDouble)head->size,a); 3893221ece2SMatthew G. Knepley (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 3903221ece2SMatthew G. Knepley SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Corrupted memory"); 3913221ece2SMatthew G. Knepley } 3923221ece2SMatthew G. Knepley } 3933221ece2SMatthew G. Knepley 3943221ece2SMatthew G. Knepley TRallocated -= head->size; 3953221ece2SMatthew G. Knepley TRfrags--; 3963221ece2SMatthew G. Knepley if (head->prev) head->prev->next = head->next; 3973221ece2SMatthew G. Knepley else TRhead = head->next; 3983221ece2SMatthew G. Knepley if (head->next) head->next->prev = head->prev; 3993221ece2SMatthew G. Knepley 4003221ece2SMatthew G. Knepley nsize = (len + (PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1); 4013221ece2SMatthew G. Knepley ierr = PetscReallocAlign(nsize+sizeof(TrSPACE)+sizeof(PetscClassId),lineno,function,filename,(void**)&inew);CHKERRQ(ierr); 4023221ece2SMatthew G. Knepley 4033221ece2SMatthew G. Knepley head = (TRSPACE*)inew; 4043221ece2SMatthew G. Knepley inew += sizeof(TrSPACE); 4053221ece2SMatthew G. Knepley 4063221ece2SMatthew G. Knepley if (TRhead) TRhead->prev = head; 4073221ece2SMatthew G. Knepley head->next = TRhead; 4083221ece2SMatthew G. Knepley TRhead = head; 4093221ece2SMatthew G. Knepley head->prev = NULL; 4103221ece2SMatthew G. Knepley head->size = nsize; 4113221ece2SMatthew G. Knepley head->id = TRid; 4123221ece2SMatthew G. Knepley head->lineno = lineno; 4133221ece2SMatthew G. Knepley 4143221ece2SMatthew G. Knepley head->filename = filename; 4153221ece2SMatthew G. Knepley head->functionname = function; 4163221ece2SMatthew G. Knepley head->classid = CLASSID_VALUE; 4173221ece2SMatthew G. Knepley *(PetscClassId*)(inew + nsize) = CLASSID_VALUE; 4183221ece2SMatthew G. Knepley 4193221ece2SMatthew G. Knepley TRallocated += nsize; 4203221ece2SMatthew G. Knepley if (TRallocated > TRMaxMem) TRMaxMem = TRallocated; 4213221ece2SMatthew G. Knepley TRfrags++; 4223221ece2SMatthew G. Knepley 4233221ece2SMatthew G. Knepley #if defined(PETSC_USE_DEBUG) 4243221ece2SMatthew G. Knepley if (PetscStackActive()) { 4253221ece2SMatthew G. Knepley ierr = PetscStackCopy(petscstack,&head->stack);CHKERRQ(ierr); 4263221ece2SMatthew G. Knepley /* fix the line number to where the malloc() was called, not the PetscFunctionBegin; */ 4273221ece2SMatthew G. Knepley head->stack.line[head->stack.currentsize-2] = lineno; 4283221ece2SMatthew G. Knepley } else { 4293221ece2SMatthew G. Knepley head->stack.currentsize = 0; 4303221ece2SMatthew G. Knepley } 4313221ece2SMatthew G. Knepley #endif 4323221ece2SMatthew G. Knepley 4333221ece2SMatthew G. Knepley /* 4343221ece2SMatthew G. Knepley Allow logging of all mallocs made 4353221ece2SMatthew G. Knepley */ 4363221ece2SMatthew G. Knepley if (PetscLogMalloc > -1 && PetscLogMalloc < PetscLogMallocMax && len >= PetscLogMallocThreshold) { 4373221ece2SMatthew G. Knepley if (!PetscLogMalloc) { 4383221ece2SMatthew G. Knepley PetscLogMallocLength = (size_t*)malloc(PetscLogMallocMax*sizeof(size_t)); 4393221ece2SMatthew G. Knepley if (!PetscLogMallocLength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 4403221ece2SMatthew G. Knepley 4413221ece2SMatthew G. Knepley PetscLogMallocFile = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 4423221ece2SMatthew G. Knepley if (!PetscLogMallocFile) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 4433221ece2SMatthew G. Knepley 4443221ece2SMatthew G. Knepley PetscLogMallocFunction = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 4453221ece2SMatthew G. Knepley if (!PetscLogMallocFunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 4463221ece2SMatthew G. Knepley } 4473221ece2SMatthew G. Knepley PetscLogMallocLength[PetscLogMalloc] = nsize; 4483221ece2SMatthew G. Knepley PetscLogMallocFile[PetscLogMalloc] = filename; 4493221ece2SMatthew G. Knepley PetscLogMallocFunction[PetscLogMalloc++] = function; 4503221ece2SMatthew G. Knepley } 4513221ece2SMatthew G. Knepley *result = (void*)inew; 4523221ece2SMatthew G. Knepley PetscFunctionReturn(0); 4533221ece2SMatthew G. Knepley } 4543221ece2SMatthew G. Knepley 4553221ece2SMatthew G. Knepley 456e5c89e4eSSatish Balay #undef __FUNCT__ 4570841954dSBarry Smith #define __FUNCT__ "PetscMemoryView" 458fe7fb379SMatthew Knepley /*@C 4590841954dSBarry Smith PetscMemoryView - Shows the amount of memory currently being used 460e5c89e4eSSatish Balay in a communicator. 461e5c89e4eSSatish Balay 462e5c89e4eSSatish Balay Collective on PetscViewer 463e5c89e4eSSatish Balay 464e5c89e4eSSatish Balay Input Parameter: 465e5c89e4eSSatish Balay + viewer - the viewer that defines the communicator 466e5c89e4eSSatish Balay - message - string printed before values 467e5c89e4eSSatish Balay 4680841954dSBarry Smith Options Database: 4690841954dSBarry Smith + -malloc - have PETSc track how much memory it has allocated 4700841954dSBarry Smith - -memory_view - during PetscFinalize() have this routine called 4710841954dSBarry Smith 472e5c89e4eSSatish Balay Level: intermediate 473e5c89e4eSSatish Balay 474e5c89e4eSSatish Balay Concepts: memory usage 475e5c89e4eSSatish Balay 4760841954dSBarry Smith .seealso: PetscMallocDump(), PetscMemoryGetCurrentUsage(), PetscMemorySetGetMaximumUsage() 477e5c89e4eSSatish Balay @*/ 4780841954dSBarry Smith PetscErrorCode PetscMemoryView(PetscViewer viewer,const char message[]) 479e5c89e4eSSatish Balay { 4800841954dSBarry Smith PetscLogDouble allocated,allocatedmax,resident,residentmax,gallocated,gallocatedmax,gresident,gresidentmax,maxgallocated,maxgallocatedmax,maxgresident,maxgresidentmax; 4810841954dSBarry Smith PetscLogDouble mingallocated,mingallocatedmax,mingresident,mingresidentmax; 482e5c89e4eSSatish Balay PetscErrorCode ierr; 483e5c89e4eSSatish Balay MPI_Comm comm; 484e5c89e4eSSatish Balay 485e5c89e4eSSatish Balay PetscFunctionBegin; 486e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD; 487e5c89e4eSSatish Balay ierr = PetscMallocGetCurrentUsage(&allocated);CHKERRQ(ierr); 4880841954dSBarry Smith ierr = PetscMallocGetMaximumUsage(&allocatedmax);CHKERRQ(ierr); 489e5c89e4eSSatish Balay ierr = PetscMemoryGetCurrentUsage(&resident);CHKERRQ(ierr); 490e5c89e4eSSatish Balay ierr = PetscMemoryGetMaximumUsage(&residentmax);CHKERRQ(ierr); 491e5c89e4eSSatish Balay if (residentmax > 0) residentmax = PetscMax(resident,residentmax); 492e5c89e4eSSatish Balay ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 493e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,message);CHKERRQ(ierr); 494e5c89e4eSSatish Balay if (resident && residentmax && allocated) { 4950841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 4960841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 4970841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 4980841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Maximum (over computational time) process memory: total %5.4e max %5.4e min %5.4e\n",gresidentmax,maxgresidentmax,mingresidentmax);CHKERRQ(ierr); 4990841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5000841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5010841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5020841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 5030841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&gallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5040841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&maxgallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5050841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&mingallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5060841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Maximum (over computational time) space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocatedmax,maxgallocatedmax,mingallocatedmax);CHKERRQ(ierr); 5070841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5080841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5090841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5100841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 511e5c89e4eSSatish Balay } else if (resident && residentmax) { 5120841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5130841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5140841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5150841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Maximum (over computational time) process memory: total %5.4e max %5.4e min %5.4e\n",gresidentmax,maxgresidentmax,mingresidentmax);CHKERRQ(ierr); 5160841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5170841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5180841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5190841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 520e5c89e4eSSatish Balay } else if (resident && allocated) { 5210841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5220841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5230841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5240841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 5250841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5260841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5270841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5280841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 5290841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr); 530e5c89e4eSSatish Balay } else if (allocated) { 5310841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 5320841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 5330841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 5340841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 5350841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr); 5360841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"OS cannot compute process memory\n");CHKERRQ(ierr); 537e5c89e4eSSatish Balay } else { 538e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"Run with -malloc to get statistics on PetscMalloc() calls\nOS cannot compute process memory\n");CHKERRQ(ierr); 539e5c89e4eSSatish Balay } 540e5c89e4eSSatish Balay ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 541e5c89e4eSSatish Balay PetscFunctionReturn(0); 542e5c89e4eSSatish Balay } 543e5c89e4eSSatish Balay 544e5c89e4eSSatish Balay #undef __FUNCT__ 545e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetCurrentUsage" 546e5c89e4eSSatish Balay /*@C 547e5c89e4eSSatish Balay PetscMallocGetCurrentUsage - gets the current amount of memory used that was PetscMalloc()ed 548e5c89e4eSSatish Balay 549e5c89e4eSSatish Balay Not Collective 550e5c89e4eSSatish Balay 551e5c89e4eSSatish Balay Output Parameters: 552e5c89e4eSSatish Balay . space - number of bytes currently allocated 553e5c89e4eSSatish Balay 554e5c89e4eSSatish Balay Level: intermediate 555e5c89e4eSSatish Balay 556e5c89e4eSSatish Balay Concepts: memory usage 557e5c89e4eSSatish Balay 558e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), 559e5c89e4eSSatish Balay PetscMemoryGetMaximumUsage() 560e5c89e4eSSatish Balay @*/ 5617087cfbeSBarry Smith PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *space) 562e5c89e4eSSatish Balay { 563e5c89e4eSSatish Balay PetscFunctionBegin; 564e5c89e4eSSatish Balay *space = (PetscLogDouble) TRallocated; 565e5c89e4eSSatish Balay PetscFunctionReturn(0); 566e5c89e4eSSatish Balay } 567e5c89e4eSSatish Balay 568e5c89e4eSSatish Balay #undef __FUNCT__ 569e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetMaximumUsage" 570e5c89e4eSSatish Balay /*@C 571e5c89e4eSSatish Balay PetscMallocGetMaximumUsage - gets the maximum amount of memory used that was PetscMalloc()ed at any time 572e5c89e4eSSatish Balay during this run. 573e5c89e4eSSatish Balay 574e5c89e4eSSatish Balay Not Collective 575e5c89e4eSSatish Balay 576e5c89e4eSSatish Balay Output Parameters: 577e5c89e4eSSatish Balay . space - maximum number of bytes ever allocated at one time 578e5c89e4eSSatish Balay 579e5c89e4eSSatish Balay Level: intermediate 580e5c89e4eSSatish Balay 581e5c89e4eSSatish Balay Concepts: memory usage 582e5c89e4eSSatish Balay 583e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), 584e5c89e4eSSatish Balay PetscMemoryGetCurrentUsage() 585e5c89e4eSSatish Balay @*/ 5867087cfbeSBarry Smith PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *space) 587e5c89e4eSSatish Balay { 588e5c89e4eSSatish Balay PetscFunctionBegin; 589e5c89e4eSSatish Balay *space = (PetscLogDouble) TRMaxMem; 590e5c89e4eSSatish Balay PetscFunctionReturn(0); 591e5c89e4eSSatish Balay } 592e5c89e4eSSatish Balay 593a64a8e02SBarry Smith #if defined(PETSC_USE_DEBUG) 594a64a8e02SBarry Smith #undef __FUNCT__ 595a64a8e02SBarry Smith #define __FUNCT__ "PetscMallocGetStack" 596a64a8e02SBarry Smith /*@C 597a64a8e02SBarry Smith PetscMallocGetStack - returns a pointer to the stack for the location in the program a call to PetscMalloc() was used to obtain that memory 598a64a8e02SBarry Smith 599a64a8e02SBarry Smith Collective on PETSC_COMM_WORLD 600a64a8e02SBarry Smith 601a64a8e02SBarry Smith Input Parameter: 602a64a8e02SBarry Smith . ptr - the memory location 603a64a8e02SBarry Smith 604a64a8e02SBarry Smith Output Paramter: 605a64a8e02SBarry Smith . stack - the stack indicating where the program allocated this memory 606a64a8e02SBarry Smith 607a64a8e02SBarry Smith Level: intermediate 608a64a8e02SBarry Smith 609a64a8e02SBarry Smith .seealso: PetscMallocGetCurrentUsage(), PetscMallocDumpLog() 610a64a8e02SBarry Smith @*/ 611a64a8e02SBarry Smith PetscErrorCode PetscMallocGetStack(void *ptr,PetscStack **stack) 612a64a8e02SBarry Smith { 613a64a8e02SBarry Smith TRSPACE *head; 614a64a8e02SBarry Smith 615a64a8e02SBarry Smith PetscFunctionBegin; 616a64a8e02SBarry Smith head = (TRSPACE*) (((char*)ptr) - HEADER_BYTES); 617a64a8e02SBarry Smith *stack = &head->stack; 618a64a8e02SBarry Smith PetscFunctionReturn(0); 619a64a8e02SBarry Smith } 62076386721SLisandro Dalcin #else 62176386721SLisandro Dalcin #undef __FUNCT__ 62276386721SLisandro Dalcin #define __FUNCT__ "PetscMallocGetStack" 62376386721SLisandro Dalcin PetscErrorCode PetscMallocGetStack(void *ptr,void **stack) 62476386721SLisandro Dalcin { 62576386721SLisandro Dalcin PetscFunctionBegin; 626f0ba7cfcSLisandro Dalcin *stack = NULL; 62776386721SLisandro Dalcin PetscFunctionReturn(0); 62876386721SLisandro Dalcin } 629a64a8e02SBarry Smith #endif 630a64a8e02SBarry Smith 631e5c89e4eSSatish Balay #undef __FUNCT__ 632e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDump" 633e5c89e4eSSatish Balay /*@C 634e5c89e4eSSatish Balay PetscMallocDump - Dumps the allocated memory blocks to a file. The information 635e5c89e4eSSatish Balay printed is: size of space (in bytes), address of space, id of space, 636e5c89e4eSSatish Balay file in which space was allocated, and line number at which it was 637e5c89e4eSSatish Balay allocated. 638e5c89e4eSSatish Balay 639e5c89e4eSSatish Balay Collective on PETSC_COMM_WORLD 640e5c89e4eSSatish Balay 641e5c89e4eSSatish Balay Input Parameter: 642e5c89e4eSSatish Balay . fp - file pointer. If fp is NULL, stdout is assumed. 643e5c89e4eSSatish Balay 644e5c89e4eSSatish Balay Options Database Key: 645e5c89e4eSSatish Balay . -malloc_dump - Dumps unfreed memory during call to PetscFinalize() 646e5c89e4eSSatish Balay 647e5c89e4eSSatish Balay Level: intermediate 648e5c89e4eSSatish Balay 649e5c89e4eSSatish Balay Fortran Note: 650e5c89e4eSSatish Balay The calling sequence in Fortran is PetscMallocDump(integer ierr) 651e5c89e4eSSatish Balay The fp defaults to stdout. 652e5c89e4eSSatish Balay 653e5c89e4eSSatish Balay Notes: uses MPI_COMM_WORLD, because this may be called in PetscFinalize() after PETSC_COMM_WORLD 654e5c89e4eSSatish Balay has been freed. 655e5c89e4eSSatish Balay 656e5c89e4eSSatish Balay Concepts: memory usage 657e5c89e4eSSatish Balay Concepts: memory bleeding 658e5c89e4eSSatish Balay Concepts: bleeding memory 659e5c89e4eSSatish Balay 6609e9a1f8fSvictor .seealso: PetscMallocGetCurrentUsage(), PetscMallocDumpLog() 661e5c89e4eSSatish Balay @*/ 6627087cfbeSBarry Smith PetscErrorCode PetscMallocDump(FILE *fp) 663e5c89e4eSSatish Balay { 664e5c89e4eSSatish Balay TRSPACE *head; 6655486ca60SMatthew G. Knepley PetscInt libAlloc = 0; 666e5c89e4eSSatish Balay PetscErrorCode ierr; 667e5c89e4eSSatish Balay PetscMPIInt rank; 668e5c89e4eSSatish Balay 669e5c89e4eSSatish Balay PetscFunctionBegin; 670e5c89e4eSSatish Balay ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 671da9f1d6bSBarry Smith if (!fp) fp = PETSC_STDOUT; 672e5c89e4eSSatish Balay head = TRhead; 673e5c89e4eSSatish Balay while (head) { 6745486ca60SMatthew G. Knepley PetscBool isLib; 6755486ca60SMatthew G. Knepley 6765486ca60SMatthew G. Knepley ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr); 6775486ca60SMatthew G. Knepley libAlloc += head->size; 6785486ca60SMatthew G. Knepley head = head->next; 6795486ca60SMatthew G. Knepley } 6805486ca60SMatthew G. Knepley if (TRallocated - libAlloc > 0) fprintf(fp,"[%d]Total space allocated %.0f bytes\n",rank,(PetscLogDouble)TRallocated); 6815486ca60SMatthew G. Knepley head = TRhead; 6825486ca60SMatthew G. Knepley while (head) { 6835486ca60SMatthew G. Knepley PetscBool isLib; 6845486ca60SMatthew G. Knepley 6855486ca60SMatthew G. Knepley ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr); 6865486ca60SMatthew G. Knepley if (!isLib) { 687efca3c55SSatish Balay fprintf(fp,"[%2d]%.0f bytes %s() line %d in %s\n",rank,(PetscLogDouble)head->size,head->functionname,head->lineno,head->filename); 6888bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 689e5c89e4eSSatish Balay ierr = PetscStackPrint(&head->stack,fp);CHKERRQ(ierr); 690e5c89e4eSSatish Balay #endif 6915486ca60SMatthew G. Knepley } 692e5c89e4eSSatish Balay head = head->next; 693e5c89e4eSSatish Balay } 694e5c89e4eSSatish Balay PetscFunctionReturn(0); 695e5c89e4eSSatish Balay } 696e5c89e4eSSatish Balay 697e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */ 698e5c89e4eSSatish Balay 699e5c89e4eSSatish Balay #undef __FUNCT__ 700e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocSetDumpLog" 701e5c89e4eSSatish Balay /*@C 702e5c89e4eSSatish Balay PetscMallocSetDumpLog - Activates logging of all calls to PetscMalloc(). 703e5c89e4eSSatish Balay 704e5c89e4eSSatish Balay Not Collective 705e5c89e4eSSatish Balay 706e5c89e4eSSatish Balay Options Database Key: 707574034a9SJed Brown + -malloc_log <filename> - Activates PetscMallocDumpLog() 708574034a9SJed Brown - -malloc_log_threshold <min> - Activates logging and sets a minimum size 709e5c89e4eSSatish Balay 710e5c89e4eSSatish Balay Level: advanced 711e5c89e4eSSatish Balay 712574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLogThreshold() 713e5c89e4eSSatish Balay @*/ 7147087cfbeSBarry Smith PetscErrorCode PetscMallocSetDumpLog(void) 715e5c89e4eSSatish Balay { 71621b680ceSJed Brown PetscErrorCode ierr; 71721b680ceSJed Brown 718e5c89e4eSSatish Balay PetscFunctionBegin; 719e5c89e4eSSatish Balay PetscLogMalloc = 0; 720a297a907SKarl Rupp 72121b680ceSJed Brown ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr); 722e5c89e4eSSatish Balay PetscFunctionReturn(0); 723e5c89e4eSSatish Balay } 724e5c89e4eSSatish Balay 725e5c89e4eSSatish Balay #undef __FUNCT__ 726574034a9SJed Brown #define __FUNCT__ "PetscMallocSetDumpLogThreshold" 727574034a9SJed Brown /*@C 728574034a9SJed Brown PetscMallocSetDumpLogThreshold - Activates logging of all calls to PetscMalloc(). 729574034a9SJed Brown 730574034a9SJed Brown Not Collective 731574034a9SJed Brown 732574034a9SJed Brown Input Arguments: 733574034a9SJed Brown . logmin - minimum allocation size to log, or PETSC_DEFAULT 734574034a9SJed Brown 735574034a9SJed Brown Options Database Key: 736574034a9SJed Brown + -malloc_log <filename> - Activates PetscMallocDumpLog() 737574034a9SJed Brown - -malloc_log_threshold <min> - Activates logging and sets a minimum size 738574034a9SJed Brown 739574034a9SJed Brown Level: advanced 740574034a9SJed Brown 741574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLog() 742574034a9SJed Brown @*/ 743574034a9SJed Brown PetscErrorCode PetscMallocSetDumpLogThreshold(PetscLogDouble logmin) 744574034a9SJed Brown { 745574034a9SJed Brown PetscErrorCode ierr; 746574034a9SJed Brown 747574034a9SJed Brown PetscFunctionBegin; 748574034a9SJed Brown ierr = PetscMallocSetDumpLog();CHKERRQ(ierr); 749574034a9SJed Brown if (logmin < 0) logmin = 0.0; /* PETSC_DEFAULT or PETSC_DECIDE */ 750574034a9SJed Brown PetscLogMallocThreshold = (size_t)logmin; 751574034a9SJed Brown PetscFunctionReturn(0); 752574034a9SJed Brown } 753574034a9SJed Brown 754574034a9SJed Brown #undef __FUNCT__ 75518a2528dSJed Brown #define __FUNCT__ "PetscMallocGetDumpLog" 75618a2528dSJed Brown /*@C 75718a2528dSJed Brown PetscMallocGetDumpLog - Determine whether all calls to PetscMalloc() are being logged 75818a2528dSJed Brown 75918a2528dSJed Brown Not Collective 76018a2528dSJed Brown 76118a2528dSJed Brown Output Arguments 76218a2528dSJed Brown . logging - PETSC_TRUE if logging is active 76318a2528dSJed Brown 76418a2528dSJed Brown Options Database Key: 76518a2528dSJed Brown . -malloc_log - Activates PetscMallocDumpLog() 76618a2528dSJed Brown 76718a2528dSJed Brown Level: advanced 76818a2528dSJed Brown 76918a2528dSJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog() 77018a2528dSJed Brown @*/ 77118a2528dSJed Brown PetscErrorCode PetscMallocGetDumpLog(PetscBool *logging) 77218a2528dSJed Brown { 77318a2528dSJed Brown 77418a2528dSJed Brown PetscFunctionBegin; 77518a2528dSJed Brown *logging = (PetscBool)(PetscLogMalloc >= 0); 77618a2528dSJed Brown PetscFunctionReturn(0); 77718a2528dSJed Brown } 77818a2528dSJed Brown 77918a2528dSJed Brown #undef __FUNCT__ 780e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDumpLog" 781e5c89e4eSSatish Balay /*@C 782e5c89e4eSSatish Balay PetscMallocDumpLog - Dumps the log of all calls to PetscMalloc(); also calls 78321b680ceSJed Brown PetscMemoryGetMaximumUsage() 784e5c89e4eSSatish Balay 785e5c89e4eSSatish Balay Collective on PETSC_COMM_WORLD 786e5c89e4eSSatish Balay 787e5c89e4eSSatish Balay Input Parameter: 7880298fd71SBarry Smith . fp - file pointer; or NULL 789e5c89e4eSSatish Balay 790e5c89e4eSSatish Balay Options Database Key: 791e5c89e4eSSatish Balay . -malloc_log - Activates PetscMallocDumpLog() 792e5c89e4eSSatish Balay 793e5c89e4eSSatish Balay Level: advanced 794e5c89e4eSSatish Balay 795e5c89e4eSSatish Balay Fortran Note: 796e5c89e4eSSatish Balay The calling sequence in Fortran is PetscMallocDumpLog(integer ierr) 797e5c89e4eSSatish Balay The fp defaults to stdout. 798e5c89e4eSSatish Balay 799e5c89e4eSSatish Balay .seealso: PetscMallocGetCurrentUsage(), PetscMallocDump(), PetscMallocSetDumpLog() 800e5c89e4eSSatish Balay @*/ 8017087cfbeSBarry Smith PetscErrorCode PetscMallocDumpLog(FILE *fp) 802e5c89e4eSSatish Balay { 803e5c89e4eSSatish Balay PetscInt i,j,n,dummy,*perm; 804e5c89e4eSSatish Balay size_t *shortlength; 805f56c2debSBarry Smith int *shortcount,err; 806e5c89e4eSSatish Balay PetscMPIInt rank,size,tag = 1212 /* very bad programming */; 807ace3abfcSBarry Smith PetscBool match; 808e5c89e4eSSatish Balay const char **shortfunction; 809e5c89e4eSSatish Balay PetscLogDouble rss; 810e5c89e4eSSatish Balay MPI_Status status; 811e5c89e4eSSatish Balay PetscErrorCode ierr; 812e5c89e4eSSatish Balay 813e5c89e4eSSatish Balay PetscFunctionBegin; 814e5c89e4eSSatish Balay ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 815e5c89e4eSSatish Balay ierr = MPI_Comm_size(MPI_COMM_WORLD,&size);CHKERRQ(ierr); 816e5c89e4eSSatish Balay /* 817e5c89e4eSSatish Balay Try to get the data printed in order by processor. This will only sometimes work 818e5c89e4eSSatish Balay */ 819f56c2debSBarry Smith err = fflush(fp); 820e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 821f56c2debSBarry Smith 822e5c89e4eSSatish Balay ierr = MPI_Barrier(MPI_COMM_WORLD);CHKERRQ(ierr); 823e5c89e4eSSatish Balay if (rank) { 824e5c89e4eSSatish Balay ierr = MPI_Recv(&dummy,1,MPIU_INT,rank-1,tag,MPI_COMM_WORLD,&status);CHKERRQ(ierr); 825e5c89e4eSSatish Balay } 826e5c89e4eSSatish Balay 827768aa557SSatish Balay if (PetscLogMalloc < 0) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"PetscMallocDumpLog() called without call to PetscMallocSetDumpLog() this is often due to\n setting the option -malloc_log AFTER PetscInitialize() with PetscOptionsInsert() or PetscOptionsInsertFile()"); 828768aa557SSatish Balay 829da9f1d6bSBarry Smith if (!fp) fp = PETSC_STDOUT; 830f3d65365SJed Brown ierr = PetscMemoryGetMaximumUsage(&rss);CHKERRQ(ierr); 831e5c89e4eSSatish Balay if (rss) { 832f3d65365SJed Brown ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] Maximum memory PetscMalloc()ed %.0f maximum size of entire process %.0f\n",rank,(PetscLogDouble)TRMaxMem,rss);CHKERRQ(ierr); 833e5c89e4eSSatish Balay } else { 834e5c89e4eSSatish Balay ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] Maximum memory PetscMalloc()ed %.0f OS cannot compute size of entire process\n",rank,(PetscLogDouble)TRMaxMem);CHKERRQ(ierr); 835e5c89e4eSSatish Balay } 836e32f2f54SBarry Smith shortcount = (int*)malloc(PetscLogMalloc*sizeof(int));if (!shortcount) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 837e32f2f54SBarry Smith shortlength = (size_t*)malloc(PetscLogMalloc*sizeof(size_t));if (!shortlength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 838e32f2f54SBarry Smith shortfunction = (const char**)malloc(PetscLogMalloc*sizeof(char*));if (!shortfunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 83997b9d747SJed Brown for (i=0,n=0; i<PetscLogMalloc; i++) { 840e5c89e4eSSatish Balay for (j=0; j<n; j++) { 841e5c89e4eSSatish Balay ierr = PetscStrcmp(shortfunction[j],PetscLogMallocFunction[i],&match);CHKERRQ(ierr); 842e5c89e4eSSatish Balay if (match) { 843e5c89e4eSSatish Balay shortlength[j] += PetscLogMallocLength[i]; 84459ffdab8SBarry Smith shortcount[j]++; 845e5c89e4eSSatish Balay goto foundit; 846e5c89e4eSSatish Balay } 847e5c89e4eSSatish Balay } 848e5c89e4eSSatish Balay shortfunction[n] = PetscLogMallocFunction[i]; 849e5c89e4eSSatish Balay shortlength[n] = PetscLogMallocLength[i]; 85059ffdab8SBarry Smith shortcount[n] = 1; 851e5c89e4eSSatish Balay n++; 852e5c89e4eSSatish Balay foundit:; 853e5c89e4eSSatish Balay } 854e5c89e4eSSatish Balay 855e32f2f54SBarry Smith perm = (PetscInt*)malloc(n*sizeof(PetscInt));if (!perm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 856e5c89e4eSSatish Balay for (i=0; i<n; i++) perm[i] = i; 857e5c89e4eSSatish Balay ierr = PetscSortStrWithPermutation(n,(const char**)shortfunction,perm);CHKERRQ(ierr); 858e5c89e4eSSatish Balay 859e5c89e4eSSatish Balay ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] Memory usage sorted by function\n",rank);CHKERRQ(ierr); 860e5c89e4eSSatish Balay for (i=0; i<n; i++) { 86159ffdab8SBarry Smith ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] %d %.0f %s()\n",rank,shortcount[perm[i]],(PetscLogDouble)shortlength[perm[i]],shortfunction[perm[i]]);CHKERRQ(ierr); 862e5c89e4eSSatish Balay } 863e5c89e4eSSatish Balay free(perm); 864e5c89e4eSSatish Balay free(shortlength); 86559ffdab8SBarry Smith free(shortcount); 866e5c89e4eSSatish Balay free((char**)shortfunction); 867f56c2debSBarry Smith err = fflush(fp); 868e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 869e5c89e4eSSatish Balay if (rank != size-1) { 870e5c89e4eSSatish Balay ierr = MPI_Send(&dummy,1,MPIU_INT,rank+1,tag,MPI_COMM_WORLD);CHKERRQ(ierr); 871e5c89e4eSSatish Balay } 872e5c89e4eSSatish Balay PetscFunctionReturn(0); 873e5c89e4eSSatish Balay } 874e5c89e4eSSatish Balay 875e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */ 876e5c89e4eSSatish Balay 877e5c89e4eSSatish Balay #undef __FUNCT__ 878e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDebug" 879e5c89e4eSSatish Balay /*@C 880e5c89e4eSSatish Balay PetscMallocDebug - Turns on/off debugging for the memory management routines. 881e5c89e4eSSatish Balay 882e5c89e4eSSatish Balay Not Collective 883e5c89e4eSSatish Balay 884e5c89e4eSSatish Balay Input Parameter: 885e5c89e4eSSatish Balay . level - PETSC_TRUE or PETSC_FALSE 886e5c89e4eSSatish Balay 887e5c89e4eSSatish Balay Level: intermediate 888e5c89e4eSSatish Balay 889e5c89e4eSSatish Balay .seealso: CHKMEMQ(), PetscMallocValidate() 890e5c89e4eSSatish Balay @*/ 8917087cfbeSBarry Smith PetscErrorCode PetscMallocDebug(PetscBool level) 892e5c89e4eSSatish Balay { 893e5c89e4eSSatish Balay PetscFunctionBegin; 894e5c89e4eSSatish Balay TRdebugLevel = level; 895e5c89e4eSSatish Balay PetscFunctionReturn(0); 896e5c89e4eSSatish Balay } 8970acecf5bSBarry Smith 8980acecf5bSBarry Smith #undef __FUNCT__ 8990acecf5bSBarry Smith #define __FUNCT__ "PetscMallocGetDebug" 9000acecf5bSBarry Smith /*@C 9010acecf5bSBarry Smith PetscMallocGetDebug - Indicates if any PETSc is doing ANY memory debugging. 9020acecf5bSBarry Smith 9030acecf5bSBarry Smith Not Collective 9040acecf5bSBarry Smith 9050acecf5bSBarry Smith Output Parameter: 9060acecf5bSBarry Smith . flg - PETSC_TRUE if any debugger 9070acecf5bSBarry Smith 9080acecf5bSBarry Smith Level: intermediate 9090acecf5bSBarry Smith 9100acecf5bSBarry Smith Note that by default, the debug version always does some debugging unless you run with -malloc no 9110acecf5bSBarry Smith 9120acecf5bSBarry Smith 9130acecf5bSBarry Smith .seealso: CHKMEMQ(), PetscMallocValidate() 9140acecf5bSBarry Smith @*/ 9150acecf5bSBarry Smith PetscErrorCode PetscMallocGetDebug(PetscBool *flg) 9160acecf5bSBarry Smith { 9170acecf5bSBarry Smith PetscFunctionBegin; 9180acecf5bSBarry Smith if (PetscTrMalloc == PetscTrMallocDefault) *flg = PETSC_TRUE; 9190acecf5bSBarry Smith else *flg = PETSC_FALSE; 9200acecf5bSBarry Smith PetscFunctionReturn(0); 9210acecf5bSBarry Smith } 922