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