xref: /petsc/src/sys/memory/mem.c (revision 26fbe8dc1a3c99fb8dddfa572c8c6b3b4ce3ca53)
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/types.h>
8 #include <sys/stat.h>
9 #if defined(PETSC_HAVE_UNISTD_H)
10 #include <unistd.h>
11 #endif
12 #if defined(PETSC_HAVE_STDLIB_H)
13 #include <stdlib.h>
14 #endif
15 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
16 #include <sys/utsname.h>
17 #endif
18 #include <fcntl.h>
19 #include <time.h>
20 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
21 #include <sys/systeminfo.h>
22 #endif
23 
24 /* task_info seems to be buggy plus pgcc doesn't like including this file
25 #if defined(PETSC_HAVE_TASK_INFO)
26 #include <mach/mach.h>
27 #endif
28 */
29 
30 #if defined(PETSC_HAVE_SYS_RESOURCE_H)
31 #include <sys/resource.h>
32 #endif
33 #if defined(PETSC_HAVE_SYS_PROCFS_H)
34 /* #include <sys/int_types.h> Required if using gcc on solaris 2.6 */
35 #include <sys/procfs.h>
36 #endif
37 #if defined(PETSC_HAVE_FCNTL_H)
38 #include <fcntl.h>
39 #endif
40 
41 #undef __FUNCT__
42 #define __FUNCT__ "PetscMemoryGetCurrentUsage"
43 /*@
44    PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used)
45    for the program.
46 
47    Not Collective
48 
49    Output Parameter:
50 .   mem - memory usage in bytes
51 
52    Options Database Key:
53 .  -memory_info - Print memory usage at end of run
54 .  -malloc_log - Activate logging of memory usage
55 
56    Level: intermediate
57 
58    Notes:
59    The memory usage reported here includes all Fortran arrays
60    (that may be used in application-defined sections of code).
61    This routine thus provides a more complete picture of memory
62    usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
63    hardwired arrays.
64 
65 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetMaximumUsage(), PetscMallocGetCurrentUsage()
66 
67    Concepts: resident set size
68    Concepts: memory usage
69 
70 @*/
71 PetscErrorCode  PetscMemoryGetCurrentUsage(PetscLogDouble *mem)
72 {
73 #if defined(PETSC_USE_PROCFS_FOR_SIZE)
74   FILE       *file;
75   int        fd;
76   char       proc[PETSC_MAX_PATH_LEN];
77   prpsinfo_t prusage;
78 #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
79   long       *ii = sbreak(0);
80   int        fd  = ii - (long*)0;
81 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
82   FILE       *file;
83   char       proc[PETSC_MAX_PATH_LEN];
84   int        mm,rss,err;
85 #elif defined(PETSC_HAVE_TASK_INFO)
86   /*  task_basic_info_data_t ti;
87       unsigned int           count; */
88   /*
89      The next line defined variables that are not used; but if they
90      are not included the code crashes. Something must be wrong
91      with either the task_info() command or compiler corrupting the
92      stack.
93   */
94   /* kern_return_t          kerr; */
95 #elif defined(PETSC_HAVE_GETRUSAGE)
96   static struct rusage temp;
97 #endif
98 
99   PetscFunctionBegin;
100 #if defined(PETSC_USE_PROCFS_FOR_SIZE)
101 
102   sprintf(proc,"/proc/%d",(int)getpid());
103   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);
104   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);
105   *mem = (PetscLogDouble)prusage.pr_byrssize;
106   close(fd);
107 
108 #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
109 
110   *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
111 
112 #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
113   sprintf(proc,"/proc/%d/statm",(int)getpid());
114   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);
115   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);
116   *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize());
117   err  = fclose(file);
118   if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
119 
120 #elif defined(PETSC_HAVE_TASK_INFO)
121   *mem = 0;
122   /* if ((kerr = task_info(mach_task_self(), TASK_BASIC_INFO, (task_info_t)&ti,&count)) != KERN_SUCCESS) SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_LIB,"Mach system call failed: kern_return_t ",kerr);
123    *mem = (PetscLogDouble) ti.resident_size; */
124 
125 #elif defined(PETSC_HAVE_GETRUSAGE)
126   getrusage(RUSAGE_SELF,&temp);
127 #if defined(PETSC_USE_KBYTES_FOR_SIZE)
128   *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss);
129 #elif defined(PETSC_HAVE_GETPAGESIZE)
130   *mem = ((PetscLogDouble)getpagesize())*((PetscLogDouble)temp.ru_maxrss);
131 #else
132   *mem = 0.0;
133 #endif
134 
135 #else
136   *mem = 0.0;
137 #endif
138   PetscFunctionReturn(0);
139 }
140 
141 PetscBool      PetscMemoryCollectMaximumUsage = PETSC_FALSE;
142 PetscLogDouble PetscMemoryMaximumUsage        = 0;
143 
144 #undef __FUNCT__
145 #define __FUNCT__ "PetscMemoryGetMaximumUsage"
146 /*@
147    PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
148    for the program.
149 
150    Not Collective
151 
152    Output Parameter:
153 .   mem - memory usage in bytes
154 
155    Options Database Key:
156 .  -memory_info - Print memory usage at end of run
157 .  -malloc_log - Activate logging of memory usage
158 
159    Level: intermediate
160 
161    Notes:
162    The memory usage reported here includes all Fortran arrays
163    (that may be used in application-defined sections of code).
164    This routine thus provides a more complete picture of memory
165    usage than PetscMallocGetCurrentUsage() for codes that employ Fortran with
166    hardwired arrays.
167 
168 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
169           PetscMemorySetGetMaximumUsage()
170 
171    Concepts: resident set size
172    Concepts: memory usage
173 
174 @*/
175 PetscErrorCode  PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
176 {
177   PetscFunctionBegin;
178   if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
179   *mem = PetscMemoryMaximumUsage;
180   PetscFunctionReturn(0);
181 }
182 
183 #undef __FUNCT__
184 #define __FUNCT__ "PetscMemorySetGetMaximumUsage"
185 /*@C
186    PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
187        PetscMemoryGetMaximumUsage() will work.
188 
189    Not Collective
190 
191    Options Database Key:
192 .  -memory_info - Print memory usage at end of run
193 .  -malloc_log - Activate logging of memory usage
194 
195    Level: intermediate
196 
197 .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
198           PetscMemoryGetMaximumUsage()
199 
200    Concepts: resident set size
201    Concepts: memory usage
202 
203 @*/
204 PetscErrorCode  PetscMemorySetGetMaximumUsage(void)
205 {
206   PetscFunctionBegin;
207   PetscMemoryCollectMaximumUsage = PETSC_TRUE;
208   PetscFunctionReturn(0);
209 }
210