xref: /petsc/src/sys/memory/mem.c (revision 030f984af8d8bb4c203755d35bded3c05b3d83ce)
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 {
58 #if defined(PETSC_USE_PROCFS_FOR_SIZE)
59   FILE       *file;
60   int        fd;
61   char       proc[PETSC_MAX_PATH_LEN];
62   prpsinfo_t prusage;
63 #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
64   long       *ii = sbreak(0);
65   int        fd  = ii - (long*)0;
66 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
67   FILE       *file;
68   char       proc[PETSC_MAX_PATH_LEN];
69   int        mm,rss,err;
70 #elif defined(PETSC_HAVE_GETRUSAGE)
71   static struct rusage temp;
72 #endif
73 
74   PetscFunctionBegin;
75 #if defined(PETSC_USE_PROCFS_FOR_SIZE)
76 
77   sprintf(proc,"/proc/%d",(int)getpid());
78   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);
79   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);
80   *mem = (PetscLogDouble)prusage.pr_byrssize;
81   close(fd);
82 
83 #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
84 
85   *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
86 
87 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
88   sprintf(proc,"/proc/%d/statm",(int)getpid());
89   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);
90   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);
91   *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize());
92   err  = fclose(file);
93   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
94 
95 #elif defined(PETSC_HAVE_GETRUSAGE)
96   getrusage(RUSAGE_SELF,&temp);
97 #if defined(PETSC_USE_KBYTES_FOR_SIZE)
98   *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss);
99 #elif defined(PETSC_USE_PAGES_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
100   *mem = ((PetscLogDouble)getpagesize())*((PetscLogDouble)temp.ru_maxrss);
101 #else
102   *mem = temp.ru_maxrss;
103 #endif
104 
105 #else
106   *mem = 0.0;
107 #endif
108   PetscFunctionReturn(0);
109 }
110 
111 PETSC_INTERN PetscBool      PetscMemoryCollectMaximumUsage;
112 PETSC_INTERN PetscLogDouble PetscMemoryMaximumUsage;
113 
114 PetscBool      PetscMemoryCollectMaximumUsage = PETSC_FALSE;
115 PetscLogDouble PetscMemoryMaximumUsage        = 0;
116 
117 /*@
118    PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
119    for the program.
120 
121    Not Collective
122 
123    Output Parameter:
124 .   mem - memory usage in bytes
125 
126    Options Database Key:
127 +  -memory_view - Print memory usage at end of run
128 -  -malloc_log - Activate logging of memory usage
129 
130    Level: intermediate
131 
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.
138 
139 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
140           PetscMemorySetGetMaximumUsage()
141 
142 @*/
143 PetscErrorCode  PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
144 {
145   PetscFunctionBegin;
146   if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
147   *mem = PetscMemoryMaximumUsage;
148   PetscFunctionReturn(0);
149 }
150 
151 /*@
152    PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
153        PetscMemoryGetMaximumUsage() will work.
154 
155    Not Collective
156 
157    Options Database Key:
158 +  -memory_view - Print memory usage at end of run
159 -  -malloc_log - Activate logging of memory usage
160 
161    Level: intermediate
162 
163 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
164           PetscMemoryGetMaximumUsage()
165 
166 @*/
167 PetscErrorCode  PetscMemorySetGetMaximumUsage(void)
168 {
169   PetscFunctionBegin;
170   PetscMemoryCollectMaximumUsage = PETSC_TRUE;
171   PetscFunctionReturn(0);
172 }
173