Actual source code: mem.c

petsc-3.5.4 2015-05-23
Report Typos and Errors
  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

 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

 33: /*@
 34:    PetscMemoryGetCurrentUsage - Returns the current resident set size (memory used)
 35:    for the program.

 37:    Not Collective

 39:    Output Parameter:
 40: .   mem - memory usage in bytes

 42:    Options Database Key:
 43: .  -memory_info - Print memory usage at end of run
 44: .  -malloc_log - Activate logging of memory usage

 46:    Level: intermediate

 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.

 55: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetMaximumUsage(), PetscMallocGetCurrentUsage()

 57:    Concepts: resident set size
 58:    Concepts: memory usage

 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

 80: #if defined(PETSC_USE_PROCFS_FOR_SIZE)

 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);

 88: #elif defined(PETSC_USE_SBREAK_FOR_SIZE)

 90:   *mem = (PetscLogDouble)(8*fd - 4294967296); /* 2^32 - upper bits */

 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");

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

110: #else
111:   *mem = 0.0;
112: #endif
113:   return(0);
114: }

116: PetscBool      PetscMemoryCollectMaximumUsage = PETSC_FALSE;
117: PetscLogDouble PetscMemoryMaximumUsage        = 0;

121: /*@
122:    PetscMemoryGetMaximumUsage - Returns the maximum resident set size (memory used)
123:    for the program.

125:    Not Collective

127:    Output Parameter:
128: .   mem - memory usage in bytes

130:    Options Database Key:
131: .  -memory_info - Print memory usage at end of run
132: .  -malloc_log - Activate logging of memory usage

134:    Level: intermediate

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.

143: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
144:           PetscMemorySetGetMaximumUsage()

146:    Concepts: resident set size
147:    Concepts: memory usage

149: @*/
150: PetscErrorCode  PetscMemoryGetMaximumUsage(PetscLogDouble *mem)
151: {
153:   if (!PetscMemoryCollectMaximumUsage) SETERRQ(PETSC_COMM_SELF,PETSC_ERR_ARG_WRONGSTATE,"To use this function you must first call PetscMemorySetGetMaximumUsage()");
154:   *mem = PetscMemoryMaximumUsage;
155:   return(0);
156: }

160: /*@C
161:    PetscMemorySetGetMaximumUsage - Tells PETSc to monitor the maximum memory usage so that
162:        PetscMemoryGetMaximumUsage() will work.

164:    Not Collective

166:    Options Database Key:
167: .  -memory_info - Print memory usage at end of run
168: .  -malloc_log - Activate logging of memory usage

170:    Level: intermediate

172: .seealso: PetscMallocGetMaximumUsage(), PetscMemoryGetCurrentUsage(), PetscMallocGetCurrentUsage(),
173:           PetscMemoryGetMaximumUsage()

175:    Concepts: resident set size
176:    Concepts: memory usage

178: @*/
179: PetscErrorCode  PetscMemorySetGetMaximumUsage(void)
180: {
182:   PetscMemoryCollectMaximumUsage = PETSC_TRUE;
183:   return(0);
184: }