Actual source code: mem.c
petsc-3.9.4 2018-09-11
1: #define PETSC_DESIRE_FEATURE_TEST_MACROS /* for getpagesize() with c89 */
2: #include <petscsys.h>
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
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
31: /*@
32: PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used)
33: for the program.
35: Not Collective
37: Output Parameter:
38: . mem - memory usage in bytes
40: Options Database Key:
41: . -memory_view - Print memory usage at end of run
42: . -malloc_log - Activate logging of memory usage
44: Level: intermediate
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.
53: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetMaximumUsage(), PetscMallocGetCurrentUsage(), PetscMemorySetGetMaximumUsage(), PetscMemoryView()
55: Concepts: resident set size
56: Concepts: memory usage
58: @*/
59: PetscErrorCode PetscMemoryGetCurrentUsage(PetscLogDouble *mem)
60: {
61: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
62: FILE *file;
63: int fd;
64: char proc[PETSC_MAX_PATH_LEN];
65: prpsinfo_t prusage;
66: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
67: long *ii = sbreak(0);
68: int fd = ii - (long*)0;
69: #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
70: FILE *file;
71: char proc[PETSC_MAX_PATH_LEN];
72: int mm,rss,err;
73: #elif defined(PETSC_HAVE_GETRUSAGE)
74: static struct rusage temp;
75: #endif
78: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
80: sprintf(proc,"/proc/%d",(int)getpid());
81: if ((fd = open(proc,O_RDONLY)) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",file);
82: if (ioctl(fd,PIOCPSINFO,&prusage) == -1) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Unable to access system file %s to get memory usage data",file);
83: *mem = (PetscLogDouble)prusage.pr_byrssize;
84: close(fd);
86: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
88: *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
90: #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
91: sprintf(proc,"/proc/%d/statm",(int)getpid());
92: if (!(file = fopen(proc,"r"))) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",proc);
93: if (fscanf(file,"%d %d",&mm,&rss) != 2) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_SYS,"Failed to read two integers (mm and rss) from %s",proc);
94: *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize());
95: err = fclose(file);
96: if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
98: #elif defined(PETSC_HAVE_GETRUSAGE)
99: getrusage(RUSAGE_SELF,&temp);
100: #if defined(PETSC_USE_KBYTES_FOR_SIZE)
101: *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss);
102: #elif defined(PETSC_USE_PAGES_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
103: *mem = ((PetscLogDouble)getpagesize())*((PetscLogDouble)temp.ru_maxrss);
104: #else
105: *mem = temp.ru_maxrss;
106: #endif
108: #else
109: *mem = 0.0;
110: #endif
111: return(0);
112: }
114: PetscBool PetscMemoryCollectMaximumUsage = PETSC_FALSE;
115: PetscLogDouble PetscMemoryMaximumUsage = 0;
117: /*@
118: PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
119: for the program.
121: Not Collective
123: Output Parameter:
124: . mem - memory usage in bytes
126: Options Database Key:
127: . -memory_view - Print memory usage at end of run
128: . -malloc_log - Activate logging of memory usage
130: Level: intermediate
132: Notes:
133: The memory usage reported here includes all Fortran arrays
134: (that may be used in application-defined sections of code).
135: This routine thus provides a more complete picture of memory
136: usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
137: hardwired arrays.
139: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
140: PetscMemorySetGetMaximumUsage()
142: Concepts: resident set size
143: Concepts: memory usage
145: @*/
146: PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
147: {
149: if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
150: *mem = PetscMemoryMaximumUsage;
151: return(0);
152: }
154: /*@
155: PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
156: PetscMemoryGetMaximumUsage() will work.
158: Not Collective
160: Options Database Key:
161: . -memory_view - Print memory usage at end of run
162: . -malloc_log - Activate logging of memory usage
164: Level: intermediate
166: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
167: PetscMemoryGetMaximumUsage()
169: Concepts: resident set size
170: Concepts: memory usage
172: @*/
173: PetscErrorCode PetscMemorySetGetMaximumUsage(void)
174: {
176: PetscMemoryCollectMaximumUsage = PETSC_TRUE;
177: return(0);
178: }