xref: /petsc/src/sys/memory/mtr.c (revision 3221ece2abbae0683ae45559f70513e361d95297)
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[]);
18*3221ece2SMatthew 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[]);
21*3221ece2SMatthew 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);
79*3221ece2SMatthew 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 
325*3221ece2SMatthew G. Knepley 
326*3221ece2SMatthew G. Knepley #undef __FUNCT__
327*3221ece2SMatthew G. Knepley #define __FUNCT__ "PetscTrReallocDefault"
328*3221ece2SMatthew G. Knepley /*
329*3221ece2SMatthew G. Knepley   PetscTrReallocDefault - Realloc with tracing.
330*3221ece2SMatthew G. Knepley 
331*3221ece2SMatthew G. Knepley   Input Parameters:
332*3221ece2SMatthew G. Knepley + len      - number of bytes to allocate
333*3221ece2SMatthew G. Knepley . lineno   - line number where used.  Use __LINE__ for this
334*3221ece2SMatthew G. Knepley . function - function calling routine. Use __FUNCT__ for this
335*3221ece2SMatthew G. Knepley . filename - file name where used.  Use __FILE__ for this
336*3221ece2SMatthew G. Knepley - result   - double aligned pointer to initial storage.
337*3221ece2SMatthew G. Knepley 
338*3221ece2SMatthew G. Knepley   Output Parameter:
339*3221ece2SMatthew G. Knepley . result - double aligned pointer to requested storage, or null if not available.
340*3221ece2SMatthew G. Knepley 
341*3221ece2SMatthew G. Knepley   Level: developer
342*3221ece2SMatthew G. Knepley 
343*3221ece2SMatthew G. Knepley .seealso: PetscTrMallocDefault(), PetscTrFreeDefault()
344*3221ece2SMatthew G. Knepley */
345*3221ece2SMatthew G. Knepley PetscErrorCode PetscTrReallocDefault(size_t len, int lineno, const char function[], const char filename[], void **result)
346*3221ece2SMatthew G. Knepley {
347*3221ece2SMatthew G. Knepley   char           *a = (char *) *result;
348*3221ece2SMatthew G. Knepley   TRSPACE        *head;
349*3221ece2SMatthew G. Knepley   char           *ahead, *inew;
350*3221ece2SMatthew G. Knepley   PetscClassId   *nend;
351*3221ece2SMatthew G. Knepley   size_t         nsize;
352*3221ece2SMatthew G. Knepley   PetscErrorCode ierr;
353*3221ece2SMatthew G. Knepley 
354*3221ece2SMatthew G. Knepley   PetscFunctionBegin;
355*3221ece2SMatthew G. Knepley   /* Do not try to handle empty blocks */
356*3221ece2SMatthew G. Knepley   if (!len) {*result = NULL; PetscFunctionReturn(0);}
357*3221ece2SMatthew G. Knepley 
358*3221ece2SMatthew G. Knepley   if (TRdebugLevel) {ierr = PetscMallocValidate(lineno,function,filename); if (ierr) PetscFunctionReturn(ierr);}
359*3221ece2SMatthew G. Knepley 
360*3221ece2SMatthew G. Knepley   ahead = a;
361*3221ece2SMatthew G. Knepley   a     = a - sizeof(TrSPACE);
362*3221ece2SMatthew G. Knepley   head  = (TRSPACE *) a;
363*3221ece2SMatthew G. Knepley   inew  = a;
364*3221ece2SMatthew G. Knepley 
365*3221ece2SMatthew G. Knepley   if (head->classid != CLASSID_VALUE) {
366*3221ece2SMatthew G. Knepley     (*PetscErrorPrintf)("PetscTrReallocDefault() called from %s() line %d in %s\n",function,lineno,filename);
367*3221ece2SMatthew G. Knepley     (*PetscErrorPrintf)("Block at address %p is corrupted; cannot free;\nmay be block not allocated with PetscMalloc()\n",a);
368*3221ece2SMatthew G. Knepley     SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Bad location or corrupted memory");
369*3221ece2SMatthew G. Knepley   }
370*3221ece2SMatthew G. Knepley   nend = (PetscClassId *)(ahead + head->size);
371*3221ece2SMatthew G. Knepley   if (*nend != CLASSID_VALUE) {
372*3221ece2SMatthew G. Knepley     if (*nend == ALREADY_FREED) {
373*3221ece2SMatthew G. Knepley       (*PetscErrorPrintf)("PetscTrReallocDefault() called from %s() line %d in %s\n",function,lineno,filename);
374*3221ece2SMatthew G. Knepley       (*PetscErrorPrintf)("Block [id=%d(%.0f)] at address %p was already freed\n",head->id,(PetscLogDouble)head->size,a + sizeof(TrSPACE));
375*3221ece2SMatthew G. Knepley       if (head->lineno > 0 && head->lineno < 50000 /* sanity check */) {
376*3221ece2SMatthew G. Knepley         (*PetscErrorPrintf)("Block freed in %s() line %d in %s\n",head->functionname,head->lineno,head->filename);
377*3221ece2SMatthew G. Knepley       } else {
378*3221ece2SMatthew G. Knepley         (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,-head->lineno,head->filename);
379*3221ece2SMatthew G. Knepley       }
380*3221ece2SMatthew G. Knepley       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONG,"Memory already freed");
381*3221ece2SMatthew G. Knepley     } else {
382*3221ece2SMatthew G. Knepley       /* Damaged tail */
383*3221ece2SMatthew G. Knepley       (*PetscErrorPrintf)("PetscTrReallocDefault() called from %s() line %d in %s\n",function,lineno,filename);
384*3221ece2SMatthew 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);
385*3221ece2SMatthew G. Knepley       (*PetscErrorPrintf)("Block allocated in %s() line %d in %s\n",head->functionname,head->lineno,head->filename);
386*3221ece2SMatthew G. Knepley       SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEMC,"Corrupted memory");
387*3221ece2SMatthew G. Knepley     }
388*3221ece2SMatthew G. Knepley   }
389*3221ece2SMatthew G. Knepley 
390*3221ece2SMatthew G. Knepley   TRallocated -= head->size;
391*3221ece2SMatthew G. Knepley   TRfrags--;
392*3221ece2SMatthew G. Knepley   if (head->prev) head->prev->next = head->next;
393*3221ece2SMatthew G. Knepley   else TRhead = head->next;
394*3221ece2SMatthew G. Knepley   if (head->next) head->next->prev = head->prev;
395*3221ece2SMatthew G. Knepley 
396*3221ece2SMatthew G. Knepley   nsize = (len + (PETSC_MEMALIGN-1)) & ~(PETSC_MEMALIGN-1);
397*3221ece2SMatthew G. Knepley   ierr  = PetscReallocAlign(nsize+sizeof(TrSPACE)+sizeof(PetscClassId),lineno,function,filename,(void**)&inew);CHKERRQ(ierr);
398*3221ece2SMatthew G. Knepley 
399*3221ece2SMatthew G. Knepley   head  = (TRSPACE*)inew;
400*3221ece2SMatthew G. Knepley   inew += sizeof(TrSPACE);
401*3221ece2SMatthew G. Knepley 
402*3221ece2SMatthew G. Knepley   if (TRhead) TRhead->prev = head;
403*3221ece2SMatthew G. Knepley   head->next   = TRhead;
404*3221ece2SMatthew G. Knepley   TRhead       = head;
405*3221ece2SMatthew G. Knepley   head->prev   = NULL;
406*3221ece2SMatthew G. Knepley   head->size   = nsize;
407*3221ece2SMatthew G. Knepley   head->id     = TRid;
408*3221ece2SMatthew G. Knepley   head->lineno = lineno;
409*3221ece2SMatthew G. Knepley 
410*3221ece2SMatthew G. Knepley   head->filename                 = filename;
411*3221ece2SMatthew G. Knepley   head->functionname             = function;
412*3221ece2SMatthew G. Knepley   head->classid                  = CLASSID_VALUE;
413*3221ece2SMatthew G. Knepley   *(PetscClassId*)(inew + nsize) = CLASSID_VALUE;
414*3221ece2SMatthew G. Knepley 
415*3221ece2SMatthew G. Knepley   TRallocated += nsize;
416*3221ece2SMatthew G. Knepley   if (TRallocated > TRMaxMem) TRMaxMem = TRallocated;
417*3221ece2SMatthew G. Knepley   TRfrags++;
418*3221ece2SMatthew G. Knepley 
419*3221ece2SMatthew G. Knepley #if defined(PETSC_USE_DEBUG)
420*3221ece2SMatthew G. Knepley   if (PetscStackActive()) {
421*3221ece2SMatthew G. Knepley     ierr = PetscStackCopy(petscstack,&head->stack);CHKERRQ(ierr);
422*3221ece2SMatthew G. Knepley     /* fix the line number to where the malloc() was called, not the PetscFunctionBegin; */
423*3221ece2SMatthew G. Knepley     head->stack.line[head->stack.currentsize-2] = lineno;
424*3221ece2SMatthew G. Knepley   } else {
425*3221ece2SMatthew G. Knepley     head->stack.currentsize = 0;
426*3221ece2SMatthew G. Knepley   }
427*3221ece2SMatthew G. Knepley #endif
428*3221ece2SMatthew G. Knepley 
429*3221ece2SMatthew G. Knepley   /*
430*3221ece2SMatthew G. Knepley          Allow logging of all mallocs made
431*3221ece2SMatthew G. Knepley   */
432*3221ece2SMatthew G. Knepley   if (PetscLogMalloc > -1 && PetscLogMalloc < PetscLogMallocMax && len >= PetscLogMallocThreshold) {
433*3221ece2SMatthew G. Knepley     if (!PetscLogMalloc) {
434*3221ece2SMatthew G. Knepley       PetscLogMallocLength = (size_t*)malloc(PetscLogMallocMax*sizeof(size_t));
435*3221ece2SMatthew G. Knepley       if (!PetscLogMallocLength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," ");
436*3221ece2SMatthew G. Knepley 
437*3221ece2SMatthew G. Knepley       PetscLogMallocFile = (const char**)malloc(PetscLogMallocMax*sizeof(char*));
438*3221ece2SMatthew G. Knepley       if (!PetscLogMallocFile) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," ");
439*3221ece2SMatthew G. Knepley 
440*3221ece2SMatthew G. Knepley       PetscLogMallocFunction = (const char**)malloc(PetscLogMallocMax*sizeof(char*));
441*3221ece2SMatthew G. Knepley       if (!PetscLogMallocFunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM," ");
442*3221ece2SMatthew G. Knepley     }
443*3221ece2SMatthew G. Knepley     PetscLogMallocLength[PetscLogMalloc]     = nsize;
444*3221ece2SMatthew G. Knepley     PetscLogMallocFile[PetscLogMalloc]       = filename;
445*3221ece2SMatthew G. Knepley     PetscLogMallocFunction[PetscLogMalloc++] = function;
446*3221ece2SMatthew G. Knepley   }
447*3221ece2SMatthew G. Knepley   *result = (void*)inew;
448*3221ece2SMatthew G. Knepley   PetscFunctionReturn(0);
449*3221ece2SMatthew G. Knepley }
450*3221ece2SMatthew G. Knepley 
451*3221ece2SMatthew G. Knepley 
452e5c89e4eSSatish Balay #undef __FUNCT__
4530841954dSBarry Smith #define __FUNCT__ "PetscMemoryView"
454fe7fb379SMatthew Knepley /*@C
4550841954dSBarry Smith     PetscMemoryView - Shows the amount of memory currently being used
456e5c89e4eSSatish Balay         in a communicator.
457e5c89e4eSSatish Balay 
458e5c89e4eSSatish Balay     Collective on PetscViewer
459e5c89e4eSSatish Balay 
460e5c89e4eSSatish Balay     Input Parameter:
461e5c89e4eSSatish Balay +    viewer - the viewer that defines the communicator
462e5c89e4eSSatish Balay -    message - string printed before values
463e5c89e4eSSatish Balay 
4640841954dSBarry Smith     Options Database:
4650841954dSBarry Smith +    -malloc - have PETSc track how much memory it has allocated
4660841954dSBarry Smith -    -memory_view - during PetscFinalize() have this routine called
4670841954dSBarry Smith 
468e5c89e4eSSatish Balay     Level: intermediate
469e5c89e4eSSatish Balay 
470e5c89e4eSSatish Balay     Concepts: memory usage
471e5c89e4eSSatish Balay 
4720841954dSBarry Smith .seealso: PetscMallocDump(), PetscMemoryGetCurrentUsage(), PetscMemorySetGetMaximumUsage()
473e5c89e4eSSatish Balay  @*/
4740841954dSBarry Smith PetscErrorCode  PetscMemoryView(PetscViewer viewer,const char message[])
475e5c89e4eSSatish Balay {
4760841954dSBarry Smith   PetscLogDouble allocated,allocatedmax,resident,residentmax,gallocated,gallocatedmax,gresident,gresidentmax,maxgallocated,maxgallocatedmax,maxgresident,maxgresidentmax;
4770841954dSBarry Smith   PetscLogDouble mingallocated,mingallocatedmax,mingresident,mingresidentmax;
478e5c89e4eSSatish Balay   PetscErrorCode ierr;
479e5c89e4eSSatish Balay   MPI_Comm       comm;
480e5c89e4eSSatish Balay 
481e5c89e4eSSatish Balay   PetscFunctionBegin;
482e5c89e4eSSatish Balay   if (!viewer) viewer = PETSC_VIEWER_STDOUT_WORLD;
483e5c89e4eSSatish Balay   ierr = PetscMallocGetCurrentUsage(&allocated);CHKERRQ(ierr);
4840841954dSBarry Smith   ierr = PetscMallocGetMaximumUsage(&allocatedmax);CHKERRQ(ierr);
485e5c89e4eSSatish Balay   ierr = PetscMemoryGetCurrentUsage(&resident);CHKERRQ(ierr);
486e5c89e4eSSatish Balay   ierr = PetscMemoryGetMaximumUsage(&residentmax);CHKERRQ(ierr);
487e5c89e4eSSatish Balay   if (residentmax > 0) residentmax = PetscMax(resident,residentmax);
488e5c89e4eSSatish Balay   ierr = PetscObjectGetComm((PetscObject)viewer,&comm);CHKERRQ(ierr);
489e5c89e4eSSatish Balay   ierr = PetscViewerASCIIPrintf(viewer,message);CHKERRQ(ierr);
490e5c89e4eSSatish Balay   if (resident && residentmax && allocated) {
4910841954dSBarry Smith     ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
4920841954dSBarry Smith     ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
4930841954dSBarry Smith     ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
4940841954dSBarry 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);
4950841954dSBarry Smith     ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
4960841954dSBarry Smith     ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
4970841954dSBarry Smith     ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
4980841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Current process memory:                                  total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr);
4990841954dSBarry Smith     ierr = MPI_Reduce(&allocatedmax,&gallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5000841954dSBarry Smith     ierr = MPI_Reduce(&allocatedmax,&maxgallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5010841954dSBarry Smith     ierr = MPI_Reduce(&allocatedmax,&mingallocatedmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5020841954dSBarry 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);
5030841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5040841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5050841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5060841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed:                           total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr);
507e5c89e4eSSatish Balay   } else if (resident && residentmax) {
5080841954dSBarry Smith     ierr = MPI_Reduce(&residentmax,&gresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5090841954dSBarry Smith     ierr = MPI_Reduce(&residentmax,&maxgresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5100841954dSBarry Smith     ierr = MPI_Reduce(&residentmax,&mingresidentmax,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5110841954dSBarry 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);
5120841954dSBarry Smith     ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5130841954dSBarry Smith     ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5140841954dSBarry Smith     ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5150841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Current process memory:                                  total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr);
516e5c89e4eSSatish Balay   } else if (resident && allocated) {
5170841954dSBarry Smith     ierr = MPI_Reduce(&resident,&gresident,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5180841954dSBarry Smith     ierr = MPI_Reduce(&resident,&maxgresident,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5190841954dSBarry Smith     ierr = MPI_Reduce(&resident,&mingresident,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5200841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Current process memory:                                  total %5.4e max %5.4e min %5.4e\n",gresident,maxgresident,mingresident);CHKERRQ(ierr);
5210841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5220841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5230841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5240841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed:                           total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr);
5250841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr);
526e5c89e4eSSatish Balay   } else if (allocated) {
5270841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&gallocated,1,MPIU_PETSCLOGDOUBLE,MPI_SUM,0,comm);CHKERRQ(ierr);
5280841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&maxgallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MAX,0,comm);CHKERRQ(ierr);
5290841954dSBarry Smith     ierr = MPI_Reduce(&allocated,&mingallocated,1,MPIU_PETSCLOGDOUBLE,MPI_MIN,0,comm);CHKERRQ(ierr);
5300841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Current space PetscMalloc()ed:                           total %5.4e max %5.4e min %5.4e\n",gallocated,maxgallocated,mingallocated);CHKERRQ(ierr);
5310841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"Run with -memory_view to get maximum memory usage\n");CHKERRQ(ierr);
5320841954dSBarry Smith     ierr = PetscViewerASCIIPrintf(viewer,"OS cannot compute process memory\n");CHKERRQ(ierr);
533e5c89e4eSSatish Balay   } else {
534e5c89e4eSSatish Balay     ierr = PetscViewerASCIIPrintf(viewer,"Run with -malloc to get statistics on PetscMalloc() calls\nOS cannot compute process memory\n");CHKERRQ(ierr);
535e5c89e4eSSatish Balay   }
536e5c89e4eSSatish Balay   ierr = PetscViewerFlush(viewer);CHKERRQ(ierr);
537e5c89e4eSSatish Balay   PetscFunctionReturn(0);
538e5c89e4eSSatish Balay }
539e5c89e4eSSatish Balay 
540e5c89e4eSSatish Balay #undef __FUNCT__
541e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetCurrentUsage"
542e5c89e4eSSatish Balay /*@C
543e5c89e4eSSatish Balay     PetscMallocGetCurrentUsage - gets the current amount of memory used that was PetscMalloc()ed
544e5c89e4eSSatish Balay 
545e5c89e4eSSatish Balay     Not Collective
546e5c89e4eSSatish Balay 
547e5c89e4eSSatish Balay     Output Parameters:
548e5c89e4eSSatish Balay .   space - number of bytes currently allocated
549e5c89e4eSSatish Balay 
550e5c89e4eSSatish Balay     Level: intermediate
551e5c89e4eSSatish Balay 
552e5c89e4eSSatish Balay     Concepts: memory usage
553e5c89e4eSSatish Balay 
554e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(),
555e5c89e4eSSatish Balay           PetscMemoryGetMaximumUsage()
556e5c89e4eSSatish Balay  @*/
5577087cfbeSBarry Smith PetscErrorCode  PetscMallocGetCurrentUsage(PetscLogDouble *space)
558e5c89e4eSSatish Balay {
559e5c89e4eSSatish Balay   PetscFunctionBegin;
560e5c89e4eSSatish Balay   *space = (PetscLogDouble) TRallocated;
561e5c89e4eSSatish Balay   PetscFunctionReturn(0);
562e5c89e4eSSatish Balay }
563e5c89e4eSSatish Balay 
564e5c89e4eSSatish Balay #undef __FUNCT__
565e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocGetMaximumUsage"
566e5c89e4eSSatish Balay /*@C
567e5c89e4eSSatish Balay     PetscMallocGetMaximumUsage - gets the maximum amount of memory used that was PetscMalloc()ed at any time
568e5c89e4eSSatish Balay         during this run.
569e5c89e4eSSatish Balay 
570e5c89e4eSSatish Balay     Not Collective
571e5c89e4eSSatish Balay 
572e5c89e4eSSatish Balay     Output Parameters:
573e5c89e4eSSatish Balay .   space - maximum number of bytes ever allocated at one time
574e5c89e4eSSatish Balay 
575e5c89e4eSSatish Balay     Level: intermediate
576e5c89e4eSSatish Balay 
577e5c89e4eSSatish Balay     Concepts: memory usage
578e5c89e4eSSatish Balay 
579e5c89e4eSSatish Balay .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(),
580e5c89e4eSSatish Balay           PetscMemoryGetCurrentUsage()
581e5c89e4eSSatish Balay  @*/
5827087cfbeSBarry Smith PetscErrorCode  PetscMallocGetMaximumUsage(PetscLogDouble *space)
583e5c89e4eSSatish Balay {
584e5c89e4eSSatish Balay   PetscFunctionBegin;
585e5c89e4eSSatish Balay   *space = (PetscLogDouble) TRMaxMem;
586e5c89e4eSSatish Balay   PetscFunctionReturn(0);
587e5c89e4eSSatish Balay }
588e5c89e4eSSatish Balay 
589a64a8e02SBarry Smith #if defined(PETSC_USE_DEBUG)
590a64a8e02SBarry Smith #undef __FUNCT__
591a64a8e02SBarry Smith #define __FUNCT__ "PetscMallocGetStack"
592a64a8e02SBarry Smith /*@C
593a64a8e02SBarry Smith    PetscMallocGetStack - returns a pointer to the stack for the location in the program a call to PetscMalloc() was used to obtain that memory
594a64a8e02SBarry Smith 
595a64a8e02SBarry Smith    Collective on PETSC_COMM_WORLD
596a64a8e02SBarry Smith 
597a64a8e02SBarry Smith    Input Parameter:
598a64a8e02SBarry Smith .    ptr - the memory location
599a64a8e02SBarry Smith 
600a64a8e02SBarry Smith    Output Paramter:
601a64a8e02SBarry Smith .    stack - the stack indicating where the program allocated this memory
602a64a8e02SBarry Smith 
603a64a8e02SBarry Smith    Level: intermediate
604a64a8e02SBarry Smith 
605a64a8e02SBarry Smith .seealso:  PetscMallocGetCurrentUsage(), PetscMallocDumpLog()
606a64a8e02SBarry Smith @*/
607a64a8e02SBarry Smith PetscErrorCode  PetscMallocGetStack(void *ptr,PetscStack **stack)
608a64a8e02SBarry Smith {
609a64a8e02SBarry Smith   TRSPACE *head;
610a64a8e02SBarry Smith 
611a64a8e02SBarry Smith   PetscFunctionBegin;
612a64a8e02SBarry Smith   head   = (TRSPACE*) (((char*)ptr) - HEADER_BYTES);
613a64a8e02SBarry Smith   *stack = &head->stack;
614a64a8e02SBarry Smith   PetscFunctionReturn(0);
615a64a8e02SBarry Smith }
61676386721SLisandro Dalcin #else
61776386721SLisandro Dalcin #undef __FUNCT__
61876386721SLisandro Dalcin #define __FUNCT__ "PetscMallocGetStack"
61976386721SLisandro Dalcin PetscErrorCode  PetscMallocGetStack(void *ptr,void **stack)
62076386721SLisandro Dalcin {
62176386721SLisandro Dalcin   PetscFunctionBegin;
622f0ba7cfcSLisandro Dalcin   *stack = NULL;
62376386721SLisandro Dalcin   PetscFunctionReturn(0);
62476386721SLisandro Dalcin }
625a64a8e02SBarry Smith #endif
626a64a8e02SBarry Smith 
627e5c89e4eSSatish Balay #undef __FUNCT__
628e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDump"
629e5c89e4eSSatish Balay /*@C
630e5c89e4eSSatish Balay    PetscMallocDump - Dumps the allocated memory blocks to a file. The information
631e5c89e4eSSatish Balay    printed is: size of space (in bytes), address of space, id of space,
632e5c89e4eSSatish Balay    file in which space was allocated, and line number at which it was
633e5c89e4eSSatish Balay    allocated.
634e5c89e4eSSatish Balay 
635e5c89e4eSSatish Balay    Collective on PETSC_COMM_WORLD
636e5c89e4eSSatish Balay 
637e5c89e4eSSatish Balay    Input Parameter:
638e5c89e4eSSatish Balay .  fp  - file pointer.  If fp is NULL, stdout is assumed.
639e5c89e4eSSatish Balay 
640e5c89e4eSSatish Balay    Options Database Key:
641e5c89e4eSSatish Balay .  -malloc_dump - Dumps unfreed memory during call to PetscFinalize()
642e5c89e4eSSatish Balay 
643e5c89e4eSSatish Balay    Level: intermediate
644e5c89e4eSSatish Balay 
645e5c89e4eSSatish Balay    Fortran Note:
646e5c89e4eSSatish Balay    The calling sequence in Fortran is PetscMallocDump(integer ierr)
647e5c89e4eSSatish Balay    The fp defaults to stdout.
648e5c89e4eSSatish Balay 
649e5c89e4eSSatish Balay    Notes: uses MPI_COMM_WORLD, because this may be called in PetscFinalize() after PETSC_COMM_WORLD
650e5c89e4eSSatish Balay           has been freed.
651e5c89e4eSSatish Balay 
652e5c89e4eSSatish Balay    Concepts: memory usage
653e5c89e4eSSatish Balay    Concepts: memory bleeding
654e5c89e4eSSatish Balay    Concepts: bleeding memory
655e5c89e4eSSatish Balay 
6569e9a1f8fSvictor .seealso:  PetscMallocGetCurrentUsage(), PetscMallocDumpLog()
657e5c89e4eSSatish Balay @*/
6587087cfbeSBarry Smith PetscErrorCode  PetscMallocDump(FILE *fp)
659e5c89e4eSSatish Balay {
660e5c89e4eSSatish Balay   TRSPACE        *head;
6615486ca60SMatthew G. Knepley   PetscInt       libAlloc = 0;
662e5c89e4eSSatish Balay   PetscErrorCode ierr;
663e5c89e4eSSatish Balay   PetscMPIInt    rank;
664e5c89e4eSSatish Balay 
665e5c89e4eSSatish Balay   PetscFunctionBegin;
666e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr);
667da9f1d6bSBarry Smith   if (!fp) fp = PETSC_STDOUT;
668e5c89e4eSSatish Balay   head = TRhead;
669e5c89e4eSSatish Balay   while (head) {
6705486ca60SMatthew G. Knepley     PetscBool isLib;
6715486ca60SMatthew G. Knepley 
6725486ca60SMatthew G. Knepley     ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr);
6735486ca60SMatthew G. Knepley     libAlloc += head->size;
6745486ca60SMatthew G. Knepley     head = head->next;
6755486ca60SMatthew G. Knepley   }
6765486ca60SMatthew G. Knepley   if (TRallocated - libAlloc > 0) fprintf(fp,"[%d]Total space allocated %.0f bytes\n",rank,(PetscLogDouble)TRallocated);
6775486ca60SMatthew G. Knepley   head = TRhead;
6785486ca60SMatthew G. Knepley   while (head) {
6795486ca60SMatthew G. Knepley     PetscBool isLib;
6805486ca60SMatthew G. Knepley 
6815486ca60SMatthew G. Knepley     ierr = PetscStrcmp(head->functionname, "PetscDLLibraryOpen", &isLib);CHKERRQ(ierr);
6825486ca60SMatthew G. Knepley     if (!isLib) {
683efca3c55SSatish Balay       fprintf(fp,"[%2d]%.0f bytes %s() line %d in %s\n",rank,(PetscLogDouble)head->size,head->functionname,head->lineno,head->filename);
6848bf1f09cSShri Abhyankar #if defined(PETSC_USE_DEBUG)
685e5c89e4eSSatish Balay       ierr = PetscStackPrint(&head->stack,fp);CHKERRQ(ierr);
686e5c89e4eSSatish Balay #endif
6875486ca60SMatthew G. Knepley     }
688e5c89e4eSSatish Balay     head = head->next;
689e5c89e4eSSatish Balay   }
690e5c89e4eSSatish Balay   PetscFunctionReturn(0);
691e5c89e4eSSatish Balay }
692e5c89e4eSSatish Balay 
693e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */
694e5c89e4eSSatish Balay 
695e5c89e4eSSatish Balay #undef __FUNCT__
696e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocSetDumpLog"
697e5c89e4eSSatish Balay /*@C
698e5c89e4eSSatish Balay     PetscMallocSetDumpLog - Activates logging of all calls to PetscMalloc().
699e5c89e4eSSatish Balay 
700e5c89e4eSSatish Balay     Not Collective
701e5c89e4eSSatish Balay 
702e5c89e4eSSatish Balay     Options Database Key:
703574034a9SJed Brown +  -malloc_log <filename> - Activates PetscMallocDumpLog()
704574034a9SJed Brown -  -malloc_log_threshold <min> - Activates logging and sets a minimum size
705e5c89e4eSSatish Balay 
706e5c89e4eSSatish Balay     Level: advanced
707e5c89e4eSSatish Balay 
708574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLogThreshold()
709e5c89e4eSSatish Balay @*/
7107087cfbeSBarry Smith PetscErrorCode PetscMallocSetDumpLog(void)
711e5c89e4eSSatish Balay {
71221b680ceSJed Brown   PetscErrorCode ierr;
71321b680ceSJed Brown 
714e5c89e4eSSatish Balay   PetscFunctionBegin;
715e5c89e4eSSatish Balay   PetscLogMalloc = 0;
716a297a907SKarl Rupp 
71721b680ceSJed Brown   ierr = PetscMemorySetGetMaximumUsage();CHKERRQ(ierr);
718e5c89e4eSSatish Balay   PetscFunctionReturn(0);
719e5c89e4eSSatish Balay }
720e5c89e4eSSatish Balay 
721e5c89e4eSSatish Balay #undef __FUNCT__
722574034a9SJed Brown #define __FUNCT__ "PetscMallocSetDumpLogThreshold"
723574034a9SJed Brown /*@C
724574034a9SJed Brown     PetscMallocSetDumpLogThreshold - Activates logging of all calls to PetscMalloc().
725574034a9SJed Brown 
726574034a9SJed Brown     Not Collective
727574034a9SJed Brown 
728574034a9SJed Brown     Input Arguments:
729574034a9SJed Brown .   logmin - minimum allocation size to log, or PETSC_DEFAULT
730574034a9SJed Brown 
731574034a9SJed Brown     Options Database Key:
732574034a9SJed Brown +  -malloc_log <filename> - Activates PetscMallocDumpLog()
733574034a9SJed Brown -  -malloc_log_threshold <min> - Activates logging and sets a minimum size
734574034a9SJed Brown 
735574034a9SJed Brown     Level: advanced
736574034a9SJed Brown 
737574034a9SJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog(), PetscMallocSetDumpLog()
738574034a9SJed Brown @*/
739574034a9SJed Brown PetscErrorCode PetscMallocSetDumpLogThreshold(PetscLogDouble logmin)
740574034a9SJed Brown {
741574034a9SJed Brown   PetscErrorCode ierr;
742574034a9SJed Brown 
743574034a9SJed Brown   PetscFunctionBegin;
744574034a9SJed Brown   ierr = PetscMallocSetDumpLog();CHKERRQ(ierr);
745574034a9SJed Brown   if (logmin < 0) logmin = 0.0; /* PETSC_DEFAULT or PETSC_DECIDE */
746574034a9SJed Brown   PetscLogMallocThreshold = (size_t)logmin;
747574034a9SJed Brown   PetscFunctionReturn(0);
748574034a9SJed Brown }
749574034a9SJed Brown 
750574034a9SJed Brown #undef __FUNCT__
75118a2528dSJed Brown #define __FUNCT__ "PetscMallocGetDumpLog"
75218a2528dSJed Brown /*@C
75318a2528dSJed Brown     PetscMallocGetDumpLog - Determine whether all calls to PetscMalloc() are being logged
75418a2528dSJed Brown 
75518a2528dSJed Brown     Not Collective
75618a2528dSJed Brown 
75718a2528dSJed Brown     Output Arguments
75818a2528dSJed Brown .   logging - PETSC_TRUE if logging is active
75918a2528dSJed Brown 
76018a2528dSJed Brown     Options Database Key:
76118a2528dSJed Brown .  -malloc_log - Activates PetscMallocDumpLog()
76218a2528dSJed Brown 
76318a2528dSJed Brown     Level: advanced
76418a2528dSJed Brown 
76518a2528dSJed Brown .seealso: PetscMallocDump(), PetscMallocDumpLog()
76618a2528dSJed Brown @*/
76718a2528dSJed Brown PetscErrorCode PetscMallocGetDumpLog(PetscBool *logging)
76818a2528dSJed Brown {
76918a2528dSJed Brown 
77018a2528dSJed Brown   PetscFunctionBegin;
77118a2528dSJed Brown   *logging = (PetscBool)(PetscLogMalloc >= 0);
77218a2528dSJed Brown   PetscFunctionReturn(0);
77318a2528dSJed Brown }
77418a2528dSJed Brown 
77518a2528dSJed Brown #undef __FUNCT__
776e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDumpLog"
777e5c89e4eSSatish Balay /*@C
778e5c89e4eSSatish Balay     PetscMallocDumpLog - Dumps the log of all calls to PetscMalloc(); also calls
77921b680ceSJed Brown        PetscMemoryGetMaximumUsage()
780e5c89e4eSSatish Balay 
781e5c89e4eSSatish Balay     Collective on PETSC_COMM_WORLD
782e5c89e4eSSatish Balay 
783e5c89e4eSSatish Balay     Input Parameter:
7840298fd71SBarry Smith .   fp - file pointer; or NULL
785e5c89e4eSSatish Balay 
786e5c89e4eSSatish Balay     Options Database Key:
787e5c89e4eSSatish Balay .  -malloc_log - Activates PetscMallocDumpLog()
788e5c89e4eSSatish Balay 
789e5c89e4eSSatish Balay     Level: advanced
790e5c89e4eSSatish Balay 
791e5c89e4eSSatish Balay    Fortran Note:
792e5c89e4eSSatish Balay    The calling sequence in Fortran is PetscMallocDumpLog(integer ierr)
793e5c89e4eSSatish Balay    The fp defaults to stdout.
794e5c89e4eSSatish Balay 
795e5c89e4eSSatish Balay .seealso: PetscMallocGetCurrentUsage(), PetscMallocDump(), PetscMallocSetDumpLog()
796e5c89e4eSSatish Balay @*/
7977087cfbeSBarry Smith PetscErrorCode  PetscMallocDumpLog(FILE *fp)
798e5c89e4eSSatish Balay {
799e5c89e4eSSatish Balay   PetscInt       i,j,n,dummy,*perm;
800e5c89e4eSSatish Balay   size_t         *shortlength;
801f56c2debSBarry Smith   int            *shortcount,err;
802e5c89e4eSSatish Balay   PetscMPIInt    rank,size,tag = 1212 /* very bad programming */;
803ace3abfcSBarry Smith   PetscBool      match;
804e5c89e4eSSatish Balay   const char     **shortfunction;
805e5c89e4eSSatish Balay   PetscLogDouble rss;
806e5c89e4eSSatish Balay   MPI_Status     status;
807e5c89e4eSSatish Balay   PetscErrorCode ierr;
808e5c89e4eSSatish Balay 
809e5c89e4eSSatish Balay   PetscFunctionBegin;
810e5c89e4eSSatish Balay   ierr = MPI_Comm_rank(MPI_COMM_WORLD,&rank);CHKERRQ(ierr);
811e5c89e4eSSatish Balay   ierr = MPI_Comm_size(MPI_COMM_WORLD,&size);CHKERRQ(ierr);
812e5c89e4eSSatish Balay   /*
813e5c89e4eSSatish Balay        Try to get the data printed in order by processor. This will only sometimes work
814e5c89e4eSSatish Balay   */
815f56c2debSBarry Smith   err = fflush(fp);
816e32f2f54SBarry Smith   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
817f56c2debSBarry Smith 
818e5c89e4eSSatish Balay   ierr = MPI_Barrier(MPI_COMM_WORLD);CHKERRQ(ierr);
819e5c89e4eSSatish Balay   if (rank) {
820e5c89e4eSSatish Balay     ierr = MPI_Recv(&dummy,1,MPIU_INT,rank-1,tag,MPI_COMM_WORLD,&status);CHKERRQ(ierr);
821e5c89e4eSSatish Balay   }
822e5c89e4eSSatish Balay 
823768aa557SSatish 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()");
824768aa557SSatish Balay 
825da9f1d6bSBarry Smith   if (!fp) fp = PETSC_STDOUT;
826f3d65365SJed Brown   ierr = PetscMemoryGetMaximumUsage(&rss);CHKERRQ(ierr);
827e5c89e4eSSatish Balay   if (rss) {
828f3d65365SJed 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);
829e5c89e4eSSatish Balay   } else {
830e5c89e4eSSatish 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);
831e5c89e4eSSatish Balay   }
832e32f2f54SBarry Smith   shortcount    = (int*)malloc(PetscLogMalloc*sizeof(int));if (!shortcount) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory");
833e32f2f54SBarry Smith   shortlength   = (size_t*)malloc(PetscLogMalloc*sizeof(size_t));if (!shortlength) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory");
834e32f2f54SBarry Smith   shortfunction = (const char**)malloc(PetscLogMalloc*sizeof(char*));if (!shortfunction) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory");
83597b9d747SJed Brown   for (i=0,n=0; i<PetscLogMalloc; i++) {
836e5c89e4eSSatish Balay     for (j=0; j<n; j++) {
837e5c89e4eSSatish Balay       ierr = PetscStrcmp(shortfunction[j],PetscLogMallocFunction[i],&match);CHKERRQ(ierr);
838e5c89e4eSSatish Balay       if (match) {
839e5c89e4eSSatish Balay         shortlength[j] += PetscLogMallocLength[i];
84059ffdab8SBarry Smith         shortcount[j]++;
841e5c89e4eSSatish Balay         goto foundit;
842e5c89e4eSSatish Balay       }
843e5c89e4eSSatish Balay     }
844e5c89e4eSSatish Balay     shortfunction[n] = PetscLogMallocFunction[i];
845e5c89e4eSSatish Balay     shortlength[n]   = PetscLogMallocLength[i];
84659ffdab8SBarry Smith     shortcount[n]    = 1;
847e5c89e4eSSatish Balay     n++;
848e5c89e4eSSatish Balay foundit:;
849e5c89e4eSSatish Balay   }
850e5c89e4eSSatish Balay 
851e32f2f54SBarry Smith   perm = (PetscInt*)malloc(n*sizeof(PetscInt));if (!perm) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_MEM,"Out of memory");
852e5c89e4eSSatish Balay   for (i=0; i<n; i++) perm[i] = i;
853e5c89e4eSSatish Balay   ierr = PetscSortStrWithPermutation(n,(const char**)shortfunction,perm);CHKERRQ(ierr);
854e5c89e4eSSatish Balay 
855e5c89e4eSSatish Balay   ierr = PetscFPrintf(MPI_COMM_WORLD,fp,"[%d] Memory usage sorted by function\n",rank);CHKERRQ(ierr);
856e5c89e4eSSatish Balay   for (i=0; i<n; i++) {
85759ffdab8SBarry 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);
858e5c89e4eSSatish Balay   }
859e5c89e4eSSatish Balay   free(perm);
860e5c89e4eSSatish Balay   free(shortlength);
86159ffdab8SBarry Smith   free(shortcount);
862e5c89e4eSSatish Balay   free((char**)shortfunction);
863f56c2debSBarry Smith   err = fflush(fp);
864e32f2f54SBarry Smith   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fflush() failed on file");
865e5c89e4eSSatish Balay   if (rank != size-1) {
866e5c89e4eSSatish Balay     ierr = MPI_Send(&dummy,1,MPIU_INT,rank+1,tag,MPI_COMM_WORLD);CHKERRQ(ierr);
867e5c89e4eSSatish Balay   }
868e5c89e4eSSatish Balay   PetscFunctionReturn(0);
869e5c89e4eSSatish Balay }
870e5c89e4eSSatish Balay 
871e5c89e4eSSatish Balay /* ---------------------------------------------------------------------------- */
872e5c89e4eSSatish Balay 
873e5c89e4eSSatish Balay #undef __FUNCT__
874e5c89e4eSSatish Balay #define __FUNCT__ "PetscMallocDebug"
875e5c89e4eSSatish Balay /*@C
876e5c89e4eSSatish Balay     PetscMallocDebug - Turns on/off debugging for the memory management routines.
877e5c89e4eSSatish Balay 
878e5c89e4eSSatish Balay     Not Collective
879e5c89e4eSSatish Balay 
880e5c89e4eSSatish Balay     Input Parameter:
881e5c89e4eSSatish Balay .   level - PETSC_TRUE or PETSC_FALSE
882e5c89e4eSSatish Balay 
883e5c89e4eSSatish Balay    Level: intermediate
884e5c89e4eSSatish Balay 
885e5c89e4eSSatish Balay .seealso: CHKMEMQ(), PetscMallocValidate()
886e5c89e4eSSatish Balay @*/
8877087cfbeSBarry Smith PetscErrorCode  PetscMallocDebug(PetscBool level)
888e5c89e4eSSatish Balay {
889e5c89e4eSSatish Balay   PetscFunctionBegin;
890e5c89e4eSSatish Balay   TRdebugLevel = level;
891e5c89e4eSSatish Balay   PetscFunctionReturn(0);
892e5c89e4eSSatish Balay }
8930acecf5bSBarry Smith 
8940acecf5bSBarry Smith #undef __FUNCT__
8950acecf5bSBarry Smith #define __FUNCT__ "PetscMallocGetDebug"
8960acecf5bSBarry Smith /*@C
8970acecf5bSBarry Smith     PetscMallocGetDebug - Indicates if any PETSc is doing ANY memory debugging.
8980acecf5bSBarry Smith 
8990acecf5bSBarry Smith     Not Collective
9000acecf5bSBarry Smith 
9010acecf5bSBarry Smith     Output Parameter:
9020acecf5bSBarry Smith .    flg - PETSC_TRUE if any debugger
9030acecf5bSBarry Smith 
9040acecf5bSBarry Smith    Level: intermediate
9050acecf5bSBarry Smith 
9060acecf5bSBarry Smith     Note that by default, the debug version always does some debugging unless you run with -malloc no
9070acecf5bSBarry Smith 
9080acecf5bSBarry Smith 
9090acecf5bSBarry Smith .seealso: CHKMEMQ(), PetscMallocValidate()
9100acecf5bSBarry Smith @*/
9110acecf5bSBarry Smith PetscErrorCode  PetscMallocGetDebug(PetscBool *flg)
9120acecf5bSBarry Smith {
9130acecf5bSBarry Smith   PetscFunctionBegin;
9140acecf5bSBarry Smith   if (PetscTrMalloc == PetscTrMallocDefault) *flg = PETSC_TRUE;
9150acecf5bSBarry Smith   else *flg = PETSC_FALSE;
9160acecf5bSBarry Smith   PetscFunctionReturn(0);
9170acecf5bSBarry Smith }
918