Actual source code: mem.c
petsc-3.3-p7 2013-05-11
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
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: */
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
43: /*@
44: PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used)
45: for the program.
47: Not Collective
49: Output Parameter:
50: . mem - memory usage in bytes
52: Options Database Key:
53: . -memory_info - Print memory usage at end of run
54: . -malloc_log - Activate logging of memory usage
56: Level: intermediate
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.
65: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetMaximumUsage(), PetscMallocGetCurrentUsage()
67: Concepts: resident set size
68: Concepts: memory usage
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
100: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
102: sprintf(proc,"/proc/%d",(int)getpid());
103: if ((fd = open(proc,O_RDONLY)) == -1) {
104: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",file);
105: }
106: if (ioctl(fd,PIOCPSINFO,&prusage) == -1) {
107: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_READ,"Unable to access system file %s to get memory usage data",file);
108: }
109: *mem = (PetscLogDouble)prusage.pr_byrssize;
110: close(fd);
112: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
114: *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
116: #elif defined(PETSC_USE_PROC_FOR_SIZE) && defined(PETSC_HAVE_GETPAGESIZE)
117: sprintf(proc,"/proc/%d/statm",(int)getpid());
118: if (!(file = fopen(proc,"r"))) {
119: SETERRQ1(PETSC_COMM_SELF,PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",proc);
120: }
121: 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);
122: *mem = ((PetscLogDouble)rss) * ((PetscLogDouble)getpagesize());
123: err = fclose(file);
124: if (err) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_SYS,"fclose() failed on file");
126: #elif defined(PETSC_HAVE_TASK_INFO)
127: *mem = 0;
128: /* 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);
129: *mem = (PetscLogDouble) ti.resident_size; */
130:
131: #elif defined(PETSC_HAVE_GETRUSAGE)
132: getrusage(RUSAGE_SELF,&temp);
133: #if defined(PETSC_USE_KBYTES_FOR_SIZE)
134: *mem = 1024.0 * ((PetscLogDouble)temp.ru_maxrss);
135: #elif defined(PETSC_HAVE_GETPAGESIZE)
136: *mem = ((PetscLogDouble)getpagesize())*((PetscLogDouble)temp.ru_maxrss);
137: #else
138: *mem = 0.0;
139: #endif
141: #else
142: *mem = 0.0;
143: #endif
144: return(0);
145: }
147: PetscBool PetscMemoryCollectMaximumUsage = PETSC_FALSE;
148: PetscLogDouble PetscMemoryMaximumUsage = 0;
152: /*@
153: PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
154: for the program.
156: Not Collective
158: Output Parameter:
159: . mem - memory usage in bytes
161: Options Database Key:
162: . -memory_info - Print memory usage at end of run
163: . -malloc_log - Activate logging of memory usage
165: Level: intermediate
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.
174: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
175: PetscMemorySetGetMaximumUsage()
177: Concepts: resident set size
178: Concepts: memory usage
180: @*/
181: PetscErrorCode PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
182: {
184: if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
185: *mem = PetscMemoryMaximumUsage;
186: return(0);
187: }
191: /*@C
192: PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
193: PetscMemoryGetMaximumUsage() will work.
195: Not Collective
197: Options Database Key:
198: . -memory_info - Print memory usage at end of run
199: . -malloc_log - Activate logging of memory usage
201: Level: intermediate
203: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
204: PetscMemoryGetMaximumUsage()
206: Concepts: resident set size
207: Concepts: memory usage
209: @*/
210: PetscErrorCode PetscMemorySetGetMaximumUsage(void)
211: {
213: PetscMemoryCollectMaximumUsage = PETSC_TRUE;
214: return(0);
215: }