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; 215*9de0f6ecSBarry Smith } else { 216*9de0f6ecSBarry Smith head->stack.currentsize = 0; 21776386721SLisandro Dalcin } 218e5c89e4eSSatish Balay #endif 219e5c89e4eSSatish Balay 220e5c89e4eSSatish Balay /* 221e5c89e4eSSatish Balay Allow logging of all mallocs made 222e5c89e4eSSatish Balay */ 223574034a9SJed Brown if (PetscLogMalloc > -1 && PetscLogMalloc < PetscLogMallocMax && a >= PetscLogMallocThreshold) { 224e5c89e4eSSatish Balay if (!PetscLogMalloc) { 225e5c89e4eSSatish Balay PetscLogMallocLength = (size_t*)malloc(PetscLogMallocMax*sizeof(size_t)); 226e32f2f54SBarry Smith if (!PetscLogMallocLength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 227a297a907SKarl Rupp 228a2ea699eSBarry Smith PetscLogMallocFile = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 229e32f2f54SBarry Smith if (!PetscLogMallocFile) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 230a297a907SKarl Rupp 231a2ea699eSBarry Smith PetscLogMallocFunction = (const char**)malloc(PetscLogMallocMax*sizeof(char*)); 232e32f2f54SBarry Smith if (!PetscLogMallocFunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," "); 233e5c89e4eSSatish Balay } 234e5c89e4eSSatish Balay PetscLogMallocLength[PetscLogMalloc] = nsize; 235e5c89e4eSSatish Balay PetscLogMallocFile[PetscLogMalloc] = filename; 236e5c89e4eSSatish Balay PetscLogMallocFunction[PetscLogMalloc++] = function; 237e5c89e4eSSatish Balay } 238e5c89e4eSSatish Balay *result = (void*)inew; 239e5c89e4eSSatish Balay PetscFunctionReturn(0); 240e5c89e4eSSatish Balay } 241e5c89e4eSSatish Balay 242e5c89e4eSSatish Balay 243e5c89e4eSSatish Balay #undef __FUNCT__ 244e5c89e4eSSatish Balay #define __FUNCT__ "PetscTrFreeDefault" 245e5c89e4eSSatish Balay /* 246e5c89e4eSSatish Balay PetscTrFreeDefault - Free with tracing. 247e5c89e4eSSatish Balay 248e5c89e4eSSatish Balay Input Parameters: 249e5c89e4eSSatish Balay . a - pointer to a block allocated with PetscTrMalloc 250e5c89e4eSSatish Balay . lineno - line number where used. Use __LINE__ for this 251e5c89e4eSSatish Balay . function - function calling routine. Use __FUNCT__ for this 252e5c89e4eSSatish Balay . file - file name where used. Use __FILE__ for this 253e5c89e4eSSatish Balay */ 254efca3c55SSatish Balay PetscErrorCode PetscTrFreeDefault(void *aa,int line,const char function[],const char file[]) 255e5c89e4eSSatish Balay { 256e5c89e4eSSatish Balay char *a = (char*)aa; 257e5c89e4eSSatish Balay TRSPACE *head; 258e5c89e4eSSatish Balay char *ahead; 259e5c89e4eSSatish Balay PetscErrorCode ierr; 2600700a824SBarry Smith PetscClassId *nend; 261e5c89e4eSSatish Balay 262e5c89e4eSSatish Balay PetscFunctionBegin; 263e5c89e4eSSatish Balay /* Do not try to handle empty blocks */ 26449d7da52SJed Brown if (!a) PetscFunctionReturn(0); 265e5c89e4eSSatish Balay 266e5c89e4eSSatish Balay if (TRdebugLevel) { 267efca3c55SSatish Balay ierr = PetscMallocValidate(line,function,file);CHKERRQ(ierr); 268e5c89e4eSSatish Balay } 269e5c89e4eSSatish Balay 270e5c89e4eSSatish Balay ahead = a; 271e5c89e4eSSatish Balay a = a - sizeof(TrSPACE); 272e5c89e4eSSatish Balay head = (TRSPACE*)a; 273e5c89e4eSSatish Balay 2740700a824SBarry Smith if (head->classid != CLASSID_VALUE) { 275efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 276e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block at address %p is corrupted; cannot free;\nmay be block not allocated with PetscMalloc()\n",a); 277e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Bad location or corrupted memory"); 278e5c89e4eSSatish Balay } 2790700a824SBarry Smith nend = (PetscClassId*)(ahead + head->size); 2800700a824SBarry Smith if (*nend != CLASSID_VALUE) { 281e5c89e4eSSatish Balay if (*nend == ALREADY_FREED) { 282efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 283e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p was already freed\n",head->id,(PetscLogDouble)head->size,a + sizeof(TrSPACE)); 284e5c89e4eSSatish Balay if (head->lineno > 0 && head->lineno < 50000 /* sanity check */) { 285efca3c55SSatish Balay (*PetscErrorPrintf)("Block freed in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 286e5c89e4eSSatish Balay } else { 287efca3c55SSatish Balay (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,-head->lineno,head->filename); 288e5c89e4eSSatish Balay } 289e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Memory already freed"); 290e5c89e4eSSatish Balay } else { 291e5c89e4eSSatish Balay /* Damaged tail */ 292efca3c55SSatish Balay (*PetscErrorPrintf)("PetscTrFreeDefault() called from %s() line %d in %s\n",function,line,file); 293e5c89e4eSSatish Balay (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p is corrupted (probably write past end of array)\n",head->id,(PetscLogDouble)head->size,a); 294efca3c55SSatish Balay (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename); 295e32f2f54SBarry Smith SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Corrupted memory"); 296e5c89e4eSSatish Balay } 297e5c89e4eSSatish Balay } 298e5c89e4eSSatish Balay /* Mark the location freed */ 299e5c89e4eSSatish Balay *nend = ALREADY_FREED; 300e5c89e4eSSatish Balay /* Save location where freed. If we suspect the line number, mark as allocated location */ 301e5c89e4eSSatish Balay if (line > 0 && line < 50000) { 302e5c89e4eSSatish Balay head->lineno = line; 303e5c89e4eSSatish Balay head->filename = file; 304e5c89e4eSSatish Balay head->functionname = function; 305e5c89e4eSSatish Balay } else { 306e5c89e4eSSatish Balay head->lineno = -head->lineno; 307e5c89e4eSSatish Balay } 308e5c89e4eSSatish Balay /* zero out memory - helps to find some reuse of already freed memory */ 309e5c89e4eSSatish Balay ierr = PetscMemzero(aa,head->size);CHKERRQ(ierr); 310e5c89e4eSSatish Balay 311e5c89e4eSSatish Balay TRallocated -= head->size; 312e5c89e4eSSatish Balay TRfrags--; 313e5c89e4eSSatish Balay if (head->prev) head->prev->next = head->next; 314e5c89e4eSSatish Balay else TRhead = head->next; 315e5c89e4eSSatish Balay 316e5c89e4eSSatish Balay if (head->next) head->next->prev = head->prev; 317efca3c55SSatish Balay ierr = PetscFreeAlign(a,line,function,file);CHKERRQ(ierr); 318e5c89e4eSSatish Balay PetscFunctionReturn(0); 319e5c89e4eSSatish Balay } 320e5c89e4eSSatish Balay 321e5c89e4eSSatish Balay 322e5c89e4eSSatish Balay #undef __FUNCT__ 3230841954dSBarry Smith #define __FUNCT__ "PetscMemoryView" 324fe7fb379SMatthew Knepley /*@C 3250841954dSBarry Smith PetscMemoryView - Shows the amount of memory currently being used 326e5c89e4eSSatish Balay in a communicator. 327e5c89e4eSSatish Balay 328e5c89e4eSSatish Balay Collective on PetscViewer 329e5c89e4eSSatish Balay 330e5c89e4eSSatish Balay Input Parameter: 331e5c89e4eSSatish Balay + viewer - the viewer that defines the communicator 332e5c89e4eSSatish Balay - message - string printed before values 333e5c89e4eSSatish Balay 3340841954dSBarry Smith Options Database: 3350841954dSBarry Smith + -malloc - have PETSc track how much memory it has allocated 3360841954dSBarry Smith - -memory_view - during PetscFinalize() have this routine called 3370841954dSBarry Smith 338e5c89e4eSSatish Balay Level: intermediate 339e5c89e4eSSatish Balay 340e5c89e4eSSatish Balay Concepts: memory usage 341e5c89e4eSSatish Balay 3420841954dSBarry Smith .seealso: PetscMallocDump(), PetscMemoryGetCurrentUsage(), PetscMemorySetGetMaximumUsage() 343e5c89e4eSSatish Balay @*/ 3440841954dSBarry Smith PetscErrorCode PetscMemoryView(PetscViewer viewer,const char message[]) 345e5c89e4eSSatish Balay { 3460841954dSBarry Smith PetscLogDouble allocated,allocatedmax,resident,residentmax,gallocated,gallocatedmax,gresident,gresidentmax,maxgallocated,maxgallocatedmax,maxgresident,maxgresidentmax; 3470841954dSBarry Smith PetscLogDouble mingallocated,mingallocatedmax,mingresident,mingresidentmax; 348e5c89e4eSSatish Balay PetscErrorCode ierr; 349e5c89e4eSSatish Balay MPI_Comm comm; 350e5c89e4eSSatish Balay 351e5c89e4eSSatish Balay PetscFunctionBegin; 352e5c89e4eSSatish Balay if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD; 353e5c89e4eSSatish Balay ierr = PetscMallocGetCurrentUsage(&allocated);CHKERRQ(ierr); 3540841954dSBarry Smith ierr = PetscMallocGetMaximumUsage(&allocatedmax);CHKERRQ(ierr); 355e5c89e4eSSatish Balay ierr = PetscMemoryGetCurrentUsage(&resident);CHKERRQ(ierr); 356e5c89e4eSSatish Balay ierr = PetscMemoryGetMaximumUsage(&residentmax);CHKERRQ(ierr); 357e5c89e4eSSatish Balay if (residentmax > 0) residentmax = PetscMax(resident,residentmax); 358e5c89e4eSSatish Balay ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr); 359e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,message);CHKERRQ(ierr); 360e5c89e4eSSatish Balay if (resident && residentmax && allocated) { 3610841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3620841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3630841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3640841954dSBarry 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); 3650841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3660841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3670841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3680841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 3690841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&gallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3700841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&maxgallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3710841954dSBarry Smith ierr = MPI_Reduce(&allocatedmax,&mingallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3720841954dSBarry 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); 3730841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3740841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3750841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3760841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 377e5c89e4eSSatish Balay } else if (resident && residentmax) { 3780841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3790841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3800841954dSBarry Smith ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3810841954dSBarry 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); 3820841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3830841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3840841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3850841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 386e5c89e4eSSatish Balay } else if (resident && allocated) { 3870841954dSBarry Smith ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3880841954dSBarry Smith ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3890841954dSBarry Smith ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3900841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current process memory: total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr); 3910841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3920841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3930841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 3940841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 3950841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr); 396e5c89e4eSSatish Balay } else if (allocated) { 3970841954dSBarry Smith ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr); 3980841954dSBarry Smith ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr); 3990841954dSBarry Smith ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr); 4000841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed: total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr); 4010841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr); 4020841954dSBarry Smith ierr = PetscViewerASCIIPrintf(viewer,"OS cannot compute process memory\n");CHKERRQ(ierr); 403e5c89e4eSSatish Balay } else { 404e5c89e4eSSatish Balay ierr = PetscViewerASCIIPrintf(viewer,"Run with -malloc to get statistics on PetscMalloc() calls\nOS cannot compute process memory\n");CHKERRQ(ierr); 405e5c89e4eSSatish Balay } 406e5c89e4eSSatish Balay ierr = PetscViewerFlush(viewer);CHKERRQ(ierr); 407e5c89e4eSSatish Balay PetscFunctionReturn(0); 408e5c89e4eSSatish Balay } 409e5c89e4eSSatish Balay 410e5c89e4eSSatish Balay #undef __FUNCT__ 411e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetCurrentUsage" 412e5c89e4eSSatish Balay /*@C 413e5c89e4eSSatish Balay PetscMallocGetCurrentUsage - gets the current amount of memory used that was PetscMalloc()ed 414e5c89e4eSSatish Balay 415e5c89e4eSSatish Balay Not Collective 416e5c89e4eSSatish Balay 417e5c89e4eSSatish Balay Output Parameters: 418e5c89e4eSSatish Balay . space - number of bytes currently allocated 419e5c89e4eSSatish Balay 420e5c89e4eSSatish Balay Level: intermediate 421e5c89e4eSSatish Balay 422e5c89e4eSSatish Balay Concepts: memory usage 423e5c89e4eSSatish Balay 424e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), 425e5c89e4eSSatish Balay PetscMemoryGetMaximumUsage() 426e5c89e4eSSatish Balay @*/ 4277087cfbeSBarry Smith PetscErrorCode PetscMallocGetCurrentUsage(PetscLogDouble *space) 428e5c89e4eSSatish Balay { 429e5c89e4eSSatish Balay PetscFunctionBegin; 430e5c89e4eSSatish Balay *space = (PetscLogDouble) TRallocated; 431e5c89e4eSSatish Balay PetscFunctionReturn(0); 432e5c89e4eSSatish Balay } 433e5c89e4eSSatish Balay 434e5c89e4eSSatish Balay #undef __FUNCT__ 435e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetMaximumUsage" 436e5c89e4eSSatish Balay /*@C 437e5c89e4eSSatish Balay PetscMallocGetMaximumUsage - gets the maximum amount of memory used that was PetscMalloc()ed at any time 438e5c89e4eSSatish Balay during this run. 439e5c89e4eSSatish Balay 440e5c89e4eSSatish Balay Not Collective 441e5c89e4eSSatish Balay 442e5c89e4eSSatish Balay Output Parameters: 443e5c89e4eSSatish Balay . space - maximum number of bytes ever allocated at one time 444e5c89e4eSSatish Balay 445e5c89e4eSSatish Balay Level: intermediate 446e5c89e4eSSatish Balay 447e5c89e4eSSatish Balay Concepts: memory usage 448e5c89e4eSSatish Balay 449e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), 450e5c89e4eSSatish Balay PetscMemoryGetCurrentUsage() 451e5c89e4eSSatish Balay @*/ 4527087cfbeSBarry Smith PetscErrorCode PetscMallocGetMaximumUsage(PetscLogDouble *space) 453e5c89e4eSSatish Balay { 454e5c89e4eSSatish Balay PetscFunctionBegin; 455e5c89e4eSSatish Balay *space = (PetscLogDouble) TRMaxMem; 456e5c89e4eSSatish Balay PetscFunctionReturn(0); 457e5c89e4eSSatish Balay } 458e5c89e4eSSatish Balay 459a64a8e02SBarry Smith #if defined(PETSC_USE_DEBUG) 460a64a8e02SBarry Smith #undef __FUNCT__ 461a64a8e02SBarry Smith #define __FUNCT__ "PetscMallocGetStack" 462a64a8e02SBarry Smith /*@C 463a64a8e02SBarry Smith PetscMallocGetStack - returns a pointer to the stack for the location in the program a call to PetscMalloc() was used to obtain that memory 464a64a8e02SBarry Smith 465a64a8e02SBarry Smith Collective on PETSC_COMM_WORLD 466a64a8e02SBarry Smith 467a64a8e02SBarry Smith Input Parameter: 468a64a8e02SBarry Smith . ptr - the memory location 469a64a8e02SBarry Smith 470a64a8e02SBarry Smith Output Paramter: 471a64a8e02SBarry Smith . stack - the stack indicating where the program allocated this memory 472a64a8e02SBarry Smith 473a64a8e02SBarry Smith Level: intermediate 474a64a8e02SBarry Smith 475a64a8e02SBarry Smith .seealso: PetscMallocGetCurrentUsage(), PetscMallocDumpLog() 476a64a8e02SBarry Smith @*/ 477a64a8e02SBarry Smith PetscErrorCode PetscMallocGetStack(void *ptr,PetscStack **stack) 478a64a8e02SBarry Smith { 479a64a8e02SBarry Smith TRSPACE *head; 480a64a8e02SBarry Smith 481a64a8e02SBarry Smith PetscFunctionBegin; 482a64a8e02SBarry Smith head = (TRSPACE*) (((char*)ptr) - HEADER_BYTES); 483a64a8e02SBarry Smith *stack = &head->stack; 484a64a8e02SBarry Smith PetscFunctionReturn(0); 485a64a8e02SBarry Smith } 48676386721SLisandro Dalcin #else 48776386721SLisandro Dalcin #undef __FUNCT__ 48876386721SLisandro Dalcin #define __FUNCT__ "PetscMallocGetStack" 48976386721SLisandro Dalcin PetscErrorCode PetscMallocGetStack(void *ptr,void **stack) 49076386721SLisandro Dalcin { 49176386721SLisandro Dalcin PetscFunctionBegin; 492f0ba7cfcSLisandro Dalcin *stack = NULL; 49376386721SLisandro Dalcin PetscFunctionReturn(0); 49476386721SLisandro Dalcin } 495a64a8e02SBarry Smith #endif 496a64a8e02SBarry Smith 497e5c89e4eSSatish Balay #undef __FUNCT__ 498e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDump" 499e5c89e4eSSatish Balay /*@C 500e5c89e4eSSatish Balay PetscMallocDump - Dumps the allocated memory blocks to a file. The information 501e5c89e4eSSatish Balay printed is: size of space (in bytes), address of space, id of space, 502e5c89e4eSSatish Balay file in which space was allocated, and line number at which it was 503e5c89e4eSSatish Balay allocated. 504e5c89e4eSSatish Balay 505e5c89e4eSSatish Balay Collective on PETSC_COMM_WORLD 506e5c89e4eSSatish Balay 507e5c89e4eSSatish Balay Input Parameter: 508e5c89e4eSSatish Balay . fp - file pointer. If fp is NULL, stdout is assumed. 509e5c89e4eSSatish Balay 510e5c89e4eSSatish Balay Options Database Key: 511e5c89e4eSSatish Balay . -malloc_dump - Dumps unfreed memory during call to PetscFinalize() 512e5c89e4eSSatish Balay 513e5c89e4eSSatish Balay Level: intermediate 514e5c89e4eSSatish Balay 515e5c89e4eSSatish Balay Fortran Note: 516e5c89e4eSSatish Balay The calling sequence in Fortran is PetscMallocDump(integer ierr) 517e5c89e4eSSatish Balay The fp defaults to stdout. 518e5c89e4eSSatish Balay 519e5c89e4eSSatish Balay Notes: uses MPI_COMM_WORLD, because this may be called in PetscFinalize() after PETSC_COMM_WORLD 520e5c89e4eSSatish Balay has been freed. 521e5c89e4eSSatish Balay 522e5c89e4eSSatish Balay Concepts: memory usage 523e5c89e4eSSatish Balay Concepts: memory bleeding 524e5c89e4eSSatish Balay Concepts: bleeding memory 525e5c89e4eSSatish Balay 5269e9a1f8fSvictor .seealso: PetscMallocGetCurrentUsage(), PetscMallocDumpLog() 527e5c89e4eSSatish Balay @*/ 5287087cfbeSBarry Smith PetscErrorCode PetscMallocDump(FILE *fp) 529e5c89e4eSSatish Balay { 530e5c89e4eSSatish Balay TRSPACE *head; 5315486ca60SMatthew G. Knepley PetscInt libAlloc = 0; 532e5c89e4eSSatish Balay PetscErrorCode ierr; 533e5c89e4eSSatish Balay PetscMPIInt rank; 534e5c89e4eSSatish Balay 535e5c89e4eSSatish Balay PetscFunctionBegin; 536e5c89e4eSSatish Balay ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 537da9f1d6bSBarry Smith if (!fp) fp = PETSC_STDOUT; 538e5c89e4eSSatish Balay head = TRhead; 539e5c89e4eSSatish Balay while (head) { 5405486ca60SMatthew G. Knepley PetscBool isLib; 5415486ca60SMatthew G. Knepley 5425486ca60SMatthew G. Knepley ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr); 5435486ca60SMatthew G. Knepley libAlloc += head->size; 5445486ca60SMatthew G. Knepley head = head->next; 5455486ca60SMatthew G. Knepley } 5465486ca60SMatthew G. Knepley if (TRallocated - libAlloc > 0) fprintf(fp,"[%d]Total space allocated %.0f bytes\n",rank,(PetscLogDouble)TRallocated); 5475486ca60SMatthew G. Knepley head = TRhead; 5485486ca60SMatthew G. Knepley while (head) { 5495486ca60SMatthew G. Knepley PetscBool isLib; 5505486ca60SMatthew G. Knepley 5515486ca60SMatthew G. Knepley ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr); 5525486ca60SMatthew G. Knepley if (!isLib) { 553efca3c55SSatish Balay fprintf(fp,"[%2d]%.0f bytes %s() line %d in %s\n",rank,(PetscLogDouble)head->size,head->functionname,head->lineno,head->filename); 5548bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG) 555e5c89e4eSSatish Balay ierr = PetscStackPrint(&head->stack,fp);CHKERRQ(ierr); 556e5c89e4eSSatish Balay #endif 5575486ca60SMatthew G. Knepley } 558e5c89e4eSSatish Balay head = head->next; 559e5c89e4eSSatish Balay } 560e5c89e4eSSatish Balay PetscFunctionReturn(0); 561e5c89e4eSSatish Balay } 562e5c89e4eSSatish Balay 563e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */ 564e5c89e4eSSatish Balay 565e5c89e4eSSatish Balay #undef __FUNCT__ 566e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocSetDumpLog" 567e5c89e4eSSatish Balay /*@C 568e5c89e4eSSatish Balay PetscMallocSetDumpLog - Activates logging of all calls to PetscMalloc(). 569e5c89e4eSSatish Balay 570e5c89e4eSSatish Balay Not Collective 571e5c89e4eSSatish Balay 572e5c89e4eSSatish Balay Options Database Key: 573574034a9SJed Brown + -malloc_log <filename> - Activates PetscMallocDumpLog() 574574034a9SJed Brown - -malloc_log_threshold <min> - Activates logging and sets a minimum size 575e5c89e4eSSatish Balay 576e5c89e4eSSatish Balay Level: advanced 577e5c89e4eSSatish Balay 578574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLogThreshold() 579e5c89e4eSSatish Balay @*/ 5807087cfbeSBarry Smith PetscErrorCode PetscMallocSetDumpLog(void) 581e5c89e4eSSatish Balay { 58221b680ceSJed Brown PetscErrorCode ierr; 58321b680ceSJed Brown 584e5c89e4eSSatish Balay PetscFunctionBegin; 585e5c89e4eSSatish Balay PetscLogMalloc = 0; 586a297a907SKarl Rupp 58721b680ceSJed Brown ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr); 588e5c89e4eSSatish Balay PetscFunctionReturn(0); 589e5c89e4eSSatish Balay } 590e5c89e4eSSatish Balay 591e5c89e4eSSatish Balay #undef __FUNCT__ 592574034a9SJed Brown #define __FUNCT__ "PetscMallocSetDumpLogThreshold" 593574034a9SJed Brown /*@C 594574034a9SJed Brown PetscMallocSetDumpLogThreshold - Activates logging of all calls to PetscMalloc(). 595574034a9SJed Brown 596574034a9SJed Brown Not Collective 597574034a9SJed Brown 598574034a9SJed Brown Input Arguments: 599574034a9SJed Brown . logmin - minimum allocation size to log, or PETSC_DEFAULT 600574034a9SJed Brown 601574034a9SJed Brown Options Database Key: 602574034a9SJed Brown + -malloc_log <filename> - Activates PetscMallocDumpLog() 603574034a9SJed Brown - -malloc_log_threshold <min> - Activates logging and sets a minimum size 604574034a9SJed Brown 605574034a9SJed Brown Level: advanced 606574034a9SJed Brown 607574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLog() 608574034a9SJed Brown @*/ 609574034a9SJed Brown PetscErrorCode PetscMallocSetDumpLogThreshold(PetscLogDouble logmin) 610574034a9SJed Brown { 611574034a9SJed Brown PetscErrorCode ierr; 612574034a9SJed Brown 613574034a9SJed Brown PetscFunctionBegin; 614574034a9SJed Brown ierr = PetscMallocSetDumpLog();CHKERRQ(ierr); 615574034a9SJed Brown if (logmin < 0) logmin = 0.0; /* PETSC_DEFAULT or PETSC_DECIDE */ 616574034a9SJed Brown PetscLogMallocThreshold = (size_t)logmin; 617574034a9SJed Brown PetscFunctionReturn(0); 618574034a9SJed Brown } 619574034a9SJed Brown 620574034a9SJed Brown #undef __FUNCT__ 62118a2528dSJed Brown #define __FUNCT__ "PetscMallocGetDumpLog" 62218a2528dSJed Brown /*@C 62318a2528dSJed Brown PetscMallocGetDumpLog - Determine whether all calls to PetscMalloc() are being logged 62418a2528dSJed Brown 62518a2528dSJed Brown Not Collective 62618a2528dSJed Brown 62718a2528dSJed Brown Output Arguments 62818a2528dSJed Brown . logging - PETSC_TRUE if logging is active 62918a2528dSJed Brown 63018a2528dSJed Brown Options Database Key: 63118a2528dSJed Brown . -malloc_log - Activates PetscMallocDumpLog() 63218a2528dSJed Brown 63318a2528dSJed Brown Level: advanced 63418a2528dSJed Brown 63518a2528dSJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog() 63618a2528dSJed Brown @*/ 63718a2528dSJed Brown PetscErrorCode PetscMallocGetDumpLog(PetscBool *logging) 63818a2528dSJed Brown { 63918a2528dSJed Brown 64018a2528dSJed Brown PetscFunctionBegin; 64118a2528dSJed Brown *logging = (PetscBool)(PetscLogMalloc >= 0); 64218a2528dSJed Brown PetscFunctionReturn(0); 64318a2528dSJed Brown } 64418a2528dSJed Brown 64518a2528dSJed Brown #undef __FUNCT__ 646e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDumpLog" 647e5c89e4eSSatish Balay /*@C 648e5c89e4eSSatish Balay PetscMallocDumpLog - Dumps the log of all calls to PetscMalloc(); also calls 64921b680ceSJed Brown PetscMemoryGetMaximumUsage() 650e5c89e4eSSatish Balay 651e5c89e4eSSatish Balay Collective on PETSC_COMM_WORLD 652e5c89e4eSSatish Balay 653e5c89e4eSSatish Balay Input Parameter: 6540298fd71SBarry Smith . fp - file pointer; or NULL 655e5c89e4eSSatish Balay 656e5c89e4eSSatish Balay Options Database Key: 657e5c89e4eSSatish Balay . -malloc_log - Activates PetscMallocDumpLog() 658e5c89e4eSSatish Balay 659e5c89e4eSSatish Balay Level: advanced 660e5c89e4eSSatish Balay 661e5c89e4eSSatish Balay Fortran Note: 662e5c89e4eSSatish Balay The calling sequence in Fortran is PetscMallocDumpLog(integer ierr) 663e5c89e4eSSatish Balay The fp defaults to stdout. 664e5c89e4eSSatish Balay 665e5c89e4eSSatish Balay .seealso: PetscMallocGetCurrentUsage(), PetscMallocDump(), PetscMallocSetDumpLog() 666e5c89e4eSSatish Balay @*/ 6677087cfbeSBarry Smith PetscErrorCode PetscMallocDumpLog(FILE *fp) 668e5c89e4eSSatish Balay { 669e5c89e4eSSatish Balay PetscInt i,j,n,dummy,*perm; 670e5c89e4eSSatish Balay size_t *shortlength; 671f56c2debSBarry Smith int *shortcount,err; 672e5c89e4eSSatish Balay PetscMPIInt rank,size,tag = 1212 /* very bad programming */; 673ace3abfcSBarry Smith PetscBool match; 674e5c89e4eSSatish Balay const char **shortfunction; 675e5c89e4eSSatish Balay PetscLogDouble rss; 676e5c89e4eSSatish Balay MPI_Status status; 677e5c89e4eSSatish Balay PetscErrorCode ierr; 678e5c89e4eSSatish Balay 679e5c89e4eSSatish Balay PetscFunctionBegin; 680e5c89e4eSSatish Balay ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr); 681e5c89e4eSSatish Balay ierr = MPI_Comm_size(MPI_COMM_WORLD,&size);CHKERRQ(ierr); 682e5c89e4eSSatish Balay /* 683e5c89e4eSSatish Balay Try to get the data printed in order by processor. This will only sometimes work 684e5c89e4eSSatish Balay */ 685f56c2debSBarry Smith err = fflush(fp); 686e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 687f56c2debSBarry Smith 688e5c89e4eSSatish Balay ierr = MPI_Barrier(MPI_COMM_WORLD);CHKERRQ(ierr); 689e5c89e4eSSatish Balay if (rank) { 690e5c89e4eSSatish Balay ierr = MPI_Recv(&dummy,1,MPIU_INT,rank-1,tag,MPI_COMM_WORLD,&status);CHKERRQ(ierr); 691e5c89e4eSSatish Balay } 692e5c89e4eSSatish Balay 693768aa557SSatish 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()"); 694768aa557SSatish Balay 695da9f1d6bSBarry Smith if (!fp) fp = PETSC_STDOUT; 696f3d65365SJed Brown ierr = PetscMemoryGetMaximumUsage(&rss);CHKERRQ(ierr); 697e5c89e4eSSatish Balay if (rss) { 698f3d65365SJed 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); 699e5c89e4eSSatish Balay } else { 700e5c89e4eSSatish 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); 701e5c89e4eSSatish Balay } 702e32f2f54SBarry Smith shortcount = (int*)malloc(PetscLogMalloc*sizeof(int));if (!shortcount) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 703e32f2f54SBarry Smith shortlength = (size_t*)malloc(PetscLogMalloc*sizeof(size_t));if (!shortlength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 704e32f2f54SBarry Smith shortfunction = (const char**)malloc(PetscLogMalloc*sizeof(char*));if (!shortfunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 70597b9d747SJed Brown for (i=0,n=0; i<PetscLogMalloc; i++) { 706e5c89e4eSSatish Balay for (j=0; j<n; j++) { 707e5c89e4eSSatish Balay ierr = PetscStrcmp(shortfunction[j],PetscLogMallocFunction[i],&match);CHKERRQ(ierr); 708e5c89e4eSSatish Balay if (match) { 709e5c89e4eSSatish Balay shortlength[j] += PetscLogMallocLength[i]; 71059ffdab8SBarry Smith shortcount[j]++; 711e5c89e4eSSatish Balay goto foundit; 712e5c89e4eSSatish Balay } 713e5c89e4eSSatish Balay } 714e5c89e4eSSatish Balay shortfunction[n] = PetscLogMallocFunction[i]; 715e5c89e4eSSatish Balay shortlength[n] = PetscLogMallocLength[i]; 71659ffdab8SBarry Smith shortcount[n] = 1; 717e5c89e4eSSatish Balay n++; 718e5c89e4eSSatish Balay foundit:; 719e5c89e4eSSatish Balay } 720e5c89e4eSSatish Balay 721e32f2f54SBarry Smith perm = (PetscInt*)malloc(n*sizeof(PetscInt));if (!perm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory"); 722e5c89e4eSSatish Balay for (i=0; i<n; i++) perm[i] = i; 723e5c89e4eSSatish Balay ierr = PetscSortStrWithPermutation(n,(const char**)shortfunction,perm);CHKERRQ(ierr); 724e5c89e4eSSatish Balay 725e5c89e4eSSatish Balay ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] Memory usage sorted by function\n",rank);CHKERRQ(ierr); 726e5c89e4eSSatish Balay for (i=0; i<n; i++) { 72759ffdab8SBarry 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); 728e5c89e4eSSatish Balay } 729e5c89e4eSSatish Balay free(perm); 730e5c89e4eSSatish Balay free(shortlength); 73159ffdab8SBarry Smith free(shortcount); 732e5c89e4eSSatish Balay free((char**)shortfunction); 733f56c2debSBarry Smith err = fflush(fp); 734e32f2f54SBarry Smith if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file"); 735e5c89e4eSSatish Balay if (rank != size-1) { 736e5c89e4eSSatish Balay ierr = MPI_Send(&dummy,1,MPIU_INT,rank+1,tag,MPI_COMM_WORLD);CHKERRQ(ierr); 737e5c89e4eSSatish Balay } 738e5c89e4eSSatish Balay PetscFunctionReturn(0); 739e5c89e4eSSatish Balay } 740e5c89e4eSSatish Balay 741e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */ 742e5c89e4eSSatish Balay 743e5c89e4eSSatish Balay #undef __FUNCT__ 744e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDebug" 745e5c89e4eSSatish Balay /*@C 746e5c89e4eSSatish Balay PetscMallocDebug - Turns on/off debugging for the memory management routines. 747e5c89e4eSSatish Balay 748e5c89e4eSSatish Balay Not Collective 749e5c89e4eSSatish Balay 750e5c89e4eSSatish Balay Input Parameter: 751e5c89e4eSSatish Balay . level - PETSC_TRUE or PETSC_FALSE 752e5c89e4eSSatish Balay 753e5c89e4eSSatish Balay Level: intermediate 754e5c89e4eSSatish Balay 755e5c89e4eSSatish Balay .seealso: CHKMEMQ(), PetscMallocValidate() 756e5c89e4eSSatish Balay @*/ 7577087cfbeSBarry Smith PetscErrorCode PetscMallocDebug(PetscBool level) 758e5c89e4eSSatish Balay { 759e5c89e4eSSatish Balay PetscFunctionBegin; 760e5c89e4eSSatish Balay TRdebugLevel = level; 761e5c89e4eSSatish Balay PetscFunctionReturn(0); 762e5c89e4eSSatish Balay } 7630acecf5bSBarry Smith 7640acecf5bSBarry Smith #undef __FUNCT__ 7650acecf5bSBarry Smith #define __FUNCT__ "PetscMallocGetDebug" 7660acecf5bSBarry Smith /*@C 7670acecf5bSBarry Smith PetscMallocGetDebug - Indicates if any PETSc is doing ANY memory debugging. 7680acecf5bSBarry Smith 7690acecf5bSBarry Smith Not Collective 7700acecf5bSBarry Smith 7710acecf5bSBarry Smith Output Parameter: 7720acecf5bSBarry Smith . flg - PETSC_TRUE if any debugger 7730acecf5bSBarry Smith 7740acecf5bSBarry Smith Level: intermediate 7750acecf5bSBarry Smith 7760acecf5bSBarry Smith Note that by default, the debug version always does some debugging unless you run with -malloc no 7770acecf5bSBarry Smith 7780acecf5bSBarry Smith 7790acecf5bSBarry Smith .seealso: CHKMEMQ(), PetscMallocValidate() 7800acecf5bSBarry Smith @*/ 7810acecf5bSBarry Smith PetscErrorCode PetscMallocGetDebug(PetscBool *flg) 7820acecf5bSBarry Smith { 7830acecf5bSBarry Smith PetscFunctionBegin; 7840acecf5bSBarry Smith if (PetscTrMalloc == PetscTrMallocDefault) *flg = PETSC_TRUE; 7850acecf5bSBarry Smith else *flg = PETSC_FALSE; 7860acecf5bSBarry Smith PetscFunctionReturn(0); 7870acecf5bSBarry Smith } 788