xref: /petsc/src/sys/error/err.h (revision 47f0ea9ef136739b95e023b641b40b5b205c25bf)
1 #ifndef PETSC_SYS_ERROR_ERR_H
2 #define PETSC_SYS_ERROR_ERR_H
3 
4 #include <petscsys.h>
5 
6 static inline PetscErrorCode PetscErrorMemoryMessage(PetscErrorCode n)
7 {
8   PetscLogDouble mem, rss;
9   PetscErrorCode ierr;
10   PetscBool      flg1 = PETSC_FALSE, flg2 = PETSC_FALSE, flg3 = PETSC_FALSE;
11 
12   if (n == PETSC_ERR_MEM) {
13     ierr = (*PetscErrorPrintf)("Out of memory. This could be due to allocating\n");
14     ierr = (*PetscErrorPrintf)("too large an object or bleeding by not properly\n");
15     ierr = (*PetscErrorPrintf)("destroying unneeded objects.\n");
16   } else {
17     ierr = (*PetscErrorPrintf)("Memory leaked due to not properly destroying\n");
18     ierr = (*PetscErrorPrintf)("unneeded objects.\n");
19   }
20   ierr = PetscMallocGetCurrentUsage(&mem);
21   ierr = PetscMemoryGetCurrentUsage(&rss);
22   ierr = PetscOptionsGetBool(NULL, NULL, "-on_error_malloc_dump", &flg1, NULL);
23   ierr = PetscOptionsGetBool(NULL, NULL, "-malloc_view", &flg2, NULL);
24   ierr = PetscOptionsHasName(NULL, NULL, "-malloc_view_threshold", &flg3);
25   if (flg2 || flg3) ierr = PetscMallocView(stdout);
26   else {
27     ierr = (*PetscErrorPrintf)("Memory allocated %.0f Memory used by process %.0f\n", mem, rss);
28     if (flg1) ierr = PetscMallocDump(stdout);
29     else ierr = (*PetscErrorPrintf)("Try running with -on_error_malloc_dump or -malloc_view for info.\n");
30   }
31   return ierr;
32 }
33 
34 #endif // #define PETSC_SYS_ERROR_ERR_H
35