1 #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for getpagesize() with c89 */ 2 #include <petscsys.h> /*I "petscsys.h" I*/ 3 #if defined(PETSC_HAVE_PWD_H) 4 #include <pwd.h> 5 #endif 6 #include <ctype.h> 7 #include <sys/stat.h> 8 #if defined(PETSC_HAVE_UNISTD_H) 9 #include <unistd.h> 10 #endif 11 #if defined(PETSC_HAVE_SYS_UTSNAME_H) 12 #include <sys/utsname.h> 13 #endif 14 #include <fcntl.h> 15 #include <time.h> 16 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H) 17 #include <sys/systeminfo.h> 18 #endif 19 20 #if defined(PETSC_HAVE_SYS_RESOURCE_H) 21 #include <sys/resource.h> 22 #endif 23 #if defined(PETSC_HAVE_SYS_PROCFS_H) 24 /* #include <sys/int_types.h> Required if using gcc on solaris 2.6 */ 25 #include <sys/procfs.h> 26 #endif 27 #if defined(PETSC_HAVE_FCNTL_H) 28 #include <fcntl.h> 29 #endif 30 31 /*@ 32 PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used) 33 for the program. 34 35 Not Collective 36 37 Output Parameter: 38 . mem - memory usage in bytes 39 40 Options Database Key: 41 + -memory_view - Print memory usage at end of run 42 - -malloc_log - Activate logging of memory usage 43 44 Level: intermediate 45 46 Notes: 47 The memory usage reported here includes all Fortran arrays 48 (that may be used in application-defined sections of code). 49 This routine thus provides a more complete picture of memory 50 usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with 51 hardwired arrays. 52 53 .seealso: `PetscMallocGetMaximumUsage()`, `PetscMemoryGetMaximumUsage()`, `PetscMallocGetCurrentUsage()`, `PetscMemorySetGetMaximumUsage()`, `PetscMemoryView()` 54 55 @*/ 56 PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *mem) { 57 #if defined(PETSC_USE_PROCFS_FOR_SIZE) 58 FILE *file; 59 int fd; 60 char proc[PETSC_MAX_PATH_LEN]; 61 prpsinfo_t prusage; 62 #elif defined(PETSC_USE_SBREAK_FOR_SIZE) 63 long *ii = sbreak(0); 64 int fd = ii - (long *)0; 65 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE) 66 FILE *file; 67 char proc[PETSC_MAX_PATH_LEN]; 68 int mm, rss, err; 69 #elif defined(PETSC_HAVE_GETRUSAGE) 70 static struct rusage temp; 71 #endif 72 73 PetscFunctionBegin; 74 #if defined(PETSC_USE_PROCFS_FOR_SIZE) 75 76 sprintf(proc, "/proc/%d", (int)getpid()); 77 PetscCheck((fd = open(proc, O_RDONLY)) != -1, PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to access system file %s to get memory usage data", file); 78 PetscCheck(ioctl(fd, PIOCPSINFO, &prusage) != -1, PETSC_COMM_SELF, PETSC_ERR_FILE_READ, "Unable to access system file %s to get memory usage data", file); 79 *mem = (PetscLogDouble)prusage.pr_byrssize; 80 close(fd); 81 82 #elif defined(PETSC_USE_SBREAK_FOR_SIZE) 83 84 *mem = (PetscLogDouble)(8 * fd - 4294967296); /* 2^32 - upper bits */ 85 86 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE) 87 sprintf(proc, "/proc/%d/statm", (int)getpid()); 88 PetscCheck((file = fopen(proc, "r")), PETSC_COMM_SELF, PETSC_ERR_FILE_OPEN, "Unable to access system file %s to get memory usage data", proc); 89 PetscCheck(fscanf(file, "%d %d", &mm, &rss) == 2, PETSC_COMM_SELF, PETSC_ERR_SYS, "Failed to read two integers (mm and rss) from %s", proc); 90 *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize()); 91 err = fclose(file); 92 PetscCheck(!err, PETSC_COMM_SELF, PETSC_ERR_SYS, "fclose() failed on file"); 93 94 #elif defined(PETSC_HAVE_GETRUSAGE) 95 getrusage(RUSAGE_SELF, &temp); 96 #if defined(PETSC_USE_KBYTES_FOR_SIZE) 97 *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss); 98 #elif defined(PETSC_USE_PAGES_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE) 99 *mem = ((PetscLogDouble)getpagesize()) * ((PetscLogDouble)temp.ru_maxrss); 100 #else 101 *mem = temp.ru_maxrss; 102 #endif 103 104 #else 105 *mem = 0.0; 106 #endif 107 PetscFunctionReturn(0); 108 } 109 110 PETSC_INTERN PetscBool PetscMemoryCollectMaximumUsage; 111 PETSC_INTERN PetscLogDouble PetscMemoryMaximumUsage; 112 113 PetscBool PetscMemoryCollectMaximumUsage = PETSC_FALSE; 114 PetscLogDouble PetscMemoryMaximumUsage = 0; 115 116 /*@ 117 PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used) 118 for the program. 119 120 Not Collective 121 122 Output Parameter: 123 . mem - memory usage in bytes 124 125 Options Database Key: 126 + -memory_view - Print memory usage at end of run 127 - -malloc_log - Activate logging of memory usage 128 129 Level: intermediate 130 131 Notes: 132 The memory usage reported here includes all Fortran arrays 133 (that may be used in application-defined sections of code). 134 This routine thus provides a more complete picture of memory 135 usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with 136 hardwired arrays. 137 138 .seealso: `PetscMallocGetMaximumUsage()`, `PetscMemoryGetCurrentUsage()`, `PetscMallocGetCurrentUsage()`, 139 `PetscMemorySetGetMaximumUsage()` 140 141 @*/ 142 PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *mem) { 143 PetscFunctionBegin; 144 PetscCheck(PetscMemoryCollectMaximumUsage, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONGSTATE, "To use this function you must first call PetscMemorySetGetMaximumUsage()"); 145 *mem = PetscMemoryMaximumUsage; 146 PetscFunctionReturn(0); 147 } 148 149 /*@ 150 PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that 151 PetscMemoryGetMaximumUsage() will work. 152 153 Not Collective 154 155 Options Database Key: 156 + -memory_view - Print memory usage at end of run 157 - -malloc_log - Activate logging of memory usage 158 159 Level: intermediate 160 161 .seealso: `PetscMallocGetMaximumUsage()`, `PetscMemoryGetCurrentUsage()`, `PetscMallocGetCurrentUsage()`, 162 `PetscMemoryGetMaximumUsage()` 163 164 @*/ 165 PetscErrorCode PetscMemorySetGetMaximumUsage(void) { 166 PetscFunctionBegin; 167 PetscMemoryCollectMaximumUsage = PETSC_TRUE; 168 PetscFunctionReturn(0); 169 } 170