Actual source code: mem.c
1: /*$Id: mem.c,v 1.55 2001/06/21 21:15:26 bsmith Exp $*/
3: #include petsc.h
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(PARCH_win32)
19: #include <sys/utsname.h>
20: #endif
21: #if defined(PARCH_win32)
22: #include <windows.h>
23: #include <io.h>
24: #include <direct.h>
25: #endif
26: #if defined (PARCH_win32_gnu)
27: #include <windows.h>
28: #endif
29: #include <fcntl.h>
30: #include <time.h>
31: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
32: #include <sys/systeminfo.h>
33: #endif
34: #include "petscfix.h"
36: #if defined (PETSC_HAVE_SYS_RESOURCE_H)
37: #include <sys/resource.h>
38: #endif
39: #if defined(PETSC_HAVE_SYS_PROCFS_H)
40: /* #include <sys/int_types.h> Required if using gcc on solaris 2.6 */
41: #include <sys/procfs.h>
42: #endif
43: #if defined(PETSC_HAVE_FCNTL_H)
44: #include <fcntl.h>
45: #endif
47: #undef __FUNCT__
49: /*@C
50: PetscGetResidentSetSize - Returns the maximum resident set size (memory used)
51: for the program.
53: Not Collective
55: Output Parameter:
56: . mem - memory usage in bytes
58: Options Database Key:
59: . -get_resident_set_size - Print memory usage at end of run
60: . -trmalloc_log - Activate logging of memory usage
62: Level: intermediate
64: Notes:
65: The memory usage reported here includes all Fortran arrays
66: (that may be used in application-defined sections of code).
67: This routine thus provides a more complete picture of memory
68: usage than PetscTrSpace() for codes that employ Fortran with
69: hardwired arrays.
71: .seealso: PetscTrSpace()
73: Concepts: resident set size
74: Concepts: memory usage
76: @*/
77: int PetscGetResidentSetSize(PetscLogDouble *mem)
78: {
79: #define PETSC_USE_PROC_FOR_SIZE
80: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
81: FILE *file;
82: int fd;
83: char proc[PETSC_MAX_PATH_LEN];
84: prpsinfo_t prusage;
85: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
86: long *ii = sbreak(0);
87: int fd = ii - (long*)0;
88: #elif defined(PETSC_USE_PROC_FOR_SIZE)
89: FILE *file;
90: char proc[PETSC_MAX_PATH_LEN];
91: #elif defined(PETSC_HAVE_NO_GETRUSAGE)
92: #else
93: static struct rusage temp;
94: #endif
97: #if defined(PETSC_USE_PROCFS_FOR_SIZE)
98: sprintf(proc,"/proc/%d",(int)getpid());
99: if ((fd = open(proc,O_RDONLY)) == -1) {
100: SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",file);
101: }
102: if (ioctl(fd,PIOCPSINFO,&prusage) == -1) {
103: SETERRQ1(PETSC_ERR_FILE_READ,"Unable to access system file %s to get memory usage data",file);
104: }
105: *mem = (double)prusage.pr_byrssize;
106: close(fd);
107: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)
108: *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */
109: #elif defined(PETSC_USE_PROC_FOR_SIZE)
110: sprintf(proc,"/proc/%d/status",(int)getpid());
111: if (!(file = fopen(proc,"r"))) {
112: SETERRQ1(PETSC_ERR_FILE_OPEN,"Unable to access system file %s to get memory usage data",proc);
113: }
114: #elif defined(PETSC_HAVE_NO_GETRUSAGE)
115: *mem = 0.0;
116: #else
117: getrusage(RUSAGE_SELF,&temp);
118: #if defined(PETSC_USE_KBYTES_FOR_SIZE)
119: *mem = 1024.0 * ((double)temp.ru_maxrss);
120: #else
121: *mem = ((double)getpagesize())*((double)temp.ru_maxrss);
122: #endif
123: #endif
124: return(0);
125: }