xref: /petsc/src/sys/memory/mem.c (revision fe998a80077c9ee0917a39496df43fc256e1b478)
1 
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 #undef __FUNCT__
32 #define __FUNCT__ "PetscMemoryGetCurrentUsage"
33 /*@
34    PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used)
35    for the program.
36 
37    Not Collective
38 
39    Output Parameter:
40 .   mem - memory usage in bytes
41 
42    Options Database Key:
43 .  -memory_info - Print memory usage at end of run
44 .  -malloc_log - Activate logging of memory usage
45 
46    Level: intermediate
47 
48    Notes:
49    The memory usage reported here includes all Fortran arrays
50    (that may be used in application-defined sections of code).
51    This routine thus provides a more complete picture of memory
52    usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
53    hardwired arrays.
54 
55 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetMaximumUsage(), PetscMallocGetCurrentUsage()
56 
57    Concepts: resident set size
58    Concepts: memory usage
59 
60 @*/
61 PetscErrorCode  PetscMemoryGetCurrentUsage(PetscLogDouble *mem)
62 {
63 #if defined(PETSC_USE_PROCFS_FOR_SIZE)
64   FILE       *file;
65   int        fd;
66   char       proc[PETSC_MAX_PATH_LEN];
67   prpsinfo_t prusage;
68 #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
69   long       *ii = sbreak(0);
70   int        fd  = ii - (long*)0;
71 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
72   FILE       *file;
73   char       proc[PETSC_MAX_PATH_LEN];
74   int        mm,rss,err;
75 #elif defined(PETSC_HAVE_GETRUSAGE)
76   static struct rusage temp;
77 #endif
78 
79   PetscFunctionBegin;
80 #if defined(PETSC_USE_PROCFS_FOR_SIZE)
81 
82   sprintf(proc,"/proc/%d",(int)getpid());
83   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);
84   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);
85   *mem = (PetscLogDouble)prusage.pr_byrssize;
86   close(fd);
87 
88 #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
89 
90   *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
91 
92 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
93   sprintf(proc,"/proc/%d/statm",(int)getpid());
94   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);
95   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);
96   *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize());
97   err  = fclose(file);
98   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
99 
100 #elif defined(PETSC_HAVE_GETRUSAGE)
101   getrusage(RUSAGE_SELF,&temp);
102 #if defined(PETSC_USE_KBYTES_FOR_SIZE)
103   *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss);
104 #elif defined(PETSC_USE_PAGES_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
105   *mem = ((PetscLogDouble)getpagesize())*((PetscLogDouble)temp.ru_maxrss);
106 #else
107   *mem = temp.ru_maxrss;
108 #endif
109 
110 #else
111   *mem = 0.0;
112 #endif
113   PetscFunctionReturn(0);
114 }
115 
116 PetscBool      PetscMemoryCollectMaximumUsage = PETSC_FALSE;
117 PetscLogDouble PetscMemoryMaximumUsage        = 0;
118 
119 #undef __FUNCT__
120 #define __FUNCT__ "PetscMemoryGetMaximumUsage"
121 /*@
122    PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
123    for the program.
124 
125    Not Collective
126 
127    Output Parameter:
128 .   mem - memory usage in bytes
129 
130    Options Database Key:
131 .  -memory_info - Print memory usage at end of run
132 .  -malloc_log - Activate logging of memory usage
133 
134    Level: intermediate
135 
136    Notes:
137    The memory usage reported here includes all Fortran arrays
138    (that may be used in application-defined sections of code).
139    This routine thus provides a more complete picture of memory
140    usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
141    hardwired arrays.
142 
143 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
144           PetscMemorySetGetMaximumUsage()
145 
146    Concepts: resident set size
147    Concepts: memory usage
148 
149 @*/
150 PetscErrorCode  PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
151 {
152   PetscFunctionBegin;
153   if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
154   *mem = PetscMemoryMaximumUsage;
155   PetscFunctionReturn(0);
156 }
157 
158 #undef __FUNCT__
159 #define __FUNCT__ "PetscMemorySetGetMaximumUsage"
160 /*@C
161    PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
162        PetscMemoryGetMaximumUsage() will work.
163 
164    Not Collective
165 
166    Options Database Key:
167 .  -memory_info - Print memory usage at end of run
168 .  -malloc_log - Activate logging of memory usage
169 
170    Level: intermediate
171 
172 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
173           PetscMemoryGetMaximumUsage()
174 
175    Concepts: resident set size
176    Concepts: memory usage
177 
178 @*/
179 PetscErrorCode  PetscMemorySetGetMaximumUsage(void)
180 {
181   PetscFunctionBegin;
182   PetscMemoryCollectMaximumUsage = PETSC_TRUE;
183   PetscFunctionReturn(0);
184 }
185