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[]); 18efca3c55SSatish Balay extern PetscErrorCode PetscTrMallocDefault(size_t,int,const char[],const char[],void**); 19efca3c55SSatish Balay extern PetscErrorCode PetscTrFreeDefault(void*,int,const char[],const char[]); 20e5c89e4eSSatish Balay 21e5c89e4eSSatish Balay 220700a824SBarry Smith #define CLASSID_VALUE ((PetscClassId) 0xf0e0d0c9) 230700a824SBarry Smith #define ALREADY_FREED ((PetscClassId) 0x0f0e0d9c) 24e5c89e4eSSatish Balay 25e5c89e4eSSatish Balay typedef struct _trSPACE { 26e5c89e4eSSatish Balay size_t size; 27e5c89e4eSSatish Balay int id; 28e5c89e4eSSatish Balay int lineno; 29e5c89e4eSSatish Balay const char *filename; 30e5c89e4eSSatish Balay const char *functionname; 310700a824SBarry Smith PetscClassId classid; 328bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 33e5c89e4eSSatish Balay PetscStack stack; 34e5c89e4eSSatish Balay #endif 35e5c89e4eSSatish Balay struct _trSPACE *next,*prev; 36e5c89e4eSSatish Balay } TRSPACE; 37e5c89e4eSSatish Balay 3825b53cc9SJed Brown /* HEADER_BYTES is the number of bytes in a PetscMalloc() header. 3925b53cc9SJed Brown It is sizeof(TRSPACE) padded to be a multiple of PETSC_MEMALIGN. 4025b53cc9SJed Brown */ 41e5c89e4eSSatish Balay 42a64a8e02SBarry Smith #define HEADER_BYTES ((sizeof(TRSPACE)+(PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1)) 43e5c89e4eSSatish Balay 44e5c89e4eSSatish Balay 4525b53cc9SJed Brown /* This union is used to insure that the block passed to the user retains 4625b53cc9SJed Brown a minimum alignment of PETSC_MEMALIGN. 4725b53cc9SJed Brown */ 48e5c89e4eSSatish Balay typedef union { 49e5c89e4eSSatish Balay TRSPACE sp; 5025b53cc9SJed Brown char v[HEADER_BYTES]; 51e5c89e4eSSatish Balay } TrSPACE; 52e5c89e4eSSatish Balay 53b022a5c1SBarry Smith 54e5c89e4eSSatish Balay static size_t TRallocated = 0; 55e5c89e4eSSatish Balay static int TRfrags = 0; 56f0ba7cfcSLisandro Dalcin static TRSPACE *TRhead = NULL; 57e5c89e4eSSatish Balay static int TRid = 0; 58ace3abfcSBarry Smith static PetscBool TRdebugLevel = PETSC_FALSE; 59e5c89e4eSSatish Balay static size_t TRMaxMem = 0; 60e5c89e4eSSatish Balay /* 61e5c89e4eSSatish Balay Arrays to log information on all Mallocs 62e5c89e4eSSatish Balay */ 63f0ba7cfcSLisandro Dalcin static int PetscLogMallocMax = 10000; 64f0ba7cfcSLisandro Dalcin static int PetscLogMalloc = -1; 65574034a9SJed Brown static size_t PetscLogMallocThreshold = 0; 66e5c89e4eSSatish Balay static size_t *PetscLogMallocLength; 67efca3c55SSatish Balay static const char **PetscLogMallocFile,**PetscLogMallocFunction; 68e5c89e4eSSatish Balay 69e5c89e4eSSatish Balay #undef __FUNCT__ 70b022a5c1SBarry Smith #define __FUNCT__ "PetscSetUseTrMalloc_Private" 71b022a5c1SBarry Smith PetscErrorCode PetscSetUseTrMalloc_Private(void) 72b022a5c1SBarry Smith { 73b022a5c1SBarry Smith PetscErrorCode ierr; 74b022a5c1SBarry Smith 75b022a5c1SBarry Smith PetscFunctionBegin; 76b022a5c1SBarry Smith ierr = PetscMallocSet(PetscTrMallocDefault,PetscTrFreeDefault);CHKERRQ(ierr); 77a297a907SKarl Rupp 78b022a5c1SBarry Smith TRallocated = 0; 79b022a5c1SBarry Smith TRfrags = 0; 80f0ba7cfcSLisandro Dalcin TRhead = NULL; 81b022a5c1SBarry Smith TRid = 0; 82b022a5c1SBarry Smith TRdebugLevel = PETSC_FALSE; 83b022a5c1SBarry Smith TRMaxMem = 0; 84b022a5c1SBarry Smith PetscLogMallocMax = 10000; 85b022a5c1SBarry Smith PetscLogMalloc = -1; 86b022a5c1SBarry Smith PetscFunctionReturn(0); 87b022a5c1SBarry Smith } 88b022a5c1SBarry Smith 89b022a5c1SBarry Smith #undef __FUNCT__ 90e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocValidate" 91e5c89e4eSSatish Balay /*@C 92e5c89e4eSSatish Balay PetscMallocValidate - Test the memory for corruption. This can be used to 93e5c89e4eSSatish Balay check for memory overwrites. 94e5c89e4eSSatish Balay 95e5c89e4eSSatish Balay Input Parameter: 96e5c89e4eSSatish Balay + line - line number where call originated. 97e5c89e4eSSatish Balay . function - name of function calling 98efca3c55SSatish Balay - file - file where function is 99e5c89e4eSSatish Balay 100e5c89e4eSSatish Balay Return value: 101e5c89e4eSSatish Balay The number of errors detected. 102e5c89e4eSSatish Balay 103e5c89e4eSSatish Balay Output Effect: 104e5c89e4eSSatish Balay Error messages are written to stdout. 105e5c89e4eSSatish Balay 106e5c89e4eSSatish Balay Level: advanced 107e5c89e4eSSatish Balay 108e5c89e4eSSatish Balay Notes: 109e5c89e4eSSatish Balay You should generally use CHKMEMQ as a short cut for calling this 110e5c89e4eSSatish Balay routine. 111e5c89e4eSSatish Balay 112efca3c55SSatish Balay The line, function, file are given by the C preprocessor as 113efca3c55SSatish Balay __LINE__, __FUNCT__, __FILE__ 114e5c89e4eSSatish Balay 115e5c89e4eSSatish Balay The Fortran calling sequence is simply PetscMallocValidate(ierr) 116e5c89e4eSSatish Balay 117e5c89e4eSSatish Balay No output is generated if there are no problems detected. 118e5c89e4eSSatish Balay 119e5c89e4eSSatish Balay .seealso: CHKMEMQ 120e5c89e4eSSatish Balay 121e5c89e4eSSatish Balay @*/ 122efca3c55SSatish Balay PetscErrorCode PetscMallocValidate(int line,const char function[],const char file[]) 123e5c89e4eSSatish Balay { 1246c093d5bSvictor TRSPACE *head,*lasthead; 125e5c89e4eSSatish Balay char *a; 1260700a824SBarry Smith PetscClassId *nend; 127e5c89e4eSSatish Balay 128e5c89e4eSSatish Balay PetscFunctionBegin; 1296c093d5bSvictor head = TRhead; lasthead = NULL; 130e5c89e4eSSatish Balay while (head) { 1310700a824SBarry Smith if (head->classid != CLASSID_VALUE) { 132efca3c55SSatish Balay (*PetscErrorPrintf)("PetscMallocValidate: error detected at %s() line %d in %s\n",function,line,file); 133e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory at address %p is corrupted\n",head); 134e5c89e4eSSatish Balay (*PetscErrorPrintf)("Probably write past beginning or end of array\n"); 135efca3c55SSatish Balay if (lasthead) (*PetscErrorPrintf)("Last intact block allocated in %s() line %d in %s\n",lasthead->functionname,lasthead->lineno,lasthead->filename); 136e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC," "); 137e5c89e4eSSatish Balay } 138e5c89e4eSSatish Balay a = (char*)(((TrSPACE*)head) + 1); 1390700a824SBarry Smith nend = (PetscClassId*)(a + head->size); 1400700a824SBarry Smith if (*nend != CLASSID_VALUE) { 141efca3c55SSatish Balay (*PetscErrorPrintf)("PetscMallocValidate: error detected at %s() line %d in %s\n",function,line,file); 142e5c89e4eSSatish Balay if (*nend == ALREADY_FREED) { 143e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory [id=%d(%.0f)] at address %p already freed\n",head->id,(PetscLogDouble)head->size,a); 144e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC," "); 145e5c89e4eSSatish Balay } else { 146e5c89e4eSSatish Balay (*PetscErrorPrintf)("Memory [id=%d(%.0f)] at address %p is corrupted (probably write past end of array)\n",head->id,(PetscLogDouble)head->size,a); 147efca3c55SSatish Balay (*PetscErrorPrintf)("Memory originally allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 148e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC," "); 149e5c89e4eSSatish Balay } 150e5c89e4eSSatish Balay } 1516c093d5bSvictor lasthead = head; 152e5c89e4eSSatish Balay head = head->next; 153e5c89e4eSSatish Balay } 154e5c89e4eSSatish Balay PetscFunctionReturn(0); 155e5c89e4eSSatish Balay } 156e5c89e4eSSatish Balay 157e5c89e4eSSatish Balay #undef __FUNCT__ 158e5c89e4eSSatish Balay #define __FUNCT__ "PetscTrMallocDefault" 159e5c89e4eSSatish Balay /* 160e5c89e4eSSatish Balay PetscTrMallocDefault - Malloc with tracing. 161e5c89e4eSSatish Balay 162e5c89e4eSSatish Balay Input Parameters: 163e5c89e4eSSatish Balay + a - number of bytes to allocate 164e5c89e4eSSatish Balay . lineno - line number where used. Use __LINE__ for this 165e5c89e4eSSatish Balay . function - function calling routine. Use __FUNCT__ for this 166efca3c55SSatish Balay - filename - file name where used. Use __FILE__ for this 167e5c89e4eSSatish Balay 168e5c89e4eSSatish Balay Returns: 169e5c89e4eSSatish Balay double aligned pointer to requested storage, or null if not 170e5c89e4eSSatish Balay available. 171e5c89e4eSSatish Balay */ 172efca3c55SSatish Balay PetscErrorCode PetscTrMallocDefault(size_t a,int lineno,const char function[],const char filename[],void **result) 173e5c89e4eSSatish Balay { 174e5c89e4eSSatish Balay TRSPACE *head; 175e5c89e4eSSatish Balay char *inew; 176e5c89e4eSSatish Balay size_t nsize; 177e5c89e4eSSatish Balay PetscErrorCode ierr; 178e5c89e4eSSatish Balay 179e5c89e4eSSatish Balay PetscFunctionBegin; 180f0ba7cfcSLisandro Dalcin /* Do not try to handle empty blocks */ 181f0ba7cfcSLisandro Dalcin if (!a) { *result = NULL; PetscFunctionReturn(0); } 182f0ba7cfcSLisandro Dalcin 183e5c89e4eSSatish Balay if (TRdebugLevel) { 184efca3c55SSatish Balay ierr = PetscMallocValidate(lineno,function,filename); if (ierr) PetscFunctionReturn(ierr); 185e5c89e4eSSatish Balay } 186e5c89e4eSSatish Balay 18725b53cc9SJed Brown nsize = (a + (PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1); 188efca3c55SSatish Balay ierr = PetscMallocAlign(nsize+sizeof(TrSPACE)+sizeof(PetscClassId),lineno,function,filename,(void**)&inew);CHKERRQ(ierr); 189e5c89e4eSSatish Balay 190e5c89e4eSSatish Balay head = (TRSPACE*)inew; 191e5c89e4eSSatish Balay inew += sizeof(TrSPACE); 192e5c89e4eSSatish Balay 193e5c89e4eSSatish Balay if (TRhead) TRhead->prev = head; 194e5c89e4eSSatish Balay head->next = TRhead; 195e5c89e4eSSatish Balay TRhead = head; 196f0ba7cfcSLisandro Dalcin head->prev = NULL; 197e5c89e4eSSatish Balay head->size = nsize; 198e5c89e4eSSatish Balay head->id = TRid; 199e5c89e4eSSatish Balay head->lineno = lineno; 200e5c89e4eSSatish Balay 201e5c89e4eSSatish Balay head->filename = filename; 202e5c89e4eSSatish Balay head->functionname = function; 2030700a824SBarry Smith head->classid = CLASSID_VALUE; 2040700a824SBarry Smith *(PetscClassId*)(inew + nsize) = CLASSID_VALUE; 205e5c89e4eSSatish Balay 206e5c89e4eSSatish Balay TRallocated += nsize; 207a297a907SKarl Rupp if (TRallocated > TRMaxMem) TRMaxMem = TRallocated; 208e5c89e4eSSatish Balay TRfrags++; 209e5c89e4eSSatish Balay 2108bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 21176386721SLisandro Dalcin if (PetscStackActive()) { 2125c25fcd7SBarry Smith ierr = PetscStackCopy(petscstack,&head->stack);CHKERRQ(ierr); 2132c9581d2SBarry Smith /* fix the line number to where the malloc() was called, not the PetscFunctionBegin; */ 2142c9581d2SBarry Smith head->stack.line[head->stack.currentsize-2] = lineno; 21576386721SLisandro Dalcin } 216e5c89e4eSSatish Balay #endif 217e5c89e4eSSatish Balay 218e5c89e4eSSatish Balay /* 219e5c89e4eSSatish Balay Allow logging of all mallocs made 220e5c89e4eSSatish Balay */ 221574034a9SJed Brown if (PetscLogMalloc > -1 && PetscLogMalloc < PetscLogMallocMax && a >= PetscLogMallocThreshold) { 222e5c89e4eSSatish Balay if (!PetscLogMalloc) { 223e5c89e4eSSatish Balay PetscLogMallocLength = (size_t*)malloc(PetscLogMallocMax*sizeof(size_t)); 224e32f2f54SBarry Smith if (!PetscLogMallocLength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 225a297a907SKarl Rupp 226a2ea699eSBarry Smith PetscLogMallocFile = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 227e32f2f54SBarry Smith if (!PetscLogMallocFile) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 228a297a907SKarl Rupp 229a2ea699eSBarry Smith PetscLogMallocFunction = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 230e32f2f54SBarry Smith if (!PetscLogMallocFunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 231e5c89e4eSSatish Balay } 232e5c89e4eSSatish Balay PetscLogMallocLength[PetscLogMalloc] = nsize; 233e5c89e4eSSatish Balay PetscLogMallocFile[PetscLogMalloc] = filename; 234e5c89e4eSSatish Balay PetscLogMallocFunction[PetscLogMalloc++] = function; 235e5c89e4eSSatish Balay } 236e5c89e4eSSatish Balay *result = (void*)inew; 237e5c89e4eSSatish Balay PetscFunctionReturn(0); 238e5c89e4eSSatish Balay } 239e5c89e4eSSatish Balay 240e5c89e4eSSatish Balay 241e5c89e4eSSatish Balay #undef __FUNCT__ 242e5c89e4eSSatish Balay #define __FUNCT__ "PetscTrFreeDefault" 243e5c89e4eSSatish Balay /* 244e5c89e4eSSatish Balay PetscTrFreeDefault - Free with tracing. 245e5c89e4eSSatish Balay 246e5c89e4eSSatish Balay Input Parameters: 247e5c89e4eSSatish Balay . a - pointer to a block allocated with PetscTrMalloc 248e5c89e4eSSatish Balay . lineno - line number where used. Use __LINE__ for this 249e5c89e4eSSatish Balay . function - function calling routine. Use __FUNCT__ for this 250e5c89e4eSSatish Balay . file - file name where used. Use __FILE__ for this 251e5c89e4eSSatish Balay */ 252efca3c55SSatish Balay PetscErrorCode PetscTrFreeDefault(void *aa,int line,const char function[],const char file[]) 253e5c89e4eSSatish Balay { 254e5c89e4eSSatish Balay char *a = (char*)aa; 255e5c89e4eSSatish Balay TRSPACE *head; 256e5c89e4eSSatish Balay char *ahead; 257e5c89e4eSSatish Balay PetscErrorCode ierr; 2580700a824SBarry Smith PetscClassId *nend; 259e5c89e4eSSatish Balay 260e5c89e4eSSatish Balay PetscFunctionBegin; 261e5c89e4eSSatish Balay /* Do not try to handle empty blocks */ 26249d7da52SJed Brown if (!a) PetscFunctionReturn(0); 263e5c89e4eSSatish Balay 264e5c89e4eSSatish Balay if (TRdebugLevel) { 265efca3c55SSatish Balay ierr = PetscMallocValidate(line,function,file);CHKERRQ(ierr); 266e5c89e4eSSatish Balay } 267e5c89e4eSSatish Balay 268e5c89e4eSSatish Balay ahead = a; 269e5c89e4eSSatish Balay a = a - sizeof(TrSPACE); 270e5c89e4eSSatish Balay head = (TRSPACE*)a; 271e5c89e4eSSatish Balay 2720700a824SBarry Smith if (head->classid != CLASSID_VALUE) { 273efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 274e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block at address %p is corrupted; cannot free;\nmay be block not allocated with PetscMalloc()\n",a); 275e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Bad location or corrupted memory"); 276e5c89e4eSSatish Balay } 2770700a824SBarry Smith nend = (PetscClassId*)(ahead + head->size); 2780700a824SBarry Smith if (*nend != CLASSID_VALUE) { 279e5c89e4eSSatish Balay if (*nend == ALREADY_FREED) { 280efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 281e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p was already freed\n",head->id,(PetscLogDouble)head->size,a + sizeof(TrSPACE)); 282e5c89e4eSSatish Balay if (head->lineno > 0 && head->lineno < 50000 /* sanity check */) { 283efca3c55SSatish Balay (*PetscErrorPrintf)("Block freed in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 284e5c89e4eSSatish Balay } else { 285efca3c55SSatish Balay (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,-head->lineno,head->filename); 286e5c89e4eSSatish Balay } 287e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Memory already freed"); 288e5c89e4eSSatish Balay } else { 289e5c89e4eSSatish Balay /* Damaged tail */ 290efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 291e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p is corrupted (probably write past end of array)\n",head->id,(PetscLogDouble)head->size,a); 292efca3c55SSatish Balay (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 293e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Corrupted memory"); 294e5c89e4eSSatish Balay } 295e5c89e4eSSatish Balay } 296e5c89e4eSSatish Balay /* Mark the location freed */ 297e5c89e4eSSatish Balay *nend = ALREADY_FREED; 298e5c89e4eSSatish Balay /* Save location where freed. If we suspect the line number, mark as allocated location */ 299e5c89e4eSSatish Balay if (line > 0 && line < 50000) { 300e5c89e4eSSatish Balay head->lineno = line; 301e5c89e4eSSatish Balay head->filename = file; 302e5c89e4eSSatish Balay head->functionname = function; 303e5c89e4eSSatish Balay } else { 304e5c89e4eSSatish Balay head->lineno = -head->lineno; 305e5c89e4eSSatish Balay } 306e5c89e4eSSatish Balay /* zero out memory - helps to find some reuse of already freed memory */ 307e5c89e4eSSatish Balay ierr = PetscMemzero(aa,head->size);CHKERRQ(ierr); 308e5c89e4eSSatish Balay 309e5c89e4eSSatish Balay TRallocated -= head->size; 310e5c89e4eSSatish Balay TRfrags--; 311e5c89e4eSSatish Balay if (head->prev) head->prev->next = head->next; 312e5c89e4eSSatish Balay else TRhead = head->next; 313e5c89e4eSSatish Balay 314e5c89e4eSSatish Balay if (head->next) head->next->prev = head->prev; 315efca3c55SSatish Balay ierr = PetscFreeAlign(a,line,function,file);CHKERRQ(ierr); 316e5c89e4eSSatish Balay PetscFunctionReturn(0); 317e5c89e4eSSatish Balay } 318e5c89e4eSSatish Balay 319e5c89e4eSSatish Balay 320e5c89e4eSSatish Balay #undef __FUNCT__ 3210841954dSBarry Smith #define __FUNCT__ "PetscMemoryView" 322fe7fb379SMatthew Knepley /*@C 3230841954dSBarry Smith PetscMemoryView - Shows the amount of memory currently being used 324e5c89e4eSSatish Balay in a communicator. 325e5c89e4eSSatish Balay 326e5c89e4eSSatish Balay Collective on PetscViewer 327e5c89e4eSSatish Balay 328e5c89e4eSSatish Balay Input Parameter: 329e5c89e4eSSatish Balay + viewer - the viewer that defines the communicator 330e5c89e4eSSatish Balay - message - string printed before values 331e5c89e4eSSatish Balay 3320841954dSBarry Smith Options Database: 3330841954dSBarry Smith + -malloc - have PETSc track how much memory it has allocated 3340841954dSBarry Smith - -memory_view - during PetscFinalize() have this routine called 3350841954dSBarry Smith 336e5c89e4eSSatish Balay Level: intermediate 337e5c89e4eSSatish Balay 338e5c89e4eSSatish Balay Concepts: memory usage 339e5c89e4eSSatish Balay 3400841954dSBarry Smith .seealso: PetscMallocDump(), PetscMemoryGetCurrentUsage(), PetscMemorySetGetMaximumUsage() 341e5c89e4eSSatish Balay @*/ 3420841954dSBarry Smith PetscErrorCode PetscMemoryView(PetscViewer viewer,const char message[]) 343e5c89e4eSSatish Balay { 3440841954dSBarry Smith PetscLogDouble allocated,allocatedmax,resident,residentmax,gallocated,gallocatedmax,gresident,gresidentmax,maxgallocated,maxgallocatedmax,maxgresident,maxgresidentmax; 3450841954dSBarry Smith PetscLogDouble mingallocated,mingallocatedmax,mingresident,mingresidentmax; 346e5c89e4eSSatish Balay PetscErrorCode ierr; 347e5c89e4eSSatish Balay MPI_Comm comm; 348e5c89e4eSSatish Balay 349e5c89e4eSSatish Balay PetscFunctionBegin; 350e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD; 351e5c89e4eSSatish Balay ierr = PetscMallocGetCurrentUsage(&allocated);CHKERRQ(ierr); 3520841954dSBarry Smith ierr = PetscMallocGetMaximumUsage(&allocatedmax);CHKERRQ(ierr); 353e5c89e4eSSatish Balay ierr = PetscMemoryGetCurrentUsage(&resident);CHKERRQ(ierr); 354e5c89e4eSSatish Balay ierr = PetscMemoryGetMaximumUsage(&residentmax);CHKERRQ(ierr); 355e5c89e4eSSatish Balay if (residentmax > 0) residentmax = PetscMax(resident,residentmax); 356e5c89e4eSSatish Balay ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 357e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,message);CHKERRQ(ierr); 358e5c89e4eSSatish Balay if (resident && residentmax && allocated) { 3590841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3600841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3610841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3620841954dSBarry 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); 3630841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3640841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3650841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3660841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 3670841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&gallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3680841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&maxgallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3690841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&mingallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3700841954dSBarry 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); 3710841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3720841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3730841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3740841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 375e5c89e4eSSatish Balay } else if (resident && residentmax) { 3760841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3770841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3780841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3790841954dSBarry 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); 3800841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3810841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3820841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3830841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 384e5c89e4eSSatish Balay } else if (resident && allocated) { 3850841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3860841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3870841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3880841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 3890841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3900841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3910841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3920841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 3930841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr); 394e5c89e4eSSatish Balay } else if (allocated) { 3950841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3960841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3970841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3980841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 3990841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr); 4000841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"OS cannot compute process memory\n");CHKERRQ(ierr); 401e5c89e4eSSatish Balay } else { 402e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"Run with -malloc to get statistics on PetscMalloc() calls\nOS cannot compute process memory\n");CHKERRQ(ierr); 403e5c89e4eSSatish Balay } 404e5c89e4eSSatish Balay ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 405e5c89e4eSSatish Balay PetscFunctionReturn(0); 406e5c89e4eSSatish Balay } 407e5c89e4eSSatish Balay 408e5c89e4eSSatish Balay #undef __FUNCT__ 409e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetCurrentUsage" 410e5c89e4eSSatish Balay /*@C 411e5c89e4eSSatish Balay PetscMallocGetCurrentUsage - gets the current amount of memory used that was PetscMalloc()ed 412e5c89e4eSSatish Balay 413e5c89e4eSSatish Balay Not Collective 414e5c89e4eSSatish Balay 415e5c89e4eSSatish Balay Output Parameters: 416e5c89e4eSSatish Balay . space - number of bytes currently allocated 417e5c89e4eSSatish Balay 418e5c89e4eSSatish Balay Level: intermediate 419e5c89e4eSSatish Balay 420e5c89e4eSSatish Balay Concepts: memory usage 421e5c89e4eSSatish Balay 422e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), 423e5c89e4eSSatish Balay PetscMemoryGetMaximumUsage() 424e5c89e4eSSatish Balay @*/ 4257087cfbeSBarry Smith PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *space) 426e5c89e4eSSatish Balay { 427e5c89e4eSSatish Balay PetscFunctionBegin; 428e5c89e4eSSatish Balay *space = (PetscLogDouble) TRallocated; 429e5c89e4eSSatish Balay PetscFunctionReturn(0); 430e5c89e4eSSatish Balay } 431e5c89e4eSSatish Balay 432e5c89e4eSSatish Balay #undef __FUNCT__ 433e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetMaximumUsage" 434e5c89e4eSSatish Balay /*@C 435e5c89e4eSSatish Balay PetscMallocGetMaximumUsage - gets the maximum amount of memory used that was PetscMalloc()ed at any time 436e5c89e4eSSatish Balay during this run. 437e5c89e4eSSatish Balay 438e5c89e4eSSatish Balay Not Collective 439e5c89e4eSSatish Balay 440e5c89e4eSSatish Balay Output Parameters: 441e5c89e4eSSatish Balay . space - maximum number of bytes ever allocated at one time 442e5c89e4eSSatish Balay 443e5c89e4eSSatish Balay Level: intermediate 444e5c89e4eSSatish Balay 445e5c89e4eSSatish Balay Concepts: memory usage 446e5c89e4eSSatish Balay 447e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), 448e5c89e4eSSatish Balay PetscMemoryGetCurrentUsage() 449e5c89e4eSSatish Balay @*/ 4507087cfbeSBarry Smith PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *space) 451e5c89e4eSSatish Balay { 452e5c89e4eSSatish Balay PetscFunctionBegin; 453e5c89e4eSSatish Balay *space = (PetscLogDouble) TRMaxMem; 454e5c89e4eSSatish Balay PetscFunctionReturn(0); 455e5c89e4eSSatish Balay } 456e5c89e4eSSatish Balay 457a64a8e02SBarry Smith #if defined(PETSC_USE_DEBUG) 458a64a8e02SBarry Smith #undef __FUNCT__ 459a64a8e02SBarry Smith #define __FUNCT__ "PetscMallocGetStack" 460a64a8e02SBarry Smith /*@C 461a64a8e02SBarry Smith PetscMallocGetStack - returns a pointer to the stack for the location in the program a call to PetscMalloc() was used to obtain that memory 462a64a8e02SBarry Smith 463a64a8e02SBarry Smith Collective on PETSC_COMM_WORLD 464a64a8e02SBarry Smith 465a64a8e02SBarry Smith Input Parameter: 466a64a8e02SBarry Smith . ptr - the memory location 467a64a8e02SBarry Smith 468a64a8e02SBarry Smith Output Paramter: 469a64a8e02SBarry Smith . stack - the stack indicating where the program allocated this memory 470a64a8e02SBarry Smith 471a64a8e02SBarry Smith Level: intermediate 472a64a8e02SBarry Smith 473a64a8e02SBarry Smith .seealso: PetscMallocGetCurrentUsage(), PetscMallocDumpLog() 474a64a8e02SBarry Smith @*/ 475a64a8e02SBarry Smith PetscErrorCode PetscMallocGetStack(void *ptr,PetscStack **stack) 476a64a8e02SBarry Smith { 477a64a8e02SBarry Smith TRSPACE *head; 478a64a8e02SBarry Smith 479a64a8e02SBarry Smith PetscFunctionBegin; 480a64a8e02SBarry Smith head = (TRSPACE*) (((char*)ptr) - HEADER_BYTES); 481a64a8e02SBarry Smith *stack = &head->stack; 482a64a8e02SBarry Smith PetscFunctionReturn(0); 483a64a8e02SBarry Smith } 48476386721SLisandro Dalcin #else 48576386721SLisandro Dalcin #undef __FUNCT__ 48676386721SLisandro Dalcin #define __FUNCT__ "PetscMallocGetStack" 48776386721SLisandro Dalcin PetscErrorCode PetscMallocGetStack(void *ptr,void **stack) 48876386721SLisandro Dalcin { 48976386721SLisandro Dalcin PetscFunctionBegin; 490f0ba7cfcSLisandro Dalcin *stack = NULL; 49176386721SLisandro Dalcin PetscFunctionReturn(0); 49276386721SLisandro Dalcin } 493a64a8e02SBarry Smith #endif 494a64a8e02SBarry Smith 495e5c89e4eSSatish Balay #undef __FUNCT__ 496e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDump" 497e5c89e4eSSatish Balay /*@C 498e5c89e4eSSatish Balay PetscMallocDump - Dumps the allocated memory blocks to a file. The information 499e5c89e4eSSatish Balay printed is: size of space (in bytes), address of space, id of space, 500e5c89e4eSSatish Balay file in which space was allocated, and line number at which it was 501e5c89e4eSSatish Balay allocated. 502e5c89e4eSSatish Balay 503e5c89e4eSSatish Balay Collective on PETSC_COMM_WORLD 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay Input Parameter: 506e5c89e4eSSatish Balay . fp - file pointer. If fp is NULL, stdout is assumed. 507e5c89e4eSSatish Balay 508e5c89e4eSSatish Balay Options Database Key: 509e5c89e4eSSatish Balay . -malloc_dump - Dumps unfreed memory during call to PetscFinalize() 510e5c89e4eSSatish Balay 511e5c89e4eSSatish Balay Level: intermediate 512e5c89e4eSSatish Balay 513e5c89e4eSSatish Balay Fortran Note: 514e5c89e4eSSatish Balay The calling sequence in Fortran is PetscMallocDump(integer ierr) 515e5c89e4eSSatish Balay The fp defaults to stdout. 516e5c89e4eSSatish Balay 517e5c89e4eSSatish Balay Notes: uses MPI_COMM_WORLD, because this may be called in PetscFinalize() after PETSC_COMM_WORLD 518e5c89e4eSSatish Balay has been freed. 519e5c89e4eSSatish Balay 520e5c89e4eSSatish Balay Concepts: memory usage 521e5c89e4eSSatish Balay Concepts: memory bleeding 522e5c89e4eSSatish Balay Concepts: bleeding memory 523e5c89e4eSSatish Balay 5249e9a1f8fSvictor .seealso: PetscMallocGetCurrentUsage(), PetscMallocDumpLog() 525e5c89e4eSSatish Balay @*/ 5267087cfbeSBarry Smith PetscErrorCode PetscMallocDump(FILE *fp) 527e5c89e4eSSatish Balay { 528e5c89e4eSSatish Balay TRSPACE *head; 529*5486ca60SMatthew G. Knepley PetscInt libAlloc = 0; 530e5c89e4eSSatish Balay PetscErrorCode ierr; 531e5c89e4eSSatish Balay PetscMPIInt rank; 532e5c89e4eSSatish Balay 533e5c89e4eSSatish Balay PetscFunctionBegin; 534e5c89e4eSSatish Balay ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 535da9f1d6bSBarry Smith if (!fp) fp = PETSC_STDOUT; 536e5c89e4eSSatish Balay head = TRhead; 537e5c89e4eSSatish Balay while (head) { 538*5486ca60SMatthew G. Knepley PetscBool isLib; 539*5486ca60SMatthew G. Knepley 540*5486ca60SMatthew G. Knepley ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr); 541*5486ca60SMatthew G. Knepley libAlloc += head->size; 542*5486ca60SMatthew G. Knepley head = head->next; 543*5486ca60SMatthew G. Knepley } 544*5486ca60SMatthew G. Knepley if (TRallocated - libAlloc > 0) fprintf(fp,"[%d]Total space allocated %.0f bytes\n",rank,(PetscLogDouble)TRallocated); 545*5486ca60SMatthew G. Knepley head = TRhead; 546*5486ca60SMatthew G. Knepley while (head) { 547*5486ca60SMatthew G. Knepley PetscBool isLib; 548*5486ca60SMatthew G. Knepley 549*5486ca60SMatthew G. Knepley ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr); 550*5486ca60SMatthew G. Knepley if (!isLib) { 551efca3c55SSatish Balay fprintf(fp,"[%2d]%.0f bytes %s() line %d in %s\n",rank,(PetscLogDouble)head->size,head->functionname,head->lineno,head->filename); 5528bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 553e5c89e4eSSatish Balay ierr = PetscStackPrint(&head->stack,fp);CHKERRQ(ierr); 554e5c89e4eSSatish Balay #endif 555*5486ca60SMatthew G. Knepley } 556e5c89e4eSSatish Balay head = head->next; 557e5c89e4eSSatish Balay } 558e5c89e4eSSatish Balay PetscFunctionReturn(0); 559e5c89e4eSSatish Balay } 560e5c89e4eSSatish Balay 561e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */ 562e5c89e4eSSatish Balay 563e5c89e4eSSatish Balay #undef __FUNCT__ 564e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocSetDumpLog" 565e5c89e4eSSatish Balay /*@C 566e5c89e4eSSatish Balay PetscMallocSetDumpLog - Activates logging of all calls to PetscMalloc(). 567e5c89e4eSSatish Balay 568e5c89e4eSSatish Balay Not Collective 569e5c89e4eSSatish Balay 570e5c89e4eSSatish Balay Options Database Key: 571574034a9SJed Brown + -malloc_log <filename> - Activates PetscMallocDumpLog() 572574034a9SJed Brown - -malloc_log_threshold <min> - Activates logging and sets a minimum size 573e5c89e4eSSatish Balay 574e5c89e4eSSatish Balay Level: advanced 575e5c89e4eSSatish Balay 576574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLogThreshold() 577e5c89e4eSSatish Balay @*/ 5787087cfbeSBarry Smith PetscErrorCode PetscMallocSetDumpLog(void) 579e5c89e4eSSatish Balay { 58021b680ceSJed Brown PetscErrorCode ierr; 58121b680ceSJed Brown 582e5c89e4eSSatish Balay PetscFunctionBegin; 583e5c89e4eSSatish Balay PetscLogMalloc = 0; 584a297a907SKarl Rupp 58521b680ceSJed Brown ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr); 586e5c89e4eSSatish Balay PetscFunctionReturn(0); 587e5c89e4eSSatish Balay } 588e5c89e4eSSatish Balay 589e5c89e4eSSatish Balay #undef __FUNCT__ 590574034a9SJed Brown #define __FUNCT__ "PetscMallocSetDumpLogThreshold" 591574034a9SJed Brown /*@C 592574034a9SJed Brown PetscMallocSetDumpLogThreshold - Activates logging of all calls to PetscMalloc(). 593574034a9SJed Brown 594574034a9SJed Brown Not Collective 595574034a9SJed Brown 596574034a9SJed Brown Input Arguments: 597574034a9SJed Brown . logmin - minimum allocation size to log, or PETSC_DEFAULT 598574034a9SJed Brown 599574034a9SJed Brown Options Database Key: 600574034a9SJed Brown + -malloc_log <filename> - Activates PetscMallocDumpLog() 601574034a9SJed Brown - -malloc_log_threshold <min> - Activates logging and sets a minimum size 602574034a9SJed Brown 603574034a9SJed Brown Level: advanced 604574034a9SJed Brown 605574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLog() 606574034a9SJed Brown @*/ 607574034a9SJed Brown PetscErrorCode PetscMallocSetDumpLogThreshold(PetscLogDouble logmin) 608574034a9SJed Brown { 609574034a9SJed Brown PetscErrorCode ierr; 610574034a9SJed Brown 611574034a9SJed Brown PetscFunctionBegin; 612574034a9SJed Brown ierr = PetscMallocSetDumpLog();CHKERRQ(ierr); 613574034a9SJed Brown if (logmin < 0) logmin = 0.0; /* PETSC_DEFAULT or PETSC_DECIDE */ 614574034a9SJed Brown PetscLogMallocThreshold = (size_t)logmin; 615574034a9SJed Brown PetscFunctionReturn(0); 616574034a9SJed Brown } 617574034a9SJed Brown 618574034a9SJed Brown #undef __FUNCT__ 61918a2528dSJed Brown #define __FUNCT__ "PetscMallocGetDumpLog" 62018a2528dSJed Brown /*@C 62118a2528dSJed Brown PetscMallocGetDumpLog - Determine whether all calls to PetscMalloc() are being logged 62218a2528dSJed Brown 62318a2528dSJed Brown Not Collective 62418a2528dSJed Brown 62518a2528dSJed Brown Output Arguments 62618a2528dSJed Brown . logging - PETSC_TRUE if logging is active 62718a2528dSJed Brown 62818a2528dSJed Brown Options Database Key: 62918a2528dSJed Brown . -malloc_log - Activates PetscMallocDumpLog() 63018a2528dSJed Brown 63118a2528dSJed Brown Level: advanced 63218a2528dSJed Brown 63318a2528dSJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog() 63418a2528dSJed Brown @*/ 63518a2528dSJed Brown PetscErrorCode PetscMallocGetDumpLog(PetscBool *logging) 63618a2528dSJed Brown { 63718a2528dSJed Brown 63818a2528dSJed Brown PetscFunctionBegin; 63918a2528dSJed Brown *logging = (PetscBool)(PetscLogMalloc >= 0); 64018a2528dSJed Brown PetscFunctionReturn(0); 64118a2528dSJed Brown } 64218a2528dSJed Brown 64318a2528dSJed Brown #undef __FUNCT__ 644e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDumpLog" 645e5c89e4eSSatish Balay /*@C 646e5c89e4eSSatish Balay PetscMallocDumpLog - Dumps the log of all calls to PetscMalloc(); also calls 64721b680ceSJed Brown PetscMemoryGetMaximumUsage() 648e5c89e4eSSatish Balay 649e5c89e4eSSatish Balay Collective on PETSC_COMM_WORLD 650e5c89e4eSSatish Balay 651e5c89e4eSSatish Balay Input Parameter: 6520298fd71SBarry Smith . fp - file pointer; or NULL 653e5c89e4eSSatish Balay 654e5c89e4eSSatish Balay Options Database Key: 655e5c89e4eSSatish Balay . -malloc_log - Activates PetscMallocDumpLog() 656e5c89e4eSSatish Balay 657e5c89e4eSSatish Balay Level: advanced 658e5c89e4eSSatish Balay 659e5c89e4eSSatish Balay Fortran Note: 660e5c89e4eSSatish Balay The calling sequence in Fortran is PetscMallocDumpLog(integer ierr) 661e5c89e4eSSatish Balay The fp defaults to stdout. 662e5c89e4eSSatish Balay 663e5c89e4eSSatish Balay .seealso: PetscMallocGetCurrentUsage(), PetscMallocDump(), PetscMallocSetDumpLog() 664e5c89e4eSSatish Balay @*/ 6657087cfbeSBarry Smith PetscErrorCode PetscMallocDumpLog(FILE *fp) 666e5c89e4eSSatish Balay { 667e5c89e4eSSatish Balay PetscInt i,j,n,dummy,*perm; 668e5c89e4eSSatish Balay size_t *shortlength; 669f56c2debSBarry Smith int *shortcount,err; 670e5c89e4eSSatish Balay PetscMPIInt rank,size,tag = 1212 /* very bad programming */; 671ace3abfcSBarry Smith PetscBool match; 672e5c89e4eSSatish Balay const char **shortfunction; 673e5c89e4eSSatish Balay PetscLogDouble rss; 674e5c89e4eSSatish Balay MPI_Status status; 675e5c89e4eSSatish Balay PetscErrorCode ierr; 676e5c89e4eSSatish Balay 677e5c89e4eSSatish Balay PetscFunctionBegin; 678e5c89e4eSSatish Balay ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 679e5c89e4eSSatish Balay ierr = MPI_Comm_size(MPI_COMM_WORLD,&size);CHKERRQ(ierr); 680e5c89e4eSSatish Balay /* 681e5c89e4eSSatish Balay Try to get the data printed in order by processor. This will only sometimes work 682e5c89e4eSSatish Balay */ 683f56c2debSBarry Smith err = fflush(fp); 684e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 685f56c2debSBarry Smith 686e5c89e4eSSatish Balay ierr = MPI_Barrier(MPI_COMM_WORLD);CHKERRQ(ierr); 687e5c89e4eSSatish Balay if (rank) { 688e5c89e4eSSatish Balay ierr = MPI_Recv(&dummy,1,MPIU_INT,rank-1,tag,MPI_COMM_WORLD,&status);CHKERRQ(ierr); 689e5c89e4eSSatish Balay } 690e5c89e4eSSatish Balay 691768aa557SSatish 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()"); 692768aa557SSatish Balay 693da9f1d6bSBarry Smith if (!fp) fp = PETSC_STDOUT; 694f3d65365SJed Brown ierr = PetscMemoryGetMaximumUsage(&rss);CHKERRQ(ierr); 695e5c89e4eSSatish Balay if (rss) { 696f3d65365SJed 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); 697e5c89e4eSSatish Balay } else { 698e5c89e4eSSatish 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); 699e5c89e4eSSatish Balay } 700e32f2f54SBarry Smith shortcount = (int*)malloc(PetscLogMalloc*sizeof(int));if (!shortcount) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 701e32f2f54SBarry Smith shortlength = (size_t*)malloc(PetscLogMalloc*sizeof(size_t));if (!shortlength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 702e32f2f54SBarry Smith shortfunction = (const char**)malloc(PetscLogMalloc*sizeof(char*));if (!shortfunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 70397b9d747SJed Brown for (i=0,n=0; i<PetscLogMalloc; i++) { 704e5c89e4eSSatish Balay for (j=0; j<n; j++) { 705e5c89e4eSSatish Balay ierr = PetscStrcmp(shortfunction[j],PetscLogMallocFunction[i],&match);CHKERRQ(ierr); 706e5c89e4eSSatish Balay if (match) { 707e5c89e4eSSatish Balay shortlength[j] += PetscLogMallocLength[i]; 70859ffdab8SBarry Smith shortcount[j]++; 709e5c89e4eSSatish Balay goto foundit; 710e5c89e4eSSatish Balay } 711e5c89e4eSSatish Balay } 712e5c89e4eSSatish Balay shortfunction[n] = PetscLogMallocFunction[i]; 713e5c89e4eSSatish Balay shortlength[n] = PetscLogMallocLength[i]; 71459ffdab8SBarry Smith shortcount[n] = 1; 715e5c89e4eSSatish Balay n++; 716e5c89e4eSSatish Balay foundit:; 717e5c89e4eSSatish Balay } 718e5c89e4eSSatish Balay 719e32f2f54SBarry Smith perm = (PetscInt*)malloc(n*sizeof(PetscInt));if (!perm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 720e5c89e4eSSatish Balay for (i=0; i<n; i++) perm[i] = i; 721e5c89e4eSSatish Balay ierr = PetscSortStrWithPermutation(n,(const char**)shortfunction,perm);CHKERRQ(ierr); 722e5c89e4eSSatish Balay 723e5c89e4eSSatish Balay ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] Memory usage sorted by function\n",rank);CHKERRQ(ierr); 724e5c89e4eSSatish Balay for (i=0; i<n; i++) { 72559ffdab8SBarry 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); 726e5c89e4eSSatish Balay } 727e5c89e4eSSatish Balay free(perm); 728e5c89e4eSSatish Balay free(shortlength); 72959ffdab8SBarry Smith free(shortcount); 730e5c89e4eSSatish Balay free((char**)shortfunction); 731f56c2debSBarry Smith err = fflush(fp); 732e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 733e5c89e4eSSatish Balay if (rank != size-1) { 734e5c89e4eSSatish Balay ierr = MPI_Send(&dummy,1,MPIU_INT,rank+1,tag,MPI_COMM_WORLD);CHKERRQ(ierr); 735e5c89e4eSSatish Balay } 736e5c89e4eSSatish Balay PetscFunctionReturn(0); 737e5c89e4eSSatish Balay } 738e5c89e4eSSatish Balay 739e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */ 740e5c89e4eSSatish Balay 741e5c89e4eSSatish Balay #undef __FUNCT__ 742e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDebug" 743e5c89e4eSSatish Balay /*@C 744e5c89e4eSSatish Balay PetscMallocDebug - Turns on/off debugging for the memory management routines. 745e5c89e4eSSatish Balay 746e5c89e4eSSatish Balay Not Collective 747e5c89e4eSSatish Balay 748e5c89e4eSSatish Balay Input Parameter: 749e5c89e4eSSatish Balay . level - PETSC_TRUE or PETSC_FALSE 750e5c89e4eSSatish Balay 751e5c89e4eSSatish Balay Level: intermediate 752e5c89e4eSSatish Balay 753e5c89e4eSSatish Balay .seealso: CHKMEMQ(), PetscMallocValidate() 754e5c89e4eSSatish Balay @*/ 7557087cfbeSBarry Smith PetscErrorCode PetscMallocDebug(PetscBool level) 756e5c89e4eSSatish Balay { 757e5c89e4eSSatish Balay PetscFunctionBegin; 758e5c89e4eSSatish Balay TRdebugLevel = level; 759e5c89e4eSSatish Balay PetscFunctionReturn(0); 760e5c89e4eSSatish Balay } 7610acecf5bSBarry Smith 7620acecf5bSBarry Smith #undef __FUNCT__ 7630acecf5bSBarry Smith #define __FUNCT__ "PetscMallocGetDebug" 7640acecf5bSBarry Smith /*@C 7650acecf5bSBarry Smith PetscMallocGetDebug - Indicates if any PETSc is doing ANY memory debugging. 7660acecf5bSBarry Smith 7670acecf5bSBarry Smith Not Collective 7680acecf5bSBarry Smith 7690acecf5bSBarry Smith Output Parameter: 7700acecf5bSBarry Smith . flg - PETSC_TRUE if any debugger 7710acecf5bSBarry Smith 7720acecf5bSBarry Smith Level: intermediate 7730acecf5bSBarry Smith 7740acecf5bSBarry Smith Note that by default, the debug version always does some debugging unless you run with -malloc no 7750acecf5bSBarry Smith 7760acecf5bSBarry Smith 7770acecf5bSBarry Smith .seealso: CHKMEMQ(), PetscMallocValidate() 7780acecf5bSBarry Smith @*/ 7790acecf5bSBarry Smith PetscErrorCode PetscMallocGetDebug(PetscBool *flg) 7800acecf5bSBarry Smith { 7810acecf5bSBarry Smith PetscFunctionBegin; 7820acecf5bSBarry Smith if (PetscTrMalloc == PetscTrMallocDefault) *flg = PETSC_TRUE; 7830acecf5bSBarry Smith else *flg = PETSC_FALSE; 7840acecf5bSBarry Smith PetscFunctionReturn(0); 7850acecf5bSBarry Smith } 786